View Javadoc
1   /**
2    * This file Copyright (c) 2018 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.admincentral.apps.messages;
35  
36  import info.magnolia.context.MgnlContext;
37  import info.magnolia.ui.api.message.Message;
38  import info.magnolia.ui.api.message.MessageType;
39  import info.magnolia.ui.contentapp.DataFilter;
40  import info.magnolia.ui.framework.message.MessagesManager;
41  
42  import java.util.List;
43  import java.util.Map;
44  import java.util.stream.Collectors;
45  import java.util.stream.Stream;
46  
47  import javax.inject.Inject;
48  
49  import org.apache.commons.collections4.CollectionUtils;
50  import org.apache.commons.lang3.StringUtils;
51  
52  import com.google.common.collect.Lists;
53  import com.vaadin.data.provider.AbstractDataProvider;
54  import com.vaadin.data.provider.Query;
55  import com.vaadin.data.provider.QuerySortOrder;
56  import com.vaadin.shared.data.sort.SortDirection;
57  
58  /**
59   * Provide consolidated APIs to work on {@link Message}.
60   */
61  public class MessageDataProvider extends AbstractDataProvider<Message, DataFilter> {
62  
63      private final MessagesManager messagesManager;
64      private MessageDataSourceDefinition definition;
65  
66      @Inject
67      public MessageDataProvider(MessagesManager messagesManager, MessageDataSourceDefinition definition) {
68          this.messagesManager = messagesManager;
69          this.definition = definition;
70      }
71  
72      @Override
73      public boolean isInMemory() {
74          return true;
75      }
76  
77      @Override
78      public int size(Query<Message, DataFilter> query) {
79          Long size = messagesManager.getMessagesAmount(MgnlContext.getUser().getName(), getFilteredMessageTypes());
80          return size.intValue();
81      }
82  
83      @Override
84      public Stream<Message> fetch(Query<Message, DataFilter> query) {
85  
86          final Map<String, Boolean> sortCriteria = query.getSortOrders().stream()
87                  .collect(Collectors.toMap(QuerySortOrder::getSorted, so -> so.getDirection() == SortDirection.ASCENDING ? Boolean.TRUE : Boolean.FALSE, (a, b) -> b));
88  
89          // by default, sort result set by timestamp
90          if (sortCriteria.isEmpty()) {
91              sortCriteria.put("timestamp", true);
92          }
93          int limit = query.getLimit();
94          int offset = query.getOffset();
95  
96          if (query.getFilter().isPresent() && query.getFilter().get().getPropertyFilters().containsKey("id")) {
97              String id = query.getFilter().get().getPropertyFilters().get("id").toString();
98              if (StringUtils.isNotEmpty(id)) {
99                  return Stream.of(messagesManager.getMessageById(MgnlContext.getUser().getName(), id));
100             }
101         }
102         final List<Message> messageBatch = messagesManager.getMessageBatch(MgnlContext.getUser().getName(), getFilteredMessageTypes(), sortCriteria, limit, offset);
103         return messageBatch.stream();
104     }
105 
106     protected List<MessageType> getFilteredMessageTypes() {
107         final List<MessageType> messageTypes = CollectionUtils.isEmpty(definition.getMessageTypes()) ? Lists.newArrayList(MessageType.values()) : definition.getMessageTypes().stream().map(type -> MessageType.valueOf(type)).collect(Collectors.toList());
108         return messageTypes;
109     }
110 }
111