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.document;
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.DocumentModelCollector;
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 AttachBlob too.
030@Operation(id = SetDocumentBlob.ID, category = Constants.CAT_DOCUMENT, label = "Set File", description = "Set the input file to the given property on the input document. 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 document.", aliases = { "Blob.Set" })
031public class SetDocumentBlob {
032
033    public static final String ID = "Document.SetBlob";
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 = "file")
042    protected Blob blob;
043
044    @Param(name = "save", required = false, values = "true")
045    protected boolean save = true;
046
047    @OperationMethod(collector = DocumentModelCollector.class)
048    public DocumentModel run(DocumentModel doc) {
049        DocumentHelper.addBlob(doc.getProperty(xpath), blob);
050        if (save) {
051            doc = session.saveDocument(doc);
052        }
053        return doc;
054    }
055
056}