001
014
015 package com.liferay.portlet.documentlibrary.util.comparator;
016
017 import com.liferay.portal.kernel.repository.model.Folder;
018 import com.liferay.portal.kernel.repository.model.RepositoryEntry;
019 import com.liferay.portal.kernel.util.DateUtil;
020 import com.liferay.portal.kernel.util.OrderByComparator;
021 import com.liferay.portlet.documentlibrary.model.DLFileEntry;
022 import com.liferay.portlet.documentlibrary.model.DLFileShortcut;
023 import com.liferay.portlet.documentlibrary.model.DLFolder;
024
025 import java.util.Date;
026
027
031 public class RepositoryModelModifiedDateComparator<T>
032 extends OrderByComparator<T> {
033
034 public static final String ORDER_BY_ASC = "modifiedDate ASC";
035
036 public static final String ORDER_BY_DESC = "modifiedDate DESC";
037
038 public static final String[] ORDER_BY_FIELDS = {"modifiedDate"};
039
040 public static final String ORDER_BY_MODEL_ASC =
041 "modelFolder DESC, modifiedDate ASC";
042
043 public static final String ORDER_BY_MODEL_DESC =
044 "modelFolder DESC, modifiedDate DESC";
045
046 public RepositoryModelModifiedDateComparator() {
047 this(false);
048 }
049
050 public RepositoryModelModifiedDateComparator(boolean ascending) {
051 _ascending = ascending;
052 _orderByModel = false;
053 }
054
055 public RepositoryModelModifiedDateComparator(
056 boolean ascending, boolean orderByModel) {
057
058 _ascending = ascending;
059 _orderByModel = orderByModel;
060 }
061
062 @Override
063 public int compare(T t1, T t2) {
064 int value = 0;
065
066 Date modifiedDate1 = getModifiedDate(t1);
067 Date modifiedDate2 = getModifiedDate(t2);
068
069 if (_orderByModel) {
070 if (((t1 instanceof DLFolder) || (t1 instanceof Folder)) &&
071 ((t2 instanceof DLFolder) || (t2 instanceof Folder))) {
072
073 value = DateUtil.compareTo(modifiedDate1, modifiedDate2);
074 }
075 else if ((t1 instanceof DLFolder) || (t1 instanceof Folder)) {
076 value = -1;
077 }
078 else if ((t2 instanceof DLFolder) || (t2 instanceof Folder)) {
079 value = 1;
080 }
081 else {
082 value = DateUtil.compareTo(modifiedDate1, modifiedDate2);
083 }
084 }
085 else {
086 value = DateUtil.compareTo(modifiedDate1, modifiedDate2);
087 }
088
089 if (_ascending) {
090 return value;
091 }
092 else {
093 return -value;
094 }
095 }
096
097 @Override
098 public String getOrderBy() {
099 if (_orderByModel) {
100 if (_ascending) {
101 return ORDER_BY_MODEL_ASC;
102 }
103 else {
104 return ORDER_BY_MODEL_DESC;
105 }
106 }
107 else {
108 if (_ascending) {
109 return ORDER_BY_ASC;
110 }
111 else {
112 return ORDER_BY_DESC;
113 }
114 }
115 }
116
117 @Override
118 public String[] getOrderByFields() {
119 return ORDER_BY_FIELDS;
120 }
121
122 @Override
123 public boolean isAscending() {
124 return _ascending;
125 }
126
127 protected Date getModifiedDate(Object obj) {
128 if (obj instanceof DLFileEntry) {
129 DLFileEntry dlFileEntry = (DLFileEntry)obj;
130
131 return dlFileEntry.getModifiedDate();
132 }
133 else if (obj instanceof DLFileShortcut) {
134 DLFileShortcut dlFileShortcut = (DLFileShortcut)obj;
135
136 return dlFileShortcut.getModifiedDate();
137 }
138 else if (obj instanceof DLFolder) {
139 DLFolder dlFolder = (DLFolder)obj;
140
141 return dlFolder.getModifiedDate();
142 }
143 else {
144 RepositoryEntry repositoryEntry = (RepositoryEntry)obj;
145
146 return repositoryEntry.getModifiedDate();
147 }
148 }
149
150 private final boolean _ascending;
151 private final boolean _orderByModel;
152
153 }