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.shopping.util.comparator;
016    
017    import com.liferay.portal.kernel.util.OrderByComparator;
018    import com.liferay.portlet.shopping.model.ShoppingItem;
019    
020    /**
021     * @author Brian Wing Shun Chan
022     */
023    public class ItemNameComparator extends OrderByComparator {
024    
025            public static String ORDER_BY_ASC = "categoryId ASC, name ASC";
026    
027            public static String ORDER_BY_DESC = "categoryId DESC, name DESC";
028    
029            public static String[] ORDER_BY_FIELDS = {"categoryId", "name"};
030    
031            public ItemNameComparator() {
032                    this(false);
033            }
034    
035            public ItemNameComparator(boolean ascending) {
036                    _ascending = ascending;
037            }
038    
039            public int compare(Object obj1, Object obj2) {
040                    ShoppingItem item1 = (ShoppingItem)obj1;
041                    ShoppingItem item2 = (ShoppingItem)obj2;
042    
043                    Long categoryId1 = new Long(item1.getCategoryId());
044                    Long categoryId2 = new Long(item2.getCategoryId());
045    
046                    int value = categoryId1.compareTo(categoryId2);
047    
048                    if (value == 0) {
049                            value = item1.getName().toLowerCase().compareTo(
050                                    item2.getName().toLowerCase());
051                    }
052    
053                    if (_ascending) {
054                            return value;
055                    }
056                    else {
057                            return -value;
058                    }
059            }
060    
061            public String getOrderBy() {
062                    if (_ascending) {
063                            return ORDER_BY_ASC;
064                    }
065                    else {
066                            return ORDER_BY_DESC;
067                    }
068            }
069    
070            public String[] getOrderByFields() {
071                    return ORDER_BY_FIELDS;
072            }
073    
074            public boolean isAscending() {
075                    return _ascending;
076            }
077    
078            private boolean _ascending;
079    
080    }