View Javadoc
1   /**
2    * This file Copyright (c) 2015-2016 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.ui.admincentral.shellapp.pulse.task.data;
35  
36  import info.magnolia.registry.RegistrationException;
37  import info.magnolia.task.Task;
38  import info.magnolia.task.TasksManager;
39  import info.magnolia.task.definition.TaskDefinition;
40  import info.magnolia.task.definition.registry.TaskDefinitionRegistry;
41  import info.magnolia.ui.admincentral.shellapp.pulse.data.LazyPulseQuery;
42  import info.magnolia.ui.admincentral.shellapp.pulse.data.PulseConstants;
43  
44  import java.util.Date;
45  import java.util.List;
46  
47  import javax.inject.Inject;
48  
49  import org.apache.commons.lang3.StringUtils;
50  import org.slf4j.Logger;
51  import org.slf4j.LoggerFactory;
52  
53  import com.vaadin.data.Item;
54  import com.vaadin.data.util.ObjectProperty;
55  
56  /**
57   * {@link LazyPulseQuery} implementation which serves {@link Task} objects via {@link TasksManager}.
58   */
59  public class TaskQuery extends LazyPulseQuery<Task.Status, Task> {
60  
61      private static final Logger log = LoggerFactory.getLogger(TaskQuery.class);
62  
63      private TaskDefinitionRegistry taskDefinitionRegistry;
64  
65      private TasksManager tasksManager;
66  
67      @Inject
68      public TaskQuery(TasksManager tasksManager, TaskDefinitionRegistry taskDefinitionRegistry, TaskQueryDefinition queryDefinition) {
69          super(queryDefinition);
70          this.tasksManager = tasksManager;
71          this.taskDefinitionRegistry = taskDefinitionRegistry;
72      }
73  
74      @Override
75      @SuppressWarnings("unchecked")
76      protected void mapObjectToItem(Task task, Item item) {
77          item.addItemProperty(TaskConstants.ID, new ObjectProperty(task.getId()));
78          item.addItemProperty(TaskConstants.STATUS_PROPERTY_ID, new ObjectProperty(task.getStatus()));
79          item.addItemProperty(TaskConstants.NEW_PROPERTY_ID, new ObjectProperty(task.getStatus() == Task.Status.Created, Boolean.class));
80          item.addItemProperty(TaskConstants.TASK_PROPERTY_ID, new ObjectProperty(getTaskTitle(task), String.class));
81          item.addItemProperty(TaskConstants.SENDER_PROPERTY_ID, new ObjectProperty(task.getRequestor(), String.class));
82          item.addItemProperty(TaskConstants.LAST_CHANGE_PROPERTY_ID, new ObjectProperty(task.getModificationDate(), Date.class));
83  
84          item.addItemProperty(TaskConstants.ASSIGNED_TO_PROPERTY_ID, new ObjectProperty(StringUtils.defaultString(task.getActorId()), String.class));
85  
86          String sentTo = "";
87          if (task.getGroupIds() != null && task.getGroupIds().size() > 0) {
88              sentTo += StringUtils.join(task.getGroupIds(), ",");
89          }
90          if (task.getActorIds() != null && task.getActorIds().size() > 0) {
91              sentTo += StringUtils.join(task.getActorIds(), ",");
92          }
93  
94          item.addItemProperty(TaskConstants.SENT_TO_PROPERTY_ID, new ObjectProperty(sentTo, String.class));
95      }
96  
97      @Override
98      protected long getEntriesAmount(List<Task.Status> types) {
99          return tasksManager.getTasksAmountByUserAndStatus(getQueryDefinition().userName(), types);
100     }
101 
102     @Override
103     protected List<Task> getEntries(List<Task.Status> types, int limit, int offset) {
104         long timeMs = System.currentTimeMillis();
105         List<Task> tasksCollection = tasksManager.findTasksByUserAndStatus(getQueryDefinition().userName(), types, getSortCriteria(), limit, offset);
106         log.debug("Fetched {} object list from {} in {}ms", limit, offset, System.currentTimeMillis() - timeMs);
107         return tasksCollection;
108     }
109 
110     @Override
111     protected Task createGroupingEntry(Task.Status type) {
112         Task groupingTask = new Task();
113         groupingTask.setId(PulseConstants.GROUP_PLACEHOLDER_ITEMID + type.name());
114         groupingTask.setStatus(type);
115         return groupingTask;
116     }
117 
118     protected String getTaskTitle(Task task) {
119         String title = task.getName();
120         if (title == null) {
121             return null;
122         }
123 
124         try {
125             TaskDefinition definition = taskDefinitionRegistry.get(task.getName());
126             if (definition != null) {
127                 title = definition.getTitle();
128             }
129         } catch (RegistrationException e) {
130             log.error("Could not get task definition for {}.", task.getName(), e);
131         }
132         String comment = (StringUtils.isNotEmpty(task.getComment())) ? "|" + task.getComment() : "";
133         return title + comment;
134     }
135 }