001/* 002 * (C) Copyright 2013 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 * bstefanescu 016 */ 017package org.nuxeo.ecm.automation; 018 019import java.io.Serializable; 020import java.util.HashMap; 021import java.util.Map; 022 023import org.apache.commons.lang.StringEscapeUtils; 024import org.nuxeo.ecm.automation.core.annotations.Param; 025import org.nuxeo.ecm.automation.core.scripting.Scripting; 026 027/** 028 * An object holding the runtime parameters that should be used by an operation when run. 029 * <p> 030 * Parameters are injected at runtime into the operation using {@link Param} annotation. 031 * 032 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 033 */ 034public class OperationParameters implements Serializable { 035 036 private static final long serialVersionUID = -3215180388563955264L; 037 038 protected final String oid; 039 040 protected final Map<String, Object> params; 041 042 public OperationParameters(String oid) { 043 this(oid, new HashMap<String, Object>()); 044 } 045 046 public OperationParameters(String oid, Map<String, Object> params) { 047 this.oid = oid; 048 this.params = params; 049 } 050 051 /** 052 * The operation ID. 053 */ 054 public String id() { 055 return oid; 056 } 057 058 /** 059 * The map of runtime parameters. 060 */ 061 public final Map<String, Object> map() { 062 return params; 063 } 064 065 public OperationParameters set(String key, Object valueRef) { 066 if (valueRef instanceof String) { 067 if (((String) valueRef).startsWith("expr:")) { 068 valueRef = ((String) valueRef).substring(5).trim(); 069 // Unescape xml checking 070 valueRef = StringEscapeUtils.unescapeXml(((String) valueRef)); 071 if (((String) valueRef).contains("@{")) { 072 params.put(key, Scripting.newTemplate(((String) valueRef))); 073 } else { 074 params.put(key, Scripting.newExpression(((String) valueRef))); 075 } 076 return this; 077 } 078 } 079 params.put(key, valueRef); 080 return this; 081 } 082 083 public OperationParameters from(Map<String, Object> params) { 084 this.params.putAll(params); 085 return this; 086 } 087 088}