001/* 002 * (C) Copyright 2011-2016 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.ecm.automation.core.operations.services; 020 021import java.io.Serializable; 022import java.util.ArrayList; 023import java.util.HashMap; 024import java.util.List; 025import java.util.Map; 026 027import org.nuxeo.ecm.automation.AutomationService; 028import org.nuxeo.ecm.automation.OperationContext; 029import org.nuxeo.ecm.automation.OperationException; 030import org.nuxeo.ecm.automation.core.Constants; 031import org.nuxeo.ecm.automation.core.annotations.Context; 032import org.nuxeo.ecm.automation.core.annotations.Operation; 033import org.nuxeo.ecm.automation.core.annotations.OperationMethod; 034import org.nuxeo.ecm.automation.core.annotations.Param; 035import org.nuxeo.ecm.automation.core.util.BlobList; 036import org.nuxeo.ecm.core.api.Blob; 037import org.nuxeo.ecm.core.api.CoreSession; 038import org.nuxeo.ecm.core.api.DocumentModel; 039import org.nuxeo.ecm.core.api.blobholder.BlobHolder; 040 041@Operation(id = BlobHolderAttach.ID, category = Constants.CAT_BLOB, label = "Attach File or files to the currentDocument.", description = "Attach the input file(s) to the current document using the BlobHolder abstraction", aliases = { 042 "BlobHolder.Attach" }) 043public class BlobHolderAttach { 044 045 public static final String ID = "BlobHolder.AttachOnCurrentDocument"; 046 047 @Context 048 protected CoreSession session; 049 050 @Context 051 protected AutomationService as; 052 053 @Context 054 protected OperationContext context; 055 056 @Param(name = "useMainBlob", required = false) 057 protected boolean useMainBlob = true; 058 059 protected DocumentModel getCurrentDocument() throws OperationException { 060 String cdRef = (String) context.get("currentDocument"); 061 return as.getAdaptedValue(context, cdRef, DocumentModel.class); 062 } 063 064 @OperationMethod 065 public DocumentModel run(Blob blob) throws OperationException { 066 DocumentModel currentDocument = getCurrentDocument(); 067 BlobHolder bh = currentDocument.getAdapter(BlobHolder.class); 068 if (bh == null) { 069 return currentDocument; 070 } 071 bh.setBlob(blob); 072 currentDocument = session.saveDocument(currentDocument); 073 context.put("currentDocument", currentDocument); 074 return currentDocument; 075 } 076 077 @OperationMethod 078 public DocumentModel run(BlobList blobs) throws OperationException { 079 DocumentModel currentDocument; 080 if (useMainBlob) { 081 Blob mainBlob = blobs.remove(0); 082 currentDocument = run(mainBlob); 083 } else { 084 currentDocument = getCurrentDocument(); 085 } 086 if (blobs.size() > 0) { 087 if (currentDocument.hasSchema("files")) { 088 List<Map<String, Object>> existingBlobs = (List<Map<String, Object>>) currentDocument.getPropertyValue( 089 "files:files"); 090 if (existingBlobs == null) { 091 existingBlobs = new ArrayList<>(); 092 } 093 for (Blob blob : blobs) { 094 Map<String, Object> map = new HashMap<>(); 095 map.put("file", blob); 096 existingBlobs.add(map); 097 } 098 currentDocument.setPropertyValue("files:files", (Serializable) existingBlobs); 099 currentDocument = session.saveDocument(currentDocument); 100 } 101 } 102 return currentDocument; 103 } 104 105}