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.core.api.LifeCycleConstants;
043import org.nuxeo.ecm.platform.task.Task;
044import org.nuxeo.ecm.platform.task.TaskEventNames;
045import org.nuxeo.ecm.platform.task.TaskService;
046import org.nuxeo.ecm.platform.task.dashboard.DashBoardItem;
047import org.nuxeo.ecm.platform.task.dashboard.DashBoardItemImpl;
048import org.nuxeo.ecm.webapp.helpers.EventNames;
049
050@Name("taskDashBoardActions")
051@Scope(ScopeType.CONVERSATION)
052@Install(precedence = Install.FRAMEWORK)
053public class TaskDashBoardActions implements Serializable {
054
055    private static final long serialVersionUID = 1L;
056
057    @In(create = true)
058    protected transient TaskService taskService;
059
060    protected Collection<DashBoardItem> currentUserTasks;
061
062    @In(required = false)
063    protected transient Principal currentUser;
064
065    @In(create = true, required = false)
066    protected transient CoreSession documentManager;
067
068    @In(create = true)
069    protected transient LocaleSelector localeSelector;
070
071    private static final Log log = LogFactory.getLog(TaskDashBoardActions.class);
072
073    public Collection<DashBoardItem> computeDashboardItems() {
074        if (currentUserTasks == null) {
075            currentUserTasks = new ArrayList<DashBoardItem>();
076            List<Task> tasks = taskService.getCurrentTaskInstances(documentManager);
077            if (tasks != null) {
078                for (Task task : tasks) {
079                    if (task.hasEnded() || task.isCancelled()) {
080                        continue;
081                    }
082                    DocumentModel doc = taskService.getTargetDocumentModel(task, documentManager);
083                    if (doc != null && !LifeCycleConstants.DELETED_STATE.equals(doc.getCurrentLifeCycleState())) {
084                        currentUserTasks.add(new DashBoardItemImpl(task, doc, localeSelector.getLocale()));
085                    } else {
086                        log.warn(
087                                String.format("User '%s' has a task of type '%s' on a " + "missing or deleted document",
088                                        currentUser.getName(), task.getName()));
089                    }
090                }
091            }
092        }
093        return currentUserTasks;
094    }
095
096    @Observer(value = { TaskEventNames.WORKFLOW_ENDED, TaskEventNames.WORKFLOW_NEW_STARTED,
097            TaskEventNames.WORKFLOW_TASK_START, TaskEventNames.WORKFLOW_TASK_STOP,
098            TaskEventNames.WORKFLOW_TASK_REJECTED, TaskEventNames.WORKFLOW_USER_ASSIGNMENT_CHANGED,
099            TaskEventNames.WORKFLOW_TASK_COMPLETED, TaskEventNames.WORKFLOW_TASK_REMOVED,
100            TaskEventNames.WORK_ITEMS_LIST_LOADED, TaskEventNames.WORKFLOW_TASKS_COMPUTED,
101            TaskEventNames.WORKFLOW_ABANDONED, TaskEventNames.WORKFLOW_CANCELED, EventNames.DOMAIN_SELECTION_CHANGED,
102            "documentPublicationRejected", "documentPublished" }, create = false)
103    @BypassInterceptors
104    public void invalidateDashboardItems() {
105        currentUserTasks = null;
106    }
107
108    public String refreshDashboardItems() {
109        currentUserTasks = null;
110        return null;
111    }
112
113}