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.net.URL;
022import java.util.ArrayList;
023import java.util.Collection;
024import java.util.List;
025
026import org.nuxeo.apidoc.api.BundleInfo;
027import org.nuxeo.apidoc.api.ComponentInfo;
028import org.nuxeo.apidoc.api.ExtensionInfo;
029import org.nuxeo.apidoc.api.ExtensionPointInfo;
030import org.nuxeo.apidoc.api.QueryHelper;
031import org.nuxeo.apidoc.api.ServiceInfo;
032import org.nuxeo.apidoc.documentation.DocumentationHelper;
033import org.nuxeo.common.utils.Path;
034import org.nuxeo.ecm.core.api.Blob;
035import org.nuxeo.ecm.core.api.Blobs;
036import org.nuxeo.ecm.core.api.CoreSession;
037import org.nuxeo.ecm.core.api.DocumentModel;
038import org.nuxeo.ecm.core.api.DocumentModelList;
039import org.nuxeo.ecm.core.api.PathRef;
040import org.nuxeo.ecm.core.api.PropertyException;
041
042public class ComponentInfoDocAdapter extends BaseNuxeoArtifactDocAdapter implements ComponentInfo {
043
044    public ComponentInfoDocAdapter(DocumentModel doc) {
045        super(doc);
046    }
047
048    public static ComponentInfoDocAdapter create(ComponentInfo componentInfo, CoreSession session, String containerPath)
049            throws IOException {
050
051        DocumentModel doc = session.createDocumentModel(TYPE_NAME);
052
053        String name = computeDocumentName("component-" + componentInfo.getId());
054        String targetPath = new Path(containerPath).append(name).toString();
055        boolean exist = false;
056        if (session.exists(new PathRef(targetPath))) {
057            exist = true;
058            doc = session.getDocument(new PathRef(targetPath));
059        }
060        doc.setPathInfo(containerPath, name);
061        doc.setPropertyValue("dc:title", componentInfo.getName());
062        doc.setPropertyValue(PROP_COMPONENT_ID, componentInfo.getId());
063        doc.setPropertyValue(PROP_COMPONENT_NAME, componentInfo.getName());
064        doc.setPropertyValue(PROP_COMPONENT_CLASS, componentInfo.getComponentClass());
065        doc.setPropertyValue(PROP_BUILT_IN_DOC, componentInfo.getDocumentation());
066        doc.setPropertyValue(PROP_IS_XML, Boolean.valueOf(componentInfo.isXmlPureComponent()));
067        doc.setPropertyValue(PROP_SERVICES, (Serializable) componentInfo.getServiceNames());
068
069        Blob xmlBlob = Blobs.createBlob(componentInfo.getXmlFileContent(), "text/xml", null,
070                componentInfo.getXmlFileName());
071        doc.setPropertyValue("file:content", (Serializable) xmlBlob);
072
073        if (exist) {
074            doc = session.saveDocument(doc);
075        } else {
076            doc = session.createDocument(doc);
077        }
078        return new ComponentInfoDocAdapter(doc);
079    }
080
081    @Override
082    public BundleInfo getBundle() {
083        DocumentModel parent = getCoreSession().getDocument(doc.getParentRef());
084        return parent.getAdapter(BundleInfo.class);
085    }
086
087    @Override
088    public String getComponentClass() {
089        return safeGet(PROP_COMPONENT_CLASS);
090    }
091
092    @Override
093    public String getDocumentation() {
094        return safeGet(PROP_BUILT_IN_DOC);
095    }
096
097    @Override
098    public String getDocumentationHtml() {
099        return DocumentationHelper.getHtml(getDocumentation());
100    }
101
102    @Override
103    public ExtensionPointInfo getExtensionPoint(String name) {
104        // TODO Auto-generated method stub
105        return null;
106    }
107
108    @Override
109    public Collection<ExtensionPointInfo> getExtensionPoints() {
110        List<ExtensionPointInfo> xps = new ArrayList<ExtensionPointInfo>();
111        String query = QueryHelper.select(ExtensionPointInfo.TYPE_NAME, doc);
112        DocumentModelList docs = getCoreSession().query(query);
113        for (DocumentModel child : docs) {
114            ExtensionPointInfo xp = child.getAdapter(ExtensionPointInfo.class);
115            if (xp != null) {
116                xps.add(xp);
117            }
118        }
119        return xps;
120    }
121
122    @Override
123    public Collection<ExtensionInfo> getExtensions() {
124        List<ExtensionInfo> contribs = new ArrayList<ExtensionInfo>();
125        String query = QueryHelper.select(ExtensionInfo.TYPE_NAME, doc);
126        DocumentModelList docs = getCoreSession().query(query);
127        for (DocumentModel child : docs) {
128            ExtensionInfo xp = child.getAdapter(ExtensionInfo.class);
129            if (xp != null) {
130                contribs.add(xp);
131            }
132        }
133        return contribs;
134    }
135
136    @Override
137    public String getName() {
138        return safeGet(PROP_COMPONENT_NAME);
139    }
140
141    @Override
142    @SuppressWarnings("unchecked")
143    public List<String> getServiceNames() {
144        try {
145            return (List<String>) doc.getPropertyValue(PROP_SERVICES);
146        } catch (PropertyException e) {
147            log.error("Error while getting service names", e);
148        }
149        return null;
150    }
151
152    @Override
153    public String getXmlFileContent() throws IOException {
154        try {
155            Blob xml = safeGet(Blob.class, "file:content", null);
156            if (xml.getEncoding() == null || "".equals(xml.getEncoding())) {
157                xml.setEncoding("utf-8");
158            }
159            return xml.getString();
160        } catch (IOException e) {
161            log.error("Error while reading blob", e);
162            return "";
163        }
164    }
165
166    @Override
167    public String getXmlFileName() {
168        Blob xml = safeGet(Blob.class, "file:content", null);
169        return xml == null ? "" : xml.getFilename() == null ? "" : xml.getFilename();
170    }
171
172    @Override
173    public URL getXmlFileUrl() {
174        return null;
175    }
176
177    @Override
178    public boolean isXmlPureComponent() {
179        Boolean isXml = safeGet(Boolean.class, PROP_IS_XML, Boolean.TRUE);
180        return isXml == null ? true : isXml.booleanValue();
181    }
182
183    @Override
184    public String getId() {
185        return getName();
186    }
187
188    @Override
189    public String getVersion() {
190
191        BundleInfo parentBundle = getParentNuxeoArtifact(BundleInfo.class);
192
193        if (parentBundle != null) {
194            return parentBundle.getVersion();
195        }
196
197        log.error("Unable to determine version for Component " + getId());
198        return "?";
199    }
200
201    @Override
202    public String getArtifactType() {
203        return TYPE_NAME;
204    }
205
206    @Override
207    public List<ServiceInfo> getServices() {
208        List<ServiceInfo> result = new ArrayList<ServiceInfo>();
209        String query = QueryHelper.select(ServiceInfo.TYPE_NAME, doc);
210        DocumentModelList docs = getCoreSession().query(query);
211        for (DocumentModel siDoc : docs) {
212            ServiceInfo si = siDoc.getAdapter(ServiceInfo.class);
213            if (si != null) {
214                result.add(si);
215            }
216        }
217        return result;
218    }
219
220}