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<>(); 062 // put categories in result in same order 063 List<String> empty = new ArrayList<>(); 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(null) { 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 getType() { 131 return null; 132 } 133 134 @Override 135 public String getTitle() { 136 if (item.getArtifactType().equals(ExtensionPointInfo.TYPE_NAME)) { 137 return ((ExtensionPointInfo) item).getName(); 138 } else if (item.getArtifactType().equals(ExtensionInfo.TYPE_NAME)) { 139 return ((ExtensionInfo) item).getExtensionPoint(); 140 } else if (item.getArtifactType().equals(ServiceInfo.TYPE_NAME)) { 141 String id = item.getId(); 142 String[] parts = id.split("\\."); 143 if (parts.length > 1) { 144 String name = parts[parts.length - 1]; 145 return name; 146 } 147 } 148 return item.getId(); 149 } 150 151 @Override 152 public String getTargetType() { 153 return item.getArtifactType(); 154 } 155 156 @Override 157 public String getTarget() { 158 return item.getId(); 159 } 160 161 @Override 162 public String getRenderingType() { 163 return "html"; 164 } 165 166 @Override 167 public String getId() { 168 return null; 169 } 170 171 @Override 172 public String getContent() { 173 String content = liveDoc.get(DefaultDocumentationType.DESCRIPTION.getValue()).getContent(); 174 if (content == null) { 175 content = ""; 176 } 177 return content; 178 } 179 180 @Override 181 public List<String> getApplicableVersion() { 182 return null; 183 } 184 185 @Override 186 public Map<String, String> getAttachments() { 187 return new HashMap<>(); 188 } 189 190 @Override 191 public boolean isPlaceHolder() { 192 return true; 193 } 194 195 @Override 196 public String getEditId() { 197 return "placeholder_" + item.getId(); 198 } 199 200 @Override 201 public boolean isReadOnly() { 202 return true; 203 } 204 }; 205 206 } 207 208 public void setLiveDoc(Map<String, ResourceDocumentationItem> liveDoc) { 209 this.liveDoc = liveDoc; 210 } 211 212}