KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: EntityOperator.java 6095 2005-11-09 01:38:54Z jonesde $
3  *
4  * Copyright (c) 2002-2005 The Open For Business Project - www.ofbiz.org
5  *
6  * 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  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * 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.Collection JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Map JavaDoc;
32
33 import org.ofbiz.base.util.Debug;
34 import org.ofbiz.entity.GenericDelegator;
35 import org.ofbiz.entity.GenericEntity;
36 import org.ofbiz.entity.GenericModelException;
37 import org.ofbiz.entity.model.ModelEntity;
38 import org.ofbiz.entity.model.ModelField;
39
40 /**
41  * Encapsulates operations between entities and entity fields. This is a immutable class.
42  *
43  *@author <a HREF='mailto:chris_maurer@altavista.com'>Chris Maurer</a>
44  *@author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
45  *@author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
46  *@version $Rev: 6095 $
47  *@since 2.0
48  */

49 public abstract class EntityOperator extends EntityConditionBase {
50
51     public static final int ID_EQUALS = 1;
52     public static final int ID_NOT_EQUAL = 2;
53     public static final int ID_LESS_THAN = 3;
54     public static final int ID_GREATER_THAN = 4;
55     public static final int ID_LESS_THAN_EQUAL_TO = 5;
56     public static final int ID_GREATER_THAN_EQUAL_TO = 6;
57     public static final int ID_IN = 7;
58     public static final int ID_BETWEEN = 8;
59     public static final int ID_NOT = 9;
60     public static final int ID_AND = 10;
61     public static final int ID_OR = 11;
62     public static final int ID_LIKE = 12;
63     public static final int ID_NOT_IN = 13;
64     public static final int ID_NOT_LIKE = 14;
65
66     private static HashMap JavaDoc registry = new HashMap JavaDoc();
67
68     private static void register(String JavaDoc name, EntityOperator operator) {
69         registry.put(name, operator);
70     }
71
72     public static EntityOperator lookup(String JavaDoc name) {
73         return (EntityOperator) registry.get(name);
74     }
75
76     public static EntityComparisonOperator lookupComparison(String JavaDoc name) {
77         EntityOperator operator = lookup(name);
78         if ( !(operator instanceof EntityComparisonOperator ) )
79             throw new IllegalArgumentException JavaDoc(name + " is not a comparison operator");
80         return (EntityComparisonOperator)operator;
81     }
82
83     public static EntityJoinOperator lookupJoin(String JavaDoc name) {
84         EntityOperator operator = lookup(name);
85         if ( !(operator instanceof EntityJoinOperator ) )
86             throw new IllegalArgumentException JavaDoc(name + " is not a join operator");
87         return (EntityJoinOperator)operator;
88     }
89
90     public static final EntityComparisonOperator EQUALS = new EntityComparisonOperator(ID_EQUALS, "=") {
91         public boolean compare(Object JavaDoc lhs, Object JavaDoc rhs) { return EntityComparisonOperator.compareEqual(lhs, rhs); }
92         protected void makeRHSWhereString(ModelEntity entity, List JavaDoc entityConditionParams, StringBuffer JavaDoc sb, ModelField field, Object JavaDoc rhs) {
93             if (rhs == null || rhs == GenericEntity.NULL_FIELD) {
94                 //Debug.logInfo("makeRHSWhereString: field IS NULL: " + field.getName(), module);
95
sb.append(" IS NULL");
96             } else {
97                 //Debug.logInfo("makeRHSWhereString: field not null, doing super: " + field.getName() + ", type: " + rhs.getClass().getName() + ", value: " + rhs, module);
98
super.makeRHSWhereString(entity, entityConditionParams, sb, field, rhs);
99             }
100         }
101     };
102     static { register( "equals", EQUALS ); }
103     public static final EntityComparisonOperator NOT_EQUAL = new EntityComparisonOperator(ID_NOT_EQUAL, "<>") {
104         public boolean compare(Object JavaDoc lhs, Object JavaDoc rhs) { return EntityComparisonOperator.compareNotEqual(lhs, rhs); }
105         protected void makeRHSWhereString(ModelEntity entity, List JavaDoc entityConditionParams, StringBuffer JavaDoc sb, ModelField field, Object JavaDoc rhs) {
106             if (rhs == null || rhs == GenericEntity.NULL_FIELD) {
107                 sb.append(" IS NOT NULL");
108             } else {
109                 super.makeRHSWhereString(entity, entityConditionParams, sb, field, rhs);
110             }
111         }
112     };
113     static { register( "not-equal", NOT_EQUAL ); }
114     static { register( "not-equals", NOT_EQUAL ); }
115     static { register( "notEqual", NOT_EQUAL ); }
116     public static final EntityComparisonOperator LESS_THAN = new EntityComparisonOperator(ID_LESS_THAN, "<") {
117         public boolean compare(Object JavaDoc lhs, Object JavaDoc rhs) { return EntityComparisonOperator.compareLessThan(lhs, rhs); }
118     };
119     static { register( "less", LESS_THAN ); }
120     static { register( "less-than", LESS_THAN ); }
121     static { register( "lessThan", LESS_THAN ); }
122     public static final EntityComparisonOperator GREATER_THAN = new EntityComparisonOperator(ID_GREATER_THAN, ">") {
123         public boolean compare(Object JavaDoc lhs, Object JavaDoc rhs) { return EntityComparisonOperator.compareGreaterThan(lhs, rhs); }
124     };
125     static { register( "greater", GREATER_THAN ); }
126     static { register( "greater-than", GREATER_THAN ); }
127     static { register( "greaterThan", GREATER_THAN ); }
128     public static final EntityComparisonOperator LESS_THAN_EQUAL_TO = new EntityComparisonOperator(ID_LESS_THAN_EQUAL_TO, "<=") {
129         public boolean compare(Object JavaDoc lhs, Object JavaDoc rhs) { return EntityComparisonOperator.compareLessThanEqualTo(lhs, rhs); }
130     };
131     static { register( "less-equals", LESS_THAN_EQUAL_TO ); }
132     static { register( "less-than-equal-to", LESS_THAN_EQUAL_TO ); }
133     static { register( "lessThanEqualTo", LESS_THAN_EQUAL_TO ); }
134     public static final EntityComparisonOperator GREATER_THAN_EQUAL_TO = new EntityComparisonOperator(ID_GREATER_THAN_EQUAL_TO, ">=") {
135         public boolean compare(Object JavaDoc lhs, Object JavaDoc rhs) { return EntityComparisonOperator.compareGreaterThanEqualTo(lhs, rhs); }
136     };
137     static { register( "greater-equals", GREATER_THAN_EQUAL_TO ); }
138     static { register( "greater-than-equal-to", GREATER_THAN_EQUAL_TO ); }
139     static { register( "greaterThanEqualTo", GREATER_THAN_EQUAL_TO ); }
140     public static final EntityComparisonOperator IN = new EntityComparisonOperator(ID_IN, "IN") {
141         public boolean compare(Object JavaDoc lhs, Object JavaDoc rhs) { return EntityComparisonOperator.compareIn(lhs, rhs); }
142         protected void makeRHSWhereStringValue(ModelEntity entity, List JavaDoc entityConditionParams, StringBuffer JavaDoc sb, ModelField field, Object JavaDoc rhs) { appendRHSList(entityConditionParams, sb, field, rhs); }
143     };
144     static { register( "in", IN ); }
145     public static final EntityComparisonOperator BETWEEN = new EntityComparisonOperator(ID_BETWEEN, "BETWEEN");
146     static { register( "between", BETWEEN ); }
147     public static final EntityComparisonOperator NOT = new EntityComparisonOperator(ID_NOT, "NOT");
148     static { register( "not", NOT ); }
149     public static final EntityJoinOperator AND = new EntityJoinOperator(ID_AND, "AND", false);
150     static { register( "and", AND ); }
151     public static final EntityJoinOperator OR = new EntityJoinOperator(ID_OR, "OR", true);
152     static { register( "or", OR ); }
153     public static final EntityComparisonOperator LIKE = new EntityComparisonOperator(ID_LIKE, "LIKE") {
154         public boolean compare(Object JavaDoc lhs, Object JavaDoc rhs) { return EntityComparisonOperator.compareLike(lhs, rhs); }
155     };
156     static { register( "like", LIKE ); }
157     public static final EntityComparisonOperator NOT_LIKE = new EntityComparisonOperator(ID_NOT_LIKE, "NOT LIKE") {
158         public boolean compare(Object JavaDoc lhs, Object JavaDoc rhs) { return !EntityComparisonOperator.compareLike(lhs, rhs); }
159     };
160     static { register( "not-like", NOT_LIKE); }
161     public static final EntityComparisonOperator NOT_IN = new EntityComparisonOperator(ID_NOT_IN, "NOT IN") {
162         public boolean compare(Object JavaDoc lhs, Object JavaDoc rhs) { return !EntityComparisonOperator.compareIn(lhs, rhs); }
163         protected void makeRHSWhereStringValue(ModelEntity entity, List JavaDoc entityConditionParams, StringBuffer JavaDoc sb, ModelField field, Object JavaDoc rhs) { appendRHSList(entityConditionParams, sb, field, rhs); }
164     };
165     static { register( "not-in", NOT_IN ); }
166
167     protected int idInt;
168     protected String JavaDoc codeString;
169
170     public EntityOperator(int id, String JavaDoc code) {
171         idInt = id;
172         codeString = code;
173     }
174
175     public String JavaDoc getCode() {
176         if (codeString == null) {
177             return "null";
178         } else {
179             return codeString;
180         }
181     }
182
183     public int getId() {
184         return idInt;
185     }
186
187     public String JavaDoc toString() {
188         return codeString;
189     }
190
191     public int hashCode() {
192         return this.codeString.hashCode();
193     }
194
195     public boolean equals(Object JavaDoc obj) {
196         EntityOperator otherOper = (EntityOperator) obj;
197         return this.idInt == otherOper.idInt;
198     }
199
200     public boolean entityMatches(GenericEntity entity, Object JavaDoc lhs, Object JavaDoc rhs) {
201         return mapMatches(entity.getDelegator(), entity, lhs, rhs);
202     }
203
204     protected void appendRHSList(List JavaDoc entityConditionParams, StringBuffer JavaDoc whereStringBuffer, ModelField field, Object JavaDoc rhs) {
205         whereStringBuffer.append('(');
206
207         if (rhs instanceof Collection JavaDoc) {
208             Iterator JavaDoc rhsIter = ((Collection JavaDoc) rhs).iterator();
209
210             while (rhsIter.hasNext()) {
211                 Object JavaDoc inObj = rhsIter.next();
212
213                 addValue(whereStringBuffer, field, inObj, entityConditionParams);
214                 if (rhsIter.hasNext()) {
215                     whereStringBuffer.append(", ");
216                 }
217             }
218         } else {
219             addValue(whereStringBuffer, field, rhs, entityConditionParams);
220         }
221         whereStringBuffer.append(')');
222     }
223
224     public Object JavaDoc eval(GenericDelegator delegator, Map JavaDoc map, Object JavaDoc lhs, Object JavaDoc rhs) {
225         return castBoolean(mapMatches(delegator, map, lhs, rhs));
226     }
227
228     public abstract boolean mapMatches(GenericDelegator delegator, Map JavaDoc map, Object JavaDoc lhs, Object JavaDoc rhs);
229     public abstract void validateSql(ModelEntity entity, Object JavaDoc lhs, Object JavaDoc rhs) throws GenericModelException;
230     public void addSqlValue(StringBuffer JavaDoc sql, ModelEntity entity, List JavaDoc entityConditionParams, Object JavaDoc lhs, Object JavaDoc rhs) {
231         addSqlValue(sql, entity, entityConditionParams, true, lhs, rhs);
232     }
233
234     public abstract void addSqlValue(StringBuffer JavaDoc sql, ModelEntity entity, List JavaDoc entityConditionParams, boolean compat, Object JavaDoc rhs, Object JavaDoc lhs);
235     public abstract EntityCondition freeze(Object JavaDoc lhs, Object JavaDoc rhs);
236     public abstract void visit(EntityConditionVisitor visitor, Object JavaDoc lhs, Object JavaDoc rhs);
237
238     public static final Object JavaDoc WILDCARD = new Object JavaDoc() {
239         public String JavaDoc toString() {
240             return "(WILDCARD)";
241         }
242     };
243 }
244
Popular Tags