001/*
002 * (C) Copyright 2013 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 *     Mariana Cedica
018 */
019package org.nuxeo.ecm.platform.routing.core.api.operation;
020
021import java.util.HashMap;
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.automation.core.collectors.DocumentModelCollector;
031import org.nuxeo.ecm.automation.core.util.Properties;
032import org.nuxeo.ecm.core.api.CoreSession;
033import org.nuxeo.ecm.core.api.DocumentModel;
034import org.nuxeo.ecm.platform.routing.api.DocumentRoutingConstants;
035import org.nuxeo.ecm.platform.routing.api.DocumentRoutingService;
036import org.nuxeo.ecm.platform.task.Task;
037
038/**
039 * Completes a task. If this is the last task the workflow will continue.
040 *
041 * @since 5.7.2
042 */
043@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. "
044        + "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)."
045        + "@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."
046        + "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, "
047        + "for instance : <p>Example:<pre>workflowVarString=A sample value<br>workflowVarDate=@{org.nuxeo.ecm.core.schema.utils.DateParser.formatW3CDateTime(CurrentDate.date)}</pre><p>"
048        + "For all values, you have to submit a JSON representation. This is an example for a variable of type StringList:"
049        + "<p><pre>nodeVarList = [\"John Doe\", \"John Test\"]</pre></p>", aliases = { "Workflow.CompleteTaskOperation" })
050public class CompleteTaskOperation {
051    public static final String ID = "WorkflowTask.Complete";
052
053    @Context
054    protected CoreSession session;
055
056    @Context
057    protected OperationContext ctx;
058
059    @Param(name = "status", required = false)
060    protected String status;
061
062    @Param(name = "comment", required = false)
063    protected String comment;
064
065    // @since 5.9.3, 5.8.0-HF11
066    @Param(name = "nodeVariables", required = false)
067    protected Properties nodeVariables;
068
069    // @since 5.9.3, 5.8.0-HF11
070    @Param(name = "workflowVariables", required = false)
071    protected Properties workflowVariables;
072
073    @Context
074    protected DocumentRoutingService documentRoutingService;
075
076    @OperationMethod(collector = DocumentModelCollector.class)
077    public DocumentModel completeTask(DocumentModel task) {
078        Map<String, Object> data = new HashMap<>();
079        if (comment != null) {
080            data.put("comment", comment);
081        }
082
083        // the service expects an unique map containing both worflow and
084        // nodeVariables
085        if (nodeVariables != null) {
086            data.put(Constants.VAR_WORKFLOW_NODE, nodeVariables);
087        }
088        if (workflowVariables != null) {
089            data.put(Constants.VAR_WORKFLOW, workflowVariables);
090        }
091        data.put(DocumentRoutingConstants._MAP_VAR_FORMAT_JSON, Boolean.TRUE);
092        documentRoutingService.endTask(session, task.getAdapter(Task.class), data, status);
093        return task;
094    }
095}