View Javadoc
1   /**
2    * This file Copyright (c) 2008-2015 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.module.templatingkit.util;
35  
36  
37  import info.magnolia.jcr.util.NodeTypes;
38  import info.magnolia.jcr.util.PropertyUtil;
39  
40  import java.util.Calendar;
41  import java.util.Collections;
42  import java.util.Comparator;
43  import java.util.List;
44  
45  import javax.jcr.Node;
46  import javax.jcr.RepositoryException;
47  
48  import org.apache.commons.collections.CollectionUtils;
49  import org.apache.commons.collections.Predicate;
50  import org.apache.commons.lang3.StringUtils;
51  import org.slf4j.Logger;
52  import org.slf4j.LoggerFactory;
53  
54  /**
55   * Date Utility. Allows to sort content lists based to their node 'date'.
56   */
57  public class STKDateContentUtil {
58  
59      public static final Object INSTANCE = new STKDateContentUtil();
60  
61      public static final String ASCENDING = "ascending";
62      public static final String DESCENDING = "descending";
63      private static final String DEFAULT_DATE_PROPERTY_NAME = "date";
64  
65      private static final Logger log = LoggerFactory.getLogger(STKDateContentUtil.class);
66  
67      public static void filterDateContentList(List<Node> itemsList, final Calendar minDate, final Calendar maxDate) {
68          filterDateContentList(itemsList, minDate, maxDate, DEFAULT_DATE_PROPERTY_NAME);
69      }
70  
71      public static void filterDateContentList(List<Node> itemsList, final Calendar minDate, final Calendar maxDate, final String datePropertyName) {
72          filterDateContentList(itemsList, minDate, maxDate, datePropertyName, false);
73      }
74  
75      /**
76       * Filters out items (nodes) which do not meet this condition: minDate <= itemDate && itemDate < maxDate.
77       *
78       * @param itemsList List of nodes.
79       * @param minDate Minimal date of the interval.
80       * @param maxDate Maximal date of the interval.
81       * @param datePropertyName Name of property in which is date stored.
82       * @param useCreationDate If true, then will look to MetaData for creation date and if exists, will use it as a date if no date found in property datePropertyName.
83       */
84      public static void filterDateContentList(List<Node> itemsList, final Calendar minDate, final Calendar maxDate, final String datePropertyName, final boolean useCreationDate) {
85          CollectionUtils.filter(itemsList, new Predicate() {
86              @Override
87              public boolean evaluate(Object object) {
88                  Node node = (Node) object;
89                  Calendar date = resolveDate(node, datePropertyName, useCreationDate, Calendar.getInstance());
90                  // INSMR-447: Increase milliseconds by one. Otherwise event will not be shown if date==minDate.
91                  if (date.getTimeInMillis() == minDate.getTimeInMillis()) {
92                      date.add(Calendar.SECOND, 1);
93                  }
94                  return date.after(minDate) && date.before(maxDate);
95              }
96          });
97      }
98  
99      public static void sortDateContentList(final List<Node> itemsList, final String sortDirection) {
100         sortDateContentList(itemsList, sortDirection, DEFAULT_DATE_PROPERTY_NAME);
101     }
102 
103     public static void sortDateContentList(final List<Node> itemsList, final String sortDirection, final String datePropertyName) {
104         Calendar veryOld = Calendar.getInstance();
105         veryOld.setTimeInMillis(0);
106         sortDateContentList(itemsList, sortDirection, datePropertyName, veryOld);
107     }
108 
109     public static void sortDateContentList(final List<Node> itemsList, final String sortDirection, final String datePropertyName, final Calendar defaultDate) {
110         sortDateContentList(itemsList, sortDirection, datePropertyName, false, defaultDate);
111     }
112 
113     /**
114      * Sorts list of nodes by date.
115      *
116      * @param itemsList List of nodes.
117      * @param sortDirection Sort direction (asc, desc).
118      * @param datePropertyName Name of property in which is date stored.
119      * @param useCreationDate If true, then will look to MetaData for creation date and if exists, will use it as a date if no date found in property datePropertyName.
120      * @param defaultDate Default date used if no date in node found.
121      */
122     public static void sortDateContentList(final List<Node> itemsList, final String sortDirection, final String datePropertyName, final boolean useCreationDate, final Calendar defaultDate) {
123         Collections.sort(itemsList, new Comparator<Node>() {
124             @Override
125             public int compare(Node c1, Node c2) {
126                 Calendar date1 = resolveDate(c1, datePropertyName, useCreationDate, defaultDate);
127                 Calendar date2 = resolveDate(c2, datePropertyName, useCreationDate, defaultDate);
128                 if (StringUtils.equals(sortDirection, ASCENDING)) {
129                     return date2.compareTo(date1);
130                 }
131                 return date1.compareTo(date2);
132             }
133         });
134     }
135 
136     private static Calendar resolveDate(Node node, String name, boolean useCreationDate, Calendar defaultValue) {
137         Calendar date = PropertyUtil.getDate(node, name);
138         if (date == null) {
139             if (useCreationDate) {
140                 try {
141                     date = NodeTypes.Created.getCreated(node);
142                     if (date != null) {
143                         PropertyUtil.setProperty(node, name, date);
144                     }
145                 } catch (RepositoryException e) {
146                     log.warn("Can't set property [" + name + "] for node [" + node + "]");
147                 }
148             }
149         }
150         return date != null ? date : defaultValue;
151     }
152 
153 }