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