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.messageboards.util.comparator;
16  
17  import com.liferay.portal.kernel.dao.db.DB;
18  import com.liferay.portal.kernel.dao.db.DBFactoryUtil;
19  import com.liferay.portal.kernel.util.DateUtil;
20  import com.liferay.portal.kernel.util.OrderByComparator;
21  import com.liferay.portlet.messageboards.model.MBThread;
22  
23  import java.util.Date;
24  
25  /**
26   * <a href="ThreadLastPostDateComparator.java.html"><b><i>View Source</i></b>
27   * </a>
28   *
29   * @author Brian Wing Shun Chan
30   */
31  public class ThreadLastPostDateComparator extends OrderByComparator {
32  
33      public static String ORDER_BY_ASC = "lastPostDate ASC, threadId ASC";
34  
35      public static String ORDER_BY_DESC = "lastPostDate DESC, threadId DESC";
36  
37      public static String[] ORDER_BY_FIELDS = {"lastPostDate", "threadId"};
38  
39      public ThreadLastPostDateComparator() {
40          this(false);
41      }
42  
43      public ThreadLastPostDateComparator(boolean asc) {
44          _asc = asc;
45      }
46  
47      public int compare(Object obj1, Object obj2) {
48          MBThread thread1 = (MBThread)obj1;
49          MBThread thread2 = (MBThread)obj2;
50  
51          Date lastPostDate1 = thread1.getLastPostDate();
52          Date lastPostDate2 = thread2.getLastPostDate();
53  
54          boolean ignoreMilliseconds = false;
55  
56          DB db = DBFactoryUtil.getDB();
57  
58          if (!db.isSupportsDateMilliseconds()) {
59              ignoreMilliseconds = true;
60          }
61  
62          int value = DateUtil.compareTo(
63              lastPostDate1, lastPostDate2, ignoreMilliseconds);
64  
65          if (value == 0) {
66              if (thread1.getThreadId() < thread2.getThreadId()) {
67                  value = -1;
68              }
69              else if (thread1.getThreadId() > thread2.getThreadId()) {
70                  value = 1;
71              }
72          }
73  
74          if (_asc) {
75              return value;
76          }
77          else {
78              return -value;
79          }
80      }
81  
82      public String getOrderBy() {
83          if (_asc) {
84              return ORDER_BY_ASC;
85          }
86          else {
87              return ORDER_BY_DESC;
88          }
89      }
90  
91      public String[] getOrderByFields() {
92          return ORDER_BY_FIELDS;
93      }
94  
95      public boolean isAscending() {
96          return _asc;
97      }
98  
99      private boolean _asc;
100 
101 }