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.IOException; 020import java.io.Serializable; 021import java.util.ArrayList; 022import java.util.Collection; 023import java.util.List; 024 025import org.nuxeo.apidoc.api.BundleInfo; 026import org.nuxeo.apidoc.api.ComponentInfo; 027import org.nuxeo.common.utils.Path; 028import org.nuxeo.ecm.core.api.Blob; 029import org.nuxeo.ecm.core.api.Blobs; 030import org.nuxeo.ecm.core.api.CoreSession; 031import org.nuxeo.ecm.core.api.DocumentModel; 032import org.nuxeo.ecm.core.api.PathRef; 033 034public class BundleInfoDocAdapter extends BaseNuxeoArtifactDocAdapter implements BundleInfo { 035 036 public static BundleInfoDocAdapter create(BundleInfo bundleInfo, CoreSession session, String containerPath) 037 { 038 039 DocumentModel doc = session.createDocumentModel(TYPE_NAME); 040 String name = computeDocumentName("bundle-" + bundleInfo.getId()); 041 String targetPath = new Path(containerPath).append(name).toString(); 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", bundleInfo.getBundleId()); 049 doc.setPropertyValue(PROP_ARTIFACT_GROUP_ID, bundleInfo.getArtifactGroupId()); 050 doc.setPropertyValue(PROP_ARTIFACT_ID, bundleInfo.getArtifactId()); 051 doc.setPropertyValue(PROP_ARTIFACT_VERSION, bundleInfo.getArtifactVersion()); 052 doc.setPropertyValue(PROP_BUNDLE_ID, bundleInfo.getId()); 053 doc.setPropertyValue(PROP_JAR_NAME, bundleInfo.getFileName()); 054 String manifest = bundleInfo.getManifest(); 055 if (manifest != null) { 056 Blob manifestBlob = Blobs.createBlob(manifest); 057 manifestBlob.setFilename("MANIFEST.MF"); 058 doc.setPropertyValue("file:content", (Serializable) manifestBlob); 059 } 060 061 if (exist) { 062 doc = session.saveDocument(doc); 063 } else { 064 doc = session.createDocument(doc); 065 } 066 067 return new BundleInfoDocAdapter(doc); 068 } 069 070 public BundleInfoDocAdapter(DocumentModel doc) { 071 super(doc); 072 } 073 074 @Override 075 public String getArtifactId() { 076 return safeGet(PROP_ARTIFACT_ID); 077 } 078 079 @Override 080 public String getBundleId() { 081 return safeGet(PROP_BUNDLE_ID); 082 } 083 084 @Override 085 public Collection<ComponentInfo> getComponents() { 086 List<ComponentInfo> components = new ArrayList<ComponentInfo>(); 087 List<DocumentModel> children = getCoreSession().getChildren(doc.getRef()); 088 for (DocumentModel child : children) { 089 ComponentInfo comp = child.getAdapter(ComponentInfo.class); 090 if (comp != null) { 091 components.add(comp); 092 } 093 } 094 return components; 095 } 096 097 @Override 098 public String getFileName() { 099 return safeGet(PROP_JAR_NAME); 100 } 101 102 @Override 103 public String getArtifactGroupId() { 104 return safeGet(PROP_ARTIFACT_GROUP_ID); 105 } 106 107 @Override 108 public String getLocation() { 109 // TODO Auto-generated method stub 110 return null; 111 } 112 113 @Override 114 public String getManifest() { 115 try { 116 Blob mf = safeGet(Blob.class, "file:content", null); 117 if (mf == null) { 118 return "No MANIFEST.MF"; 119 } 120 if (mf.getEncoding() == null || "".equals(mf.getEncoding())) { 121 mf.setEncoding("utf-8"); 122 } 123 return mf.getString(); 124 } catch (IOException e) { 125 log.error("Error while reading blob", e); 126 return ""; 127 } 128 } 129 130 @Override 131 public String[] getRequirements() { 132 return null; 133 } 134 135 @Override 136 public String getArtifactVersion() { 137 return safeGet(PROP_ARTIFACT_VERSION, null); 138 } 139 140 @Override 141 public String getId() { 142 return getBundleId(); 143 } 144 145 @Override 146 public String getVersion() { 147 return getArtifactVersion(); 148 } 149 150 @Override 151 public String getArtifactType() { 152 return TYPE_NAME; 153 } 154 155}