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        DocumentModel doc = session.createDocumentModel(TYPE_NAME);
038
039        String name = computeDocumentName("service-" + si.getId());
040        String targetPath = new Path(containerPath).append(name).toString();
041        boolean exist = false;
042        if (session.exists(new PathRef(targetPath))) {
043            exist = true;
044            doc = session.getDocument(new PathRef(targetPath));
045        }
046        doc.setPathInfo(containerPath, name);
047        doc.setPropertyValue("dc:title", si.getId());
048
049        doc.setPropertyValue(PROP_CLASS_NAME, si.getId());
050        doc.setPropertyValue(PROP_COMPONENT_ID, si.getComponentId());
051        doc.setPropertyValue(PROP_OVERRIDEN, si.isOverriden());
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 boolean isOverriden() {
069        return safeGet(Boolean.class, PROP_OVERRIDEN, false);
070    }
071    @Override
072    public String getArtifactType() {
073        return TYPE_NAME;
074    }
075
076    @Override
077    public String getVersion() {
078        BundleInfo parentBundle = getParentNuxeoArtifact(BundleInfo.class);
079
080        if (parentBundle != null) {
081            return parentBundle.getVersion();
082        }
083
084        log.error("Unable to determine version for Service " + getId());
085        return "?";
086    }
087
088    @Override
089    public String getComponentId() {
090        return safeGet(PROP_COMPONENT_ID, "unknown_service");
091    }
092
093    @Override
094    public String getHierarchyPath() {
095        String path = super.getHierarchyPath() + "###";
096        String toReplace = "/" + getId() + "###";
097        return path.replace(toReplace, "/" + VirtualNodesConsts.Services_VNODE_NAME + "/" + getId());
098    }
099}