001    /**
002     * Copyright (c) 2000-2011 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            @Override
044            public int compare(Object obj1, Object obj2) {
045                    ShoppingItem item1 = (ShoppingItem)obj1;
046                    ShoppingItem item2 = (ShoppingItem)obj2;
047    
048                    Long categoryId1 = new Long(item1.getCategoryId());
049                    Long categoryId2 = new Long(item2.getCategoryId());
050    
051                    int value = categoryId1.compareTo(categoryId2);
052    
053                    if (value == 0) {
054                            if (item1.getMinQuantity() < item2.getMinQuantity()) {
055                                    value = -1;
056                            }
057                            else if (item1.getMinQuantity() > item2.getMinQuantity()) {
058                                    value = 1;
059                            }
060                    }
061    
062                    if (value == 0) {
063                            value = item1.getName().toLowerCase().compareTo(
064                                    item2.getName().toLowerCase());
065                    }
066    
067                    if (_ascending) {
068                            return value;
069                    }
070                    else {
071                            return -value;
072                    }
073            }
074    
075            @Override
076            public String getOrderBy() {
077                    if (_ascending) {
078                            return ORDER_BY_ASC;
079                    }
080                    else {
081                            return ORDER_BY_DESC;
082                    }
083            }
084    
085            @Override
086            public String[] getOrderByFields() {
087                    return ORDER_BY_FIELDS;
088            }
089    
090            @Override
091            public boolean isAscending() {
092                    return _ascending;
093            }
094    
095            private boolean _ascending;
096    
097    }