001/* 002 * (C) Copyright 2010 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 * Stephane Lacoin 018 */ 019package org.nuxeo.ecm.automation.task; 020 021import java.io.IOException; 022import java.util.ArrayList; 023import java.util.List; 024import java.util.Map; 025 026import org.apache.commons.logging.Log; 027import org.apache.commons.logging.LogFactory; 028import org.nuxeo.ecm.automation.OperationContext; 029import org.nuxeo.ecm.automation.core.Constants; 030import org.nuxeo.ecm.automation.core.annotations.Context; 031import org.nuxeo.ecm.automation.core.annotations.Operation; 032import org.nuxeo.ecm.automation.core.annotations.OperationMethod; 033import org.nuxeo.ecm.core.api.Blob; 034import org.nuxeo.ecm.core.api.Blobs; 035import org.nuxeo.ecm.core.api.CoreSession; 036import org.nuxeo.ecm.core.api.DocumentModel; 037import org.nuxeo.ecm.core.api.NuxeoPrincipal; 038import org.nuxeo.ecm.platform.task.Task; 039import org.nuxeo.ecm.platform.task.TaskService; 040import org.nuxeo.ecm.platform.task.dashboard.DashBoardItem; 041import org.nuxeo.ecm.platform.task.dashboard.DashBoardItemImpl; 042 043/** 044 * Returns tasks assigned to current user or one of its groups. 045 * 046 * @since 5.5 047 */ 048@Operation(id = GetUserTasks.ID, category = Constants.CAT_SERVICES, label = "Get user tasks", since = "5.4", description = "List tasks assigned to this user or one of its group." 049 + "Task properties are serialized using JSON and returned in a Blob.", aliases = { "Workflow.GetTask" }) 050public class GetUserTasks { 051 052 public static final String ID = "Task.GetAssigned"; 053 054 private static final Log log = LogFactory.getLog(Log.class); 055 056 @Context 057 protected OperationContext ctx; 058 059 @Context 060 protected CoreSession repo; 061 062 @Context 063 protected TaskService taskService; 064 065 @OperationMethod 066 public Blob run() throws IOException { 067 List<Task> tasks = taskService.getCurrentTaskInstances(repo); 068 if (tasks == null) { 069 return null; 070 } 071 List<Map<String, Object>> rows = new ArrayList<>(); 072 for (Task task : tasks) { 073 DocumentModel doc = taskService.getTargetDocumentModel(task, repo); 074 if (doc == null) { 075 log.warn(String.format("User '%s' has a task of type '%s' on an " + "unexisting or invisible document", 076 principal().getName(), task.getName())); 077 continue; 078 } 079 080 DashBoardItem item = new DashBoardItemImpl(task, doc, null); 081 Map<String, Object> obj = item.asMap(); 082 rows.add(obj); 083 } 084 return Blobs.createJSONBlobFromValue(rows); 085 } 086 087 protected NuxeoPrincipal principal() { 088 return (NuxeoPrincipal) ctx.getPrincipal(); 089 } 090 091}