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 org.nuxeo.apidoc.api.BundleInfo;
022import org.nuxeo.apidoc.api.ServiceInfo;
023import org.nuxeo.apidoc.api.VirtualNodesConsts;
024import org.nuxeo.common.utils.Path;
025import org.nuxeo.ecm.core.api.CoreSession;
026import org.nuxeo.ecm.core.api.DocumentModel;
027import org.nuxeo.ecm.core.api.PathRef;
028
029public class ServiceInfoDocAdapter extends BaseNuxeoArtifactDocAdapter implements ServiceInfo {
030
031    public ServiceInfoDocAdapter(DocumentModel doc) {
032        super(doc);
033    }
034
035    public static ServiceInfoDocAdapter create(ServiceInfo si, CoreSession session, String containerPath)
036            {
037
038        DocumentModel doc = session.createDocumentModel(TYPE_NAME);
039
040        String name = computeDocumentName("service-" + si.getId());
041        String targetPath = new Path(containerPath).append(name).toString();
042        boolean exist = false;
043        if (session.exists(new PathRef(targetPath))) {
044            exist = true;
045            doc = session.getDocument(new PathRef(targetPath));
046        }
047        doc.setPathInfo(containerPath, name);
048        doc.setPropertyValue("dc:title", si.getId());
049
050        doc.setPropertyValue(PROP_CLASS_NAME, si.getId());
051        doc.setPropertyValue(PROP_COMPONENT_ID, si.getComponentId());
052
053        if (exist) {
054            doc = session.saveDocument(doc);
055        } else {
056            doc = session.createDocument(doc);
057        }
058
059        return new ServiceInfoDocAdapter(doc);
060    }
061
062    @Override
063    public String getId() {
064        return safeGet(PROP_CLASS_NAME, "unknown_service");
065    }
066
067    @Override
068    public String getArtifactType() {
069        return TYPE_NAME;
070    }
071
072    @Override
073    public String getVersion() {
074        BundleInfo parentBundle = getParentNuxeoArtifact(BundleInfo.class);
075
076        if (parentBundle != null) {
077            return parentBundle.getVersion();
078        }
079
080        log.error("Unable to determine version for Service " + getId());
081        return "?";
082    }
083
084    @Override
085    public String getComponentId() {
086        return safeGet(PROP_COMPONENT_ID, "unknown_service");
087    }
088
089    @Override
090    public String getHierarchyPath() {
091        String path = super.getHierarchyPath() + "###";
092        String toReplace = "/" + getId() + "###";
093        return path.replace(toReplace, "/" + VirtualNodesConsts.Services_VNODE_NAME + "/" + getId());
094    }
095}