001
014
015 package com.liferay.portlet.documentlibrary.util.comparator;
016
017 import com.liferay.portal.kernel.repository.model.FileEntry;
018 import com.liferay.portal.kernel.repository.model.FileShortcut;
019 import com.liferay.portal.kernel.repository.model.Folder;
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 import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
025
026
030 public class RepositoryModelReadCountComparator<T>
031 extends OrderByComparator<T> {
032
033 public static final String ORDER_BY_ASC = "readCount ASC";
034
035 public static final String ORDER_BY_DESC = "readCount DESC";
036
037 public static final String[] ORDER_BY_FIELDS = {"readCount"};
038
039 public static final String ORDER_BY_MODEL_ASC =
040 "modelFolder DESC, readCount ASC";
041
042 public static final String ORDER_BY_MODEL_DESC =
043 "modelFolder DESC, readCount DESC";
044
045 public RepositoryModelReadCountComparator() {
046 this(false);
047 }
048
049 public RepositoryModelReadCountComparator(boolean ascending) {
050 _ascending = ascending;
051 _orderByModel = false;
052 }
053
054 public RepositoryModelReadCountComparator(
055 boolean ascending, boolean orderByModel) {
056
057 _ascending = ascending;
058 _orderByModel = orderByModel;
059 }
060
061 @Override
062 public int compare(T t1, T t2) {
063 int value = 0;
064
065 Long readCount1 = getReadCount(t1);
066 Long readCount2 = getReadCount(t2);
067
068 if (_orderByModel) {
069 if (((t1 instanceof DLFolder) || (t1 instanceof Folder)) &&
070 ((t2 instanceof DLFolder) || (t2 instanceof Folder))) {
071
072 value = readCount1.compareTo(readCount2);
073 }
074 else if ((t1 instanceof DLFolder) || (t1 instanceof Folder)) {
075 value = -1;
076 }
077 else if ((t2 instanceof DLFolder) || (t2 instanceof Folder)) {
078 value = 1;
079 }
080 else {
081 value = readCount1.compareTo(readCount2);
082 }
083 }
084 else {
085 value = readCount1.compareTo(readCount2);
086 }
087
088 if (_ascending) {
089 return value;
090 }
091 else {
092 return -value;
093 }
094 }
095
096 @Override
097 public String getOrderBy() {
098 if (_orderByModel) {
099 if (_ascending) {
100 return ORDER_BY_MODEL_ASC;
101 }
102 else {
103 return ORDER_BY_MODEL_DESC;
104 }
105 }
106 else {
107 if (_ascending) {
108 return ORDER_BY_ASC;
109 }
110 else {
111 return ORDER_BY_DESC;
112 }
113 }
114 }
115
116 @Override
117 public String[] getOrderByFields() {
118 return ORDER_BY_FIELDS;
119 }
120
121 @Override
122 public boolean isAscending() {
123 return _ascending;
124 }
125
126 protected long getFileShortcutReadCount(Object obj) {
127 long toFileEntryId = 0;
128
129 if (obj instanceof FileShortcut) {
130 FileShortcut fileShortcut = (FileShortcut)obj;
131
132 toFileEntryId = fileShortcut.getToFileEntryId();
133 }
134 else {
135 DLFileShortcut fileShortcut = (DLFileShortcut)obj;
136
137 toFileEntryId = fileShortcut.getToFileEntryId();
138 }
139
140 try {
141 DLFileEntry dlFileEntry = DLFileEntryLocalServiceUtil.getFileEntry(
142 toFileEntryId);
143
144 return dlFileEntry.getReadCount();
145 }
146 catch (Exception e) {
147 return 0;
148 }
149 }
150
151 protected long getReadCount(Object obj) {
152 if (obj instanceof DLFileEntry) {
153 DLFileEntry dlFileEntry = (DLFileEntry)obj;
154
155 return dlFileEntry.getReadCount();
156 }
157 else if ((obj instanceof DLFileShortcut) ||
158 (obj instanceof FileShortcut)) {
159
160 return getFileShortcutReadCount(obj);
161 }
162 else if ((obj instanceof DLFolder) || (obj instanceof Folder)) {
163 return 0;
164 }
165 else {
166 FileEntry fileEntry = (FileEntry)obj;
167
168 return fileEntry.getReadCount();
169 }
170 }
171
172 private final boolean _ascending;
173 private final boolean _orderByModel;
174
175 }