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 java.io.Serializable;
020import java.util.List;
021
022import org.nuxeo.apidoc.api.SeamComponentInfo;
023import org.nuxeo.apidoc.snapshot.DistributionSnapshot;
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;
028import org.nuxeo.ecm.core.api.PropertyException;
029
030public class SeamComponentInfoDocAdapter extends BaseNuxeoArtifactDocAdapter implements SeamComponentInfo {
031
032    protected SeamComponentInfoDocAdapter(DocumentModel doc) {
033        super(doc);
034    }
035
036    @Override
037    public String getId() {
038        return "seam:" + getName();
039    }
040
041    @Override
042    public String getClassName() {
043        return safeGet(PROP_CLASS_NAME);
044    }
045
046    @Override
047    @SuppressWarnings("unchecked")
048    public List<String> getInterfaceNames() {
049        try {
050            return (List<String>) doc.getPropertyValue(PROP_INTERFACES);
051        } catch (PropertyException e) {
052            log.error("Error while getting service names", e);
053        }
054        return null;
055    }
056
057    @Override
058    public String getName() {
059        return safeGet(PROP_COMPONENT_NAME);
060    }
061
062    @Override
063    public String getPrecedence() {
064        return safeGet(PROP_PRECEDENCE);
065    }
066
067    @Override
068    public String getScope() {
069        return safeGet(PROP_SCOPE);
070    }
071
072    @Override
073    public String getArtifactType() {
074        return SeamComponentInfo.TYPE_NAME;
075    }
076
077    @Override
078    public String getVersion() {
079        DistributionSnapshot parentSnapshot = getParentNuxeoArtifact(DistributionSnapshot.class);
080
081        if (parentSnapshot == null) {
082            log.error("Unable to determine version for bundleGroup " + getId());
083            return "?";
084        }
085
086        return parentSnapshot.getVersion();
087    }
088
089    @Override
090    public int compareTo(SeamComponentInfo o) {
091        return getClassName().compareTo(o.getClassName());
092    }
093
094    public static SeamComponentInfo create(SeamComponentInfo sci, CoreSession session, String containerPath)
095            {
096
097        DocumentModel doc = session.createDocumentModel(TYPE_NAME);
098
099        String name = computeDocumentName(sci.getId());
100        String targetPath = new Path(containerPath).append(name).toString();
101        boolean exist = false;
102        if (session.exists(new PathRef(targetPath))) {
103            exist = true;
104            doc = session.getDocument(new PathRef(targetPath));
105        }
106
107        doc.setPathInfo(containerPath, name);
108        doc.setPropertyValue("dc:title", sci.getName());
109
110        doc.setPropertyValue(PROP_COMPONENT_NAME, sci.getName());
111        doc.setPropertyValue(PROP_CLASS_NAME, sci.getClassName());
112        doc.setPropertyValue(PROP_SCOPE, sci.getScope());
113        doc.setPropertyValue(PROP_INTERFACES, (Serializable) sci.getInterfaceNames());
114
115        if (exist) {
116            doc = session.saveDocument(doc);
117        } else {
118            doc = session.createDocument(doc);
119        }
120
121        return new SeamComponentInfoDocAdapter(doc);
122    }
123
124}