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