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