View Javadoc

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