001/*
002 * (C) Copyright 2011 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 = { "BlobHolder.Attach" })
042public class BlobHolderAttach {
043
044    public static final String ID = "BlobHolder.AttachOnCurrentDocument";
045
046    @Context
047    protected CoreSession session;
048
049    @Context
050    protected AutomationService as;
051
052    @Context
053    protected OperationContext context;
054
055    @Param(name = "useMainBlob", required = false)
056    protected boolean useMainBlob = true;
057
058    protected DocumentModel getCurrentDocument() throws OperationException {
059        String cdRef = (String) context.get("currentDocument");
060        return as.getAdaptedValue(context, cdRef, DocumentModel.class);
061    }
062
063    @OperationMethod
064    public DocumentModel run(Blob blob) throws OperationException {
065        DocumentModel currentDocument = getCurrentDocument();
066        BlobHolder bh = currentDocument.getAdapter(BlobHolder.class);
067        if (bh == null) {
068            return currentDocument;
069        }
070        bh.setBlob(blob);
071        currentDocument = session.saveDocument(currentDocument);
072        context.put("currentDocument", currentDocument);
073        return currentDocument;
074    }
075
076    @OperationMethod
077    public DocumentModel run(BlobList blobs) throws OperationException {
078        DocumentModel currentDocument = null;
079        if (useMainBlob) {
080            Blob mainBlob = blobs.remove(0);
081            currentDocument = run(mainBlob);
082        } else {
083            currentDocument = getCurrentDocument();
084        }
085        if (blobs.size() > 0) {
086            if (currentDocument.hasSchema("files")) {
087                List<Map<String, Object>> existingBlobs = (List<Map<String, Object>>) currentDocument.getPropertyValue("files:files");
088                if (existingBlobs == null) {
089                    existingBlobs = new ArrayList<Map<String, Object>>();
090                }
091                for (Blob blob : blobs) {
092                    Map<String, Object> map = new HashMap<String, Object>();
093                    map.put("file", blob);
094                    map.put("filename", blob.getFilename());
095                    existingBlobs.add(map);
096                }
097                currentDocument.setPropertyValue("files:files", (Serializable) existingBlobs);
098                currentDocument = session.saveDocument(currentDocument);
099            }
100        }
101        return currentDocument;
102    }
103
104}