001/*
002 * (C) Copyright 2010 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 *     Stephane Lacoin
016 */
017package org.nuxeo.ecm.automation.task;
018
019import java.util.List;
020
021import net.sf.json.JSONArray;
022import net.sf.json.JSONObject;
023
024import org.apache.commons.logging.Log;
025import org.apache.commons.logging.LogFactory;
026import org.nuxeo.ecm.automation.OperationContext;
027import org.nuxeo.ecm.automation.core.Constants;
028import org.nuxeo.ecm.automation.core.annotations.Context;
029import org.nuxeo.ecm.automation.core.annotations.Operation;
030import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
031import org.nuxeo.ecm.core.api.Blob;
032import org.nuxeo.ecm.core.api.Blobs;
033import org.nuxeo.ecm.core.api.CoreSession;
034import org.nuxeo.ecm.core.api.DocumentModel;
035import org.nuxeo.ecm.core.api.DocumentNotFoundException;
036import org.nuxeo.ecm.core.api.NuxeoPrincipal;
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() {
066        List<Task> tasks = taskService.getCurrentTaskInstances(repo);
067        if (tasks == null) {
068            return null;
069        }
070        JSONArray rows = new JSONArray();
071        for (Task task : tasks) {
072            DocumentModel doc = null;
073            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            JSONObject obj = item.asJSON();
082            rows.add(obj);
083        }
084        return Blobs.createBlob(rows.toString(), "application/json");
085    }
086
087    protected NuxeoPrincipal principal() {
088        return (NuxeoPrincipal) ctx.getPrincipal();
089    }
090
091}