1 package org.ofbiz.entity.condition; 2 3 import java.util.ArrayList ; 4 import java.util.Collection ; 5 import java.util.Comparator ; 6 import java.util.Iterator ; 7 import java.util.List ; 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 { 15 protected List orderByList = new ArrayList (); 16 17 public OrderByList() { 18 } 19 20 public OrderByList(Collection orderByList) { 21 addOrderBy(orderByList); 22 } 23 24 public void addOrderBy(Collection orderByList) { 25 Iterator it = orderByList.iterator(); 26 while (it.hasNext()) { 27 addOrderBy(OrderByItem.parse(it.next())); 28 } 29 } 30 31 public void addOrderBy(String 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 makeOrderByString(ModelEntity modelEntity, boolean includeTablenamePrefix, DatasourceInfo datasourceInfo) { 55 StringBuffer sb = new StringBuffer (); 56 makeOrderByString(sb, modelEntity, includeTablenamePrefix, datasourceInfo); 57 return sb.toString(); 58 } 59 60 public void makeOrderByString(StringBuffer 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 obj1, Object 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 obj) { 85 if (!(obj instanceof OrderByList)) return false; 86 OrderByList that = (OrderByList) obj; 87 return orderByList.equals(that.orderByList); 88 } 89 90 public String toString() { 91 return makeOrderByString(null, false, null); 92 } 93 } 94 | Popular Tags |