001
014
015 package com.liferay.portlet.messageboards.util.comparator;
016
017 import com.liferay.portal.kernel.dao.db.DB;
018 import com.liferay.portal.kernel.dao.db.DBFactoryUtil;
019 import com.liferay.portal.kernel.util.DateUtil;
020 import com.liferay.portal.kernel.util.OrderByComparator;
021 import com.liferay.portlet.messageboards.model.MBThread;
022
023 import java.util.Date;
024
025
028 public class ThreadLastPostDateComparator extends OrderByComparator {
029
030 public static String ORDER_BY_ASC = "lastPostDate ASC, threadId ASC";
031
032 public static String ORDER_BY_DESC = "lastPostDate DESC, threadId DESC";
033
034 public static String[] ORDER_BY_FIELDS = {"lastPostDate", "threadId"};
035
036 public ThreadLastPostDateComparator() {
037 this(false);
038 }
039
040 public ThreadLastPostDateComparator(boolean ascending) {
041 _ascending = ascending;
042 }
043
044 public int compare(Object obj1, Object obj2) {
045 MBThread thread1 = (MBThread)obj1;
046 MBThread thread2 = (MBThread)obj2;
047
048 Date lastPostDate1 = thread1.getLastPostDate();
049 Date lastPostDate2 = thread2.getLastPostDate();
050
051 boolean ignoreMilliseconds = false;
052
053 DB db = DBFactoryUtil.getDB();
054
055 if (!db.isSupportsDateMilliseconds()) {
056 ignoreMilliseconds = true;
057 }
058
059 int value = DateUtil.compareTo(
060 lastPostDate1, lastPostDate2, ignoreMilliseconds);
061
062 if (value == 0) {
063 if (thread1.getThreadId() < thread2.getThreadId()) {
064 value = -1;
065 }
066 else if (thread1.getThreadId() > thread2.getThreadId()) {
067 value = 1;
068 }
069 }
070
071 if (_ascending) {
072 return value;
073 }
074 else {
075 return -value;
076 }
077 }
078
079 public String getOrderBy() {
080 if (_ascending) {
081 return ORDER_BY_ASC;
082 }
083 else {
084 return ORDER_BY_DESC;
085 }
086 }
087
088 public String[] getOrderByFields() {
089 return ORDER_BY_FIELDS;
090 }
091
092 public boolean isAscending() {
093 return _ascending;
094 }
095
096 private boolean _ascending;
097
098 }