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