001/*
002 * (C) Copyright 2014 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 *      Vladimir Pasquier <vpasquier@nuxeo.com>
018 */
019package org.nuxeo.binary.metadata.internals.operations;
020
021import java.util.HashMap;
022import java.util.Map;
023
024import org.nuxeo.binary.metadata.api.BinaryMetadataException;
025import org.nuxeo.binary.metadata.api.BinaryMetadataService;
026import org.nuxeo.ecm.automation.core.Constants;
027import org.nuxeo.ecm.automation.core.annotations.Context;
028import org.nuxeo.ecm.automation.core.annotations.Operation;
029import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
030import org.nuxeo.ecm.automation.core.annotations.Param;
031import org.nuxeo.ecm.automation.core.util.Properties;
032import org.nuxeo.ecm.core.api.Blob;
033import org.nuxeo.ecm.core.api.CoreSession;
034import org.nuxeo.ecm.core.api.DocumentModel;
035import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
036import org.nuxeo.ecm.core.api.blobholder.DocumentBlobHolder;
037
038/**
039 * @since 7.1
040 */
041@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" })
042public class WriteMetadataToBinaryFromDocument {
043
044    public static final String ID = "Blob.SetMetadataFromDocument";
045
046    @Context
047    protected CoreSession session;
048
049    @Context
050    protected BinaryMetadataService binaryMetadataService;
051
052    @Param(name = "processor", required = false, description = "The processor to execute for overriding the input document blob.")
053    protected String processor = "exifTool";
054
055    @Param(name = "metadata", required = true, description = "Metadata to write into the input document blob.")
056    protected Properties metadata;
057
058    @Param(name = "blobXPath", required = false, description = "The blob xpath on the document. Default blob property for empty parameter.")
059    protected String blobXPath;
060
061    @Param(name = "ignorePrefix", required = false, description = "Ignore metadata prefixes or not")
062    boolean ignorePrefix = true;
063
064    @Param(name = "save", required = false, values = "true")
065    protected boolean save = true;
066
067    @OperationMethod
068    public DocumentModel run(DocumentModel doc) {
069        Map<String, Object> metadataMap = new HashMap<>(metadata.size());
070        for (Map.Entry<String, String> entry : metadata.entrySet()) {
071            metadataMap.put(entry.getKey(), entry.getValue());
072        }
073        BlobHolder blobHolder;
074        if (blobXPath != null) {
075            blobHolder = new DocumentBlobHolder(doc, blobXPath);
076        } else {
077            blobHolder = doc.getAdapter(BlobHolder.class);
078        }
079        Blob blob = blobHolder.getBlob();
080        if (blob == null) {
081            String message;
082            if (blobXPath == null) {
083                message = "No blob attached for document '" + doc.getId() + "'. Please specify a blobXPath parameter.";
084            } else {
085                message = "No blob attached for document '" + doc.getId() + "' and blob xpath '" + blobXPath
086                        + "'. Please specify another blobXPath parameter.";
087            }
088            throw new BinaryMetadataException(message);
089        }
090        Blob newBlob = binaryMetadataService.writeMetadata(processor, blob, metadataMap, ignorePrefix);
091        blobHolder.setBlob(newBlob);
092        if (save) {
093            doc = session.saveDocument(doc);
094        }
095        return doc;
096    }
097
098}