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.Folder;
019 import com.liferay.portal.kernel.util.OrderByComparator;
020 import com.liferay.portlet.documentlibrary.model.DLFileEntry;
021 import com.liferay.portlet.documentlibrary.model.DLFileShortcut;
022 import com.liferay.portlet.documentlibrary.model.DLFolder;
023 import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
024
025
029 public class RepositoryModelReadCountComparator<T>
030 extends OrderByComparator<T> {
031
032 public static final String ORDER_BY_ASC = "readCount ASC";
033
034 public static final String ORDER_BY_DESC = "readCount DESC";
035
036 public static final String[] ORDER_BY_FIELDS = {"readCount"};
037
038 public RepositoryModelReadCountComparator() {
039 this(false);
040 }
041
042 public RepositoryModelReadCountComparator(boolean ascending) {
043 _ascending = ascending;
044 }
045
046 @Override
047 public int compare(T t1, T t2) {
048 Long readCount1 = getReadCount(t1);
049 Long readCount2 = getReadCount(t2);
050
051 int value = readCount1.compareTo(readCount2);
052
053 if (_ascending) {
054 return value;
055 }
056 else {
057 return -value;
058 }
059 }
060
061 @Override
062 public String getOrderBy() {
063 if (_ascending) {
064 return ORDER_BY_ASC;
065 }
066 else {
067 return ORDER_BY_DESC;
068 }
069 }
070
071 @Override
072 public String[] getOrderByFields() {
073 return ORDER_BY_FIELDS;
074 }
075
076 @Override
077 public boolean isAscending() {
078 return _ascending;
079 }
080
081 protected long getReadCount(Object obj) {
082 if (obj instanceof DLFileEntry) {
083 DLFileEntry dlFileEntry = (DLFileEntry)obj;
084
085 return dlFileEntry.getReadCount();
086 }
087 else if (obj instanceof DLFileShortcut) {
088 DLFileShortcut dlFileShortcut = (DLFileShortcut)obj;
089
090 long toFileEntryId = dlFileShortcut.getToFileEntryId();
091
092 try {
093 DLFileEntry dlFileEntry =
094 DLFileEntryLocalServiceUtil.getFileEntry(toFileEntryId);
095
096 return dlFileEntry.getReadCount();
097 }
098 catch (Exception e) {
099 return 0;
100 }
101 }
102 else if ((obj instanceof DLFolder) || (obj instanceof Folder)) {
103 return 0;
104 }
105 else {
106 FileEntry fileEntry = (FileEntry)obj;
107
108 return fileEntry.getReadCount();
109 }
110 }
111
112 private final boolean _ascending;
113
114 }