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