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 {
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(Object obj1, Object obj2) {
047 BlogsEntry entry1 = (BlogsEntry)obj1;
048 BlogsEntry entry2 = (BlogsEntry)obj2;
049
050 int value = DateUtil.compareTo(
051 entry1.getDisplayDate(), entry2.getDisplayDate());
052
053 if (value == 0) {
054 if (entry1.getEntryId() < entry2.getEntryId()) {
055 value = -1;
056 }
057 else if (entry1.getEntryId() > entry2.getEntryId()) {
058 value = 1;
059 }
060 }
061
062 if (_ascending) {
063 return value;
064 }
065 else {
066 return -value;
067 }
068 }
069
070 @Override
071 public String getOrderBy() {
072 if (_ascending) {
073 return ORDER_BY_ASC;
074 }
075 else {
076 return ORDER_BY_DESC;
077 }
078 }
079
080 @Override
081 public String[] getOrderByConditionFields() {
082 return ORDER_BY_CONDITION_FIELDS;
083 }
084
085 @Override
086 public String[] getOrderByFields() {
087 return ORDER_BY_FIELDS;
088 }
089
090 @Override
091 public boolean isAscending() {
092 return _ascending;
093 }
094
095 private boolean _ascending;
096
097 }