001/* 002 * (C) Copyright 2012 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 * mcedica 018 */ 019package org.nuxeo.ecm.platform.routing.core.api.operation; 020 021import java.io.Serializable; 022import java.util.Map; 023 024import org.nuxeo.ecm.automation.OperationContext; 025import org.nuxeo.ecm.automation.core.Constants; 026import org.nuxeo.ecm.automation.core.annotations.Context; 027import org.nuxeo.ecm.automation.core.annotations.Operation; 028import org.nuxeo.ecm.automation.core.annotations.OperationMethod; 029import org.nuxeo.ecm.automation.core.annotations.Param; 030import org.nuxeo.ecm.core.api.CoreSession; 031import org.nuxeo.ecm.core.api.DocumentModel; 032import org.nuxeo.ecm.core.api.IdRef; 033import org.nuxeo.ecm.platform.routing.core.impl.GraphRoute; 034 035/** 036 * Set a workflow variable. The workflow variable must exists on the workflow. If no workflowId is specified the 037 * variable is set on the current workflow. 038 * 039 * @since 5.6 040 */ 041@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 " 042 + "the workflow. If no workflowId is specified the variable is set on the current workflow." 043 + "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.") 044public class SetWorkflowVar { 045 046 public static final String ID = "Context.SetWorkflowVar"; 047 048 @Context 049 protected OperationContext ctx; 050 051 @Context 052 protected CoreSession session; 053 054 @Param(name = "name") 055 protected String name; 056 057 @Param(name = "value", required = false) 058 protected Object value; 059 060 @Param(name = "workflowInstanceId", required = false) 061 protected String workflowInstanceId; 062 063 @SuppressWarnings("unchecked") 064 @OperationMethod 065 public void run() { 066 if (ctx.get(Constants.VAR_WORKFLOW) != null) { 067 ((Map<String, Serializable>) ctx.get(Constants.VAR_WORKFLOW)).put(name, (Serializable) value); 068 return; 069 } 070 if (workflowInstanceId == null && ctx.get("workflowInstanceId") != null) { 071 workflowInstanceId = (String) ctx.get("workflowInstanceId"); 072 } 073 if (workflowInstanceId == null) { 074 return; 075 } 076 DocumentModel workflowInstance = session.getDocument(new IdRef(workflowInstanceId)); 077 GraphRoute graph = workflowInstance.getAdapter(GraphRoute.class); 078 Map<String, Serializable> vars = graph.getVariables(); 079 vars.put(name, (Serializable) value); 080 graph.setVariables(vars); 081 } 082}