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 java.util.Calendar;
39  import java.util.Collections;
40  import java.util.Comparator;
41  import java.util.List;
42  
43  import javax.jcr.Node;
44  import javax.jcr.RepositoryException;
45  
46  import org.apache.commons.collections.CollectionUtils;
47  import org.apache.commons.collections.Predicate;
48  import org.apache.commons.lang.StringUtils;
49  
50  /**
51   * Date Utility. Allows to sort content list based to theirs.
52   * node 'date'.
53   * @author cringele
54   * @version $Id$
55   */
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      public static void filterDateContentList(List<Node> itemsList, final Calendar minDate, final Calendar maxDate) {
66          filterDateContentList(itemsList, minDate, maxDate, DEFAULT_DATE_PROPERTY_NAME);
67      }
68  
69      public static void filterDateContentList(List<Node> itemsList, final Calendar minDate, final Calendar maxDate, final String datePropertyName) {
70          CollectionUtils.filter(itemsList, new Predicate(){
71              @Override
72              public boolean evaluate(Object object) {
73                   Node node = (Node) object;
74                   Calendar date = getDate(node, datePropertyName, Calendar.getInstance());
75                   // INSMR-447: Increase milliseconds by one. Otherwise event will not be shown if date==minDate.
76                   if (date.getTimeInMillis() ==  minDate.getTimeInMillis()) {
77                       date.add(Calendar.SECOND, 1);
78                   }
79                   return date.after(minDate) && date.before(maxDate);
80               }
81           });
82      }
83  
84      public static void sortDateContentList(final List<Node> itemsList, final String sortDirection) {
85          sortDateContentList(itemsList, sortDirection, DEFAULT_DATE_PROPERTY_NAME);
86      }
87  
88      public static void sortDateContentList(final List<Node> itemsList, final String sortDirection, final String datePropertyName) {
89          Calendar veryOld = Calendar.getInstance();
90          veryOld.setTimeInMillis(0);
91          sortDateContentList(itemsList, sortDirection, datePropertyName, veryOld);
92      }
93  
94      public static void sortDateContentList(final List<Node> itemsList, final String sortDirection, final String datePropertyName, final Calendar defaultDate) {
95          Collections.sort(itemsList, new Comparator<Node>(){
96              @Override
97              public int compare(Node c1, Node c2) {
98                  Calendar date1 = getDate(c1, datePropertyName, defaultDate);
99                  Calendar date2 = getDate(c2, datePropertyName, defaultDate);
100                 if(StringUtils.equals(sortDirection, ASCENDING)){
101                     return date2.compareTo(date1);
102                 }
103                 return date1.compareTo(date2);
104             }
105         });
106     }
107 
108     //FIXME: Should be put in a helper class
109     private static Calendar getDate(Node node, String name, Calendar defaultValue) {
110         try {
111             if (node.hasProperty(name)) {
112                 return node.getProperty(name).getDate();
113             }
114         } catch (RepositoryException e) {
115         }
116         return defaultValue;
117     }
118 }