001/*
002 * (C) Copyright 2006-2010 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Thierry Delprat
016 */
017package org.nuxeo.apidoc.adapters;
018
019import org.nuxeo.apidoc.api.BundleInfo;
020import org.nuxeo.apidoc.api.ServiceInfo;
021import org.nuxeo.apidoc.api.VirtualNodesConsts;
022import org.nuxeo.common.utils.Path;
023import org.nuxeo.ecm.core.api.CoreSession;
024import org.nuxeo.ecm.core.api.DocumentModel;
025import org.nuxeo.ecm.core.api.PathRef;
026
027public class ServiceInfoDocAdapter extends BaseNuxeoArtifactDocAdapter implements ServiceInfo {
028
029    public ServiceInfoDocAdapter(DocumentModel doc) {
030        super(doc);
031    }
032
033    public static ServiceInfoDocAdapter create(ServiceInfo si, CoreSession session, String containerPath)
034            {
035
036        DocumentModel doc = session.createDocumentModel(TYPE_NAME);
037
038        String name = computeDocumentName("service-" + si.getId());
039        String targetPath = new Path(containerPath).append(name).toString();
040        boolean exist = false;
041        if (session.exists(new PathRef(targetPath))) {
042            exist = true;
043            doc = session.getDocument(new PathRef(targetPath));
044        }
045        doc.setPathInfo(containerPath, name);
046        doc.setPropertyValue("dc:title", si.getId());
047
048        doc.setPropertyValue(PROP_CLASS_NAME, si.getId());
049        doc.setPropertyValue(PROP_COMPONENT_ID, si.getComponentId());
050
051        if (exist) {
052            doc = session.saveDocument(doc);
053        } else {
054            doc = session.createDocument(doc);
055        }
056
057        return new ServiceInfoDocAdapter(doc);
058    }
059
060    @Override
061    public String getId() {
062        return safeGet(PROP_CLASS_NAME, "unknown_service");
063    }
064
065    @Override
066    public String getArtifactType() {
067        return TYPE_NAME;
068    }
069
070    @Override
071    public String getVersion() {
072        BundleInfo parentBundle = getParentNuxeoArtifact(BundleInfo.class);
073
074        if (parentBundle != null) {
075            return parentBundle.getVersion();
076        }
077
078        log.error("Unable to determine version for Service " + getId());
079        return "?";
080    }
081
082    @Override
083    public String getComponentId() {
084        return safeGet(PROP_COMPONENT_ID, "unknown_service");
085    }
086
087    @Override
088    public String getHierarchyPath() {
089        String path = super.getHierarchyPath() + "###";
090        String toReplace = "/" + getId() + "###";
091        return path.replace(toReplace, "/" + VirtualNodesConsts.Services_VNODE_NAME + "/" + getId());
092    }
093}