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.ui.datasource.optionlist;
35  
36  import info.magnolia.ui.filter.DataFilter;
37  
38  import java.util.Comparator;
39  import java.util.stream.Stream;
40  
41  import javax.inject.Inject;
42  
43  import org.apache.commons.collections4.ComparatorUtils;
44  import org.apache.commons.lang3.StringUtils;
45  
46  import com.vaadin.data.provider.AbstractDataProvider;
47  import com.vaadin.data.provider.Query;
48  
49  /**
50   * A data provider implementation for "static" sets of data unlikely to vary in a user request time span.
51   * They typically reside in some kind of configuration file, e.g. a yaml file on the file system, JCR config, etc.
52   */
53  public class OptionListProvider extends AbstractDataProvider<Option, DataFilter> {
54  
55      private final OptionListDefinition definition;
56  
57      private static final PreconfiguredOptionComparator PRECONFIGURED_OPTION_COMPARATOR = new PreconfiguredOptionComparator();
58  
59      @Inject
60      public OptionListProvider(OptionListDefinition definition) {
61          this.definition = definition;
62      }
63  
64      @Override
65      public boolean isInMemory() {
66          return true;
67      }
68  
69      @Override
70      public int size(Query<Option, DataFilter> query) {
71          return (int) fetch(query).count();
72      }
73  
74      @Override
75      public Stream<Option> fetch(Query<Option, DataFilter> query) {
76          final Stream<Option> stream = definition.getOptions()
77                  .stream()
78                  .filter(property ->
79                          query.getFilter()
80                                  .map(f -> isValueMatching(f, property.getValue()))
81                                  .orElse(true));
82  
83          return definition.isSortable() ? stream.sorted(PRECONFIGURED_OPTION_COMPARATOR) : stream;
84      }
85  
86      /*
87       * A null safe comparator based on the label or value.
88       */
89      private static class PreconfiguredOptionComparator implements Comparator<Option> {
90          @Override
91          public int compare(Optionref="../../../../../info/magnolia/ui/datasource/optionlist/Option.html#Option">Option def, Option otherDef) {
92              // Null safe comparison of Comparables. null is assumed to be less than a non-null value.
93              return ComparatorUtils.nullLowComparator(String.CASE_INSENSITIVE_ORDER)
94                      .compare(StringUtils.defaultIfBlank(def.getLabel(), def.getValue()),
95                              StringUtils.defaultIfBlank(otherDef.getLabel(), otherDef.getValue()));
96          }
97      }
98  
99      private boolean isValueMatching(DataFilter filter, String property) {
100         final String value = String.valueOf(filter.getValue());
101         switch (filter.getFilteringMode()) {
102         case STARTSWITH:
103             return property.startsWith(value);
104         case CONTAINS:
105             return property.contains(value);
106         }
107         return false;
108     }
109 }