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