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