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.io.IOException; 022import java.io.Serializable; 023import java.util.Collections; 024import java.util.List; 025 026import org.dom4j.DocumentException; 027import org.nuxeo.apidoc.api.BundleInfo; 028import org.nuxeo.apidoc.api.ComponentInfo; 029import org.nuxeo.apidoc.api.ExtensionInfo; 030import org.nuxeo.apidoc.api.VirtualNodesConsts; 031import org.nuxeo.apidoc.documentation.ContributionItem; 032import org.nuxeo.apidoc.documentation.DocumentationHelper; 033import org.nuxeo.apidoc.documentation.XMLContributionParser; 034import org.nuxeo.common.utils.Path; 035import org.nuxeo.ecm.core.api.Blob; 036import org.nuxeo.ecm.core.api.Blobs; 037import org.nuxeo.ecm.core.api.CoreSession; 038import org.nuxeo.ecm.core.api.DocumentModel; 039import org.nuxeo.ecm.core.api.PathRef; 040import org.nuxeo.runtime.model.ComponentName; 041 042public class ExtensionInfoDocAdapter extends BaseNuxeoArtifactDocAdapter implements ExtensionInfo { 043 044 public static ExtensionInfoDocAdapter create(ExtensionInfo xi, CoreSession session, String containerPath) { 045 046 DocumentModel doc = session.createDocumentModel(TYPE_NAME); 047 048 String name = computeDocumentName("contrib-" + xi.getId()); 049 String targetPath = new Path(containerPath).append(name).toString(); 050 boolean exist = false; 051 if (session.exists(new PathRef(targetPath))) { 052 exist = true; 053 doc = session.getDocument(new PathRef(targetPath)); 054 } 055 doc.setPathInfo(containerPath, name); 056 doc.setPropertyValue("dc:title", xi.getId()); 057 058 doc.setPropertyValue(PROP_CONTRIB_ID, xi.getId()); 059 doc.setPropertyValue(PROP_DOC, xi.getDocumentation()); 060 doc.setPropertyValue(PROP_EXTENSION_POINT, xi.getExtensionPoint()); 061 doc.setPropertyValue(PROP_TARGET_COMPONENT_NAME, xi.getTargetComponentName().getName()); 062 063 Blob xmlBlob = Blobs.createBlob(xi.getXml(), "text/xml", null, "contrib.xml"); // !!!!! 064 doc.setPropertyValue("file:content", (Serializable) xmlBlob); 065 066 if (exist) { 067 doc = session.saveDocument(doc); 068 } else { 069 doc = session.createDocument(doc); 070 } 071 072 return new ExtensionInfoDocAdapter(doc); 073 } 074 075 public ExtensionInfoDocAdapter(DocumentModel doc) { 076 super(doc); 077 } 078 079 @Override 080 public String getDocumentation() { 081 return safeGet(PROP_DOC); 082 } 083 084 @Override 085 public String getDocumentationHtml() { 086 return DocumentationHelper.getHtml(getDocumentation()); 087 } 088 089 @Override 090 public String getExtensionPoint() { 091 return safeGet(PROP_EXTENSION_POINT); 092 } 093 094 @Override 095 public String getId() { 096 return safeGet(PROP_CONTRIB_ID); 097 } 098 099 @Override 100 public ComponentName getTargetComponentName() { 101 return new ComponentName(safeGet(PROP_TARGET_COMPONENT_NAME)); 102 } 103 104 @Override 105 public String getXml() { 106 try { 107 Blob xml = safeGet(Blob.class, "file:content", null); 108 if (xml == null) { 109 return ""; 110 } 111 if (xml.getEncoding() == null || "".equals(xml.getEncoding())) { 112 xml.setEncoding("utf-8"); 113 } 114 return xml.getString(); 115 } catch (IOException e) { 116 log.error("Error while reading blob", e); 117 return ""; 118 } 119 } 120 121 @Override 122 public String getVersion() { 123 124 BundleInfo parentBundle = getParentNuxeoArtifact(BundleInfo.class); 125 126 if (parentBundle != null) { 127 return parentBundle.getVersion(); 128 } 129 130 log.error("Unable to determine version for Contribution " + getId()); 131 return "?"; 132 } 133 134 @Override 135 public String getArtifactType() { 136 return TYPE_NAME; 137 } 138 139 @Override 140 public String getHierarchyPath() { 141 String path = super.getHierarchyPath() + "###"; 142 String toReplace = "/" + getId() + "###"; 143 return path.replace(toReplace, "/" + VirtualNodesConsts.Contributions_VNODE_NAME + "/" + getId()); 144 } 145 146 @Override 147 public List<ContributionItem> getContributionItems() { 148 try { 149 return XMLContributionParser.extractContributionItems(getXml()); 150 } catch (DocumentException e) { 151 log.error(e, e); 152 return Collections.emptyList(); 153 } 154 } 155 156 @Override 157 public ComponentInfo getComponent() { 158 String cId = getId().split("--")[0]; 159 ComponentInfo parentComponent = getParentNuxeoArtifact(ComponentInfo.class); 160 if (parentComponent.getId().equals(cId)) { 161 return parentComponent; 162 } 163 return null; 164 } 165 166}