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