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.documentation;
020
021import java.util.ArrayList;
022import java.util.HashMap;
023import java.util.LinkedHashMap;
024import java.util.List;
025import java.util.Map;
026
027import org.apache.commons.logging.Log;
028import org.apache.commons.logging.LogFactory;
029import org.nuxeo.apidoc.api.AbstractDocumentationItem;
030import org.nuxeo.apidoc.api.AssociatedDocuments;
031import org.nuxeo.apidoc.api.DocumentationItem;
032import org.nuxeo.apidoc.api.ExtensionInfo;
033import org.nuxeo.apidoc.api.ExtensionPointInfo;
034import org.nuxeo.apidoc.api.NuxeoArtifact;
035import org.nuxeo.apidoc.api.ServiceInfo;
036import org.nuxeo.ecm.core.api.CoreSession;
037import org.nuxeo.runtime.api.Framework;
038
039public class AssociatedDocumentsImpl implements AssociatedDocuments {
040
041    protected String id;
042
043    protected final NuxeoArtifact item;
044
045    protected final CoreSession session;
046
047    protected Map<String, ResourceDocumentationItem> liveDoc;
048
049    protected final Log log = LogFactory.getLog(AssociatedDocumentsImpl.class);
050
051    public AssociatedDocumentsImpl(NuxeoArtifact item, CoreSession session) {
052        this.item = item;
053        this.session = session;
054    }
055
056    @Override
057    public Map<String, List<DocumentationItem>> getDocumentationItems(CoreSession session) {
058        DocumentationService ds = Framework.getLocalService(DocumentationService.class);
059        List<DocumentationItem> docItems = ds.findDocumentItems(session, item);
060        Map<String, String> categories = getCategories();
061        Map<String, List<DocumentationItem>> result = new LinkedHashMap<String, List<DocumentationItem>>();
062        // put categories in result in same order
063        List<String> empty = new ArrayList<String>();
064        for (String catLabel : categories.values()) {
065            result.put(catLabel, new ArrayList<DocumentationItem>());
066            empty.add(catLabel);
067        }
068        // read items
069        for (DocumentationItem docItem : docItems) {
070            String cat = docItem.getType();
071            String catLabel = categories.get(cat);
072            result.get(catLabel).add(docItem);
073            empty.remove(catLabel);
074        }
075        // add virtual items
076        if (liveDoc != null) {
077            for (String vCat : liveDoc.keySet()) {
078                String catLabel = categories.get(vCat);
079                if (result.get(catLabel) != null) {
080                    result.get(catLabel).add(liveDoc.get(vCat));
081                    empty.remove(catLabel);
082                } else {
083                    log.warn("Live doc for category " + catLabel + " won't be visible");
084                }
085            }
086        }
087        // clear empty
088        for (String catLabel : empty) {
089            result.remove(catLabel);
090        }
091        return result;
092    }
093
094    @Override
095    public Map<String, String> getCategories() {
096        DocumentationService ds = Framework.getLocalService(DocumentationService.class);
097        return ds.getCategories();
098    }
099
100    @Override
101    public List<String> getCategoryKeys() {
102        DocumentationService ds = Framework.getLocalService(DocumentationService.class);
103        return ds.getCategoryKeys();
104    }
105
106    @Override
107    public DocumentationItem getDescription(CoreSession session) {
108        DocumentationService ds = Framework.getLocalService(DocumentationService.class);
109        List<DocumentationItem> docItems = ds.findDocumentItems(session, item);
110        for (DocumentationItem docItem : docItems) {
111            String cat = docItem.getType();
112            if (DefaultDocumentationType.DESCRIPTION.getValue().equals(cat)) {
113                return docItem;
114            }
115        }
116
117        return new AbstractDocumentationItem() {
118
119            @Override
120            public boolean isApproved() {
121                return false;
122            }
123
124            @Override
125            public String getUUID() {
126                return null;
127            }
128
129            @Override
130            public String getTypeLabel() {
131                return null;
132            }
133
134            @Override
135            public String getType() {
136                return null;
137            }
138
139            @Override
140            public String getTitle() {
141                if (item.getArtifactType().equals(ExtensionPointInfo.TYPE_NAME)) {
142                    return ((ExtensionPointInfo) item).getName();
143                } else if (item.getArtifactType().equals(ExtensionInfo.TYPE_NAME)) {
144                    return ((ExtensionInfo) item).getExtensionPoint();
145                } else if (item.getArtifactType().equals(ServiceInfo.TYPE_NAME)) {
146                    String id = item.getId();
147                    String[] parts = id.split("\\.");
148                    if (parts.length > 1) {
149                        String name = parts[parts.length - 1];
150                        return name;
151                    }
152                }
153                return item.getId();
154            }
155
156            @Override
157            public String getTargetType() {
158                return item.getArtifactType();
159            }
160
161            @Override
162            public String getTarget() {
163                return item.getId();
164            }
165
166            @Override
167            public String getRenderingType() {
168                return "html";
169            }
170
171            @Override
172            public String getId() {
173                return null;
174            }
175
176            @Override
177            public String getContent() {
178                String content = liveDoc.get(DefaultDocumentationType.DESCRIPTION.getValue()).getContent();
179                if (content == null) {
180                    content = "";
181                }
182                return content;
183            }
184
185            @Override
186            public List<String> getApplicableVersion() {
187                return null;
188            }
189
190            @Override
191            public Map<String, String> getAttachments() {
192                return new HashMap<String, String>();
193            }
194
195            @Override
196            public boolean isPlaceHolder() {
197                return true;
198            }
199
200            @Override
201            public String getEditId() {
202                return "placeholder_" + item.getId();
203            }
204
205            public boolean isReadOnly() {
206                return true;
207            }
208        };
209
210    }
211
212    public void setLiveDoc(Map<String, ResourceDocumentationItem> liveDoc) {
213        this.liveDoc = liveDoc;
214    }
215
216}