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 Alexander Chow
028     */
029    public class RepositoryModelSizeComparator<T> extends OrderByComparator<T> {
030    
031            public static final String ORDER_BY_ASC = "size_ ASC";
032    
033            public static final String ORDER_BY_DESC = "size_ DESC";
034    
035            public static final String[] ORDER_BY_FIELDS = {"size"};
036    
037            public RepositoryModelSizeComparator() {
038                    this(false);
039            }
040    
041            public RepositoryModelSizeComparator(boolean ascending) {
042                    _ascending = ascending;
043            }
044    
045            @Override
046            public int compare(T t1, T t2) {
047                    Long size1 = getSize(t1);
048                    Long size2 = getSize(t2);
049    
050                    int value = size1.compareTo(size2);
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 getFileShortcutSize(Object obj) {
081                    long toFileEntryId = 0;
082    
083                    if (obj instanceof FileShortcut) {
084                            FileShortcut fileShortcut = (FileShortcut)obj;
085    
086                            toFileEntryId = fileShortcut.getToFileEntryId();
087                    }
088                    else {
089                            DLFileShortcut dlFileShortcut = (DLFileShortcut)obj;
090    
091                            toFileEntryId = dlFileShortcut.getToFileEntryId();
092                    }
093    
094                    try {
095                            DLFileEntry dlFileEntry = DLFileEntryLocalServiceUtil.getFileEntry(
096                                    toFileEntryId);
097    
098                            return dlFileEntry.getSize();
099                    }
100                    catch (Exception e) {
101                            return 0;
102                    }
103            }
104    
105            protected long getSize(Object obj) {
106                    if (obj instanceof DLFileEntry) {
107                            DLFileEntry dlFileEntry = (DLFileEntry)obj;
108    
109                            return dlFileEntry.getSize();
110                    }
111                    else if ((obj instanceof DLFileShortcut) ||
112                                     (obj instanceof FileShortcut)) {
113    
114                            return getFileShortcutSize(obj);
115                    }
116                    else if ((obj instanceof DLFolder) || (obj instanceof Folder)) {
117                            return 0;
118                    }
119                    else {
120                            FileEntry fileEntry = (FileEntry)obj;
121    
122                            return fileEntry.getSize();
123                    }
124            }
125    
126            private final boolean _ascending;
127    
128    }