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