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.ArrayList;
022import java.util.List;
023
024import org.nuxeo.ecm.automation.core.Constants;
025import org.nuxeo.ecm.automation.core.annotations.Context;
026import org.nuxeo.ecm.automation.core.annotations.Operation;
027import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
028import org.nuxeo.ecm.automation.core.annotations.Param;
029import org.nuxeo.ecm.core.api.CoreSession;
030import org.nuxeo.ecm.core.api.DocumentModel;
031import org.nuxeo.ecm.core.api.DocumentModelList;
032import org.nuxeo.ecm.core.api.NuxeoException;
033import org.nuxeo.ecm.core.api.NuxeoPrincipal;
034import org.nuxeo.ecm.core.api.impl.DocumentModelListImpl;
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    @Param(name = "nodeId", required = false)
058    protected String nodeId;
059
060    @Param(name = "processId", required = false)
061    protected String processId;
062
063    @Param(name = "username", required = false)
064    protected String username;
065
066    @Context
067    protected TaskService taskService;
068
069    @Context
070    protected UserManager userManager;
071
072    @OperationMethod
073    public DocumentModelList getAllTasks(DocumentModel doc) {
074        DocumentModelList taskDocs = new DocumentModelListImpl();
075        List<Task> tasks = new ArrayList<Task>();
076        if (nodeId == null && processId == null) {
077            NuxeoPrincipal principal = username != null ? userManager.getPrincipal(username) : null;
078            tasks = taskService.getTaskInstances(doc, principal, session);
079            for (Task task : tasks) {
080                taskDocs.add(task.getDocument());
081            }
082            return taskDocs;
083        }
084        if (nodeId == null || processId == null) {
085            throw new NuxeoException("Need both nodeId and processId to invoke the operation with parameters");
086        }
087        tasks = taskService.getAllTaskInstances(processId, nodeId, session);
088        for (Task task : tasks) {
089            if (doc.getId().equals(task.getTargetDocumentId())) {
090                if (username == null) {
091                    taskDocs.add(task.getDocument());
092                } else {
093                    if (task.getActors().contains(username)) {
094                        taskDocs.add(task.getDocument());
095                    }
096                }
097            }
098        }
099        return taskDocs;
100    }
101
102    @OperationMethod
103    public DocumentModelList getAllTasks(DocumentModelList docs) {
104        DocumentModelList taskDocs = new DocumentModelListImpl();
105        for (DocumentModel doc : docs) {
106            taskDocs.addAll(getAllTasks(doc));
107        }
108        return taskDocs;
109    }
110}