KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > widget > screen > ModelScreenCondition


1 /*
2  * $Id: ModelScreenCondition.java 7061 2006-03-24 04:37:21Z 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.screen;
25
26 import java.io.Serializable JavaDoc;
27 import java.lang.reflect.Method JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.LinkedList 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.base.util.GeneralException;
35 import org.ofbiz.base.util.ObjectType;
36 import org.ofbiz.base.util.UtilXml;
37 import org.ofbiz.base.util.collections.FlexibleMapAccessor;
38 import org.ofbiz.base.util.string.FlexibleStringExpander;
39 import org.ofbiz.entity.GenericValue;
40 import org.ofbiz.entityext.permission.EntityPermissionChecker;
41 import org.ofbiz.minilang.operation.BaseCompare;
42 import org.ofbiz.security.Security;
43
44 import org.apache.oro.text.regex.MalformedPatternException;
45 import org.apache.oro.text.regex.Pattern;
46 import org.apache.oro.text.regex.PatternCompiler;
47 import org.apache.oro.text.regex.PatternMatcher;
48 import org.apache.oro.text.regex.Perl5Compiler;
49 import org.apache.oro.text.regex.Perl5Matcher;
50 import org.w3c.dom.Element JavaDoc;
51
52 /**
53  * Widget Library - Screen model condition class
54  *
55  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
56  * @version $Rev: 7061 $
57  * @since 3.1
58  */

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