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
047        DocumentModel doc = session.createDocumentModel(TYPE_NAME);
048
049        String name = computeDocumentName("contrib-" + xi.getId());
050        String targetPath = new Path(containerPath).append(name).toString();
051        boolean exist = false;
052        if (session.exists(new PathRef(targetPath))) {
053            exist = true;
054            doc = session.getDocument(new PathRef(targetPath));
055        }
056        doc.setPathInfo(containerPath, name);
057        doc.setPropertyValue("dc:title", xi.getId());
058
059        doc.setPropertyValue(PROP_CONTRIB_ID, xi.getId());
060        doc.setPropertyValue(PROP_DOC, xi.getDocumentation());
061        doc.setPropertyValue(PROP_EXTENSION_POINT, xi.getExtensionPoint());
062        doc.setPropertyValue(PROP_TARGET_COMPONENT_NAME, xi.getTargetComponentName().getName());
063
064        Blob xmlBlob = Blobs.createBlob(xi.getXml(), "text/xml", null, "contrib.xml"); // !!!!!
065        doc.setPropertyValue("file:content", (Serializable) xmlBlob);
066
067        if (exist) {
068            doc = session.saveDocument(doc);
069        } else {
070            doc = session.createDocument(doc);
071        }
072
073        return new ExtensionInfoDocAdapter(doc);
074    }
075
076    public ExtensionInfoDocAdapter(DocumentModel doc) {
077        super(doc);
078    }
079
080    @Override
081    public String getDocumentation() {
082        return safeGet(PROP_DOC);
083    }
084
085    @Override
086    public String getDocumentationHtml() {
087        return DocumentationHelper.getHtml(getDocumentation());
088    }
089
090    @Override
091    public String getExtensionPoint() {
092        return safeGet(PROP_EXTENSION_POINT);
093    }
094
095    @Override
096    public String getId() {
097        return safeGet(PROP_CONTRIB_ID);
098    }
099
100    @Override
101    public ComponentName getTargetComponentName() {
102        return new ComponentName(safeGet(PROP_TARGET_COMPONENT_NAME));
103    }
104
105    @Override
106    public String getXml() {
107        try {
108            Blob xml = safeGet(Blob.class, "file:content", null);
109            if (xml == null) {
110                return "";
111            }
112            if (xml.getEncoding() == null || "".equals(xml.getEncoding())) {
113                xml.setEncoding("utf-8");
114            }
115            return xml.getString();
116        } catch (IOException e) {
117            log.error("Error while reading blob", e);
118            return "";
119        }
120    }
121
122    @Override
123    public String getVersion() {
124
125        BundleInfo parentBundle = getParentNuxeoArtifact(BundleInfo.class);
126
127        if (parentBundle != null) {
128            return parentBundle.getVersion();
129        }
130
131        log.error("Unable to determine version for Contribution " + getId());
132        return "?";
133    }
134
135    @Override
136    public String getArtifactType() {
137        return TYPE_NAME;
138    }
139
140    @Override
141    public String getHierarchyPath() {
142        String path = super.getHierarchyPath() + "###";
143        String toReplace = "/" + getId() + "###";
144        return path.replace(toReplace, "/" + VirtualNodesConsts.Contributions_VNODE_NAME + "/" + getId());
145    }
146
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}