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.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 }