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.util.ArrayList;
022import java.util.Collection;
023import java.util.List;
024
025import org.nuxeo.apidoc.api.BundleInfo;
026import org.nuxeo.apidoc.api.ComponentInfo;
027import org.nuxeo.apidoc.api.ExtensionInfo;
028import org.nuxeo.apidoc.api.ExtensionPointInfo;
029import org.nuxeo.apidoc.api.QueryHelper;
030import org.nuxeo.apidoc.api.VirtualNodesConsts;
031import org.nuxeo.apidoc.documentation.DocumentationHelper;
032import org.nuxeo.apidoc.snapshot.DistributionSnapshot;
033import org.nuxeo.common.utils.Path;
034import org.nuxeo.ecm.core.api.CoreSession;
035import org.nuxeo.ecm.core.api.DocumentModel;
036import org.nuxeo.ecm.core.api.DocumentModelList;
037import org.nuxeo.ecm.core.api.PathRef;
038import org.nuxeo.ecm.core.api.PropertyException;
039
040public class ExtensionPointInfoDocAdapter extends BaseNuxeoArtifactDocAdapter implements ExtensionPointInfo {
041
042    public static ExtensionPointInfoDocAdapter create(ExtensionPointInfo xpi, CoreSession session, String containerPath)
043            {
044
045        DocumentModel doc = session.createDocumentModel(TYPE_NAME);
046
047        String name = computeDocumentName("xp-" + xpi.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", xpi.getId());
056
057        doc.setPropertyValue(PROP_NAME, xpi.getName());
058        doc.setPropertyValue(PROP_EP_ID, xpi.getId());
059        doc.setPropertyValue(PROP_DOC, xpi.getDocumentation());
060        // TODO incoherent naming here, also schema has no types
061        doc.setPropertyValue(PROP_DESCRIPTORS, xpi.getDescriptors());
062
063        if (exist) {
064            doc = session.saveDocument(doc);
065        } else {
066            doc = session.createDocument(doc);
067        }
068        return new ExtensionPointInfoDocAdapter(doc);
069    }
070
071    public ExtensionPointInfoDocAdapter(DocumentModel doc) {
072        super(doc);
073    }
074
075    @Override
076    public ComponentInfo getComponent() {
077        log.error("getComponent Not implemented");
078        return null;
079    }
080
081    @Override
082    public String getComponentId() {
083        return getId().split("--")[0];
084    }
085
086    @Override
087    public String getDocumentation() {
088        return safeGet(PROP_DOC);
089    }
090
091    @Override
092    public String getDocumentationHtml() {
093        return DocumentationHelper.getHtml(getDocumentation());
094    }
095
096    @Override
097    public Collection<ExtensionInfo> getExtensions() {
098        List<ExtensionInfo> result = new ArrayList<ExtensionInfo>();
099        // find root doc for distribution
100        DocumentModel dist = doc;
101        while (!DistributionSnapshot.TYPE_NAME.equals(dist.getType())) {
102            dist = getCoreSession().getParentDocument(dist.getRef());
103        }
104        String query = QueryHelper.select(ExtensionInfo.TYPE_NAME, dist, ExtensionInfo.PROP_EXTENSION_POINT, getId());
105        DocumentModelList docs = getCoreSession().query(query);
106        for (DocumentModel contribDoc : docs) {
107            ExtensionInfo contrib = contribDoc.getAdapter(ExtensionInfo.class);
108            if (contrib != null) {
109                result.add(contrib);
110            }
111        }
112        return result;
113    }
114
115    @Override
116    public String getName() {
117        return safeGet(PROP_NAME);
118    }
119
120    @Override
121    public String[] getDescriptors() {
122        try {
123            @SuppressWarnings("unchecked")
124            List<String> descriptors = (List<String>) doc.getPropertyValue(PROP_DESCRIPTORS);
125            return descriptors.toArray(new String[0]);
126        } catch (PropertyException e) {
127            log.error("Unable to get descriptors field", e);
128        }
129        return null;
130    }
131
132    @Override
133    public String getId() {
134        return safeGet(PROP_EP_ID);
135    }
136
137    @Override
138    public String getVersion() {
139        BundleInfo parentBundle = getParentNuxeoArtifact(BundleInfo.class);
140
141        if (parentBundle != null) {
142            return parentBundle.getVersion();
143        }
144
145        log.error("Unable to determine version for ExtensionPoint " + getId());
146        return "?";
147    }
148
149    @Override
150    public String getArtifactType() {
151        return TYPE_NAME;
152    }
153
154    @Override
155    public String getLabel() {
156        return getName() + " (" + getComponent().getId() + ")";
157    }
158
159    @Override
160    public String getHierarchyPath() {
161        String path = super.getHierarchyPath() + "###";
162        String toReplace = "/" + getId() + "###";
163        return path.replace(toReplace, "/" + VirtualNodesConsts.ExtensionPoints_VNODE_NAME + "/" + getId());
164    }
165
166}