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 static final String ORDER_BY_MODEL_ASC =
038                    "modelFolder DESC, size_ ASC";
039    
040            public static final String ORDER_BY_MODEL_DESC =
041                    "modelFolder DESC, size_ DESC";
042    
043            public RepositoryModelSizeComparator() {
044                    this(false);
045            }
046    
047            public RepositoryModelSizeComparator(boolean ascending) {
048                    _ascending = ascending;
049                    _orderByModel = false;
050            }
051    
052            public RepositoryModelSizeComparator(
053                    boolean ascending, boolean orderByModel) {
054    
055                    _ascending = ascending;
056                    _orderByModel = orderByModel;
057            }
058    
059            @Override
060            public int compare(T t1, T t2) {
061                    int value = 0;
062    
063                    Long size1 = getSize(t1);
064                    Long size2 = getSize(t2);
065    
066                    if (_orderByModel) {
067                            if (((t1 instanceof DLFolder) || (t1 instanceof Folder)) &&
068                                    ((t2 instanceof DLFolder) || (t2 instanceof Folder))) {
069    
070                                    value = size1.compareTo(size2);
071                            }
072                            else if ((t1 instanceof DLFolder) || (t1 instanceof Folder)) {
073                                    value = -1;
074                            }
075                            else if ((t2 instanceof DLFolder) || (t2 instanceof Folder)) {
076                                    value = 1;
077                            }
078                            else {
079                                    value = size1.compareTo(size2);
080                            }
081                    }
082                    else {
083                            value = size1.compareTo(size2);
084                    }
085    
086                    if (_ascending) {
087                            return value;
088                    }
089                    else {
090                            return -value;
091                    }
092            }
093    
094            @Override
095            public String getOrderBy() {
096                    if (_orderByModel) {
097                            if (_ascending) {
098                                    return ORDER_BY_MODEL_ASC;
099                            }
100                            else {
101                                    return ORDER_BY_MODEL_DESC;
102                            }
103                    }
104                    else {
105                            if (_ascending) {
106                                    return ORDER_BY_ASC;
107                            }
108                            else {
109                                    return ORDER_BY_DESC;
110                            }
111                    }
112            }
113    
114            @Override
115            public String[] getOrderByFields() {
116                    return ORDER_BY_FIELDS;
117            }
118    
119            @Override
120            public boolean isAscending() {
121                    return _ascending;
122            }
123    
124            protected long getFileShortcutSize(Object obj) {
125                    long toFileEntryId = 0;
126    
127                    if (obj instanceof FileShortcut) {
128                            FileShortcut fileShortcut = (FileShortcut)obj;
129    
130                            toFileEntryId = fileShortcut.getToFileEntryId();
131                    }
132                    else {
133                            DLFileShortcut dlFileShortcut = (DLFileShortcut)obj;
134    
135                            toFileEntryId = dlFileShortcut.getToFileEntryId();
136                    }
137    
138                    try {
139                            DLFileEntry dlFileEntry = DLFileEntryLocalServiceUtil.getFileEntry(
140                                    toFileEntryId);
141    
142                            return dlFileEntry.getSize();
143                    }
144                    catch (Exception e) {
145                            return 0;
146                    }
147            }
148    
149            protected long getSize(Object obj) {
150                    if (obj instanceof DLFileEntry) {
151                            DLFileEntry dlFileEntry = (DLFileEntry)obj;
152    
153                            return dlFileEntry.getSize();
154                    }
155                    else if ((obj instanceof DLFileShortcut) ||
156                                     (obj instanceof FileShortcut)) {
157    
158                            return getFileShortcutSize(obj);
159                    }
160                    else if ((obj instanceof DLFolder) || (obj instanceof Folder)) {
161                            return 0;
162                    }
163                    else {
164                            FileEntry fileEntry = (FileEntry)obj;
165    
166                            return fileEntry.getSize();
167                    }
168            }
169    
170            private final boolean _ascending;
171            private final boolean _orderByModel;
172    
173    }