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