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