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 @Override
045 public int compare(Object obj1, Object obj2) {
046 MBThread thread1 = (MBThread)obj1;
047 MBThread thread2 = (MBThread)obj2;
048
049 Date lastPostDate1 = thread1.getLastPostDate();
050 Date lastPostDate2 = thread2.getLastPostDate();
051
052 boolean ignoreMilliseconds = false;
053
054 DB db = DBFactoryUtil.getDB();
055
056 if (!db.isSupportsDateMilliseconds()) {
057 ignoreMilliseconds = true;
058 }
059
060 int value = DateUtil.compareTo(
061 lastPostDate1, lastPostDate2, ignoreMilliseconds);
062
063 if (value == 0) {
064 if (thread1.getThreadId() < thread2.getThreadId()) {
065 value = -1;
066 }
067 else if (thread1.getThreadId() > thread2.getThreadId()) {
068 value = 1;
069 }
070 }
071
072 if (_ascending) {
073 return value;
074 }
075 else {
076 return -value;
077 }
078 }
079
080 @Override
081 public String getOrderBy() {
082 if (_ascending) {
083 return ORDER_BY_ASC;
084 }
085 else {
086 return ORDER_BY_DESC;
087 }
088 }
089
090 @Override
091 public String[] getOrderByFields() {
092 return ORDER_BY_FIELDS;
093 }
094
095 @Override
096 public boolean isAscending() {
097 return _ascending;
098 }
099
100 private boolean _ascending;
101
102 }