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.ArrayList; 016import java.util.HashMap; 017import java.util.List; 018import java.util.Map; 019import java.util.Map.Entry; 020 021import org.apache.commons.logging.Log; 022import org.apache.commons.logging.LogFactory; 023import org.nuxeo.ecm.automation.OperationContext; 024import org.nuxeo.ecm.automation.core.Constants; 025import org.nuxeo.ecm.automation.core.annotations.Context; 026import org.nuxeo.ecm.automation.core.annotations.Operation; 027import org.nuxeo.ecm.automation.core.annotations.OperationMethod; 028import org.nuxeo.ecm.automation.core.annotations.Param; 029import org.nuxeo.ecm.automation.core.util.Properties; 030import org.nuxeo.ecm.core.api.CoreSession; 031import org.nuxeo.ecm.core.api.DocumentModel; 032import org.nuxeo.ecm.core.api.DocumentModelList; 033import org.nuxeo.ecm.platform.routing.api.DocumentRoutingConstants; 034import org.nuxeo.ecm.platform.routing.api.DocumentRoutingService; 035 036/** 037 * Starts the workflow with the given model id on the input documents. Returns back the input documents. The id of the 038 * created workflow instance is available under the \"WorkflowId\" context variable. 039 * 040 * @since 5.6 041 */ 042@Operation(id = StartWorkflowOperation.ID, category = Constants.CAT_WORKFLOW, label = "Start workflow", requires = Constants.WORKFLOW_CONTEXT, description = "Starts the workflow with the given model id on the input documents. Returns back the input documents." 043 + "The id of the created workflow instance is available under the \"workflowInstanceId\" context variable." 044 + "@Since 5.7.2 you can set multiple variables on the workflow (before 5.8 only scalar types are supported). The variables are specified as <i>key=value</i> pairs separated by a new line." 045 + "To specify multi-line values you can use a \\ character followed by a new line. <p>Example:<pre>description=foo bar</pre>For updating a date, you will need to expose the value as ISO 8601 format, " 046 + "for instance : <p>Example:<pre>title=The Document Title<br>issued=@{org.nuxeo.ecm.core.schema.utils.DateParser.formatW3CDateTime(CurrentDate.date)}</pre><p>" 047 + " @since 5.9.3 and 5.8.0-HF10 you can also set variables of complex types, by submiting a JSON representation: " 048 + "<p><pre>assignees = [\"John Doe\", \"John Test\"]</pre></p>") 049public class StartWorkflowOperation { 050 051 public static final String ID = "Context.StartWorkflow"; 052 053 private static Log log = LogFactory.getLog(StartWorkflowOperation.class); 054 055 @Context 056 protected CoreSession session; 057 058 @Context 059 protected OperationContext ctx; 060 061 @Param(name = "id", required = true) 062 protected String id; 063 064 @Param(name = "start", required = false, values = "true") 065 protected Boolean start = true; 066 067 // @since 5.7.2 068 @Param(name = "variables", required = false) 069 protected Properties variables; 070 071 @Context 072 protected DocumentRoutingService documentRoutingService; 073 074 @OperationMethod 075 public DocumentModelList run(DocumentModelList docs) { 076 List<String> ids = new ArrayList<String>(); 077 for (DocumentModel doc : docs) { 078 ids.add(doc.getId()); 079 } 080 startNewInstance(ids); 081 return docs; 082 } 083 084 @OperationMethod 085 public DocumentModel run(DocumentModel doc) { 086 List<String> ids = new ArrayList<String>(); 087 ids.add(doc.getId()); 088 startNewInstance(ids); 089 return doc; 090 } 091 092 protected void startNewInstance(List<String> ids) { 093 Map<String, Serializable> vars = new HashMap<String, Serializable>(); 094 if (variables != null) { 095 for (Entry<String, String> entry : variables.entrySet()) { 096 vars.put(entry.getKey(), entry.getValue()); 097 } 098 } 099 vars.put(DocumentRoutingConstants._MAP_VAR_FORMAT_JSON, Boolean.TRUE); 100 String workflowId = documentRoutingService.createNewInstance(id, ids, vars, session, Boolean.TRUE.equals(start)); 101 ctx.put("WorkflowId", workflowId); 102 // to be consistent with all the other workflow variablesin the context 103 // @since 5.7.2 104 ctx.put("workflowInstanceId", workflowId); 105 106 } 107}