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 ItemSKUComparator extends OrderByComparator {
024
025 public static String ORDER_BY_ASC = "categoryId ASC, sku ASC";
026
027 public static String ORDER_BY_DESC = "categoryId DESC, sku DESC";
028
029 public static String[] ORDER_BY_FIELDS = {"categoryId", "sku"};
030
031 public ItemSKUComparator() {
032 this(false);
033 }
034
035 public ItemSKUComparator(boolean ascending) {
036 _ascending = ascending;
037 }
038
039 public int compare(Object obj1, Object obj2) {
040 ShoppingItem item1 = (ShoppingItem)obj1;
041 ShoppingItem item2 = (ShoppingItem)obj2;
042
043 Long categoryId1 = new Long(item1.getCategoryId());
044 Long categoryId2 = new Long(item2.getCategoryId());
045
046 int value = categoryId1.compareTo(categoryId2);
047
048 if (value == 0) {
049 value = item1.getSku().compareTo(item2.getSku());
050 }
051
052 if (_ascending) {
053 return value;
054 }
055 else {
056 return -value;
057 }
058 }
059
060 public String getOrderBy() {
061 if (_ascending) {
062 return ORDER_BY_ASC;
063 }
064 else {
065 return ORDER_BY_DESC;
066 }
067 }
068
069 public String[] getOrderByFields() {
070 return ORDER_BY_FIELDS;
071 }
072
073 public boolean isAscending() {
074 return _ascending;
075 }
076
077 private boolean _ascending;
078
079 }