001/*
002 * Copyright (c) 2012 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     mcedica
011 */
012package org.nuxeo.ecm.platform.routing.core.api.operation;
013
014import java.io.Serializable;
015import java.util.Map;
016
017import org.nuxeo.ecm.automation.OperationContext;
018import org.nuxeo.ecm.automation.core.Constants;
019import org.nuxeo.ecm.automation.core.annotations.Context;
020import org.nuxeo.ecm.automation.core.annotations.Operation;
021import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
022import org.nuxeo.ecm.automation.core.annotations.Param;
023import org.nuxeo.ecm.core.api.CoreSession;
024import org.nuxeo.ecm.core.api.DocumentModel;
025import org.nuxeo.ecm.core.api.IdRef;
026import org.nuxeo.ecm.platform.routing.core.impl.GraphRoute;
027
028/**
029 * Set a workflow variable. The workflow variable must exists on the workflow. If no workflowId is specified the
030 * variable is set on the current workflow.
031 *
032 * @since 5.6
033 */
034@Operation(id = SetWorkflowVar.ID, category = Constants.CAT_WORKFLOW, label = "Set Workflow Variable", requires = Constants.WORKFLOW_CONTEXT, description = "Set a workflow variable. The workflow variable must exists on "
035        + "the workflow. If no workflowId is specified the variable is set on the current workflow."
036        + "To compute the value at runtime from the current context you should use a MVEL expression as the value. This operation works on any input type and return back the input as the output.")
037public class SetWorkflowVar {
038
039    public static final String ID = "Context.SetWorkflowVar";
040
041    @Context
042    protected OperationContext ctx;
043
044    @Context
045    protected CoreSession session;
046
047    @Param(name = "name")
048    protected String name;
049
050    @Param(name = "value", required = false)
051    protected Object value;
052
053    @Param(name = "workflowInstanceId", required = false)
054    protected String workflowInstanceId;
055
056    @SuppressWarnings("unchecked")
057    @OperationMethod
058    public void run() {
059        if (workflowInstanceId == null) {
060            if (ctx.get(Constants.VAR_WORKFLOW) != null) {
061                ((Map<String, Serializable>) ctx.get(Constants.VAR_WORKFLOW)).put(name, (Serializable) value);
062            }
063            if (ctx.get("workflowInstanceId") != null) {
064                workflowInstanceId = (String) ctx.get("workflowInstanceId");
065            }
066        }
067        if (workflowInstanceId == null) {
068            return;
069        }
070        DocumentModel workflowInstance = session.getDocument(new IdRef(workflowInstanceId));
071        GraphRoute graph = workflowInstance.getAdapter(GraphRoute.class);
072        Map<String, Serializable> vars = graph.getVariables();
073        vars.put(name, (Serializable) value);
074        graph.setVariables(vars);
075    }
076}