001/*
002 * (C) Copyright 2013-2016 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.List;
022
023import org.nuxeo.ecm.automation.core.Constants;
024import org.nuxeo.ecm.automation.core.annotations.Context;
025import org.nuxeo.ecm.automation.core.annotations.Operation;
026import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
027import org.nuxeo.ecm.automation.core.annotations.Param;
028import org.nuxeo.ecm.core.api.CoreSession;
029import org.nuxeo.ecm.core.api.DocumentModel;
030import org.nuxeo.ecm.core.api.DocumentModelList;
031import org.nuxeo.ecm.core.api.NuxeoException;
032import org.nuxeo.ecm.core.api.NuxeoPrincipal;
033import org.nuxeo.ecm.core.api.impl.DocumentModelListImpl;
034import org.nuxeo.ecm.platform.routing.core.impl.GraphNodeImpl;
035import org.nuxeo.ecm.platform.task.Task;
036import org.nuxeo.ecm.platform.task.TaskService;
037import org.nuxeo.ecm.platform.usermanager.UserManager;
038
039/**
040 * Returns all open tasks on the input document(s). If the operation is invoked with parameters, all tasks instances for
041 * the given 'processId' originating from the given 'nodeId' are returned. The 'processId' is the id of the document
042 * representing the workflow instance. The parameter 'username' is used to fetch only tasks assigned to the given user.
043 * Tasks are queried using an unrestricted session.
044 *
045 * @since 5.7.2
046 */
047@Operation(id = GetOpenTasksOperation.ID, category = Constants.CAT_WORKFLOW, label = "Get open tasks", requires = Constants.WORKFLOW_CONTEXT, description = "Returns all open tasks for the input document(s). "
048        + "If the operation is invoked with parameters, all tasks instances for the given 'processId' "
049        + "originating from the given 'nodeId' are returned. The 'processId' is the id of the document representing the workflow instance. The parameter 'username' is used to fetch only tasks assigned to the given user. "
050        + "Tasks are queried using an unrestricted session.", aliases = { "Context.GetOpenTasks" })
051public class GetOpenTasksOperation {
052    public static final String ID = "Workflow.GetOpenTasks";
053
054    @Context
055    protected CoreSession session;
056
057    /**
058     * If not provided and the operation is run within a workflow (chain called by a transition or an escalation rule
059     * for instance), this parameter is bound to the "nodeId" operation context variable, see
060     * {@link GraphNodeImpl#getWorkflowContextualInfo(CoreSession, boolean)}.
061     */
062    @Param(name = "nodeId", required = false)
063    protected String nodeId;
064
065    /**
066     * If not provided and the operation is run within a workflow (chain called by a transition or an escalation rule
067     * for instance), this parameter is bound to the "workflowInstanceId" operation context variable, see
068     * {@link GraphNodeImpl#getWorkflowContextualInfo(CoreSession, boolean)}.
069     */
070    @Param(name = "processId", alias = "workflowInstanceId", required = false)
071    protected String processId;
072
073    @Param(name = "username", required = false)
074    protected String username;
075
076    @Context
077    protected TaskService taskService;
078
079    @Context
080    protected UserManager userManager;
081
082    @OperationMethod
083    public DocumentModelList getAllTasks(DocumentModel doc) {
084        DocumentModelList taskDocs = new DocumentModelListImpl();
085        List<Task> tasks;
086        if (nodeId == null && processId == null) {
087            NuxeoPrincipal principal = username != null ? userManager.getPrincipal(username) : null;
088            tasks = taskService.getTaskInstances(doc, principal, session);
089            for (Task task : tasks) {
090                taskDocs.add(task.getDocument());
091            }
092            return taskDocs;
093        }
094        if (nodeId == null || processId == null) {
095            throw new NuxeoException("Need both nodeId and processId to invoke the operation with parameters");
096        }
097        tasks = taskService.getAllTaskInstances(processId, nodeId, session);
098        for (Task task : tasks) {
099            if (task.getTargetDocumentsIds().contains(doc.getId())) {
100                if (username == null || task.getActors().contains(username)) {
101                    taskDocs.add(task.getDocument());
102                }
103            }
104        }
105        return taskDocs;
106    }
107
108    @OperationMethod
109    public DocumentModelList getAllTasks(DocumentModelList docs) {
110        DocumentModelList taskDocs = new DocumentModelListImpl();
111        for (DocumentModel doc : docs) {
112            taskDocs.addAll(getAllTasks(doc));
113        }
114        return taskDocs;
115    }
116}