001
014
015 package com.liferay.portlet.blogs.util.comparator;
016
017 import com.liferay.portal.kernel.util.DateUtil;
018 import com.liferay.portal.kernel.util.OrderByComparator;
019 import com.liferay.portlet.blogs.model.BlogsEntry;
020
021
024 public class EntryDisplayDateComparator extends OrderByComparator<BlogsEntry> {
025
026 public static final String ORDER_BY_ASC =
027 "BlogsEntry.displayDate ASC, BlogsEntry.entryId ASC";
028
029 public static final String[] ORDER_BY_CONDITION_FIELDS =
030 {"displayDate", "entryId"};
031
032 public static final String ORDER_BY_DESC =
033 "BlogsEntry.displayDate DESC, BlogsEntry.entryId DESC";
034
035 public static final String[] ORDER_BY_FIELDS = {"displayDate", "entryId"};
036
037 public EntryDisplayDateComparator() {
038 this(false);
039 }
040
041 public EntryDisplayDateComparator(boolean ascending) {
042 _ascending = ascending;
043 }
044
045 @Override
046 public int compare(BlogsEntry entry1, BlogsEntry entry2) {
047 int value = DateUtil.compareTo(
048 entry1.getDisplayDate(), entry2.getDisplayDate());
049
050 if (value == 0) {
051 if (entry1.getEntryId() < entry2.getEntryId()) {
052 value = -1;
053 }
054 else if (entry1.getEntryId() > entry2.getEntryId()) {
055 value = 1;
056 }
057 }
058
059 if (_ascending) {
060 return value;
061 }
062 else {
063 return -value;
064 }
065 }
066
067 @Override
068 public String getOrderBy() {
069 if (_ascending) {
070 return ORDER_BY_ASC;
071 }
072 else {
073 return ORDER_BY_DESC;
074 }
075 }
076
077 @Override
078 public String[] getOrderByConditionFields() {
079 return ORDER_BY_CONDITION_FIELDS;
080 }
081
082 @Override
083 public String[] getOrderByFields() {
084 return ORDER_BY_FIELDS;
085 }
086
087 @Override
088 public boolean isAscending() {
089 return _ascending;
090 }
091
092 private final boolean _ascending;
093
094 }