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.Collection;
023import java.util.Collections;
024import java.util.Comparator;
025import java.util.List;
026
027import javax.ws.rs.GET;
028import javax.ws.rs.Path;
029import javax.ws.rs.Produces;
030
031import org.nuxeo.apidoc.api.BundleInfo;
032import org.nuxeo.apidoc.api.ComponentInfo;
033import org.nuxeo.apidoc.api.NuxeoArtifact;
034import org.nuxeo.ecm.webengine.model.WebObject;
035
036@WebObject(type = "bundle")
037public class BundleWO extends NuxeoArtifactWebObject {
038
039    @Override
040    @GET
041    @Produces("text/html")
042    @Path("introspection")
043    public Object doGet() {
044        BundleInfo bi = getTargetBundleInfo();
045        Collection<ComponentInfo> ci = bi.getComponents();
046        return getView("view").arg("bundle", bi).arg("components", ci);
047    }
048
049    public BundleInfo getTargetBundleInfo() {
050        return getSnapshotManager().getSnapshot(getDistributionId(), ctx.getCoreSession()).getBundle(nxArtifactId);
051    }
052
053    @Override
054    public NuxeoArtifact getNxArtifact() {
055        return getTargetBundleInfo();
056    }
057
058    protected class ComponentInfoSorter implements Comparator<ComponentInfo> {
059        @Override
060        public int compare(ComponentInfo ci0, ComponentInfo ci1) {
061
062            if (ci0.isXmlPureComponent() && !ci1.isXmlPureComponent()) {
063                return 1;
064            }
065            if (!ci0.isXmlPureComponent() && ci1.isXmlPureComponent()) {
066                return -1;
067            }
068
069            return ci0.getId().compareTo(ci1.getId());
070        }
071    }
072
073    public List<ComponentWO> getComponents() {
074        List<ComponentWO> result = new ArrayList<ComponentWO>();
075        BundleInfo bundle = getTargetBundleInfo();
076
077        List<ComponentInfo> cis = new ArrayList<ComponentInfo>(bundle.getComponents());
078        Collections.sort(cis, new ComponentInfoSorter());
079
080        for (ComponentInfo ci : cis) {
081            result.add((ComponentWO) ctx.newObject("component", ci.getId()));
082        }
083        return result;
084    }
085
086    @Override
087    public String getSearchCriterion() {
088        return "'" + super.getSearchCriterion() + "' Bundle";
089    }
090}