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