001/*
002 * (C) Copyright 2013 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Mariana Cedica
016 */
017package org.nuxeo.ecm.platform.routing.core.api.operation;
018
019import java.util.ArrayList;
020import java.util.List;
021
022import org.nuxeo.ecm.automation.core.Constants;
023import org.nuxeo.ecm.automation.core.annotations.Context;
024import org.nuxeo.ecm.automation.core.annotations.Operation;
025import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
026import org.nuxeo.ecm.automation.core.annotations.Param;
027import org.nuxeo.ecm.core.api.CoreSession;
028import org.nuxeo.ecm.core.api.DocumentModel;
029import org.nuxeo.ecm.core.api.DocumentModelList;
030import org.nuxeo.ecm.core.api.NuxeoException;
031import org.nuxeo.ecm.core.api.NuxeoPrincipal;
032import org.nuxeo.ecm.core.api.impl.DocumentModelListImpl;
033import org.nuxeo.ecm.platform.task.Task;
034import org.nuxeo.ecm.platform.task.TaskService;
035import org.nuxeo.ecm.platform.usermanager.UserManager;
036
037/**
038 * Returns all open tasks on the input document(s). If the operation is invoked with parameters, all tasks instances for
039 * the given 'processId' originating from the given 'nodeId' are returned. The 'processId' is the id of the document
040 * representing the workflow instance. The parameter 'username' is used to fetch only tasks assigned to the given user.
041 * Tasks are queried using an unrestricted session.
042 *
043 * @since 5.7.2
044 */
045@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). "
046        + "If the operation is invoked with parameters, all tasks instances for the given 'processId' "
047        + "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. "
048        + "Tasks are queried using an unrestricted session.", aliases = { "Context.GetOpenTasks" })
049public class GetOpenTasksOperation {
050    public static final String ID = "Workflow.GetOpenTasks";
051
052    @Context
053    protected CoreSession session;
054
055    @Param(name = "nodeId", required = false)
056    protected String nodeId;
057
058    @Param(name = "processId", required = false)
059    protected String processId;
060
061    @Param(name = "username", required = false)
062    protected String username;
063
064    @Context
065    protected TaskService taskService;
066
067    @Context
068    protected UserManager userManager;
069
070    @OperationMethod
071    public DocumentModelList getAllTasks(DocumentModel doc) {
072        DocumentModelList taskDocs = new DocumentModelListImpl();
073        List<Task> tasks = new ArrayList<Task>();
074        if (nodeId == null && processId == null) {
075            NuxeoPrincipal principal = username != null ? userManager.getPrincipal(username) : null;
076            tasks = taskService.getTaskInstances(doc, principal, session);
077            for (Task task : tasks) {
078                taskDocs.add(task.getDocument());
079            }
080            return taskDocs;
081        }
082        if (nodeId == null || processId == null) {
083            throw new NuxeoException("Need both nodeId and processId to invoke the operation with parameters");
084        }
085        tasks = taskService.getAllTaskInstances(processId, nodeId, session);
086        for (Task task : tasks) {
087            if (doc.getId().equals(task.getTargetDocumentId())) {
088                if (username == null) {
089                    taskDocs.add(task.getDocument());
090                } else {
091                    if (task.getActors().contains(username)) {
092                        taskDocs.add(task.getDocument());
093                    }
094                }
095            }
096        }
097        return taskDocs;
098    }
099
100    @OperationMethod
101    public DocumentModelList getAllTasks(DocumentModelList docs) {
102        DocumentModelList taskDocs = new DocumentModelListImpl();
103        for (DocumentModel doc : docs) {
104            taskDocs.addAll(getAllTasks(doc));
105        }
106        return taskDocs;
107    }
108}