001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portlet.wiki.util.comparator;
016    
017    import com.liferay.portal.kernel.util.OrderByComparator;
018    import com.liferay.portlet.wiki.model.WikiPage;
019    
020    /**
021     * @author Samuel Liu
022     */
023    public class PageTitleComparator extends OrderByComparator {
024    
025            public static String ORDER_BY_ASC = "title ASC";
026    
027            public static String ORDER_BY_DESC = "title DESC";
028    
029            public static String[] ORDER_BY_FIELDS = {"title"};
030    
031            public PageTitleComparator() {
032                    this(false);
033            }
034    
035            public PageTitleComparator(boolean ascending) {
036                    _ascending = ascending;
037            }
038    
039            public int compare(Object obj1, Object obj2) {
040                    WikiPage page1 = (WikiPage)obj1;
041                    WikiPage page2 = (WikiPage)obj2;
042    
043                    String title1 = new String(page1.getTitle());
044                    String title2 = new String(page2.getTitle());
045    
046                    int value = title1.compareTo(title2);
047    
048                    if (_ascending) {
049                            return value;
050                    }
051                    else {
052                            return -value;
053                    }
054            }
055    
056            public String getOrderBy() {
057                    if (_ascending) {
058                            return ORDER_BY_ASC;
059                    }
060                    else {
061                            return ORDER_BY_DESC;
062                    }
063            }
064    
065            public String[] getOrderByFields() {
066                    return ORDER_BY_FIELDS;
067            }
068    
069            public boolean isAscending() {
070                    return _ascending;
071            }
072    
073            private boolean _ascending;
074    
075    }