1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.journal.util.comparator;
16  
17  import com.liferay.portal.kernel.util.DateUtil;
18  import com.liferay.portal.kernel.util.OrderByComparator;
19  import com.liferay.portlet.journal.model.JournalArticle;
20  
21  /**
22   * <a href="ArticleDisplayDateComparator.java.html"><b><i>View Source</i></b>
23   * </a>
24   *
25   * @author Brian Wing Shun Chan
26   */
27  public class ArticleDisplayDateComparator extends OrderByComparator {
28  
29      public static String ORDER_BY_ASC = "displayDate ASC, version ASC";
30  
31      public static String ORDER_BY_DESC = "displayDate DESC, version DESC";
32  
33      public static String[] ORDER_BY_FIELDS = {"displayDate", "version"};
34  
35      public ArticleDisplayDateComparator() {
36          this(false);
37      }
38  
39      public ArticleDisplayDateComparator(boolean asc) {
40          _asc = asc;
41      }
42  
43      public int compare(Object obj1, Object obj2) {
44          JournalArticle article1 = (JournalArticle)obj1;
45          JournalArticle article2 = (JournalArticle)obj2;
46  
47          int value = DateUtil.compareTo(
48              article1.getDisplayDate(), article2.getDisplayDate());
49  
50          if (value == 0) {
51              if (article1.getVersion() < article2.getVersion()) {
52                  value = -1;
53              }
54              else if (article1.getVersion() > article2.getVersion()) {
55                  value = 1;
56              }
57          }
58  
59          if (_asc) {
60              return value;
61          }
62          else {
63              return -value;
64          }
65      }
66  
67      public String getOrderBy() {
68          if (_asc) {
69              return ORDER_BY_ASC;
70          }
71          else {
72              return ORDER_BY_DESC;
73          }
74      }
75  
76      public String[] getOrderByFields() {
77          return ORDER_BY_FIELDS;
78      }
79  
80      public boolean isAscending() {
81          return _asc;
82      }
83  
84      private boolean _asc;
85  
86  }