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