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 static final String ORDER_BY_MODEL_ASC =
038                    "modelFolder DESC, title ASC";
039    
040            public static final String ORDER_BY_MODEL_DESC =
041                    "modelFolder DESC, title DESC";
042    
043            public RepositoryModelTitleComparator() {
044                    this(false);
045            }
046    
047            public RepositoryModelTitleComparator(boolean ascending) {
048                    _ascending = ascending;
049                    _orderByModel = false;
050            }
051    
052            public RepositoryModelTitleComparator(
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                    String name1 = getName(t1);
064                    String name2 = getName(t2);
065    
066                    if (_orderByModel) {
067                            if (((t1 instanceof DLFolder) || (t1 instanceof Folder)) &&
068                                    ((t2 instanceof DLFolder) || (t2 instanceof Folder))) {
069    
070                                    name1.compareToIgnoreCase(name2);
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                                    name1.compareToIgnoreCase(name2);
080                            }
081                    }
082                    else {
083                            value = name1.compareToIgnoreCase(name2);
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 String getName(Object obj) {
125                    if (obj instanceof DLFileEntry) {
126                            DLFileEntry dlFileEntry = (DLFileEntry)obj;
127    
128                            return dlFileEntry.getTitle();
129                    }
130                    else if (obj instanceof DLFileShortcut) {
131                            DLFileShortcut dlFileShortcut = (DLFileShortcut)obj;
132    
133                            return dlFileShortcut.getToTitle();
134                    }
135                    else if (obj instanceof DLFolder) {
136                            DLFolder dlFolder = (DLFolder)obj;
137    
138                            return dlFolder.getName();
139                    }
140                    else if (obj instanceof FileEntry) {
141                            FileEntry fileEntry = (FileEntry)obj;
142    
143                            return fileEntry.getTitle();
144                    }
145                    else if (obj instanceof FileShortcut) {
146                            FileShortcut fileShortcut = (FileShortcut)obj;
147    
148                            return fileShortcut.getToTitle();
149                    }
150                    else {
151                            Folder folder = (Folder)obj;
152    
153                            return folder.getName();
154                    }
155            }
156    
157            private final boolean _ascending;
158            private final boolean _orderByModel;
159    
160    }