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