001/*
002 * (C) Copyright 2006-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 *     bstefanescu
018 */
019package org.nuxeo.ecm.automation.core.operations.blob;
020
021import org.nuxeo.ecm.automation.core.Constants;
022import org.nuxeo.ecm.automation.core.annotations.Context;
023import org.nuxeo.ecm.automation.core.annotations.Operation;
024import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
025import org.nuxeo.ecm.automation.core.annotations.Param;
026import org.nuxeo.ecm.automation.core.collectors.BlobCollector;
027import org.nuxeo.ecm.automation.core.util.DocumentHelper;
028import org.nuxeo.ecm.core.api.Blob;
029import org.nuxeo.ecm.core.api.CoreSession;
030import org.nuxeo.ecm.core.api.DocumentModel;
031
032/**
033 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
034 */
035// TODO accept xpath that points to a blob entry in a list and insert a blob
036// before. see SetDocumentBlob too.
037@Operation(id = AttachBlob.ID, category = Constants.CAT_BLOB, label = "Attach File", description = "Attach the input file to the document given as a parameter. If the xpath points to a blob list then the blob is appended to the list, otherwise the xpath should point to a blob property. If the save parameter is set the document modification will be automatically saved. Return the blob.", aliases = { "Blob.Attach" })
038public class AttachBlob {
039
040    public static final String ID = "Blob.AttachOnDocument";
041
042    @Context
043    protected CoreSession session;
044
045    @Param(name = "xpath", required = false, values = "file:content")
046    protected String xpath = "file:content";
047
048    @Param(name = "document")
049    protected DocumentModel doc;
050
051    @Param(name = "save", required = false, values = "true")
052    protected boolean save = true;
053
054    @OperationMethod(collector = BlobCollector.class)
055    public Blob run(Blob blob) {
056        DocumentHelper.addBlob(doc.getProperty(xpath), blob);
057        if (save) {
058            doc = session.saveDocument(doc);
059        }
060        return blob;
061    }
062
063}