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.adapters; 018 019import java.util.ArrayList; 020import java.util.Collection; 021import java.util.List; 022 023import org.nuxeo.apidoc.api.BundleInfo; 024import org.nuxeo.apidoc.api.ComponentInfo; 025import org.nuxeo.apidoc.api.ExtensionInfo; 026import org.nuxeo.apidoc.api.ExtensionPointInfo; 027import org.nuxeo.apidoc.api.QueryHelper; 028import org.nuxeo.apidoc.api.VirtualNodesConsts; 029import org.nuxeo.apidoc.documentation.DocumentationHelper; 030import org.nuxeo.apidoc.snapshot.DistributionSnapshot; 031import org.nuxeo.common.utils.Path; 032import org.nuxeo.ecm.core.api.CoreSession; 033import org.nuxeo.ecm.core.api.DocumentModel; 034import org.nuxeo.ecm.core.api.DocumentModelList; 035import org.nuxeo.ecm.core.api.PathRef; 036import org.nuxeo.ecm.core.api.PropertyException; 037 038public class ExtensionPointInfoDocAdapter extends BaseNuxeoArtifactDocAdapter implements ExtensionPointInfo { 039 040 public static ExtensionPointInfoDocAdapter create(ExtensionPointInfo xpi, CoreSession session, String containerPath) 041 { 042 043 DocumentModel doc = session.createDocumentModel(TYPE_NAME); 044 045 String name = computeDocumentName("xp-" + xpi.getId()); 046 String targetPath = new Path(containerPath).append(name).toString(); 047 boolean exist = false; 048 if (session.exists(new PathRef(targetPath))) { 049 exist = true; 050 doc = session.getDocument(new PathRef(targetPath)); 051 } 052 doc.setPathInfo(containerPath, name); 053 doc.setPropertyValue("dc:title", xpi.getId()); 054 055 doc.setPropertyValue(PROP_NAME, xpi.getName()); 056 doc.setPropertyValue(PROP_EP_ID, xpi.getId()); 057 doc.setPropertyValue(PROP_DOC, xpi.getDocumentation()); 058 // TODO incoherent naming here, also schema has no types 059 doc.setPropertyValue(PROP_DESCRIPTORS, xpi.getDescriptors()); 060 061 if (exist) { 062 doc = session.saveDocument(doc); 063 } else { 064 doc = session.createDocument(doc); 065 } 066 return new ExtensionPointInfoDocAdapter(doc); 067 } 068 069 public ExtensionPointInfoDocAdapter(DocumentModel doc) { 070 super(doc); 071 } 072 073 @Override 074 public ComponentInfo getComponent() { 075 log.error("getComponent Not implemented"); 076 return null; 077 } 078 079 @Override 080 public String getComponentId() { 081 return getId().split("--")[0]; 082 } 083 084 @Override 085 public String getDocumentation() { 086 return safeGet(PROP_DOC); 087 } 088 089 @Override 090 public String getDocumentationHtml() { 091 return DocumentationHelper.getHtml(getDocumentation()); 092 } 093 094 @Override 095 public Collection<ExtensionInfo> getExtensions() { 096 List<ExtensionInfo> result = new ArrayList<ExtensionInfo>(); 097 // find root doc for distribution 098 DocumentModel dist = doc; 099 while (!DistributionSnapshot.TYPE_NAME.equals(dist.getType())) { 100 dist = getCoreSession().getParentDocument(dist.getRef()); 101 } 102 String query = QueryHelper.select(ExtensionInfo.TYPE_NAME, dist, ExtensionInfo.PROP_EXTENSION_POINT, getId()); 103 DocumentModelList docs = getCoreSession().query(query); 104 for (DocumentModel contribDoc : docs) { 105 ExtensionInfo contrib = contribDoc.getAdapter(ExtensionInfo.class); 106 if (contrib != null) { 107 result.add(contrib); 108 } 109 } 110 return result; 111 } 112 113 @Override 114 public String getName() { 115 return safeGet(PROP_NAME); 116 } 117 118 @Override 119 public String[] getDescriptors() { 120 try { 121 @SuppressWarnings("unchecked") 122 List<String> descriptors = (List<String>) doc.getPropertyValue(PROP_DESCRIPTORS); 123 return descriptors.toArray(new String[0]); 124 } catch (PropertyException e) { 125 log.error("Unable to get descriptors field", e); 126 } 127 return null; 128 } 129 130 @Override 131 public String getId() { 132 return safeGet(PROP_EP_ID); 133 } 134 135 @Override 136 public String getVersion() { 137 BundleInfo parentBundle = getParentNuxeoArtifact(BundleInfo.class); 138 139 if (parentBundle != null) { 140 return parentBundle.getVersion(); 141 } 142 143 log.error("Unable to determine version for ExtensionPoint " + getId()); 144 return "?"; 145 } 146 147 @Override 148 public String getArtifactType() { 149 return TYPE_NAME; 150 } 151 152 @Override 153 public String getLabel() { 154 return getName() + " (" + getComponent().getId() + ")"; 155 } 156 157 @Override 158 public String getHierarchyPath() { 159 String path = super.getHierarchyPath() + "###"; 160 String toReplace = "/" + getId() + "###"; 161 return path.replace(toReplace, "/" + VirtualNodesConsts.ExtensionPoints_VNODE_NAME + "/" + getId()); 162 } 163 164}