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 ItemMinQuantityComparator extends OrderByComparator {
024    
025            public static String ORDER_BY_ASC =
026                    "categoryId ASC, minQuantity ASC, name ASC";
027    
028            public static String ORDER_BY_DESC =
029                    "categoryId DESC, minQuantity DESC, name DESC";
030    
031            public static String[] ORDER_BY_FIELDS = {
032                    "categoryId", "minQuantity", "name"
033            };
034    
035            public ItemMinQuantityComparator() {
036                    this(false);
037            }
038    
039            public ItemMinQuantityComparator(boolean ascending) {
040                    _ascending = ascending;
041            }
042    
043            public int compare(Object obj1, Object obj2) {
044                    ShoppingItem item1 = (ShoppingItem)obj1;
045                    ShoppingItem item2 = (ShoppingItem)obj2;
046    
047                    Long categoryId1 = new Long(item1.getCategoryId());
048                    Long categoryId2 = new Long(item2.getCategoryId());
049    
050                    int value = categoryId1.compareTo(categoryId2);
051    
052                    if (value == 0) {
053                            if (item1.getMinQuantity() < item2.getMinQuantity()) {
054                                    value = -1;
055                            }
056                            else if (item1.getMinQuantity() > item2.getMinQuantity()) {
057                                    value = 1;
058                            }
059                    }
060    
061                    if (value == 0) {
062                            value = item1.getName().toLowerCase().compareTo(
063                                    item2.getName().toLowerCase());
064                    }
065    
066                    if (_ascending) {
067                            return value;
068                    }
069                    else {
070                            return -value;
071                    }
072            }
073    
074            public String getOrderBy() {
075                    if (_ascending) {
076                            return ORDER_BY_ASC;
077                    }
078                    else {
079                            return ORDER_BY_DESC;
080                    }
081            }
082    
083            public String[] getOrderByFields() {
084                    return ORDER_BY_FIELDS;
085            }
086    
087            public boolean isAscending() {
088                    return _ascending;
089            }
090    
091            private boolean _ascending;
092    
093    }