KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > entity > condition > OrderByList


1 package org.ofbiz.entity.condition;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.Collection JavaDoc;
5 import java.util.Comparator JavaDoc;
6 import java.util.Iterator JavaDoc;
7 import java.util.List JavaDoc;
8
9 import org.ofbiz.entity.GenericEntity;
10 import org.ofbiz.entity.GenericModelException;
11 import org.ofbiz.entity.config.DatasourceInfo;
12 import org.ofbiz.entity.model.ModelEntity;
13
14 public class OrderByList implements Comparator JavaDoc {
15     protected List JavaDoc orderByList = new ArrayList JavaDoc();
16
17     public OrderByList() {
18     }
19
20     public OrderByList(Collection JavaDoc orderByList) {
21         addOrderBy(orderByList);
22     }
23     
24     public void addOrderBy(Collection JavaDoc orderByList) {
25         Iterator JavaDoc it = orderByList.iterator();
26         while (it.hasNext()) {
27             addOrderBy(OrderByItem.parse(it.next()));
28         }
29     }
30
31     public void addOrderBy(String JavaDoc text) {
32         addOrderBy(OrderByItem.parse(text));
33     }
34
35     public void addOrderBy(EntityConditionValue value) {
36         addOrderBy(value, false);
37     }
38
39     public void addOrderBy(EntityConditionValue value, boolean descending) {
40         addOrderBy(new OrderByItem(value, descending));
41     }
42     
43     public void addOrderBy(OrderByItem orderByItem) {
44         orderByList.add(orderByItem);
45     }
46
47     public void checkOrderBy(ModelEntity modelEntity) throws GenericModelException {
48         for (int i = 0; i < orderByList.size(); i++) {
49             OrderByItem orderByItem = (OrderByItem) orderByList.get(i);
50             orderByItem.checkOrderBy(modelEntity);
51         }
52     }
53
54     public String JavaDoc makeOrderByString(ModelEntity modelEntity, boolean includeTablenamePrefix, DatasourceInfo datasourceInfo) {
55         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
56         makeOrderByString(sb, modelEntity, includeTablenamePrefix, datasourceInfo);
57         return sb.toString();
58     }
59
60     public void makeOrderByString(StringBuffer JavaDoc sb, ModelEntity modelEntity, boolean includeTablenamePrefix, DatasourceInfo datasourceInfo) {
61         if (!orderByList.isEmpty()) {
62             sb.append(" ORDER BY ");
63         }
64         for (int i = 0; i < orderByList.size(); i++) {
65             if (i != 0) sb.append(", ");
66             OrderByItem orderByItem = (OrderByItem) orderByList.get(i);
67             orderByItem.makeOrderByString(sb, modelEntity, includeTablenamePrefix, datasourceInfo);
68         }
69     }
70
71     public int compare(Object JavaDoc obj1, Object JavaDoc obj2) {
72         return compare((GenericEntity) obj1, (GenericEntity) obj2);
73     }
74
75     public int compare(GenericEntity entity1, GenericEntity entity2) {
76         int result = 0;
77         for (int i = 0; i < orderByList.size() && result == 0; i++) {
78             OrderByItem orderByItem = (OrderByItem) orderByList.get(i);
79             result = orderByItem.compare(entity1, entity2);
80         }
81         return result;
82     }
83
84     public boolean equals(java.lang.Object JavaDoc obj) {
85         if (!(obj instanceof OrderByList)) return false;
86         OrderByList that = (OrderByList) obj;
87         return orderByList.equals(that.orderByList);
88     }
89
90     public String JavaDoc toString() {
91         return makeOrderByString(null, false, null);
92     }
93 }
94
Popular Tags