001/*
002 * (C) Copyright 2014 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *      Vladimir Pasquier <vpasquier@nuxeo.com>
016 */
017package org.nuxeo.binary.metadata.internals.operations;
018
019import java.util.HashMap;
020import java.util.Map;
021
022import org.nuxeo.binary.metadata.api.BinaryMetadataException;
023import org.nuxeo.binary.metadata.api.BinaryMetadataService;
024import org.nuxeo.ecm.automation.core.Constants;
025import org.nuxeo.ecm.automation.core.annotations.Context;
026import org.nuxeo.ecm.automation.core.annotations.Operation;
027import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
028import org.nuxeo.ecm.automation.core.annotations.Param;
029import org.nuxeo.ecm.automation.core.util.Properties;
030import org.nuxeo.ecm.core.api.Blob;
031import org.nuxeo.ecm.core.api.CoreSession;
032import org.nuxeo.ecm.core.api.DocumentModel;
033import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
034import org.nuxeo.ecm.core.api.blobholder.DocumentBlobHolder;
035
036/**
037 * @since 7.1
038 */
039@Operation(id = WriteMetadataToBinaryFromDocument.ID, category = Constants.CAT_BLOB, label = "Write Metadata To Binary From Document", description = "Write metadata to a Blob (xpath parameter, or BlobHolder if empty) from a document (input) given a custom metadata mapping defined in a Properties parameter, using a named processor (exifTool for instance).", since = "7.1", addToStudio = true, aliases = { "Binary.WriteMetadataFromDocument" })
040public class WriteMetadataToBinaryFromDocument {
041
042    public static final String ID = "Blob.SetMetadataFromDocument";
043
044    @Context
045    protected CoreSession session;
046
047    @Context
048    protected BinaryMetadataService binaryMetadataService;
049
050    @Param(name = "processor", required = false, description = "The processor to execute for overriding the input document blob.")
051    protected String processor = "exifTool";
052
053    @Param(name = "metadata", required = true, description = "Metadata to write into the input document blob.")
054    protected Properties metadata;
055
056    @Param(name = "blobXPath", required = false, description = "The blob xpath on the document. Default blob property for empty parameter.")
057    protected String blobXPath;
058
059    @Param(name = "ignorePrefix", required = false, description = "Ignore metadata prefixes or not")
060    boolean ignorePrefix = true;
061
062    @Param(name = "save", required = false, values = "true")
063    protected boolean save = true;
064
065    @OperationMethod
066    public DocumentModel run(DocumentModel doc) {
067        Map<String, String> metadataMap = new HashMap<>(metadata.size());
068        for (Map.Entry<String, String> entry : metadata.entrySet()) {
069            metadataMap.put(entry.getKey(), entry.getValue());
070        }
071        BlobHolder blobHolder;
072        if (blobXPath != null) {
073            blobHolder = new DocumentBlobHolder(doc, blobXPath);
074        } else {
075            blobHolder = doc.getAdapter(BlobHolder.class);
076        }
077        Blob blob = blobHolder.getBlob();
078        if (blob == null) {
079            String message;
080            if (blobXPath == null) {
081                message = "No blob attached for document '" + doc.getId() + "'. Please specify a blobXPath parameter.";
082            } else {
083                message = "No blob attached for document '" + doc.getId() + "' and blob xpath '" + blobXPath
084                        + "'. Please specify another blobXPath parameter.";
085            }
086            throw new BinaryMetadataException(message);
087        }
088        Blob newBlob = binaryMetadataService.writeMetadata(processor, blob, metadataMap, ignorePrefix);
089        blobHolder.setBlob(newBlob);
090        if (save) {
091            doc = session.saveDocument(doc);
092        }
093        return doc;
094    }
095
096}