1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
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  }