001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     bstefanescu
011 */
012package org.nuxeo.ecm.automation.core.operations.blob;
013
014import org.nuxeo.ecm.automation.core.Constants;
015import org.nuxeo.ecm.automation.core.annotations.Context;
016import org.nuxeo.ecm.automation.core.annotations.Operation;
017import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
018import org.nuxeo.ecm.automation.core.annotations.Param;
019import org.nuxeo.ecm.automation.core.collectors.BlobCollector;
020import org.nuxeo.ecm.automation.core.util.DocumentHelper;
021import org.nuxeo.ecm.core.api.Blob;
022import org.nuxeo.ecm.core.api.CoreSession;
023import org.nuxeo.ecm.core.api.DocumentModel;
024
025/**
026 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
027 */
028// TODO accept xpath that points to a blob entry in a list and insert a blob
029// before. see SetDocumentBlob too.
030@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" })
031public class AttachBlob {
032
033    public static final String ID = "Blob.AttachOnDocument";
034
035    @Context
036    protected CoreSession session;
037
038    @Param(name = "xpath", required = false, values = "file:content")
039    protected String xpath = "file:content";
040
041    @Param(name = "document")
042    protected DocumentModel doc;
043
044    @Param(name = "save", required = false, values = "true")
045    protected boolean save = true;
046
047    @OperationMethod(collector = BlobCollector.class)
048    public Blob run(Blob blob) {
049        DocumentHelper.addBlob(doc.getProperty(xpath), blob);
050        if (save) {
051            doc = session.saveDocument(doc);
052        }
053        return blob;
054    }
055
056}