001    /**
002     * Copyright (c) 2000-present 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<ShoppingItem> {
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(ShoppingItem item1, ShoppingItem item2) {
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                            String name1 = StringUtil.toLowerCase(item1.getName());
064                            String name2 = StringUtil.toLowerCase(item2.getName());
065    
066                            value = name1.compareTo(name2);
067                    }
068    
069                    if (_ascending) {
070                            return value;
071                    }
072                    else {
073                            return -value;
074                    }
075            }
076    
077            @Override
078            public String getOrderBy() {
079                    if (_ascending) {
080                            return ORDER_BY_ASC;
081                    }
082                    else {
083                            return ORDER_BY_DESC;
084                    }
085            }
086    
087            @Override
088            public String[] getOrderByFields() {
089                    return ORDER_BY_FIELDS;
090            }
091    
092            @Override
093            public boolean isAscending() {
094                    return _ascending;
095            }
096    
097            private final boolean _ascending;
098    
099    }