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.util.ArrayList;
022import java.util.List;
023
024import org.nuxeo.apidoc.api.BundleGroup;
025import org.nuxeo.apidoc.api.BundleInfo;
026import org.nuxeo.apidoc.api.QueryHelper;
027import org.nuxeo.apidoc.snapshot.DistributionSnapshot;
028import org.nuxeo.common.utils.Path;
029import org.nuxeo.ecm.core.api.CoreSession;
030import org.nuxeo.ecm.core.api.DocumentModel;
031import org.nuxeo.ecm.core.api.DocumentModelList;
032import org.nuxeo.ecm.core.api.PathRef;
033
034public class BundleGroupDocAdapter extends BaseNuxeoArtifactDocAdapter implements BundleGroup {
035
036    public static BundleGroupDocAdapter create(BundleGroup bundleGroup, CoreSession session, String containerPath)
037            {
038
039        DocumentModel doc = session.createDocumentModel(TYPE_NAME);
040        String name = computeDocumentName("bg-" + bundleGroup.getId());
041        String targetPath = new Path(containerPath).append(name).toString();
042
043        boolean exist = false;
044        if (session.exists(new PathRef(targetPath))) {
045            exist = true;
046            doc = session.getDocument(new PathRef(targetPath));
047        }
048        doc.setPathInfo(containerPath, name);
049        doc.setPropertyValue("dc:title", bundleGroup.getName());
050        doc.setPropertyValue(PROP_GROUP_NAME, bundleGroup.getName());
051        doc.setPropertyValue(PROP_KEY, bundleGroup.getId());
052        if (exist) {
053            doc = session.saveDocument(doc);
054        } else {
055            doc = session.createDocument(doc);
056        }
057        return new BundleGroupDocAdapter(doc);
058    }
059
060    public BundleGroupDocAdapter(DocumentModel doc) {
061        super(doc);
062    }
063
064    @Override
065    public List<String> getBundleIds() {
066        List<String> bundles = new ArrayList<String>();
067        String query = QueryHelper.select(BundleInfo.TYPE_NAME, doc);
068        DocumentModelList docs = getCoreSession().query(query);
069        for (DocumentModel child : docs) {
070            BundleInfo bi = child.getAdapter(BundleInfo.class);
071            if (bi != null && !bi.getId().equals(this.getId())) {
072                bundles.add(bi.getId());
073            }
074        }
075        return bundles;
076    }
077
078    private String getKey() {
079        return safeGet(PROP_KEY, "unknown_bundle_group");
080    }
081
082    @Override
083    public String getName() {
084        return safeGet(PROP_GROUP_NAME, "unknown_bundle_group");
085    }
086
087    @Override
088    public List<BundleGroup> getSubGroups() {
089        List<BundleGroup> grps = new ArrayList<BundleGroup>();
090        String query = QueryHelper.select(TYPE_NAME, doc);
091        DocumentModelList docs = getCoreSession().query(query);
092        for (DocumentModel child : docs) {
093            BundleGroup grp = child.getAdapter(BundleGroup.class);
094            if (grp != null) {
095                grps.add(grp);
096            }
097        }
098        return grps;
099    }
100
101    @Override
102    public String getId() {
103        return getKey();
104    }
105
106    @Override
107    public String getVersion() {
108        DistributionSnapshot parentSnapshot = getParentNuxeoArtifact(DistributionSnapshot.class);
109
110        if (parentSnapshot == null) {
111            log.error("Unable to determine version for bundleGroup " + getId());
112            return "?";
113        }
114        return parentSnapshot.getVersion();
115    }
116
117    @Override
118    public String getArtifactType() {
119        return TYPE_NAME;
120    }
121
122}