KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: OrderByItem.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * <p>Copyright (c) 2004 The Open For Business Project - www.ofbiz.org
5  *
6  * <p>Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * <p>The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * <p>THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24
25 package org.ofbiz.entity.condition;
26
27 import java.util.Comparator JavaDoc;
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 JavaDoc {
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 JavaDoc obj) {
60         if (obj instanceof String JavaDoc) {
61             return parse((String JavaDoc) 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 JavaDoc("unknown orderBy item: " + obj);
68         }
69     }
70
71     public static final OrderByItem parse(String JavaDoc 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 JavaDoc 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 JavaDoc obj1, java.lang.Object JavaDoc 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 JavaDoc value1 = value.getValue(obj1);
141         Object JavaDoc value2 = value.getValue(obj2);
142
143         int result;
144         // null is defined as the largest possible value
145
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 JavaDoc) value1).compareTo(value2);
151         }
152         // if (Debug.infoOn()) Debug.logInfo("[OrderByComparator.compareAsc] Result is " + result + " for [" + value + "] and [" + value2 + "]", module);
153
return descending ? -result : result;
154     }
155
156     public String JavaDoc makeOrderByString(ModelEntity modelEntity, boolean includeTablenamePrefix, DatasourceInfo datasourceInfo) {
157         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
158         makeOrderByString(sb, modelEntity, includeTablenamePrefix, datasourceInfo);
159         return sb.toString();
160     }
161
162     public void makeOrderByString(StringBuffer JavaDoc 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 JavaDoc 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 JavaDoc toString() {
175         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
176         sb.append(getValue());
177         sb.append(descending ? " DESC" : " ASC");
178         return sb.toString();
179     }
180 }
181
Popular Tags