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.platform.task.Task; 038import org.nuxeo.ecm.platform.task.TaskService; 039import org.nuxeo.ecm.platform.task.dashboard.DashBoardItem; 040import org.nuxeo.ecm.platform.task.dashboard.DashBoardItemImpl; 041 042/** 043 * Returns tasks assigned to current user or one of its groups. 044 * 045 * @since 5.5 046 */ 047@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." 048 + "Task properties are serialized using JSON and returned in a Blob.", aliases = { "Workflow.GetTask" }) 049public class GetUserTasks { 050 051 public static final String ID = "Task.GetAssigned"; 052 053 private static final Log log = LogFactory.getLog(Log.class); 054 055 @Context 056 protected OperationContext ctx; 057 058 @Context 059 protected CoreSession repo; 060 061 @Context 062 protected TaskService taskService; 063 064 @OperationMethod 065 public Blob run() throws IOException { 066 List<Task> tasks = taskService.getCurrentTaskInstances(repo); 067 if (tasks == null) { 068 return null; 069 } 070 List<Map<String, Object>> rows = new ArrayList<>(); 071 for (Task task : tasks) { 072 DocumentModel doc = taskService.getTargetDocumentModel(task, repo); 073 if (doc == null) { 074 log.warn(String.format("User '%s' has a task of type '%s' on an " + "unexisting or invisible document", 075 ctx.getPrincipal().getName(), task.getName())); 076 continue; 077 } 078 079 DashBoardItem item = new DashBoardItemImpl(task, doc, null); 080 Map<String, Object> obj = item.asMap(); 081 rows.add(obj); 082 } 083 return Blobs.createJSONBlobFromValue(rows); 084 } 085 086}