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.tree;
018
019import java.util.ArrayList;
020import java.util.List;
021
022import org.nuxeo.apidoc.api.BundleGroup;
023import org.nuxeo.apidoc.api.BundleInfo;
024import org.nuxeo.apidoc.api.ComponentInfo;
025import org.nuxeo.apidoc.api.ExtensionInfo;
026import org.nuxeo.apidoc.api.ExtensionPointInfo;
027import org.nuxeo.apidoc.api.NuxeoArtifact;
028import org.nuxeo.apidoc.api.ServiceInfo;
029import org.nuxeo.apidoc.api.VirtualNodesConsts;
030import org.nuxeo.apidoc.snapshot.DistributionSnapshot;
031import org.nuxeo.ecm.webengine.ui.tree.ContentProvider;
032
033public class NuxeoArtifactContentProvider implements ContentProvider {
034
035    private static final long serialVersionUID = 1L;
036
037    protected final DistributionSnapshot ds;
038
039    public NuxeoArtifactContentProvider(DistributionSnapshot ds) {
040        this.ds = ds;
041    }
042
043    @Override
044    public Object[] getElements(Object input) {
045        return getChildren(input);
046    }
047
048    @Override
049    public Object[] getChildren(Object ob) {
050
051        List<NuxeoArtifact> result = new ArrayList<NuxeoArtifact>();
052        NuxeoArtifact obj = (NuxeoArtifact) ob;
053
054        if (obj.getArtifactType().equals(DistributionSnapshot.TYPE_NAME)) {
055            for (BundleGroup bg : ds.getBundleGroups()) {
056                result.add(bg);
057            }
058        } else if (obj.getArtifactType().equals(BundleInfo.TYPE_NAME)) {
059            for (ComponentInfo ci : ds.getBundle(obj.getId()).getComponents()) {
060                result.add(ci);
061            }
062        } else if (obj.getArtifactType().equals(BundleGroup.TYPE_NAME)) {
063            for (String bid : ds.getBundleGroup(obj.getId()).getBundleIds()) {
064                result.add(ds.getBundle(bid));
065            }
066            for (BundleGroup sbg : ds.getBundleGroup(obj.getId()).getSubGroups()) {
067                result.add(sbg);
068            }
069        } else if (obj.getArtifactType().equals(ComponentInfo.TYPE_NAME)) {
070            ComponentInfo ci = ds.getComponent(obj.getId());
071            if (!ci.getExtensionPoints().isEmpty()) {
072                result.add(new VirtualNode(ci, VirtualNodesConsts.ExtensionPoints_VNODE,
073                        VirtualNodesConsts.ExtensionPoints_VNODE_NAME));
074            }
075            if (!ci.getServices().isEmpty()) {
076                result.add(new VirtualNode(ci, VirtualNodesConsts.Services_VNODE,
077                        VirtualNodesConsts.Services_VNODE_NAME));
078            }
079            if (!ci.getExtensions().isEmpty()) {
080                result.add(new VirtualNode(ci, VirtualNodesConsts.Contributions_VNODE,
081                        VirtualNodesConsts.Contributions_VNODE_NAME));
082            }
083        } else if (obj.getArtifactType().equals(VirtualNodesConsts.ExtensionPoints_VNODE)) {
084            String cid = ((VirtualNode) obj).getComponentId();
085            ComponentInfo ci = ds.getComponent(cid);
086            for (ExtensionPointInfo epi : ci.getExtensionPoints()) {
087                result.add(epi);
088            }
089        } else if (obj.getArtifactType().equals(VirtualNodesConsts.Contributions_VNODE)) {
090            String cid = ((VirtualNode) obj).getComponentId();
091            ComponentInfo ci = ds.getComponent(cid);
092            for (ExtensionInfo ei : ci.getExtensions()) {
093                result.add(ei);
094            }
095        } else if (obj.getArtifactType().equals(VirtualNodesConsts.Services_VNODE)) {
096            String cid = ((VirtualNode) obj).getComponentId();
097            ComponentInfo ci = ds.getComponent(cid);
098            for (ServiceInfo si : ci.getServices()) {
099                result.add(si);
100            }
101        }
102
103        return result.toArray(new NuxeoArtifact[result.size()]);
104    }
105
106    @Override
107    public String[] getFacets(Object object) {
108        return null;
109    }
110
111    @Override
112    public String getLabel(Object obj) {
113
114        String label = null;
115        if (obj instanceof NuxeoArtifact) {
116            NuxeoArtifact nx = (NuxeoArtifact) obj;
117            label = nx.getId();
118            if (nx.getArtifactType().equals(ExtensionPointInfo.TYPE_NAME)) {
119                label = ((ExtensionPointInfo) nx).getName();
120            } else if (nx.getArtifactType().equals(ExtensionInfo.TYPE_NAME)) {
121                String[] parts = label.split("--");
122                // String component = parts[0];
123                String ep = parts[1];
124                label = ep;
125            } else if (nx.getArtifactType().equals(ServiceInfo.TYPE_NAME)) {
126                String[] parts = label.split("\\.");
127                label = parts[parts.length - 1];
128            } else if (nx.getArtifactType().equals(BundleGroup.TYPE_NAME)) {
129                label = label.replace("grp:", "");
130            } else if (nx.getArtifactType().equals(ComponentInfo.TYPE_NAME)) {
131                if (label.startsWith("org.nuxeo.ecm.platform.")) {
132                    label = label.replace("org.nuxeo.ecm.platform.", "...");
133                } else if (label.startsWith("org.nuxeo.ecm.")) {
134                    label = label.replace("org.nuxeo.ecm.", "...");
135                } else if (label.startsWith("org.nuxeo.")) {
136                    label = label.replace("org.nuxeo.", "...");
137                }
138            }
139        }
140        return label;
141    }
142
143    @Override
144    public String getName(Object obj) {
145        if (obj instanceof NuxeoArtifact) {
146            NuxeoArtifact nx = (NuxeoArtifact) obj;
147            return nx.getId();
148        }
149        return null;
150    }
151
152    @Override
153    public boolean isContainer(Object ob) {
154        NuxeoArtifact obj = (NuxeoArtifact) ob;
155
156        return !(obj.getArtifactType().equals(ExtensionPointInfo.TYPE_NAME)
157                || obj.getArtifactType().equals(ExtensionInfo.TYPE_NAME) || obj.getArtifactType().equals(
158                ServiceInfo.TYPE_NAME));
159    }
160
161}