001package org.nuxeo.template.xdocreport.jaxrs; 002 003import java.io.ByteArrayOutputStream; 004import java.io.IOException; 005import java.util.ArrayList; 006import java.util.List; 007 008import javax.ws.rs.GET; 009import javax.ws.rs.Path; 010import javax.ws.rs.PathParam; 011 012import org.apache.commons.logging.Log; 013import org.apache.commons.logging.LogFactory; 014import org.codehaus.jackson.JsonGenerator; 015import org.nuxeo.ecm.automation.jaxrs.io.JsonHelper; 016import org.nuxeo.ecm.core.api.Blob; 017import org.nuxeo.ecm.core.api.Blobs; 018import org.nuxeo.ecm.core.api.CoreSession; 019import org.nuxeo.ecm.core.api.DocumentModel; 020import org.nuxeo.ecm.core.api.IdRef; 021import org.nuxeo.ecm.core.api.PropertyException; 022import org.nuxeo.ecm.core.schema.DocumentType; 023import org.nuxeo.ecm.core.schema.SchemaManager; 024import org.nuxeo.runtime.api.Framework; 025import org.nuxeo.template.api.adapters.TemplateSourceDocument; 026import org.nuxeo.template.processors.xdocreport.FieldDefinitionGenerator; 027 028import fr.opensagres.xdocreport.remoting.resources.domain.BinaryData; 029import fr.opensagres.xdocreport.remoting.resources.domain.Filter; 030import fr.opensagres.xdocreport.remoting.resources.domain.LargeBinaryData; 031import fr.opensagres.xdocreport.remoting.resources.domain.Resource; 032import fr.opensagres.xdocreport.remoting.resources.domain.ResourceType; 033import fr.opensagres.xdocreport.remoting.resources.services.ResourcesException; 034import fr.opensagres.xdocreport.remoting.resources.services.jaxrs.JAXRSResourcesService; 035 036/** 037 * @author <a href="mailto:tdelprat@nuxeo.com">Tiry</a> 038 */ 039public class XDocReportResourceService extends AbstractResourceService implements JAXRSResourcesService { 040 041 protected static final Log log = LogFactory.getLog(XDocReportResourceService.class); 042 043 public XDocReportResourceService(CoreSession session) { 044 super(session); 045 } 046 047 public List<BinaryData> download(List<String> resourcePaths) { 048 return null; 049 } 050 051 @Override 052 public String getName() { 053 return "Nuxeo-Repository"; 054 } 055 056 @Override 057 public Resource getRoot() { 058 Resource root = new NonRecursiveResource(); 059 root.setType(ResourceType.CATEGORY); 060 root.setName("Nuxeo"); 061 root.setId("nuxeo"); 062 List<Resource> children = new ArrayList<Resource>(); 063 List<TemplateSourceDocument> templates = getTemplates(); 064 for (TemplateSourceDocument template : templates) { 065 children.add(ResourceWrapper.wrap(template)); 066 } 067 root.getChildren().addAll(children); 068 return root; 069 } 070 071 @GET 072 @Path("model/list") 073 public String listModels() throws IOException { 074 SchemaManager sm = Framework.getLocalService(SchemaManager.class); 075 DocumentType[] docTypes = sm.getDocumentTypes(); 076 List<String> names = new ArrayList<String>(); 077 078 for (DocumentType dt : docTypes) { 079 names.add(dt.getName()); 080 } 081 082 ByteArrayOutputStream out = new ByteArrayOutputStream(); 083 084 JsonGenerator gen = JsonHelper.createJsonGenerator(out); 085 gen.writeObject(names); 086 087 return out.toString(); 088 } 089 090 @GET 091 @Path("model/{type}") 092 public String getFieldDefinition(@PathParam("type") String type) { 093 return FieldDefinitionGenerator.generate(type); 094 } 095 096 @Override 097 public void upload(BinaryData dataIn) { 098 String id = dataIn.getResourceId(); 099 if (id != null) { 100 IdRef ref = new IdRef(id); 101 try { 102 DocumentModel target = session.getDocument(ref); 103 TemplateSourceDocument template = target.getAdapter(TemplateSourceDocument.class); 104 if (template != null) { 105 Blob oldBlob = template.getTemplateBlob(); 106 107 Blob newBlob = Blobs.createBlob(dataIn.getContent(), oldBlob.getMimeType()); 108 newBlob.setFilename(oldBlob.getFilename()); 109 template.setTemplateBlob(newBlob, true); 110 } 111 } catch (PropertyException | IOException e) { 112 log.error("Error during template upload", e); 113 } 114 } 115 } 116 117 @Override 118 public List<BinaryData> downloadMultiple(List<String> arg0) throws ResourcesException { 119 return null; 120 } 121 122 @Override 123 public Resource getRootWithFilter(Filter filter) throws ResourcesException { 124 return getRoot(); 125 } 126 127 @Override 128 public LargeBinaryData downloadLarge(String resourcePath) throws ResourcesException { 129 CoreSession session = getCoreSession(); 130 if (resourcePath.endsWith(".fields.xml")) { 131 String uuid = resourcePath.replace(".fields.xml", ""); 132 DocumentModel targetDoc = session.getDocument(new IdRef(uuid)); 133 TemplateSourceDocument template = targetDoc.getAdapter(TemplateSourceDocument.class); 134 135 List<String> types = template.getApplicableTypes(); 136 String targetType = "File"; 137 if (types.size() > 0) { 138 targetType = types.get(0); 139 } 140 String xml = FieldDefinitionGenerator.generate(targetType); 141 try { 142 return BinaryDataWrapper.wrapXml(xml, resourcePath); 143 } catch (IOException e) { 144 throw new ResourcesException(e); 145 } 146 } else { 147 String uuid = resourcePath; 148 DocumentModel targetDoc = session.getDocument(new IdRef(uuid)); 149 TemplateSourceDocument template = targetDoc.getAdapter(TemplateSourceDocument.class); 150 try { 151 return BinaryDataWrapper.wrap(template); 152 } catch (IOException e) { 153 throw new ResourcesException(e); 154 } 155 } 156 } 157 158 @Override 159 public BinaryData download(String resourcePath) { 160 return null; 161 } 162 163 @Override 164 public void uploadLarge(LargeBinaryData dataIn) throws ResourcesException { 165 String id = dataIn.getResourceId(); 166 if (id != null) { 167 IdRef ref = new IdRef(id); 168 try { 169 DocumentModel target = session.getDocument(ref); 170 TemplateSourceDocument template = target.getAdapter(TemplateSourceDocument.class); 171 if (template != null) { 172 Blob oldBlob = template.getTemplateBlob(); 173 Blob newBlob = Blobs.createBlob(dataIn.getContent(), oldBlob.getMimeType()); 174 newBlob.setFilename(oldBlob.getFilename()); 175 template.setTemplateBlob(newBlob, true); 176 } 177 } catch (PropertyException | IOException e) { 178 log.error("Error during template upload", e); 179 } 180 } 181 } 182 183}