001/* 002 * (C) Copyright 2011-2018 Nuxeo (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 * Anahide Tchertchian 018 */ 019package org.nuxeo.ecm.platform.task.providers; 020 021import java.io.Serializable; 022import java.util.ArrayList; 023import java.util.List; 024import java.util.Locale; 025import java.util.Map; 026 027import org.apache.commons.lang3.LocaleUtils; 028import org.apache.commons.lang3.StringUtils; 029import org.apache.commons.logging.Log; 030import org.apache.commons.logging.LogFactory; 031import org.nuxeo.ecm.core.api.CoreSession; 032import org.nuxeo.ecm.core.api.DocumentModel; 033import org.nuxeo.ecm.core.api.NuxeoException; 034import org.nuxeo.ecm.core.api.NuxeoPrincipal; 035import org.nuxeo.ecm.platform.query.api.AbstractPageProvider; 036import org.nuxeo.ecm.platform.query.api.PageProvider; 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; 041import org.nuxeo.runtime.api.Framework; 042 043/** 044 * Page provider for {@link DashBoardItem} elements. 045 * <p> 046 * Useful for content views displaying users' tasks. 047 * <p> 048 * WARNING: this page provider does not handle sorting, and its pagination management is not efficient (done in post 049 * filter). 050 * <p> 051 * This page provider requires the property {@link #CORE_SESSION_PROPERTY} to be filled with a core session. It also 052 * accepts an optional property {@link #FILTER_DOCS_FROM_TRASH}, defaulting to true. 053 * 054 * @since 5.5 055 */ 056public class UserTaskPageProvider extends AbstractPageProvider<DashBoardItem> implements PageProvider<DashBoardItem> { 057 058 private static final long serialVersionUID = 1L; 059 060 private static final Log log = LogFactory.getLog(UserTaskPageProvider.class); 061 062 public static final String CORE_SESSION_PROPERTY = "coreSession"; 063 064 public static final String FILTER_DOCS_FROM_TRASH = "filterDocumentsFromTrash"; 065 066 protected List<DashBoardItem> userTasks; 067 068 protected List<DashBoardItem> pageTasks; 069 070 @Override 071 public List<DashBoardItem> getCurrentPage() { 072 if (pageTasks == null) { 073 pageTasks = new ArrayList<>(); 074 if (userTasks == null) { 075 getAllTasks(); 076 } 077 if (!hasError()) { 078 long resultsCount = userTasks.size(); 079 setResultsCount(resultsCount); 080 // post-filter the results "by hand" to handle pagination 081 long pageSize = getMinMaxPageSize(); 082 if (pageSize == 0) { 083 pageTasks.addAll(userTasks); 084 } else { 085 // handle offset 086 long offset = getCurrentPageOffset(); 087 if (offset <= resultsCount) { 088 for (int i = (int) offset; i < resultsCount && i < offset + pageSize; i++) { 089 pageTasks.add(userTasks.get(i)); 090 } 091 } 092 } 093 } 094 } 095 return pageTasks; 096 } 097 098 protected Locale getLocale() { 099 String locale = (String) getProperties().get("locale"); 100 if (!StringUtils.isBlank(locale)) { 101 return LocaleUtils.toLocale(locale); 102 } 103 return Locale.getDefault(); 104 } 105 106 protected void getAllTasks() { 107 error = null; 108 errorMessage = null; 109 userTasks = new ArrayList<>(); 110 CoreSession coreSession = getCoreSession(); 111 boolean filterTrashDocs = getFilterDocumentsInTrash(); 112 NuxeoPrincipal pal = coreSession.getPrincipal(); 113 TaskService taskService = Framework.getService(TaskService.class); 114 List<Task> tasks = taskService.getAllCurrentTaskInstances(coreSession, getSortInfos()); 115 if (tasks != null) { 116 for (Task task : tasks) { 117 List<String> targetDocumentsIds = task.getTargetDocumentsIds(); 118 boolean hasTargetDocuments = targetDocumentsIds != null && !targetDocumentsIds.isEmpty(); 119 if (task.hasEnded() || task.isCancelled() || !hasTargetDocuments) { 120 continue; 121 } 122 DocumentModel doc = taskService.getTargetDocumentModel(task, coreSession); 123 if (doc != null) { 124 if (!filterTrashDocs || !doc.isTrashed()) { 125 userTasks.add(new DashBoardItemImpl(task, doc, getLocale())); 126 } 127 } else { 128 log.warn(String.format("User '%s' has a task of type '%s' on a missing or deleted document", 129 pal.getName(), task.getName())); 130 } 131 } 132 } 133 } 134 135 protected boolean getFilterDocumentsInTrash() { 136 Map<String, Serializable> props = getProperties(); 137 if (props.containsKey(FILTER_DOCS_FROM_TRASH)) { 138 return Boolean.TRUE.equals(Boolean.valueOf((String) props.get(FILTER_DOCS_FROM_TRASH))); 139 } 140 return true; 141 } 142 143 protected CoreSession getCoreSession() { 144 Map<String, Serializable> props = getProperties(); 145 CoreSession coreSession = (CoreSession) props.get(CORE_SESSION_PROPERTY); 146 if (coreSession == null) { 147 throw new NuxeoException("cannot find core session"); 148 } 149 return coreSession; 150 } 151 152 /** 153 * This page provider does not support sort for now => override what may be contributed in the definition 154 */ 155 @Override 156 public boolean isSortable() { 157 return false; 158 } 159 160 @Override 161 protected void pageChanged() { 162 pageTasks = null; 163 super.pageChanged(); 164 } 165 166 @Override 167 public void refresh() { 168 userTasks = null; 169 pageTasks = null; 170 super.refresh(); 171 } 172 173}