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