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