KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > widget > tree > ModelTreeCondition


1 /*
2  * $Id: ModelTreeCondition.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2004 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 package org.ofbiz.widget.tree;
25
26 import java.lang.reflect.Method JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.LinkedList JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Map JavaDoc;
31
32 import org.ofbiz.base.util.Debug;
33 import org.ofbiz.base.util.GeneralException;
34 import org.ofbiz.base.util.ObjectType;
35 import org.ofbiz.base.util.UtilXml;
36 import org.ofbiz.base.util.collections.FlexibleMapAccessor;
37 import org.ofbiz.base.util.string.FlexibleStringExpander;
38 import org.ofbiz.entity.GenericValue;
39 import org.ofbiz.entityext.permission.EntityPermissionChecker;
40 import org.ofbiz.minilang.operation.BaseCompare;
41 import org.ofbiz.security.Security;
42
43 import org.apache.oro.text.regex.MalformedPatternException;
44 import org.apache.oro.text.regex.Pattern;
45 import org.apache.oro.text.regex.PatternCompiler;
46 import org.apache.oro.text.regex.PatternMatcher;
47 import org.apache.oro.text.regex.Perl5Compiler;
48 import org.apache.oro.text.regex.Perl5Matcher;
49 import org.w3c.dom.Element JavaDoc;
50
51 /**
52  * Widget Library - Screen model condition class
53  *
54  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
55  * @version $Rev: 5462 $
56  * @since 3.1
57  */

58 public class ModelTreeCondition {
59     public static final String JavaDoc module = ModelTreeCondition.class.getName();
60
61     protected ModelTree modelTree;
62     protected TreeCondition rootCondition;
63
64     public ModelTreeCondition(ModelTree modelTree, Element JavaDoc conditionElement) {
65         this.modelTree = modelTree;
66         Element JavaDoc firstChildElement = UtilXml.firstChildElement(conditionElement);
67         this.rootCondition = readCondition(modelTree, firstChildElement);
68     }
69
70     public boolean eval(Map JavaDoc context) {
71         if (rootCondition == null) {
72             return true;
73         }
74         return rootCondition.eval(context);
75     }
76     
77     public static abstract class TreeCondition {
78         protected ModelTree modelTree;
79
80         public TreeCondition(ModelTree modelTree, Element JavaDoc conditionElement) {
81             this.modelTree = modelTree;
82         }
83         
84         public abstract boolean eval(Map JavaDoc context);
85     }
86     
87     public static List JavaDoc readSubConditions(ModelTree modelTree, Element JavaDoc conditionElement) {
88         List JavaDoc condList = new LinkedList JavaDoc();
89         List JavaDoc subElementList = UtilXml.childElementList(conditionElement);
90         Iterator JavaDoc subElementIter = subElementList.iterator();
91         while (subElementIter.hasNext()) {
92             Element JavaDoc subElement = (Element JavaDoc) subElementIter.next();
93             condList.add(readCondition(modelTree, subElement));
94         }
95         return condList;
96     }
97     
98     public static TreeCondition readCondition(ModelTree modelTree, Element JavaDoc conditionElement) {
99         if (conditionElement == null) {
100             return null;
101         }
102         if ("and".equals(conditionElement.getNodeName())) {
103             return new And(modelTree, conditionElement);
104         } else if ("xor".equals(conditionElement.getNodeName())) {
105             return new Xor(modelTree, conditionElement);
106         } else if ("or".equals(conditionElement.getNodeName())) {
107             return new Or(modelTree, conditionElement);
108         } else if ("not".equals(conditionElement.getNodeName())) {
109             return new Not(modelTree, conditionElement);
110         } else if ("if-has-permission".equals(conditionElement.getNodeName())) {
111             return new IfHasPermission(modelTree, conditionElement);
112         } else if ("if-validate-method".equals(conditionElement.getNodeName())) {
113             return new IfValidateMethod(modelTree, conditionElement);
114         } else if ("if-compare".equals(conditionElement.getNodeName())) {
115             return new IfCompare(modelTree, conditionElement);
116         } else if ("if-compare-field".equals(conditionElement.getNodeName())) {
117             return new IfCompareField(modelTree, conditionElement);
118         } else if ("if-regexp".equals(conditionElement.getNodeName())) {
119             return new IfRegexp(modelTree, conditionElement);
120         } else if ("if-empty".equals(conditionElement.getNodeName())) {
121             return new IfEmpty(modelTree, conditionElement);
122         } else if ("if-entity-permission".equals(conditionElement.getNodeName())) {
123             return new IfEntityPermission(modelTree, conditionElement);
124         } else {
125             throw new IllegalArgumentException JavaDoc("Condition element not supported with name: " + conditionElement.getNodeName());
126         }
127     }
128     
129     public static class And extends TreeCondition {
130         protected List JavaDoc subConditions;
131         
132         public And(ModelTree modelTree, Element JavaDoc condElement) {
133             super (modelTree, condElement);
134             this.subConditions = readSubConditions(modelTree, condElement);
135         }
136         
137         public boolean eval(Map JavaDoc context) {
138             // return false for the first one in the list that is false, basic and algo
139
Iterator JavaDoc subConditionIter = this.subConditions.iterator();
140             while (subConditionIter.hasNext()) {
141                 TreeCondition subCondition = (TreeCondition) subConditionIter.next();
142                 if (!subCondition.eval(context)) {
143                     return false;
144                 }
145             }
146             return true;
147         }
148     }
149     
150     public static class Xor extends TreeCondition {
151         protected List JavaDoc subConditions;
152         
153         public Xor(ModelTree modelTree, Element JavaDoc condElement) {
154             super (modelTree, condElement);
155             this.subConditions = readSubConditions(modelTree, condElement);
156         }
157         
158         public boolean eval(Map JavaDoc context) {
159             // if more than one is true stop immediately and return false; if all are false return false; if only one is true return true
160
boolean foundOneTrue = false;
161             Iterator JavaDoc subConditionIter = this.subConditions.iterator();
162             while (subConditionIter.hasNext()) {
163                 TreeCondition subCondition = (TreeCondition) subConditionIter.next();
164                 if (subCondition.eval(context)) {
165                     if (foundOneTrue) {
166                         // now found two true, so return false
167
return false;
168                     } else {
169                         foundOneTrue = true;
170                     }
171                 }
172             }
173             return foundOneTrue;
174         }
175     }
176     
177     public static class Or extends TreeCondition {
178         protected List JavaDoc subConditions;
179         
180         public Or(ModelTree modelTree, Element JavaDoc condElement) {
181             super (modelTree, condElement);
182             this.subConditions = readSubConditions(modelTree, condElement);
183         }
184         
185         public boolean eval(Map JavaDoc context) {
186             // return true for the first one in the list that is true, basic or algo
187
Iterator JavaDoc subConditionIter = this.subConditions.iterator();
188             while (subConditionIter.hasNext()) {
189                 TreeCondition subCondition = (TreeCondition) subConditionIter.next();
190                 if (subCondition.eval(context)) {
191                     return true;
192                 }
193             }
194             return false;
195         }
196     }
197     
198     public static class Not extends TreeCondition {
199         protected TreeCondition subCondition;
200         
201         public Not(ModelTree modelTree, Element JavaDoc condElement) {
202             super (modelTree, condElement);
203             Element JavaDoc firstChildElement = UtilXml.firstChildElement(condElement);
204             this.subCondition = readCondition(modelTree, firstChildElement);
205         }
206         
207         public boolean eval(Map JavaDoc context) {
208             return !this.subCondition.eval(context);
209         }
210     }
211     
212     public static class IfHasPermission extends TreeCondition {
213         protected FlexibleStringExpander permissionExdr;
214         protected FlexibleStringExpander actionExdr;
215         
216         public IfHasPermission(ModelTree modelTree, Element JavaDoc condElement) {
217             super (modelTree, condElement);
218             this.permissionExdr = new FlexibleStringExpander(condElement.getAttribute("permission"));
219             this.actionExdr = new FlexibleStringExpander(condElement.getAttribute("action"));
220         }
221         
222         public boolean eval(Map JavaDoc context) {
223             // if no user is logged in, treat as if the user does not have permission
224
GenericValue userLogin = (GenericValue) context.get("userLogin");
225             if (userLogin != null) {
226                 String JavaDoc permission = permissionExdr.expandString(context);
227                 String JavaDoc action = actionExdr.expandString(context);
228                 
229                 Security security = (Security) context.get("security");
230                 if (action != null && action.length() > 0) {
231                     // run hasEntityPermission
232
if (security.hasEntityPermission(permission, action, userLogin)) {
233                         return true;
234                     }
235                 } else {
236                     // run hasPermission
237
if (security.hasPermission(permission, userLogin)) {
238                         return true;
239                     }
240                 }
241             }
242             return false;
243         }
244     }
245
246     public static class IfValidateMethod extends TreeCondition {
247         protected FlexibleMapAccessor fieldAcsr;
248         protected FlexibleStringExpander methodExdr;
249         protected FlexibleStringExpander classExdr;
250         
251         public IfValidateMethod(ModelTree modelTree, Element JavaDoc condElement) {
252             super (modelTree, condElement);
253             this.fieldAcsr = new FlexibleMapAccessor(condElement.getAttribute("field-name"));
254             this.methodExdr = new FlexibleStringExpander(condElement.getAttribute("method"));
255             this.classExdr = new FlexibleStringExpander(condElement.getAttribute("class"));
256         }
257         
258         public boolean eval(Map JavaDoc context) {
259             String JavaDoc methodName = this.methodExdr.expandString(context);
260             String JavaDoc className = this.classExdr.expandString(context);
261             
262             Object JavaDoc fieldVal = this.fieldAcsr.get(context);
263             String JavaDoc fieldString = null;
264             if (fieldVal != null) {
265                 try {
266                     fieldString = (String JavaDoc) ObjectType.simpleTypeConvert(fieldVal, "String", null, null);
267                 } catch (GeneralException e) {
268                     Debug.logError(e, "Could not convert object to String, using empty String", module);
269                 }
270             }
271
272             // always use an empty string by default
273
if (fieldString == null) fieldString = "";
274
275             Class JavaDoc[] paramTypes = new Class JavaDoc[] {String JavaDoc.class};
276             Object JavaDoc[] params = new Object JavaDoc[] {fieldString};
277
278             Class JavaDoc valClass;
279             try {
280                 valClass = ObjectType.loadClass(className);
281             } catch (ClassNotFoundException JavaDoc cnfe) {
282                 Debug.logError("Could not find validation class: " + className, module);
283                 return false;
284             }
285
286             Method JavaDoc valMethod;
287             try {
288                 valMethod = valClass.getMethod(methodName, paramTypes);
289             } catch (NoSuchMethodException JavaDoc cnfe) {
290                 Debug.logError("Could not find validation method: " + methodName + " of class " + className, module);
291                 return false;
292             }
293
294             Boolean JavaDoc resultBool = Boolean.FALSE;
295             try {
296                 resultBool = (Boolean JavaDoc) valMethod.invoke(null, params);
297             } catch (Exception JavaDoc e) {
298                 Debug.logError(e, "Error in IfValidationMethod " + methodName + " of class " + className + ", defaulting to false ", module);
299             }
300
301             return resultBool.booleanValue();
302         }
303     }
304     
305     public static class IfCompare extends TreeCondition {
306         protected FlexibleMapAccessor fieldAcsr;
307         protected FlexibleStringExpander valueExdr;
308
309         protected String JavaDoc operator;
310         protected String JavaDoc type;
311         protected FlexibleStringExpander formatExdr;
312         
313         public IfCompare(ModelTree modelTree, Element JavaDoc condElement) {
314             super (modelTree, condElement);
315             this.fieldAcsr = new FlexibleMapAccessor(condElement.getAttribute("field-name"));
316             this.valueExdr = new FlexibleStringExpander(condElement.getAttribute("value"));
317             
318             this.operator = condElement.getAttribute("operator");
319             this.type = condElement.getAttribute("type");
320
321             this.formatExdr = new FlexibleStringExpander(condElement.getAttribute("format"));
322         }
323         
324         public boolean eval(Map JavaDoc context) {
325             String JavaDoc value = this.valueExdr.expandString(context);
326             String JavaDoc format = this.formatExdr.expandString(context);
327             
328             Object JavaDoc fieldVal = this.fieldAcsr.get(context);
329             
330             // always use an empty string by default
331
if (fieldVal == null) {
332                 fieldVal = "";
333             }
334
335             List JavaDoc messages = new LinkedList JavaDoc();
336             Boolean JavaDoc resultBool = BaseCompare.doRealCompare(fieldVal, value, operator, type, format, messages, null, null);
337             if (messages.size() > 0) {
338                 messages.add(0, "Error with comparison in if-compare between field [" + fieldAcsr.toString() + "] with value [" + fieldVal + "] and value [" + value + "] with operator [" + operator + "] and type [" + type + "]: ");
339
340                 StringBuffer JavaDoc fullString = new StringBuffer JavaDoc();
341                 Iterator JavaDoc miter = messages.iterator();
342                 while (miter.hasNext()) {
343                     fullString.append((String JavaDoc) miter.next());
344                 }
345                 Debug.logWarning(fullString.toString(), module);
346
347                 throw new IllegalArgumentException JavaDoc(fullString.toString());
348             }
349             
350             return resultBool.booleanValue();
351         }
352     }
353     
354     public static class IfCompareField extends TreeCondition {
355         protected FlexibleMapAccessor fieldAcsr;
356         protected FlexibleMapAccessor toFieldAcsr;
357
358         protected String JavaDoc operator;
359         protected String JavaDoc type;
360         protected FlexibleStringExpander formatExdr;
361         
362         public IfCompareField(ModelTree modelTree, Element JavaDoc condElement) {
363             super (modelTree, condElement);
364             this.fieldAcsr = new FlexibleMapAccessor(condElement.getAttribute("field-name"));
365             this.toFieldAcsr = new FlexibleMapAccessor(condElement.getAttribute("to-field-name"));
366             
367             this.operator = condElement.getAttribute("operator");
368             this.type = condElement.getAttribute("type");
369
370             this.formatExdr = new FlexibleStringExpander(condElement.getAttribute("format"));
371         }
372         
373         public boolean eval(Map JavaDoc context) {
374             String JavaDoc format = this.formatExdr.expandString(context);
375             
376             Object JavaDoc fieldVal = this.fieldAcsr.get(context);
377             Object JavaDoc toFieldVal = this.toFieldAcsr.get(context);
378             
379             // always use an empty string by default
380
if (fieldVal == null) {
381                 fieldVal = "";
382             }
383
384             List JavaDoc messages = new LinkedList JavaDoc();
385             Boolean JavaDoc resultBool = BaseCompare.doRealCompare(fieldVal, toFieldVal, operator, type, format, messages, null, null);
386             if (messages.size() > 0) {
387                 messages.add(0, "Error with comparison in if-compare-field between field [" + fieldAcsr.toString() + "] with value [" + fieldVal + "] and to-field [" + toFieldVal.toString() + "] with value [" + toFieldVal + "] with operator [" + operator + "] and type [" + type + "]: ");
388
389                 StringBuffer JavaDoc fullString = new StringBuffer JavaDoc();
390                 Iterator JavaDoc miter = messages.iterator();
391                 while (miter.hasNext()) {
392                     fullString.append((String JavaDoc) miter.next());
393                 }
394                 Debug.logWarning(fullString.toString(), module);
395
396                 throw new IllegalArgumentException JavaDoc(fullString.toString());
397             }
398             
399             return resultBool.booleanValue();
400         }
401     }
402     
403     public static class IfRegexp extends TreeCondition {
404         static PatternMatcher matcher = new Perl5Matcher();
405         static PatternCompiler compiler = new Perl5Compiler();
406
407         protected FlexibleMapAccessor fieldAcsr;
408         protected FlexibleStringExpander exprExdr;
409         
410         public IfRegexp(ModelTree modelTree, Element JavaDoc condElement) {
411             super (modelTree, condElement);
412             this.fieldAcsr = new FlexibleMapAccessor(condElement.getAttribute("field-name"));
413             this.exprExdr = new FlexibleStringExpander(condElement.getAttribute("expr"));
414         }
415         
416         public boolean eval(Map JavaDoc context) {
417             Object JavaDoc fieldVal = this.fieldAcsr.get(context);
418             String JavaDoc expr = this.exprExdr.expandString(context);
419             Pattern pattern = null;
420             try {
421                 pattern = compiler.compile(expr);
422             } catch (MalformedPatternException e) {
423                 String JavaDoc errMsg = "Error in evaluation in if-regexp in screen: " + e.toString();
424                 Debug.logError(e, errMsg, module);
425                 throw new IllegalArgumentException JavaDoc(errMsg);
426             }
427
428             String JavaDoc fieldString = null;
429             try {
430                 fieldString = (String JavaDoc) ObjectType.simpleTypeConvert(fieldVal, "String", null, null);
431             } catch (GeneralException e) {
432                 Debug.logError(e, "Could not convert object to String, using empty String", module);
433             }
434             // always use an empty string by default
435
if (fieldString == null) fieldString = "";
436     
437             return matcher.matches(fieldString, pattern);
438         }
439     }
440     
441     public static class IfEmpty extends TreeCondition {
442         protected FlexibleMapAccessor fieldAcsr;
443         
444         public IfEmpty(ModelTree modelTree, Element JavaDoc condElement) {
445             super (modelTree, condElement);
446             this.fieldAcsr = new FlexibleMapAccessor(condElement.getAttribute("field-name"));
447         }
448         
449         public boolean eval(Map JavaDoc context) {
450             Object JavaDoc fieldVal = this.fieldAcsr.get(context);
451             return ObjectType.isEmpty(fieldVal);
452         }
453     }
454     public static class IfEntityPermission extends TreeCondition {
455         protected EntityPermissionChecker permissionChecker;
456         
457         public IfEntityPermission(ModelTree modelTree, Element JavaDoc condElement) {
458             super (modelTree, condElement);
459             this.permissionChecker = new EntityPermissionChecker(condElement);
460         }
461         
462         public boolean eval(Map JavaDoc context) {
463             
464             boolean passed = permissionChecker.runPermissionCheck(context);
465             return passed;
466         }
467     }
468 }
469
470
Popular Tags