001/*
002 * (C) Copyright 2015 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 *     thibaud
018 */
019
020package org.nuxeo.diff.pictures;
021
022import java.io.IOException;
023import java.io.Serializable;
024import java.util.HashMap;
025import java.util.Map;
026import java.util.Set;
027
028import org.apache.commons.lang.StringUtils;
029import org.nuxeo.ecm.automation.OperationContext;
030import org.nuxeo.ecm.automation.OperationException;
031import org.nuxeo.ecm.automation.core.Constants;
032import org.nuxeo.ecm.automation.core.annotations.Context;
033import org.nuxeo.ecm.automation.core.annotations.Operation;
034import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
035import org.nuxeo.ecm.automation.core.annotations.Param;
036import org.nuxeo.ecm.automation.core.util.Properties;
037import org.nuxeo.ecm.core.api.Blob;
038import org.nuxeo.ecm.core.api.CoreSession;
039import org.nuxeo.ecm.core.api.DocumentModel;
040import org.nuxeo.ecm.platform.commandline.executor.api.CommandNotAvailable;
041
042/**
043 * @since 7.4
044 */
045@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")
046public class DiffPicturesWithDocsOp {
047
048    public static final String ID = "Pictures.DiffWithDocs";
049
050    @Context
051    protected CoreSession session;
052
053    @Context
054    protected OperationContext ctx;
055
056    @Param(name = "leftDoc", required = true)
057    protected DocumentModel leftDoc;
058
059    @Param(name = "rightDoc", required = true)
060    protected DocumentModel rightDoc;
061
062    @Param(name = "xpath", required = false, values = { "file:content" })
063    protected String xpath;
064
065    @Param(name = "commandLine", required = false, values = { "diff-pictures-default" })
066    protected String commandLine = "diff-pictures-default";
067
068    @Param(name = "parameters", required = false)
069    protected Properties parameters;
070
071    @Param(name = "targetFileName", required = false)
072    protected String targetFileName;
073
074    @Param(name = "targetFileNameSuffix", required = false)
075    protected String targetFileNameSuffix = "";
076
077    @OperationMethod()
078    public Blob run() throws OperationException, CommandNotAvailable, IOException {
079        
080        Blob result = null;
081
082        Map<String, Serializable> serializableParameters = new HashMap<String, Serializable>();
083        if (parameters != null && parameters.size() > 0) {
084            Set<String> parameterNames = parameters.keySet();
085            for (String parameterName : parameterNames) {
086                serializableParameters.put(parameterName, parameters.get(parameterName));
087            }
088        }
089
090        if (StringUtils.isNotBlank(targetFileName) || StringUtils.isNotBlank(targetFileNameSuffix)) {
091            targetFileName = DiffPicturesUtils.addSuffixToFileName(targetFileName, targetFileNameSuffix);
092            serializableParameters.put("targetFileName", targetFileName);
093        }
094
095        DiffPictures dp = new DiffPictures(leftDoc, rightDoc, xpath);
096        result = dp.compare(commandLine, serializableParameters);
097
098        return result;
099    }
100
101}