001/*
002 * (C) Copyright 2006-2010 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Thierry Delprat
016 */
017package org.nuxeo.apidoc.browse;
018
019import java.util.ArrayList;
020import java.util.Collection;
021import java.util.Collections;
022import java.util.Comparator;
023import java.util.List;
024
025import javax.ws.rs.GET;
026import javax.ws.rs.Path;
027import javax.ws.rs.Produces;
028
029import org.nuxeo.apidoc.api.BundleInfo;
030import org.nuxeo.apidoc.api.ComponentInfo;
031import org.nuxeo.apidoc.api.NuxeoArtifact;
032import org.nuxeo.ecm.webengine.model.WebObject;
033
034@WebObject(type = "bundle")
035public class BundleWO extends NuxeoArtifactWebObject {
036
037    @Override
038    @GET
039    @Produces("text/html")
040    @Path("introspection")
041    public Object doGet() {
042        BundleInfo bi = getTargetBundleInfo();
043        Collection<ComponentInfo> ci = bi.getComponents();
044        return getView("view").arg("bundle", bi).arg("components", ci);
045    }
046
047    public BundleInfo getTargetBundleInfo() {
048        return getSnapshotManager().getSnapshot(getDistributionId(), ctx.getCoreSession()).getBundle(nxArtifactId);
049    }
050
051    @Override
052    public NuxeoArtifact getNxArtifact() {
053        return getTargetBundleInfo();
054    }
055
056    protected class ComponentInfoSorter implements Comparator<ComponentInfo> {
057        @Override
058        public int compare(ComponentInfo ci0, ComponentInfo ci1) {
059
060            if (ci0.isXmlPureComponent() && !ci1.isXmlPureComponent()) {
061                return 1;
062            }
063            if (!ci0.isXmlPureComponent() && ci1.isXmlPureComponent()) {
064                return -1;
065            }
066
067            return ci0.getId().compareTo(ci1.getId());
068        }
069    }
070
071    public List<ComponentWO> getComponents() {
072        List<ComponentWO> result = new ArrayList<ComponentWO>();
073        BundleInfo bundle = getTargetBundleInfo();
074
075        List<ComponentInfo> cis = new ArrayList<ComponentInfo>(bundle.getComponents());
076        Collections.sort(cis, new ComponentInfoSorter());
077
078        for (ComponentInfo ci : cis) {
079            result.add((ComponentWO) ctx.newObject("component", ci.getId()));
080        }
081        return result;
082    }
083
084}