KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > widget > menu > ModelMenuCondition


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