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