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