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.task.Task; 035import org.nuxeo.ecm.platform.task.TaskService; 036import org.nuxeo.ecm.platform.usermanager.UserManager; 037 038/** 039 * Returns all open tasks on the input document(s). If the operation is invoked with parameters, all tasks instances for 040 * the given 'processId' originating from the given 'nodeId' are returned. The 'processId' is the id of the document 041 * representing the workflow instance. The parameter 'username' is used to fetch only tasks assigned to the given user. 042 * Tasks are queried using an unrestricted session. 043 * 044 * @since 5.7.2 045 */ 046@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). " 047 + "If the operation is invoked with parameters, all tasks instances for the given 'processId' " 048 + "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. " 049 + "Tasks are queried using an unrestricted session.", aliases = { "Context.GetOpenTasks" }) 050public class GetOpenTasksOperation { 051 public static final String ID = "Workflow.GetOpenTasks"; 052 053 @Context 054 protected CoreSession session; 055 056 @Param(name = "nodeId", required = false) 057 protected String nodeId; 058 059 @Param(name = "processId", required = false) 060 protected String processId; 061 062 @Param(name = "username", required = false) 063 protected String username; 064 065 @Context 066 protected TaskService taskService; 067 068 @Context 069 protected UserManager userManager; 070 071 @OperationMethod 072 public DocumentModelList getAllTasks(DocumentModel doc) { 073 DocumentModelList taskDocs = new DocumentModelListImpl(); 074 List<Task> tasks; 075 if (nodeId == null && processId == null) { 076 NuxeoPrincipal principal = username != null ? userManager.getPrincipal(username) : null; 077 tasks = taskService.getTaskInstances(doc, principal, session); 078 for (Task task : tasks) { 079 taskDocs.add(task.getDocument()); 080 } 081 return taskDocs; 082 } 083 if (nodeId == null || processId == null) { 084 throw new NuxeoException("Need both nodeId and processId to invoke the operation with parameters"); 085 } 086 tasks = taskService.getAllTaskInstances(processId, nodeId, session); 087 for (Task task : tasks) { 088 if (task.getTargetDocumentsIds().contains(doc.getId())) { 089 if (username == null || task.getActors().contains(username)) { 090 taskDocs.add(task.getDocument()); 091 } 092 } 093 } 094 return taskDocs; 095 } 096 097 @OperationMethod 098 public DocumentModelList getAllTasks(DocumentModelList docs) { 099 DocumentModelList taskDocs = new DocumentModelListImpl(); 100 for (DocumentModel doc : docs) { 101 taskDocs.addAll(getAllTasks(doc)); 102 } 103 return taskDocs; 104 } 105}