001/*
002 * (C) Copyright 2006-2009 Nuxeo SAS (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 *     Nuxeo - initial API and implementation
016 *
017 * $Id$
018 */
019
020package org.nuxeo.ecm.webapp.dashboard.workflow;
021
022import java.io.Serializable;
023import java.security.Principal;
024import java.util.ArrayList;
025import java.util.Collection;
026import java.util.List;
027
028import org.apache.commons.logging.Log;
029import org.apache.commons.logging.LogFactory;
030import org.jboss.seam.ScopeType;
031import org.jboss.seam.annotations.In;
032import org.jboss.seam.annotations.Install;
033import org.jboss.seam.annotations.Name;
034import org.jboss.seam.annotations.Observer;
035import org.jboss.seam.annotations.Scope;
036import org.jboss.seam.annotations.intercept.BypassInterceptors;
037import org.jboss.seam.international.LocaleSelector;
038import org.nuxeo.ecm.core.api.CoreSession;
039import org.nuxeo.ecm.core.api.DocumentModel;
040import org.nuxeo.ecm.core.api.LifeCycleConstants;
041import org.nuxeo.ecm.platform.task.Task;
042import org.nuxeo.ecm.platform.task.TaskEventNames;
043import org.nuxeo.ecm.platform.task.TaskService;
044import org.nuxeo.ecm.platform.task.dashboard.DashBoardItem;
045import org.nuxeo.ecm.platform.task.dashboard.DashBoardItemImpl;
046import org.nuxeo.ecm.webapp.helpers.EventNames;
047
048@Name("taskDashBoardActions")
049@Scope(ScopeType.CONVERSATION)
050@Install(precedence = Install.FRAMEWORK)
051public class TaskDashBoardActions implements Serializable {
052
053    private static final long serialVersionUID = 1L;
054
055    @In(create = true)
056    protected transient TaskService taskService;
057
058    protected Collection<DashBoardItem> currentUserTasks;
059
060    @In(required = false)
061    protected transient Principal currentUser;
062
063    @In(create = true, required = false)
064    protected transient CoreSession documentManager;
065
066    @In(create = true)
067    protected transient LocaleSelector localeSelector;
068
069    private static final Log log = LogFactory.getLog(TaskDashBoardActions.class);
070
071    public Collection<DashBoardItem> computeDashboardItems() {
072        if (currentUserTasks == null) {
073            currentUserTasks = new ArrayList<DashBoardItem>();
074            List<Task> tasks = taskService.getCurrentTaskInstances(documentManager);
075            if (tasks != null) {
076                for (Task task : tasks) {
077                    if (task.hasEnded() || task.isCancelled()) {
078                        continue;
079                    }
080                    DocumentModel doc = taskService.getTargetDocumentModel(task, documentManager);
081                    if (doc != null && !LifeCycleConstants.DELETED_STATE.equals(doc.getCurrentLifeCycleState())) {
082                        currentUserTasks.add(new DashBoardItemImpl(task, doc, localeSelector.getLocale()));
083                    } else {
084                        log.warn(
085                                String.format("User '%s' has a task of type '%s' on a " + "missing or deleted document",
086                                        currentUser.getName(), task.getName()));
087                    }
088                }
089            }
090        }
091        return currentUserTasks;
092    }
093
094    @Observer(value = { TaskEventNames.WORKFLOW_ENDED, TaskEventNames.WORKFLOW_NEW_STARTED,
095            TaskEventNames.WORKFLOW_TASK_START, TaskEventNames.WORKFLOW_TASK_STOP,
096            TaskEventNames.WORKFLOW_TASK_REJECTED, TaskEventNames.WORKFLOW_USER_ASSIGNMENT_CHANGED,
097            TaskEventNames.WORKFLOW_TASK_COMPLETED, TaskEventNames.WORKFLOW_TASK_REMOVED,
098            TaskEventNames.WORK_ITEMS_LIST_LOADED, TaskEventNames.WORKFLOW_TASKS_COMPUTED,
099            TaskEventNames.WORKFLOW_ABANDONED, TaskEventNames.WORKFLOW_CANCELED, EventNames.DOMAIN_SELECTION_CHANGED,
100            "documentPublicationRejected", "documentPublished" }, create = false)
101    @BypassInterceptors
102    public void invalidateDashboardItems() {
103        currentUserTasks = null;
104    }
105
106    public String refreshDashboardItems() {
107        currentUserTasks = null;
108        return null;
109    }
110
111}