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