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.collectors.BlobCollector; 037import org.nuxeo.ecm.automation.core.util.Properties; 038import org.nuxeo.ecm.core.api.Blob; 039import org.nuxeo.ecm.core.api.CoreSession; 040import org.nuxeo.ecm.platform.commandline.executor.api.CommandNotAvailable; 041 042/** 043 * @since 7.4 044 */ 045@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") 046public class DiffPicturesWithBlobsOp { 047 048 public static final String ID = "Pictures.DiffWithBlobs"; 049 050 @Context 051 protected CoreSession session; 052 053 @Context 054 protected OperationContext ctx; 055 056 @Param(name = "blob2VarName", required = true) 057 protected String blob2VarName; 058 059 @Param(name = "commandLine", required = false, values = { "diff-pictures-default" }) 060 protected String commandLine = "diff-pictures-default"; 061 062 @Param(name = "parameters", required = false) 063 protected Properties parameters; 064 065 @Param(name = "targetFileName", required = false) 066 protected String targetFileName; 067 068 @Param(name = "targetFileNameSuffix", required = false) 069 protected String targetFileNameSuffix = ""; 070 071 @OperationMethod(collector = BlobCollector.class) 072 public Blob run(Blob inBlob) throws OperationException, CommandNotAvailable, IOException { 073 Blob result = null; 074 075 Blob blob2 = (Blob) ctx.get(blob2VarName); 076 if (blob2 == null) { 077 throw new OperationException("The blob to append from variable context: '" + blob2VarName + "' is null."); 078 } 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.updateTargetFileName(inBlob, targetFileName, targetFileNameSuffix); 090 serializableParameters.put("targetFileName", targetFileName); 091 } 092 093 DiffPictures dp = new DiffPictures(inBlob, blob2); 094 result = dp.compare(commandLine, serializableParameters); 095 096 return result; 097 } 098 099}