001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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    /**
027     * @author Brian Wing Shun Chan
028     * @author Alexander Chow
029     */
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 RepositoryModelReadCountComparator() {
040                    this(false);
041            }
042    
043            public RepositoryModelReadCountComparator(boolean ascending) {
044                    _ascending = ascending;
045            }
046    
047            @Override
048            public int compare(T t1, T t2) {
049                    Long readCount1 = getReadCount(t1);
050                    Long readCount2 = getReadCount(t2);
051    
052                    int value = readCount1.compareTo(readCount2);
053    
054                    if (_ascending) {
055                            return value;
056                    }
057                    else {
058                            return -value;
059                    }
060            }
061    
062            @Override
063            public String getOrderBy() {
064                    if (_ascending) {
065                            return ORDER_BY_ASC;
066                    }
067                    else {
068                            return ORDER_BY_DESC;
069                    }
070            }
071    
072            @Override
073            public String[] getOrderByFields() {
074                    return ORDER_BY_FIELDS;
075            }
076    
077            @Override
078            public boolean isAscending() {
079                    return _ascending;
080            }
081    
082            protected long getFileShortcutReadCount(Object obj) {
083                    long toFileEntryId = 0;
084    
085                    if (obj instanceof FileShortcut) {
086                            FileShortcut fileShortcut = (FileShortcut)obj;
087    
088                            toFileEntryId = fileShortcut.getToFileEntryId();
089                    }
090                    else {
091                            DLFileShortcut fileShortcut = (DLFileShortcut)obj;
092    
093                            toFileEntryId = fileShortcut.getToFileEntryId();
094                    }
095    
096                    try {
097                            DLFileEntry dlFileEntry = DLFileEntryLocalServiceUtil.getFileEntry(
098                                    toFileEntryId);
099    
100                            return dlFileEntry.getReadCount();
101                    }
102                    catch (Exception e) {
103                            return 0;
104                    }
105            }
106    
107            protected long getReadCount(Object obj) {
108                    if (obj instanceof DLFileEntry) {
109                            DLFileEntry dlFileEntry = (DLFileEntry)obj;
110    
111                            return dlFileEntry.getReadCount();
112                    }
113                    else if ((obj instanceof DLFileShortcut) ||
114                                     (obj instanceof FileShortcut)) {
115    
116                            return getFileShortcutReadCount(obj);
117                    }
118                    else if ((obj instanceof DLFolder) || (obj instanceof Folder)) {
119                            return 0;
120                    }
121                    else {
122                            FileEntry fileEntry = (FileEntry)obj;
123    
124                            return fileEntry.getReadCount();
125                    }
126            }
127    
128            private final boolean _ascending;
129    
130    }