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.introspection;
020
021import java.util.ArrayList;
022import java.util.HashMap;
023import java.util.List;
024import java.util.Map;
025
026import org.nuxeo.apidoc.api.BaseNuxeoArtifact;
027import org.nuxeo.apidoc.api.BundleGroup;
028import org.nuxeo.apidoc.documentation.AssociatedDocumentsImpl;
029import org.nuxeo.apidoc.documentation.ResourceDocumentationItem;
030import org.nuxeo.ecm.core.api.CoreSession;
031
032public class BundleGroupImpl extends BaseNuxeoArtifact implements BundleGroup {
033
034    protected final String key;
035
036    protected final String name;
037
038    protected final List<BundleGroup> subGroups = new ArrayList<BundleGroup>();
039
040    protected final List<String> bundleIds = new ArrayList<String>();
041
042    protected final String version;
043
044    protected final List<String> parentIds = new ArrayList<String>();
045
046    protected Map<String, ResourceDocumentationItem> liveDoc;
047
048    public BundleGroupImpl(String key, String version) {
049        this.key = key;
050        if (key.startsWith("grp:")) {
051            name = key.substring(4);
052        } else {
053            name = key;
054        }
055        this.version = version;
056    }
057
058    void addParent(String bgId) {
059        parentIds.add(bgId);
060    }
061
062    public String getKey() {
063        return key;
064    }
065
066    @Override
067    public String getName() {
068        return name;
069    }
070
071    public void add(BundleGroupImpl group) {
072        subGroups.add(group);
073    }
074
075    public void add(String bundleId) {
076        bundleIds.add(bundleId);
077    }
078
079    @Override
080    public List<BundleGroup> getSubGroups() {
081        return subGroups;
082    }
083
084    @Override
085    public List<String> getBundleIds() {
086        return bundleIds;
087    }
088
089    @Override
090    public String getId() {
091        return key;
092    }
093
094    @Override
095    public String getVersion() {
096        return version;
097    }
098
099    @Override
100    public String getArtifactType() {
101        return TYPE_NAME;
102    }
103
104    @Override
105    public String getHierarchyPath() {
106        String path = "";
107        for (String parentId : parentIds) {
108            path = path + "/" + parentId;
109        }
110        return path + "/" + getId();
111    }
112
113    public void addLiveDoc(Map<String, ResourceDocumentationItem> newLiveDoc) {
114        if (liveDoc == null) {
115            liveDoc = new HashMap<String, ResourceDocumentationItem>();
116        }
117        if (newLiveDoc != null) {
118            for (String key : newLiveDoc.keySet()) {
119                if (newLiveDoc.get(key) != null) {
120                    liveDoc.put(key, new ResourceDocumentationItem(newLiveDoc.get(key), this));
121                }
122            }
123        }
124    }
125
126    @Override
127    public AssociatedDocumentsImpl getAssociatedDocuments(CoreSession session) {
128        AssociatedDocumentsImpl docs = super.getAssociatedDocuments(session);
129        if (liveDoc != null) {
130            docs.setLiveDoc(liveDoc);
131        }
132        return docs;
133    }
134
135    public Map<String, ResourceDocumentationItem> getLiveDoc() {
136        return liveDoc;
137    }
138
139}