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="ArticleReviewDateComparator.java.html"><b><i>View Source</i></b></a>
23   *
24   * @author Brian Wing Shun Chan
25   */
26  public class ArticleReviewDateComparator extends OrderByComparator {
27  
28      public static String ORDER_BY_ASC = "reviewDate ASC, version ASC";
29  
30      public static String ORDER_BY_DESC = "reviewDate DESC, version DESC";
31  
32      public static String[] ORDER_BY_FIELDS = {"reviewDate", "version"};
33  
34      public ArticleReviewDateComparator() {
35          this(false);
36      }
37  
38      public ArticleReviewDateComparator(boolean asc) {
39          _asc = asc;
40      }
41  
42      public int compare(Object obj1, Object obj2) {
43          JournalArticle article1 = (JournalArticle)obj1;
44          JournalArticle article2 = (JournalArticle)obj2;
45  
46          int value = DateUtil.compareTo(
47              article1.getReviewDate(), article2.getReviewDate());
48  
49          if (value == 0) {
50              if (article1.getVersion() < article2.getVersion()) {
51                  value = -1;
52              }
53              else if (article1.getVersion() > article2.getVersion()) {
54                  value = 1;
55              }
56          }
57  
58          if (_asc) {
59              return value;
60          }
61          else {
62              return -value;
63          }
64      }
65  
66      public String getOrderBy() {
67          if (_asc) {
68              return ORDER_BY_ASC;
69          }
70          else {
71              return ORDER_BY_DESC;
72          }
73      }
74  
75      public String[] getOrderByFields() {
76          return ORDER_BY_FIELDS;
77      }
78  
79      public boolean isAscending() {
80          return _asc;
81      }
82  
83      private boolean _asc;
84  
85  }