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