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.util.Properties;
035import org.nuxeo.ecm.core.api.Blob;
036import org.nuxeo.ecm.core.api.CoreSession;
037import org.nuxeo.ecm.core.api.DocumentModel;
038import org.nuxeo.ecm.platform.commandline.executor.api.CommandNotAvailable;
039
040/**
041 * @since 7.4
042 */
043@Operation(id = DiffPicturesWithDocsOp.ID, category = Constants.CAT_CONVERSION, label = "Pictures: Diff with Docs", description = "Compare the pictures of the two documents (referenced by their ID or their path), using the commandLine and its parameters (default values apply). Does nopt check the documents contain pictures. Return the result of the diff as a picture")
044public class DiffPicturesWithDocsOp {
045
046    public static final String ID = "Pictures.DiffWithDocs";
047
048    @Context
049    protected CoreSession session;
050
051    @Context
052    protected OperationContext ctx;
053
054    @Param(name = "leftDoc", required = true)
055    protected DocumentModel leftDoc;
056
057    @Param(name = "rightDoc", required = true)
058    protected DocumentModel rightDoc;
059
060    @Param(name = "xpath", required = false, values = { "file:content" })
061    protected String xpath;
062
063    @Param(name = "commandLine", required = false, values = { "diff-pictures-default" })
064    protected String commandLine = "diff-pictures-default";
065
066    @Param(name = "parameters", required = false)
067    protected Properties parameters;
068
069    @Param(name = "targetFileName", required = false)
070    protected String targetFileName;
071
072    @Param(name = "targetFileNameSuffix", required = false)
073    protected String targetFileNameSuffix = "";
074
075    @OperationMethod()
076    public Blob run() throws OperationException, CommandNotAvailable, IOException {
077        
078        Blob result = null;
079
080        Map<String, Serializable> serializableParameters = new HashMap<String, Serializable>();
081        if (parameters != null && parameters.size() > 0) {
082            Set<String> parameterNames = parameters.keySet();
083            for (String parameterName : parameterNames) {
084                serializableParameters.put(parameterName, parameters.get(parameterName));
085            }
086        }
087
088        if (StringUtils.isNotBlank(targetFileName) || StringUtils.isNotBlank(targetFileNameSuffix)) {
089            targetFileName = DiffPicturesUtils.addSuffixToFileName(targetFileName, targetFileNameSuffix);
090            serializableParameters.put("targetFileName", targetFileName);
091        }
092
093        DiffPictures dp = new DiffPictures(leftDoc, rightDoc, xpath);
094        result = dp.compare(commandLine, serializableParameters);
095
096        return result;
097    }
098
099}