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 *     vpasquier <vpasquier@nuxeo.com>
016 *     slacoin <slacoin@nuxeo.com>
017 */
018package org.nuxeo.ecm.automation.core.trace;
019
020import java.util.Arrays;
021import java.util.HashMap;
022import java.util.LinkedList;
023import java.util.List;
024import java.util.Map;
025
026import org.apache.commons.logging.Log;
027import org.apache.commons.logging.LogFactory;
028import org.nuxeo.ecm.automation.OperationContext;
029import org.nuxeo.ecm.automation.OperationType;
030import org.nuxeo.ecm.automation.core.impl.InvokableMethod;
031import org.nuxeo.ecm.automation.core.scripting.Expression;
032
033/**
034 * @since 5.7.3
035 */
036public class Call {
037
038    private static final Log log = LogFactory.getLog(Call.class);
039
040    protected final String chainId;
041
042    protected final String aliases;
043
044    protected final OperationType type;
045
046    protected final InvokableMethod method;
047
048    protected final Map<String, Object> parameters;
049
050    protected final Map<String, Object> variables;
051
052    protected final List<Trace> nested = new LinkedList<Trace>();
053
054    protected final Object input;
055
056    public Call(OperationType chain, OperationContext context, OperationType type, InvokableMethod method,
057            Map<String, Object> parms) {
058        this.type = type;
059        variables = (context != null) ? new HashMap<>(context) : null;
060        this.method = method;
061        input = (context != null) ? context.getInput() : null;
062        parameters = new HashMap<>();
063        if (parms != null) {
064            for (String paramId : parms.keySet()) {
065                Object paramValue = parms.get(paramId);
066                if (paramValue instanceof Expression) {
067                    try {
068                        ExpressionParameter expressionParameter = new ExpressionParameter(paramId,
069                                ((Expression) paramValue).eval(context));
070                        parameters.put(paramId, expressionParameter);
071                    } catch (RuntimeException e) {
072                        log.warn("Cannot evaluate mvel expression for parameter: " + paramId, e);
073                    }
074                } else {
075                    parameters.put(paramId, paramValue);
076                }
077            }
078        }
079        chainId = (chain != null) ? chain.getId() : "Not bound to a chain";
080        aliases = (chain != null) ? Arrays.toString(chain.getAliases()) : null;
081    }
082
083    /**
084     * @since 7.1
085     */
086    public class ExpressionParameter {
087
088        protected final String parameterId;
089
090        protected final Object parameterValue;
091
092        public ExpressionParameter(String parameterId, Object parameterValue) {
093            this.parameterId = parameterId;
094            this.parameterValue = parameterValue;
095        }
096
097        public Object getParameterValue() {
098            return parameterValue;
099        }
100
101        public String getParameterId() {
102
103            return parameterId;
104        }
105    }
106
107    public OperationType getType() {
108        return type;
109    }
110
111    public InvokableMethod getMethod() {
112        return method;
113    }
114
115    public Map<String, Object> getParmeters() {
116        return parameters;
117    }
118
119    public Map<String, Object> getVariables() {
120        return variables;
121    }
122
123    public Object getInput() {
124        return input;
125    }
126
127    public List<Trace> getNested() {
128        return nested;
129    }
130
131    public String getChainId() {
132        return chainId;
133    }
134
135    public String getAliases() {
136        return aliases;
137    }
138
139}