KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > entityext > eca > EntityEcaCondition


1 /*
2  * $Id: EntityEcaCondition.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2002-2003 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.entityext.eca;
26
27 import java.util.Iterator JavaDoc;
28 import java.util.LinkedList JavaDoc;
29 import java.util.List JavaDoc;
30
31 import org.ofbiz.base.util.Debug;
32 import org.ofbiz.base.util.ObjectType;
33 import org.ofbiz.entity.GenericEntity;
34 import org.ofbiz.entity.GenericEntityException;
35 import org.ofbiz.service.DispatchContext;
36 import org.w3c.dom.Element JavaDoc;
37
38 /**
39  * EntityEcaCondition
40  *
41  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
42  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
43  * @version $Rev: 5462 $
44  * @since 2.1
45  */

46 public class EntityEcaCondition implements java.io.Serializable JavaDoc {
47     
48     public static final String JavaDoc module = EntityEcaCondition.class.getName();
49
50     protected String JavaDoc lhsValueName, rhsValueName;
51     protected String JavaDoc operator;
52     protected String JavaDoc compareType;
53     protected String JavaDoc format;
54     protected boolean constant = false;
55
56     protected EntityEcaCondition() {}
57
58     public EntityEcaCondition(Element JavaDoc condition, boolean constant) {
59         this.lhsValueName = condition.getAttribute("field-name");
60
61         this.constant = constant;
62         if (constant) {
63             this.rhsValueName = condition.getAttribute("value");
64         } else {
65             this.rhsValueName = condition.getAttribute("to-field-name");
66         }
67
68         this.operator = condition.getAttribute("operator");
69         this.compareType = condition.getAttribute("type");
70         this.format = condition.getAttribute("format");
71
72         if (lhsValueName == null)
73             lhsValueName = "";
74         if (rhsValueName == null)
75             rhsValueName = "";
76     }
77
78     public boolean eval(DispatchContext dctx, GenericEntity value) throws GenericEntityException {
79         if (dctx == null || value == null || dctx.getClassLoader() == null) {
80             throw new GenericEntityException("Cannot have null Value or DispatchContext!");
81         }
82
83         if (Debug.verboseOn()) Debug.logVerbose(this.toString(), module);
84
85         Object JavaDoc lhsValue = value.get(lhsValueName);
86
87         Object JavaDoc rhsValue;
88         if (constant) {
89             rhsValue = rhsValueName;
90         } else {
91             rhsValue = value.get(rhsValueName);
92         }
93
94         if (Debug.verboseOn()) Debug.logVerbose("Comparing : " + lhsValue + " " + operator + " " + rhsValue, module);
95
96         // evaluate the condition & invoke the action(s)
97
List JavaDoc messages = new LinkedList JavaDoc();
98         Boolean JavaDoc cond = ObjectType.doRealCompare(lhsValue, rhsValue, operator, compareType, format, messages, null, dctx.getClassLoader());
99
100         // if any messages were returned send them out
101
if (messages.size() > 0) {
102             Iterator JavaDoc m = messages.iterator();
103             while (m.hasNext()) {
104                 Debug.logWarning((String JavaDoc) m.next(), module);
105             }
106         }
107         if (cond != null) {
108             return cond.booleanValue();
109         } else {
110             return false;
111         }
112     }
113
114     public String JavaDoc toString() {
115         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
116         buf.append("[" + lhsValueName + "]");
117         buf.append("[" + operator + "]");
118         buf.append("[" + rhsValueName + "]");
119         buf.append("[" + constant + "]");
120         buf.append("[" + compareType + "]");
121         buf.append("[" + format + "]");
122         return buf.toString();
123     }
124 }
125
Popular Tags