1
19
20 package com.liferay.portlet.shopping.util.comparator;
21
22 import com.liferay.portal.kernel.util.OrderByComparator;
23 import com.liferay.portlet.shopping.model.ShoppingItem;
24
25
31 public class ItemNameComparator extends OrderByComparator {
32
33 public static String ORDER_BY_ASC = "categoryId ASC, name ASC";
34
35 public static String ORDER_BY_DESC = "categoryId DESC, name DESC";
36
37 public static String[] ORDER_BY_FIELDS = {"categoryId", "name"};
38
39 public ItemNameComparator() {
40 this(false);
41 }
42
43 public ItemNameComparator(boolean asc) {
44 _asc = asc;
45 }
46
47 public int compare(Object obj1, Object obj2) {
48 ShoppingItem item1 = (ShoppingItem)obj1;
49 ShoppingItem item2 = (ShoppingItem)obj2;
50
51 Long categoryId1 = new Long(item1.getCategoryId());
52 Long categoryId2 = new Long(item2.getCategoryId());
53
54 int value = categoryId1.compareTo(categoryId2);
55
56 if (value == 0) {
57 value = item1.getName().toLowerCase().compareTo(
58 item2.getName().toLowerCase());
59 }
60
61 if (_asc) {
62 return value;
63 }
64 else {
65 return -value;
66 }
67 }
68
69 public String getOrderBy() {
70 if (_asc) {
71 return ORDER_BY_ASC;
72 }
73 else {
74 return ORDER_BY_DESC;
75 }
76 }
77
78 public String[] getOrderByFields() {
79 return ORDER_BY_FIELDS;
80 }
81
82 public boolean isAscending() {
83 return _asc;
84 }
85
86 private boolean _asc;
87
88 }