001/*
002 * (C) Copyright 2015 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 *     thibaud
016 */
017
018package org.nuxeo.diff.pictures;
019
020import java.io.IOException;
021import java.io.Serializable;
022import java.util.HashMap;
023import java.util.Map;
024import java.util.Set;
025
026import org.apache.commons.lang.StringUtils;
027import org.nuxeo.ecm.automation.OperationContext;
028import org.nuxeo.ecm.automation.OperationException;
029import org.nuxeo.ecm.automation.core.Constants;
030import org.nuxeo.ecm.automation.core.annotations.Context;
031import org.nuxeo.ecm.automation.core.annotations.Operation;
032import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
033import org.nuxeo.ecm.automation.core.annotations.Param;
034import org.nuxeo.ecm.automation.core.collectors.BlobCollector;
035import org.nuxeo.ecm.automation.core.util.Properties;
036import org.nuxeo.ecm.core.api.Blob;
037import org.nuxeo.ecm.core.api.CoreSession;
038import org.nuxeo.ecm.platform.commandline.executor.api.CommandNotAvailable;
039
040/**
041 * @since 7.4
042 */
043@Operation(id = DiffPicturesWithBlobsOp.ID, category = Constants.CAT_CONVERSION, label = "Pictures: Diff with Blobs", description = "Compare input blob with blob referenced in the context variable blob2VarName, using the commandLine and its parameters (default values apply). Return the result of the diff as a picture")
044public class DiffPicturesWithBlobsOp {
045
046    public static final String ID = "Pictures.DiffWithBlobs";
047
048    @Context
049    protected CoreSession session;
050
051    @Context
052    protected OperationContext ctx;
053
054    @Param(name = "blob2VarName", required = true)
055    protected String blob2VarName;
056
057    @Param(name = "commandLine", required = false, values = { "diff-pictures-default" })
058    protected String commandLine = "diff-pictures-default";
059
060    @Param(name = "parameters", required = false)
061    protected Properties parameters;
062
063    @Param(name = "targetFileName", required = false)
064    protected String targetFileName;
065
066    @Param(name = "targetFileNameSuffix", required = false)
067    protected String targetFileNameSuffix = "";
068
069    @OperationMethod(collector = BlobCollector.class)
070    public Blob run(Blob inBlob) throws OperationException, CommandNotAvailable, IOException {
071        Blob result = null;
072
073        Blob blob2 = (Blob) ctx.get(blob2VarName);
074        if (blob2 == null) {
075            throw new OperationException("The blob to append from variable context: '" + blob2VarName + "' is null.");
076        }
077
078        Map<String, Serializable> serializableParameters = new HashMap<String, Serializable>();
079        if (parameters != null && parameters.size() > 0) {
080            Set<String> parameterNames = parameters.keySet();
081            for (String parameterName : parameterNames) {
082                serializableParameters.put(parameterName, parameters.get(parameterName));
083            }
084        }
085
086        if (StringUtils.isNotBlank(targetFileName) || StringUtils.isNotBlank(targetFileNameSuffix)) {
087            targetFileName = DiffPicturesUtils.updateTargetFileName(inBlob, targetFileName, targetFileNameSuffix);
088            serializableParameters.put("targetFileName", targetFileName);
089        }
090
091        DiffPictures dp = new DiffPictures(inBlob, blob2);
092        result = dp.compare(commandLine, serializableParameters);
093
094        return result;
095    }
096
097}