001
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
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 }