KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: EntityComparisonOperator.java 6155 2005-11-21 07:30:52Z jonesde $
3  *
4  * Copyright (c) 2002 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.List JavaDoc;
29 import java.util.Map JavaDoc;
30
31 import org.apache.oro.text.perl.Perl5Util;
32 import org.apache.oro.text.regex.MalformedPatternException;
33 import org.apache.oro.text.regex.Pattern;
34 import org.apache.oro.text.regex.PatternCompiler;
35 import org.apache.oro.text.regex.PatternMatcher;
36 import org.apache.oro.text.regex.Perl5Compiler;
37 import org.apache.oro.text.regex.Perl5Matcher;
38 import org.ofbiz.base.util.Debug;
39 import org.ofbiz.entity.GenericDelegator;
40 import org.ofbiz.entity.GenericModelException;
41 import org.ofbiz.entity.model.ModelEntity;
42 import org.ofbiz.entity.model.ModelField;
43
44 /**
45  * Encapsulates operations between entities and entity fields. This is a immutable class.
46  *
47  * @author <a HREF="mailto:adam@doogie.org">Adam Heath</a>
48  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
49  * @version $Rev: 6155 $
50  * @since 3.0
51  */

52 public class EntityComparisonOperator extends EntityOperator {
53     
54     public static final String JavaDoc module = EntityComparisonOperator.class.getName();
55
56     protected static PatternMatcher matcher = new Perl5Matcher();
57     protected static Perl5Util perl5Util = new Perl5Util();
58     protected static PatternCompiler compiler = new Perl5Compiler();
59
60     public static Pattern makeOroPattern(String JavaDoc sqlLike) {
61         try {
62             sqlLike = perl5Util.substitute("s/([$^.+*?])/\\\\$1/g", sqlLike);
63             sqlLike = perl5Util.substitute("s/%/.*/g", sqlLike);
64             sqlLike = perl5Util.substitute("s/_/./g", sqlLike);
65         } catch (Throwable JavaDoc t) {
66             String JavaDoc errMsg = "Error in ORO pattern substitution for SQL like clause [" + sqlLike + "]: " + t.toString();
67             Debug.logError(t, errMsg, module);
68             throw new IllegalArgumentException JavaDoc(errMsg);
69         }
70         try {
71             return compiler.compile(sqlLike);
72         } catch (MalformedPatternException e) {
73             e.printStackTrace();
74         }
75         return null;
76     }
77
78     public void validateSql(ModelEntity entity, Object JavaDoc lhs, Object JavaDoc rhs) throws GenericModelException {
79         if (lhs instanceof EntityConditionValue) {
80             EntityConditionValue ecv = (EntityConditionValue) lhs;
81             ecv.validateSql(entity);
82         }
83         if (rhs instanceof EntityConditionValue) {
84             EntityConditionValue ecv = (EntityConditionValue) rhs;
85             ecv.validateSql(entity);
86         }
87     }
88
89     public void visit(EntityConditionVisitor visitor, Object JavaDoc lhs, Object JavaDoc rhs) {
90         visitor.accept(lhs);
91         visitor.accept(rhs);
92     }
93
94     public void addSqlValue(StringBuffer JavaDoc sql, ModelEntity entity, List JavaDoc entityConditionParams, boolean compat, Object JavaDoc lhs, Object JavaDoc rhs) {
95         //Debug.logInfo("EntityComparisonOperator.addSqlValue field=" + lhs + ", value=" + rhs + ", value type=" + (rhs == null ? "null object" : rhs.getClass().getName()), module);
96
ModelField field;
97         if (lhs instanceof EntityConditionValue) {
98             EntityConditionValue ecv = (EntityConditionValue) lhs;
99             ecv.addSqlValue(sql, entity, entityConditionParams, false, null);
100             field = ecv.getModelField(entity);
101         } else if (compat && lhs instanceof String JavaDoc) {
102             field = getField(entity, (String JavaDoc) lhs);
103             if (field == null) {
104                 sql.append(lhs);
105             } else {
106                 sql.append(field.getColName());
107             }
108         } else {
109             addValue(sql, null, lhs, entityConditionParams);
110             field = null;
111         }
112
113         makeRHSWhereString(entity, entityConditionParams, sql, field, rhs);
114     }
115
116     protected void makeRHSWhereString(ModelEntity entity, List JavaDoc entityConditionParams, StringBuffer JavaDoc sql, ModelField field, Object JavaDoc rhs) {
117         sql.append(' ').append(getCode()).append(' ');
118         makeRHSWhereStringValue(entity, entityConditionParams, sql, field, rhs);
119     }
120
121     protected void makeRHSWhereStringValue(ModelEntity entity, List JavaDoc entityConditionParams, StringBuffer JavaDoc sql, ModelField field, Object JavaDoc rhs) {
122         if (rhs instanceof EntityConditionValue) {
123             EntityConditionValue ecv = (EntityConditionValue) rhs;
124             ecv.addSqlValue(sql, entity, entityConditionParams, false, null);
125         } else {
126             addValue(sql, field, rhs, entityConditionParams);
127         }
128     }
129             
130     public boolean compare(Object JavaDoc lhs, Object JavaDoc rhs) {
131         throw new UnsupportedOperationException JavaDoc(codeString);
132     }
133
134     public Object JavaDoc eval(GenericDelegator delegator, Map JavaDoc map, Object JavaDoc lhs, Object JavaDoc rhs) {
135         return mapMatches(delegator, map, lhs, rhs) ? Boolean.TRUE : Boolean.FALSE;
136     }
137
138     public boolean mapMatches(GenericDelegator delegator, Map JavaDoc map, Object JavaDoc lhs, Object JavaDoc rhs) {
139         Object JavaDoc leftValue;
140         if (lhs instanceof EntityConditionValue) {
141             EntityConditionValue ecv = (EntityConditionValue) lhs;
142             leftValue = ecv.getValue(delegator, map);
143         } else if (lhs instanceof String JavaDoc) {
144             leftValue = map.get(lhs);
145         } else {
146             leftValue = lhs;
147         }
148         Object JavaDoc rightValue;
149         if (rhs instanceof EntityConditionValue) {
150             EntityConditionValue ecv = (EntityConditionValue) rhs;
151             rightValue = ecv.getValue(delegator, map);
152         } else {
153             rightValue = rhs;
154         }
155
156         if (leftValue == WILDCARD || rightValue == WILDCARD) return true;
157         return compare(leftValue, rightValue);
158     }
159
160     public EntityCondition freeze(Object JavaDoc lhs, Object JavaDoc rhs) {
161         return new EntityExpr(freeze(lhs), this, freeze(rhs));
162     }
163
164     protected Object JavaDoc freeze(Object JavaDoc item) {
165         if (item instanceof EntityConditionValue) {
166             EntityConditionValue ecv = (EntityConditionValue) item;
167             return ecv.freeze();
168         } else {
169             return item;
170         }
171     }
172
173     public EntityComparisonOperator(int id, String JavaDoc code) {
174         super(id, code);
175     }
176
177     public static final boolean compareEqual(Object JavaDoc lhs, Object JavaDoc rhs) {
178         if (lhs == null) {
179             if (rhs != null) {
180                 return false;
181             }
182         } else if (!lhs.equals(rhs)) {
183             return false;
184         }
185         return true;
186     }
187
188     public static final boolean compareNotEqual(Object JavaDoc lhs, Object JavaDoc rhs) {
189         if (lhs == null) {
190             if (rhs == null) {
191                 return false;
192             }
193         } else if (lhs.equals(rhs)) {
194             return false;
195         }
196         return true;
197     }
198
199     public static final boolean compareGreaterThan(Object JavaDoc lhs, Object JavaDoc rhs) {
200         if (lhs == null) {
201             if (rhs != null) {
202                 return false;
203             }
204         } else if (((Comparable JavaDoc) lhs).compareTo(rhs) <= 0) {
205             return false;
206         }
207         return true;
208     }
209
210     public static final boolean compareGreaterThanEqualTo(Object JavaDoc lhs, Object JavaDoc rhs) {
211         if (lhs == null) {
212             if (rhs != null) {
213                 return false;
214             }
215         } else if (((Comparable JavaDoc) lhs).compareTo(rhs) < 0) {
216             return false;
217         }
218         return true;
219     }
220
221     public static final boolean compareLessThan(Object JavaDoc lhs, Object JavaDoc rhs) {
222         if (lhs == null) {
223             if (rhs != null) {
224                 return false;
225             }
226         } else if (((Comparable JavaDoc) lhs).compareTo(rhs) >= 0) {
227             return false;
228         }
229         return true;
230     }
231
232     public static final boolean compareLessThanEqualTo(Object JavaDoc lhs, Object JavaDoc rhs) {
233         if (lhs == null) {
234             if (rhs != null) {
235                 return false;
236             }
237         } else if (((Comparable JavaDoc) lhs).compareTo(rhs) > 0) {
238             return false;
239         }
240         return true;
241     }
242
243     public static final boolean compareIn(Object JavaDoc lhs, Object JavaDoc rhs) {
244         if (lhs == null) {
245             if (rhs != null) {
246                 return false;
247             } else {
248                 return true;
249             }
250         } else if (rhs instanceof Collection JavaDoc) {
251             if (((Collection JavaDoc) rhs).contains(lhs)) {
252                 return true;
253             } else {
254                 return false;
255             }
256         } else if (lhs.equals(rhs)) {
257             return true;
258         } else {
259             return false;
260         }
261     }
262
263     public static final boolean compareLike(Object JavaDoc lhs, Object JavaDoc rhs) {
264         if (lhs == null) {
265             if (rhs != null) {
266                 return false;
267             }
268         } else if (lhs instanceof String JavaDoc && rhs instanceof String JavaDoc) {
269             //see if the lhs value is like the rhs value, rhs will have the pattern characters in it...
270
return matcher.matches((String JavaDoc) lhs, makeOroPattern((String JavaDoc) rhs));
271         }
272         return true;
273     }
274 }
275
Popular Tags