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