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    
025    /**
026     * @author Brian Wing Shun Chan
027     * @author Alexander Chow
028     */
029    public class RepositoryModelTitleComparator<T> extends OrderByComparator<T> {
030    
031            public static final String ORDER_BY_ASC = "title ASC";
032    
033            public static final String ORDER_BY_DESC = "title DESC";
034    
035            public static final String[] ORDER_BY_FIELDS = {"title"};
036    
037            public RepositoryModelTitleComparator() {
038                    this(false);
039            }
040    
041            public RepositoryModelTitleComparator(boolean ascending) {
042                    _ascending = ascending;
043            }
044    
045            @Override
046            public int compare(T t1, T t2) {
047                    String name1 = getName(t1);
048                    String name2 = getName(t2);
049    
050                    int value = name1.compareToIgnoreCase(name2);
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 String getName(Object obj) {
081                    if (obj instanceof DLFileEntry) {
082                            DLFileEntry dlFileEntry = (DLFileEntry)obj;
083    
084                            return dlFileEntry.getTitle();
085                    }
086                    else if (obj instanceof DLFileShortcut) {
087                            DLFileShortcut dlFileShortcut = (DLFileShortcut)obj;
088    
089                            return dlFileShortcut.getToTitle();
090                    }
091                    else if (obj instanceof DLFolder) {
092                            DLFolder dlFolder = (DLFolder)obj;
093    
094                            return dlFolder.getName();
095                    }
096                    else if (obj instanceof FileEntry) {
097                            FileEntry fileEntry = (FileEntry)obj;
098    
099                            return fileEntry.getTitle();
100                    }
101                    else if (obj instanceof FileShortcut) {
102                            FileShortcut fileShortcut = (FileShortcut)obj;
103    
104                            return fileShortcut.getToTitle();
105                    }
106                    else {
107                            Folder folder = (Folder)obj;
108    
109                            return folder.getName();
110                    }
111            }
112    
113            private final boolean _ascending;
114    
115    }