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 ItemNameComparator extends OrderByComparator {
024
025 public static String ORDER_BY_ASC = "categoryId ASC, name ASC";
026
027 public static String ORDER_BY_DESC = "categoryId DESC, name DESC";
028
029 public static String[] ORDER_BY_FIELDS = {"categoryId", "name"};
030
031 public ItemNameComparator() {
032 this(false);
033 }
034
035 public ItemNameComparator(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.getName().toLowerCase().compareTo(
050 item2.getName().toLowerCase());
051 }
052
053 if (_ascending) {
054 return value;
055 }
056 else {
057 return -value;
058 }
059 }
060
061 public String getOrderBy() {
062 if (_ascending) {
063 return ORDER_BY_ASC;
064 }
065 else {
066 return ORDER_BY_DESC;
067 }
068 }
069
070 public String[] getOrderByFields() {
071 return ORDER_BY_FIELDS;
072 }
073
074 public boolean isAscending() {
075 return _ascending;
076 }
077
078 private boolean _ascending;
079
080 }