001/*
002 * (C) Copyright 2006-2010 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Thierry Delprat
018 */
019package org.nuxeo.apidoc.browse;
020
021import java.util.ArrayList;
022import java.util.Arrays;
023import java.util.List;
024import java.util.Map;
025
026import javax.ws.rs.GET;
027import javax.ws.rs.POST;
028import javax.ws.rs.Path;
029import javax.ws.rs.Produces;
030
031import org.apache.commons.lang3.StringUtils;
032import org.nuxeo.apidoc.api.ExtensionInfo;
033import org.nuxeo.apidoc.api.ExtensionPointInfo;
034import org.nuxeo.apidoc.api.NuxeoArtifact;
035import org.nuxeo.ecm.webengine.forms.FormData;
036import org.nuxeo.ecm.webengine.model.WebObject;
037
038@WebObject(type = "contribution")
039public class ContributionWO extends NuxeoArtifactWebObject {
040
041    @Override
042    @GET
043    @Produces("text/html")
044    @Path("introspection")
045    public Object doGet() {
046        ExtensionInfo ei = getTargetExtensionInfo();
047        return getView("view").arg("contribution", ei);
048    }
049
050    public ExtensionInfo getTargetExtensionInfo() {
051        return getSnapshotManager().getSnapshot(getDistributionId(), ctx.getCoreSession()).getContribution(nxArtifactId);
052    }
053
054    @Override
055    public NuxeoArtifact getNxArtifact() {
056        return getTargetExtensionInfo();
057    }
058
059    @POST
060    @Produces("text/xml")
061    @Path("override")
062    public Object generateOverride() {
063
064        ExtensionInfo ei = getTargetExtensionInfo();
065        String epid = ei.getExtensionPoint();
066        ExtensionPointInfo ep = getSnapshotManager().getSnapshot(getDistributionId(), ctx.getCoreSession()).getExtensionPoint(
067                epid);
068
069        FormData formData = ctx.getForm();
070        Map<String, String[]> fields = formData.getFormFields();
071        List<String> selectedContribs = new ArrayList<String>(fields.keySet());
072        return getView("override").arg("contribution", ei).arg("selectedContribs", selectedContribs).arg("ep", ep);
073    }
074
075    @Override
076    public String getSearchCriterion() {
077        String[] split = getNxArtifactId().split("--");
078        if (split.length == 2) {
079            return String.format("'%s' %s", split[0], split[1]);
080        } else if (split.length> 1) {
081            return StringUtils.join(split, " ");
082        }
083        return getNxArtifactId();
084    }
085}