KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: EntityJoinOperator.java 5462 2005-08-05 18:35:48Z 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.ArrayList JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Map JavaDoc;
31
32 import org.ofbiz.entity.GenericDelegator;
33 import org.ofbiz.entity.GenericEntity;
34 import org.ofbiz.entity.GenericModelException;
35 import org.ofbiz.entity.model.ModelEntity;
36
37 /**
38  * Encapsulates operations between entities and entity fields. This is a immutable class.
39  *
40  *@author <a HREF='mailto:chris_maurer@altavista.com'>Chris Maurer</a>
41  *@author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
42  *@author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
43  *@since 1.0
44  *@version $Rev: 5462 $
45  */

46 public class EntityJoinOperator extends EntityOperator {
47
48     protected boolean shortCircuitValue;
49
50     protected EntityJoinOperator(int id, String JavaDoc code, boolean shortCircuitValue) {
51         super(id, code);
52         this.shortCircuitValue = shortCircuitValue;
53     }
54
55     public void addSqlValue(StringBuffer JavaDoc sql, ModelEntity modelEntity, List JavaDoc entityConditionParams, boolean compat, Object JavaDoc lhs, Object JavaDoc rhs) {
56         sql.append('(');
57         sql.append(((EntityCondition) lhs).makeWhereString(modelEntity, entityConditionParams));
58         sql.append(' ');
59         sql.append(getCode());
60         sql.append(' ');
61         if (rhs instanceof EntityCondition) {
62             sql.append(((EntityCondition) rhs).makeWhereString(modelEntity, entityConditionParams));
63         } else {
64             addValue(sql, null, rhs, entityConditionParams);
65         }
66         sql.append(')');
67     }
68
69     public void addSqlValue(StringBuffer JavaDoc sql, ModelEntity modelEntity, List JavaDoc entityConditionParams, List JavaDoc conditionList) {
70         if (conditionList != null && conditionList.size() > 0) {
71             sql.append('(');
72             Iterator JavaDoc conditionIter = conditionList.iterator();
73             while (conditionIter.hasNext()) {
74                 EntityCondition condition = (EntityCondition) conditionIter.next();
75                 sql.append(condition.makeWhereString(modelEntity, entityConditionParams));
76                  if (conditionIter.hasNext()) {
77                      sql.append(' ');
78                      sql.append(getCode());
79                      sql.append(' ');
80                  }
81             }
82             sql.append(')');
83         }
84     }
85
86     protected EntityCondition freeze(Object JavaDoc item) {
87         return ((EntityCondition) item).freeze();
88     }
89
90     public EntityCondition freeze(Object JavaDoc lhs, Object JavaDoc rhs) {
91         return new EntityExpr(freeze(lhs), this, freeze(rhs));
92     }
93
94     public EntityCondition freeze(List JavaDoc conditionList) {
95         List JavaDoc newList = new ArrayList JavaDoc(conditionList.size());
96         for (int i = 0; i < conditionList.size(); i++) {
97             EntityCondition condition = (EntityCondition) conditionList.get(i);
98             newList.add(condition.freeze());
99         }
100         return new EntityConditionList(newList, this);
101     }
102
103     public void visit(EntityConditionVisitor visitor, List JavaDoc conditionList) {
104         if (conditionList != null && conditionList.size() > 0) {
105             for (int i = 0; i < conditionList.size(); i++) {
106                 visitor.visit(conditionList.get(i));
107             }
108         }
109     }
110
111     public void visit(EntityConditionVisitor visitor, Object JavaDoc lhs, Object JavaDoc rhs) {
112         ((EntityCondition) lhs).visit(visitor);
113         visitor.visit(rhs);
114     }
115
116     public boolean entityMatches(GenericEntity entity, Object JavaDoc lhs, Object JavaDoc rhs) {
117         return entityMatches(entity, (EntityCondition) lhs, (EntityCondition) rhs);
118     }
119
120     public Object JavaDoc eval(GenericEntity entity, EntityCondition lhs, EntityCondition rhs) {
121         return entityMatches(entity, lhs, rhs) ? Boolean.TRUE : Boolean.FALSE;
122     }
123
124     public boolean entityMatches(GenericEntity entity, EntityCondition lhs, EntityCondition rhs) {
125         if (lhs.entityMatches(entity)) return shortCircuitValue;
126         if (rhs.entityMatches(entity)) return shortCircuitValue;
127         return !shortCircuitValue;
128     }
129
130     public boolean entityMatches(GenericEntity entity, List JavaDoc conditionList) {
131         return mapMatches(entity.getDelegator(), entity, conditionList);
132     }
133
134     public Object JavaDoc eval(GenericDelegator delegator, Map JavaDoc map, Object JavaDoc lhs, Object JavaDoc rhs) {
135         return castBoolean(mapMatches(delegator, map, lhs, rhs));
136     }
137
138     public boolean mapMatches(GenericDelegator delegator, Map JavaDoc map, Object JavaDoc lhs, Object JavaDoc rhs) {
139         if (((EntityCondition) lhs).mapMatches(delegator, map)) return shortCircuitValue;
140         if (((EntityCondition) rhs).mapMatches(delegator, map)) return shortCircuitValue;
141         return !shortCircuitValue;
142     }
143
144     public Object JavaDoc eval(GenericDelegator delegator, Map JavaDoc map, List JavaDoc conditionList) {
145         return castBoolean(mapMatches(delegator, map, conditionList));
146     }
147
148     public boolean mapMatches(GenericDelegator delegator, Map JavaDoc map, List JavaDoc conditionList) {
149         if (conditionList != null && conditionList.size() > 0) {
150             for (int i = 0; i < conditionList.size(); i++) {
151                 EntityCondition condition = (EntityCondition) conditionList.get(i);
152                 if (condition.mapMatches(delegator, map) == shortCircuitValue) return shortCircuitValue;
153             }
154         }
155         return !shortCircuitValue;
156     }
157
158     public void validateSql(ModelEntity modelEntity, Object JavaDoc lhs, Object JavaDoc rhs) throws GenericModelException {
159         validateSql(modelEntity, (EntityCondition) lhs, (EntityCondition) rhs);
160     }
161
162     public void validateSql(ModelEntity modelEntity, EntityCondition lhs, EntityCondition rhs) throws GenericModelException {
163         lhs.checkCondition(modelEntity);
164         rhs.checkCondition(modelEntity);
165     }
166
167     public void validateSql(ModelEntity modelEntity, List JavaDoc conditionList) throws GenericModelException {
168         if (conditionList == null && conditionList.size() == 0)
169             throw new GenericModelException("Empty list for joining");
170         for (int i = 0; i < conditionList.size(); i++) {
171             Object JavaDoc condObj = conditionList.get(i);
172             if (!(condObj instanceof EntityCondition)) {
173                 throw new GenericModelException("Object is not a valid EntityCondition [" + condObj.getClass().getName() + "]");
174             }
175             EntityCondition condition = (EntityCondition) condObj;
176             condition.checkCondition(modelEntity);
177         }
178     }
179 }
180
Popular Tags