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.ArrayList;
024import java.util.Collection;
025import java.util.List;
026import java.util.Map;
027
028import org.nuxeo.apidoc.api.BundleInfo;
029import org.nuxeo.apidoc.api.ComponentInfo;
030import org.nuxeo.apidoc.documentation.ResourceDocumentationItem;
031import org.nuxeo.common.utils.Path;
032import org.nuxeo.ecm.core.api.Blob;
033import org.nuxeo.ecm.core.api.Blobs;
034import org.nuxeo.ecm.core.api.CoreSession;
035import org.nuxeo.ecm.core.api.DocumentModel;
036import org.nuxeo.ecm.core.api.PathRef;
037
038public class BundleInfoDocAdapter extends BaseNuxeoArtifactDocAdapter implements BundleInfo {
039
040    public static BundleInfoDocAdapter create(BundleInfo bundleInfo, CoreSession session, String containerPath) {
041
042        DocumentModel doc = session.createDocumentModel(TYPE_NAME);
043        String name = computeDocumentName("bundle-" + bundleInfo.getId());
044        String targetPath = new Path(containerPath).append(name).toString();
045        boolean exist = false;
046        if (session.exists(new PathRef(targetPath))) {
047            exist = true;
048            doc = session.getDocument(new PathRef(targetPath));
049        }
050        doc.setPathInfo(containerPath, name);
051        doc.setPropertyValue("dc:title", bundleInfo.getBundleId());
052        doc.setPropertyValue(PROP_ARTIFACT_GROUP_ID, bundleInfo.getGroupId());
053        doc.setPropertyValue(PROP_ARTIFACT_ID, bundleInfo.getArtifactId());
054        doc.setPropertyValue(PROP_ARTIFACT_VERSION, bundleInfo.getArtifactVersion());
055        doc.setPropertyValue(PROP_BUNDLE_ID, bundleInfo.getId());
056        doc.setPropertyValue(PROP_JAR_NAME, bundleInfo.getFileName());
057        String manifest = bundleInfo.getManifest();
058        if (manifest != null) {
059            Blob manifestBlob = Blobs.createBlob(manifest);
060            manifestBlob.setFilename("MANIFEST.MF");
061            doc.setPropertyValue("file:content", (Serializable) manifestBlob);
062        }
063
064        if (exist) {
065            doc = session.saveDocument(doc);
066        } else {
067            doc = session.createDocument(doc);
068        }
069
070        return new BundleInfoDocAdapter(doc);
071    }
072
073    public BundleInfoDocAdapter(DocumentModel doc) {
074        super(doc);
075    }
076
077    @Override
078    public String getArtifactId() {
079        return safeGet(PROP_ARTIFACT_ID);
080    }
081
082    @Override
083    public String getBundleId() {
084        return safeGet(PROP_BUNDLE_ID);
085    }
086
087    @Override
088    public Collection<ComponentInfo> getComponents() {
089        List<ComponentInfo> components = new ArrayList<>();
090        List<DocumentModel> children = getCoreSession().getChildren(doc.getRef());
091        for (DocumentModel child : children) {
092            ComponentInfo comp = child.getAdapter(ComponentInfo.class);
093            if (comp != null) {
094                components.add(comp);
095            }
096        }
097        return components;
098    }
099
100    @Override
101    public String getFileName() {
102        return safeGet(PROP_JAR_NAME);
103    }
104
105    @Override
106    public String getGroupId() {
107        return safeGet(PROP_ARTIFACT_GROUP_ID);
108    }
109
110    @Override
111    public String getLocation() {
112        // TODO Auto-generated method stub
113        return null;
114    }
115
116    @Override
117    public String getManifest() {
118        try {
119            Blob mf = safeGet(Blob.class, "file:content", null);
120            if (mf == null) {
121                return "No MANIFEST.MF";
122            }
123            if (mf.getEncoding() == null || "".equals(mf.getEncoding())) {
124                mf.setEncoding("utf-8");
125            }
126            return mf.getString();
127        } catch (IOException e) {
128            log.error("Error while reading blob", e);
129            return "";
130        }
131    }
132
133    @Override
134    public String[] getRequirements() {
135        return null;
136    }
137
138    @Override
139    public String getArtifactVersion() {
140        return safeGet(PROP_ARTIFACT_VERSION, null);
141    }
142
143    @Override
144    public String getId() {
145        return getBundleId();
146    }
147
148    @Override
149    public String getVersion() {
150        return getArtifactVersion();
151    }
152
153    @Override
154    public String getArtifactType() {
155        return TYPE_NAME;
156    }
157
158    @Override
159    public Map<String, ResourceDocumentationItem> getLiveDoc() {
160        throw new UnsupportedOperationException();
161    }
162
163    @Override
164    public Map<String, ResourceDocumentationItem> getParentLiveDoc() {
165        throw new UnsupportedOperationException();
166    }
167
168}