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<MBThread> {
029
030 public static final String ORDER_BY_ASC =
031 "MBThread.lastPostDate ASC, MBThread.threadId ASC";
032
033 public static final String[] ORDER_BY_CONDITION_FIELDS = {"lastPostDate"};
034
035 public static final String ORDER_BY_DESC =
036 "MBThread.lastPostDate DESC, MBThread.threadId DESC";
037
038 public static final String[] ORDER_BY_FIELDS = {"lastPostDate", "threadId"};
039
040 public ThreadLastPostDateComparator() {
041 this(false);
042 }
043
044 public ThreadLastPostDateComparator(boolean ascending) {
045 _ascending = ascending;
046 }
047
048 @Override
049 public int compare(MBThread thread1, MBThread thread2) {
050 Date lastPostDate1 = thread1.getLastPostDate();
051 Date lastPostDate2 = thread2.getLastPostDate();
052
053 boolean ignoreMilliseconds = false;
054
055 DB db = DBFactoryUtil.getDB();
056
057 if (!db.isSupportsDateMilliseconds()) {
058 ignoreMilliseconds = true;
059 }
060
061 int value = DateUtil.compareTo(
062 lastPostDate1, lastPostDate2, ignoreMilliseconds);
063
064 if (value == 0) {
065 if (thread1.getThreadId() < thread2.getThreadId()) {
066 value = -1;
067 }
068 else if (thread1.getThreadId() > thread2.getThreadId()) {
069 value = 1;
070 }
071 }
072
073 if (_ascending) {
074 return value;
075 }
076 else {
077 return -value;
078 }
079 }
080
081 @Override
082 public String getOrderBy() {
083 if (_ascending) {
084 return ORDER_BY_ASC;
085 }
086 else {
087 return ORDER_BY_DESC;
088 }
089 }
090
091 @Override
092 public String[] getOrderByConditionFields() {
093 return ORDER_BY_CONDITION_FIELDS;
094 }
095
096 @Override
097 public String[] getOrderByFields() {
098 return ORDER_BY_FIELDS;
099 }
100
101 @Override
102 public boolean isAscending() {
103 return _ascending;
104 }
105
106 private final boolean _ascending;
107
108 }