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