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