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