001/*
002 * Copyright (c) 2013 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 *     Mariana Cedica
011 */
012package org.nuxeo.ecm.platform.routing.core.api.operation;
013
014import java.util.HashMap;
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.automation.core.collectors.DocumentModelCollector;
024import org.nuxeo.ecm.automation.core.util.Properties;
025import org.nuxeo.ecm.core.api.CoreSession;
026import org.nuxeo.ecm.core.api.DocumentModel;
027import org.nuxeo.ecm.platform.routing.api.DocumentRoutingConstants;
028import org.nuxeo.ecm.platform.routing.api.DocumentRoutingService;
029import org.nuxeo.ecm.platform.task.Task;
030
031/**
032 * Completes a task. If this is the last task the workflow will continue.
033 *
034 * @since 5.7.2
035 */
036@Operation(id = CompleteTaskOperation.ID, category = Constants.CAT_WORKFLOW, label = "Complete task", requires = Constants.WORKFLOW_CONTEXT, description = "Completes the input task. If this is the last task the workflow will continue. "
037        + "Returns back the task document. \"Status\" is the id of the button the user would have clicked to submit the task form (if the outgoing transitions of the workflow node that created the task have conditions depending on it)."
038        + "@since 5.9.3 and 5.8.0-HF11 you can set multiple  node or workflow variables when completing the task (also similar to ending the task via form submision from the UI).The variables are specified as <i>key=value</i> pairs separated by a new line."
039        + "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, "
040        + "for instance : <p>Example:<pre>workflowVarString=A sample value<br>workflowVarDate=@{org.nuxeo.ecm.core.schema.utils.DateParser.formatW3CDateTime(CurrentDate.date)}</pre><p>"
041        + "For all values, you have to submit a JSON representation. This is an example for a variable of type StringList:"
042        + "<p><pre>nodeVarList = [\"John Doe\", \"John Test\"]</pre></p>", aliases = { "Workflow.CompleteTaskOperation" })
043public class CompleteTaskOperation {
044    public static final String ID = "WorkflowTask.Complete";
045
046    @Context
047    protected CoreSession session;
048
049    @Context
050    protected OperationContext ctx;
051
052    @Param(name = "status", required = false)
053    protected String status;
054
055    @Param(name = "comment", required = false)
056    protected String comment;
057
058    // @since 5.9.3, 5.8.0-HF11
059    @Param(name = "nodeVariables", required = false)
060    protected Properties nodeVariables;
061
062    // @since 5.9.3, 5.8.0-HF11
063    @Param(name = "workflowVariables", required = false)
064    protected Properties workflowVariables;
065
066    @Context
067    protected DocumentRoutingService documentRoutingService;
068
069    @OperationMethod(collector = DocumentModelCollector.class)
070    public DocumentModel completeTask(DocumentModel task) {
071        Map<String, Object> data = new HashMap<String, Object>();
072        if (comment != null) {
073            data.put("comment", comment);
074        }
075
076        // the service expects an unique map containing both worflow and
077        // nodeVariables
078        if (nodeVariables != null) {
079            data.put(Constants.VAR_WORKFLOW_NODE, nodeVariables);
080        }
081        if (workflowVariables != null) {
082            data.put(Constants.VAR_WORKFLOW, workflowVariables);
083        }
084        data.put(DocumentRoutingConstants._MAP_VAR_FORMAT_JSON, Boolean.TRUE);
085        documentRoutingService.endTask(session, task.getAdapter(Task.class), data, status);
086        return task;
087    }
088}