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.BinaryMetadataService;
023import org.nuxeo.ecm.automation.core.Constants;
024import org.nuxeo.ecm.automation.core.annotations.Context;
025import org.nuxeo.ecm.automation.core.annotations.Operation;
026import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
027import org.nuxeo.ecm.automation.core.annotations.Param;
028import org.nuxeo.ecm.automation.core.util.Properties;
029import org.nuxeo.ecm.core.api.Blob;
030
031/**
032 * @since 7.1
033 */
034@Operation(id = WriteMetadataToBinaryFromContext.ID, category = Constants.CAT_BLOB, label = "Write Metadata To Blob From"
035        + " Context", description = "Write Metadata To Blob From Context "
036                + "given a processor name (or the default Nuxeo one) and given metadata, and return the updated Blob"
037                + ".", since = "7.1", addToStudio = true, aliases = { "Binary.WriteMetadataFromContext" })
038public class WriteMetadataToBinaryFromContext {
039
040    public static final String ID = "Blob.SetMetadataFromContext";
041
042    @Context
043    protected BinaryMetadataService binaryMetadataService;
044
045    @Param(name = "ignorePrefix", required = false, description = "Ignore metadata prefixes or not")
046    boolean ignorePrefix = true;
047
048    @Param(name = "processor", required = false, description = "The processor to execute for overriding the input blob.")
049    protected String processor = "exifTool";
050
051    @Param(name = "metadata", required = true, description = "Metadata to write into the input blob.")
052    protected Properties metadata;
053
054    @OperationMethod
055    public Blob run(Blob blob) {
056        Map<String, String> metadataMap = new HashMap<>(metadata.size());
057        for (Map.Entry<String, String> entry : metadata.entrySet()) {
058            metadataMap.put(entry.getKey(), entry.getValue());
059        }
060        return binaryMetadataService.writeMetadata(processor, blob, metadataMap, ignorePrefix);
061    }
062}