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.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    /**
026     * @author Brian Wing Shun Chan
027     * @author Alexander Chow
028     */
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    }