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.HashMap;
023import java.util.List;
024import java.util.Map;
025
026import javax.ws.rs.GET;
027import javax.ws.rs.Path;
028import javax.ws.rs.Produces;
029import javax.ws.rs.QueryParam;
030
031import org.nuxeo.apidoc.api.ComponentInfo;
032import org.nuxeo.apidoc.api.ExtensionInfo;
033import org.nuxeo.apidoc.api.ExtensionPointInfo;
034import org.nuxeo.apidoc.api.NuxeoArtifact;
035import org.nuxeo.apidoc.api.ServiceInfo;
036import org.nuxeo.ecm.webengine.model.WebObject;
037
038@WebObject(type = "component")
039public class ComponentWO extends NuxeoArtifactWebObject {
040
041    @Override
042    @GET
043    @Produces("text/html")
044    @Path("introspection")
045    public Object doGet() {
046        ComponentInfo ci = getTargetComponentInfo();
047        String bundleId = ci.getBundle().getBundleId();
048        return getView("view").arg("bundleId", bundleId).arg("component", ci);
049    }
050
051    @GET
052    @Produces("text/xml")
053    @Path("override")
054    public Object override(@QueryParam("extensionId") String extensionId) {
055        ComponentInfo component = getTargetComponentInfo();
056        Map<String, String> extension = null;
057        if (extensionId.contains("--")) {
058            String[] split = extensionId.split("--");
059            extension = new HashMap<>();
060            extension.put("target", split[0]);
061            extension.put("point", split[1]);
062        }
063        return getView("override").arg("component", component).arg("extension", extension);
064    }
065
066    public ComponentInfo getTargetComponentInfo() {
067        return getSnapshotManager().getSnapshot(getDistributionId(), ctx.getCoreSession()).getComponent(nxArtifactId);
068    }
069
070    @Override
071    public NuxeoArtifact getNxArtifact() {
072        return getTargetComponentInfo();
073    }
074
075    public List<ServiceWO> getServices() {
076        List<ServiceWO> result = new ArrayList<ServiceWO>();
077        ComponentInfo ci = getTargetComponentInfo();
078        for (ServiceInfo si : ci.getServices()) {
079            result.add((ServiceWO) ctx.newObject("service", si.getId()));
080        }
081        return result;
082    }
083
084    public List<ExtensionPointWO> getExtensionPoints() {
085        List<ExtensionPointWO> result = new ArrayList<ExtensionPointWO>();
086        ComponentInfo ci = getTargetComponentInfo();
087        for (ExtensionPointInfo ei : ci.getExtensionPoints()) {
088            result.add((ExtensionPointWO) ctx.newObject("extensionPoint", ei.getId()));
089        }
090        return result;
091    }
092
093    public List<ContributionWO> getContributions() {
094        List<ContributionWO> result = new ArrayList<ContributionWO>();
095        ComponentInfo ci = getTargetComponentInfo();
096        for (ExtensionInfo ei : ci.getExtensions()) {
097            result.add((ContributionWO) ctx.newObject("contribution", ei.getId()));
098        }
099        return result;
100    }
101
102}