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.CoreSession;
022import org.nuxeo.ecm.core.api.DocumentModel;
023
024/**
025 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
026 */
027@Operation(id = RemoveDocumentBlob.ID, category = Constants.CAT_BLOB, label = "Remove File", description = "Remove the file attached to the input document as specified by the 'xpath' parameter. If the 'xpath' point to a blob list then the list will be cleared. If the file to remove is part of a list it will be removed from the list otherwise the 'xpath' should point to a blob property that will be removed. If the save parameter is set the document modification will be automatically saved. Return the document.", aliases = { "Blob.Remove" })
028public class RemoveDocumentBlob {
029
030    public static final String ID = "Blob.RemoveFromDocument";
031
032    @Context
033    protected CoreSession session;
034
035    @Param(name = "xpath", required = false, values = "file:content")
036    protected String xpath = "file:content";
037
038    @Param(name = "save", required = false, values = "true")
039    protected boolean save = true;
040
041    @OperationMethod(collector = DocumentModelCollector.class)
042    public DocumentModel run(DocumentModel doc) {
043        DocumentHelper.removeProperty(doc, xpath);
044        if (save) {
045            doc = session.saveDocument(doc);
046        }
047        return doc;
048    }
049
050}