View Javadoc
1   /**
2    * This file Copyright (c) 2012-2014 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.message;
35  
36  import static info.magnolia.ui.admincentral.shellapp.pulse.item.list.AbstractPulseListView.GROUP_PLACEHOLDER_ITEMID;
37  
38  import info.magnolia.ui.admincentral.shellapp.pulse.item.detail.PulseItemCategory;
39  import info.magnolia.ui.admincentral.shellapp.pulse.item.list.AbstractPulseListContainer;
40  import info.magnolia.ui.api.message.Message;
41  import info.magnolia.ui.api.message.MessageType;
42  
43  import java.util.Collection;
44  import java.util.Date;
45  
46  import com.vaadin.data.Container;
47  import com.vaadin.data.Item;
48  import com.vaadin.data.util.HierarchicalContainer;
49  
50  /**
51   * The messages container instantiates and manages an {@link HierarchicalContainer} with messages.
52   */
53  public class MessagesContainer extends AbstractPulseListContainer<Message> {
54  
55      public static final String NEW_PROPERTY_ID = "new";
56      public static final String TYPE_PROPERTY_ID = "type";
57      public static final String SUBJECT_PROPERTY_ID = "subject";
58      public static final String TEXT_PROPERTY_ID = "text";
59      public static final String SENDER_PROPERTY_ID = "sender";
60      public static final String DATE_PROPERTY_ID = "date";
61      public static final String QUICKDO_PROPERTY_ID = "quickdo";
62  
63      /*
64       * This filter hides grouping titles when
65       * grouping is not on or group would be empty
66       */
67      private Container.Filter sectionFilter = new Container.Filter() {
68  
69          @Override
70          public boolean passesFilter(Object itemId, Item item) throws UnsupportedOperationException {
71              if (itemId.toString().startsWith(GROUP_PLACEHOLDER_ITEMID) && (!grouping || isTypeGroupEmpty(itemId))) {
72                  return false;
73              }
74  
75              return true;
76          }
77  
78          @Override
79          public boolean appliesToProperty(Object propertyId) {
80              return TYPE_PROPERTY_ID.equals(propertyId);
81          }
82  
83          private boolean isTypeGroupEmpty(Object typeId) {
84              return container.getChildren(typeId) == null || container.getChildren(typeId).isEmpty();
85          }
86  
87      };
88      @Override
89      public HierarchicalContainer createDataSource(Collection<Message> messages) {
90          container = new HierarchicalContainer();
91          container.addContainerProperty(NEW_PROPERTY_ID, Boolean.class, true);
92          container.addContainerProperty(SUBJECT_PROPERTY_ID, String.class, null);
93          container.addContainerProperty(TYPE_PROPERTY_ID, MessageType.class, MessageType.UNKNOWN);
94          container.addContainerProperty(TEXT_PROPERTY_ID, String.class, null);
95          container.addContainerProperty(SENDER_PROPERTY_ID, String.class, null);
96          container.addContainerProperty(DATE_PROPERTY_ID, Date.class, null);
97          container.addContainerProperty(QUICKDO_PROPERTY_ID, String.class, null);
98  
99          createSuperItems();
100 
101         for (Message message : messages) {
102             addBeanAsItem(message);
103         }
104 
105         container.addContainerFilter(getSectionFilter());
106 
107         return container;
108     }
109 
110     @Override
111     public void addBeanAsItem(Message message) {
112         // filter out local messages that have id == null
113         if (message.getId() != null) {
114             final Item item = container.addItem(message.getId());
115             container.setChildrenAllowed(message.getId(), false);
116             assignPropertiesFromBean(message, item);
117         }
118     }
119 
120     @Override
121     public void assignPropertiesFromBean(Message message, Item item) {
122         if (item != null && message != null) {
123             item.getItemProperty(NEW_PROPERTY_ID).setValue(!message.isCleared());
124             item.getItemProperty(TYPE_PROPERTY_ID).setValue(message.getType());
125             item.getItemProperty(SENDER_PROPERTY_ID).setValue(message.getSender());
126             item.getItemProperty(SUBJECT_PROPERTY_ID).setValue(message.getSubject());
127             item.getItemProperty(TEXT_PROPERTY_ID).setValue((message.getMessage()));
128             item.getItemProperty(DATE_PROPERTY_ID).setValue(new Date(message.getTimestamp()));
129         }
130     }
131 
132     @Override
133     protected void createSuperItems() {
134         for (MessageType type : MessageType.values()) {
135             Object itemId = getSuperItem(type);
136             Item item = container.addItem(itemId);
137             item.getItemProperty(TYPE_PROPERTY_ID).setValue(type);
138             container.setChildrenAllowed(itemId, true);
139         }
140     }
141 
142     @Override
143     protected void clearSuperItems() {
144         for (Object itemId : container.getItemIds()) {
145             container.setParent(itemId, null);
146         }
147     }
148 
149     private Object getSuperItem(MessageType type) {
150         return GROUP_PLACEHOLDER_ITEMID + type;
151     }
152 
153     /*
154      * Assign messages under correct parents so that
155      * grouping works.
156     */
157     @Override
158     public void buildTree() {
159 
160         for (Object itemId : container.getItemIds()) {
161             // Skip super items
162             if (!itemId.toString().startsWith(GROUP_PLACEHOLDER_ITEMID)) {
163                 Item item = container.getItem(itemId);
164                 MessageType type = (MessageType) item.getItemProperty(TYPE_PROPERTY_ID).getValue();
165                 Object parentItemId = getSuperItem(type);
166                 Item parentItem = container.getItem(parentItemId);
167                 if (parentItem != null) {
168                     container.setParent(itemId, parentItemId);
169                 }
170             }
171         }
172     }
173 
174     @Override
175     protected Container.Filter getSectionFilter() {
176         return sectionFilter;
177     }
178 
179     @Override
180     protected void applyCategoryFilter(final PulseItemCategory category) {
181 
182         final Container.Filter filter = new Container.Filter() {
183 
184             @Override
185             public boolean passesFilter(Object itemId, Item item) throws UnsupportedOperationException {
186                 final MessageType type = (MessageType) item.getItemProperty(TYPE_PROPERTY_ID).getValue();
187 
188                 switch (category) {
189                 case PROBLEM:
190                     return type == MessageType.ERROR || type == MessageType.WARNING;
191                 case INFO:
192                     return type == MessageType.INFO;
193                 default:
194                     return true;
195                 }
196             }
197 
198             @Override
199             public boolean appliesToProperty(Object propertyId) {
200                 return TYPE_PROPERTY_ID.equals(propertyId);
201             }
202 
203         };
204         container.addContainerFilter(filter);
205     }
206 
207 }