001
014
015 package com.liferay.portlet.shopping.util.comparator;
016
017 import com.liferay.portal.kernel.util.OrderByComparator;
018 import com.liferay.portlet.shopping.model.ShoppingItem;
019
020
023 public class ItemMinQuantityComparator extends OrderByComparator {
024
025 public static final String ORDER_BY_ASC =
026 "ShoppingItem.categoryId ASC, ShoppingItem.minQuantity ASC, " +
027 "ShoppingItem.name ASC";
028
029 public static final String ORDER_BY_DESC =
030 "ShoppingItem.categoryId DESC, ShoppingItem.minQuantity DESC, " +
031 "ShoppingItem.name DESC";
032
033 public static final String[] ORDER_BY_FIELDS = {
034 "categoryId", "minQuantity", "name"
035 };
036
037 public ItemMinQuantityComparator() {
038 this(false);
039 }
040
041 public ItemMinQuantityComparator(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 if (item1.getMinQuantity() < item2.getMinQuantity()) {
057 value = -1;
058 }
059 else if (item1.getMinQuantity() > item2.getMinQuantity()) {
060 value = 1;
061 }
062 }
063
064 if (value == 0) {
065 value = item1.getName().toLowerCase().compareTo(
066 item2.getName().toLowerCase());
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 boolean _ascending;
098
099 }