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.messageboards.util.comparator;
16  
17  import com.liferay.portal.kernel.util.DateUtil;
18  import com.liferay.portal.kernel.util.OrderByComparator;
19  import com.liferay.portlet.messageboards.model.MBMessage;
20  
21  /**
22   * <a href="MessageCreateDateComparator.java.html"><b><i>View Source</i></b></a>
23   *
24   * @author Brian Wing Shun Chan
25   */
26  public class MessageCreateDateComparator extends OrderByComparator {
27  
28      public static String ORDER_BY_ASC = "createDate ASC";
29  
30      public static String ORDER_BY_DESC = "createDate DESC";
31  
32      public static String[] ORDER_BY_FIELDS = {"createDate"};
33  
34      public MessageCreateDateComparator(boolean asc) {
35          _asc = asc;
36      }
37  
38      public int compare(Object obj1, Object obj2) {
39          MBMessage message1 = (MBMessage)obj1;
40          MBMessage message2 = (MBMessage)obj2;
41  
42          int value = DateUtil.compareTo(
43              message1.getCreateDate(), message2.getCreateDate());
44  
45          if (_asc) {
46              return value;
47          }
48          else {
49              return -value;
50          }
51      }
52  
53      public String getOrderBy() {
54          if (_asc) {
55              return ORDER_BY_ASC;
56          }
57          else {
58              return ORDER_BY_DESC;
59          }
60      }
61  
62      public String[] getOrderByFields() {
63          return ORDER_BY_FIELDS;
64      }
65  
66      public boolean isAscending() {
67          return _asc;
68      }
69  
70      private boolean _asc;
71  
72  }