1 24 25 package org.ofbiz.entity.condition; 26 27 import java.util.Comparator ; 28 29 import org.ofbiz.entity.GenericEntity; 30 import org.ofbiz.entity.GenericModelException; 31 import org.ofbiz.entity.config.DatasourceInfo; 32 import org.ofbiz.entity.model.ModelEntity; 33 34 public class OrderByItem implements Comparator { 35 public static final int DEFAULT = 0; 36 public static final int UPPER = 1; 37 public static final int LOWER = 2; 38 39 protected boolean descending; 40 protected EntityConditionValue value; 41 42 public OrderByItem(EntityConditionValue value) { 43 this.value = value; 44 } 45 46 public OrderByItem(EntityConditionValue value, boolean descending) { 47 this(value); 48 this.descending = descending; 49 } 50 51 public EntityConditionValue getValue() { 52 return value; 53 } 54 55 public boolean getDescending() { 56 return descending; 57 } 58 59 public static final OrderByItem parse(Object obj) { 60 if (obj instanceof String ) { 61 return parse((String ) obj); 62 } else if (obj instanceof EntityConditionValue) { 63 return new OrderByItem((EntityConditionValue) obj, false); 64 } else if (obj instanceof OrderByItem) { 65 return (OrderByItem) obj; 66 } else { 67 throw new IllegalArgumentException ("unknown orderBy item: " + obj); 68 } 69 } 70 71 public static final OrderByItem parse(String text) { 72 text = text.trim(); 73 int startIndex = 0, endIndex = text.length(); 74 boolean descending; 75 int caseSensitivity; 76 if (text.endsWith(" DESC")) { 77 descending = true; 78 endIndex -= 5; 79 } else if (text.endsWith(" ASC")) { 80 descending = false; 81 endIndex -= 4; 82 } else if (text.startsWith("-")) { 83 descending = true; 84 startIndex++; 85 } else if (text.startsWith("+")) { 86 descending = false; 87 startIndex++; 88 } else { 89 descending = false; 90 } 91 92 if (startIndex != 0 || endIndex != text.length()) { 93 text = text.substring(startIndex, endIndex); 94 startIndex = 0; 95 endIndex = text.length(); 96 } 97 98 if (text.endsWith(")")) { 99 String upperText = text.toUpperCase(); 100 endIndex--; 101 if (upperText.startsWith("UPPER(")) { 102 caseSensitivity = UPPER; 103 startIndex = 6; 104 } else if (upperText.startsWith("LOWER(")) { 105 caseSensitivity = LOWER; 106 startIndex = 6; 107 } else { 108 caseSensitivity = DEFAULT; 109 } 110 } else { 111 caseSensitivity = DEFAULT; 112 } 113 114 if (startIndex != 0 || endIndex != text.length()) { 115 text = text.substring(startIndex, endIndex); 116 startIndex = 0; 117 endIndex = text.length(); 118 } 119 EntityConditionValue value = new EntityFieldValue(text); 120 switch (caseSensitivity) { 121 case UPPER: 122 value = new EntityFunction.UPPER(value); 123 break; 124 case LOWER: 125 value = new EntityFunction.LOWER(value); 126 break; 127 } 128 return new OrderByItem(value, descending); 129 } 130 131 public int compare(java.lang.Object obj1, java.lang.Object obj2) { 132 return compare((GenericEntity) obj1, (GenericEntity) obj2); 133 } 134 135 public void checkOrderBy(ModelEntity modelEntity) throws GenericModelException { 136 value.validateSql(modelEntity); 137 } 138 139 public int compare(GenericEntity obj1, GenericEntity obj2) { 140 Object value1 = value.getValue(obj1); 141 Object value2 = value.getValue(obj2); 142 143 int result; 144 if (value1 == null) { 146 result = value2 == null ? 0 : 1; 147 } else if (value2 == null) { 148 result = value1 == null ? 0 : -1; 149 } else { 150 result = ((Comparable ) value1).compareTo(value2); 151 } 152 return descending ? -result : result; 154 } 155 156 public String makeOrderByString(ModelEntity modelEntity, boolean includeTablenamePrefix, DatasourceInfo datasourceInfo) { 157 StringBuffer sb = new StringBuffer (); 158 makeOrderByString(sb, modelEntity, includeTablenamePrefix, datasourceInfo); 159 return sb.toString(); 160 } 161 162 public void makeOrderByString(StringBuffer sb, ModelEntity modelEntity, boolean includeTablenamePrefix, DatasourceInfo datasourceInfo) { 163 getValue().addSqlValue(sb, modelEntity, null, includeTablenamePrefix, datasourceInfo); 164 sb.append(descending ? " DESC" : " ASC"); 165 } 166 167 public boolean equals(java.lang.Object obj) { 168 if (!(obj instanceof OrderByItem)) return false; 169 OrderByItem that = (OrderByItem) obj; 170 171 return getValue().equals(that.getValue()) && getDescending() == that.getDescending(); 172 } 173 174 public String toString() { 175 StringBuffer sb = new StringBuffer (); 176 sb.append(getValue()); 177 sb.append(descending ? " DESC" : " ASC"); 178 return sb.toString(); 179 } 180 } 181 | Popular Tags |