001: /*******************************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *******************************************************************************/package org.ofbiz.entity.condition;
019:
020: import java.util.ArrayList;
021: import java.util.Iterator;
022: import java.util.List;
023: import java.util.Map;
024:
025: import org.ofbiz.entity.GenericDelegator;
026: import org.ofbiz.entity.GenericEntity;
027: import org.ofbiz.entity.GenericModelException;
028: import org.ofbiz.entity.config.DatasourceInfo;
029: import org.ofbiz.entity.model.ModelEntity;
030:
031: /**
032: * Encapsulates operations between entities and entity fields. This is a immutable class.
033: *
034: */
035: public class EntityJoinOperator extends EntityOperator {
036:
037: protected boolean shortCircuitValue;
038:
039: protected EntityJoinOperator(int id, String code,
040: boolean shortCircuitValue) {
041: super (id, code);
042: this .shortCircuitValue = shortCircuitValue;
043: }
044:
045: public void addSqlValue(StringBuffer sql, ModelEntity modelEntity,
046: List entityConditionParams, boolean compat, Object lhs,
047: Object rhs, DatasourceInfo datasourceInfo) {
048: sql.append('(');
049: sql.append(((EntityCondition) lhs).makeWhereString(modelEntity,
050: entityConditionParams, datasourceInfo));
051: sql.append(' ');
052: sql.append(getCode());
053: sql.append(' ');
054: if (rhs instanceof EntityCondition) {
055: sql
056: .append(((EntityCondition) rhs).makeWhereString(
057: modelEntity, entityConditionParams,
058: datasourceInfo));
059: } else {
060: addValue(sql, null, rhs, entityConditionParams);
061: }
062: sql.append(')');
063: }
064:
065: public void addSqlValue(StringBuffer sql, ModelEntity modelEntity,
066: List entityConditionParams, List conditionList,
067: DatasourceInfo datasourceInfo) {
068: if (conditionList != null && conditionList.size() > 0) {
069: sql.append('(');
070: Iterator conditionIter = conditionList.iterator();
071: while (conditionIter.hasNext()) {
072: EntityCondition condition = (EntityCondition) conditionIter
073: .next();
074: sql.append(condition.makeWhereString(modelEntity,
075: entityConditionParams, datasourceInfo));
076: if (conditionIter.hasNext()) {
077: sql.append(' ');
078: sql.append(getCode());
079: sql.append(' ');
080: }
081: }
082: sql.append(')');
083: }
084: }
085:
086: protected EntityCondition freeze(Object item) {
087: return ((EntityCondition) item).freeze();
088: }
089:
090: public EntityCondition freeze(Object lhs, Object rhs) {
091: return new EntityExpr(freeze(lhs), this , freeze(rhs));
092: }
093:
094: public EntityCondition freeze(List conditionList) {
095: List newList = new ArrayList(conditionList.size());
096: for (int i = 0; i < conditionList.size(); i++) {
097: EntityCondition condition = (EntityCondition) conditionList
098: .get(i);
099: newList.add(condition.freeze());
100: }
101: return new EntityConditionList(newList, this );
102: }
103:
104: public void visit(EntityConditionVisitor visitor, List conditionList) {
105: if (conditionList != null && conditionList.size() > 0) {
106: for (int i = 0; i < conditionList.size(); i++) {
107: visitor.visit(conditionList.get(i));
108: }
109: }
110: }
111:
112: public void visit(EntityConditionVisitor visitor, Object lhs,
113: Object rhs) {
114: ((EntityCondition) lhs).visit(visitor);
115: visitor.visit(rhs);
116: }
117:
118: public boolean entityMatches(GenericEntity entity, Object lhs,
119: Object rhs) {
120: return entityMatches(entity, (EntityCondition) lhs,
121: (EntityCondition) rhs);
122: }
123:
124: public Object eval(GenericEntity entity, EntityCondition lhs,
125: EntityCondition rhs) {
126: return entityMatches(entity, lhs, rhs) ? Boolean.TRUE
127: : Boolean.FALSE;
128: }
129:
130: public boolean entityMatches(GenericEntity entity,
131: EntityCondition lhs, EntityCondition rhs) {
132: if (lhs.entityMatches(entity))
133: return shortCircuitValue;
134: if (rhs.entityMatches(entity))
135: return shortCircuitValue;
136: return !shortCircuitValue;
137: }
138:
139: public boolean entityMatches(GenericEntity entity,
140: List conditionList) {
141: return mapMatches(entity.getDelegator(), entity, conditionList);
142: }
143:
144: public Object eval(GenericDelegator delegator, Map map, Object lhs,
145: Object rhs) {
146: return castBoolean(mapMatches(delegator, map, lhs, rhs));
147: }
148:
149: public boolean mapMatches(GenericDelegator delegator, Map map,
150: Object lhs, Object rhs) {
151: if (((EntityCondition) lhs).mapMatches(delegator, map))
152: return shortCircuitValue;
153: if (((EntityCondition) rhs).mapMatches(delegator, map))
154: return shortCircuitValue;
155: return !shortCircuitValue;
156: }
157:
158: public Object eval(GenericDelegator delegator, Map map,
159: List conditionList) {
160: return castBoolean(mapMatches(delegator, map, conditionList));
161: }
162:
163: public boolean mapMatches(GenericDelegator delegator, Map map,
164: List conditionList) {
165: if (conditionList != null && conditionList.size() > 0) {
166: for (int i = 0; i < conditionList.size(); i++) {
167: EntityCondition condition = (EntityCondition) conditionList
168: .get(i);
169: if (condition.mapMatches(delegator, map) == shortCircuitValue)
170: return shortCircuitValue;
171: }
172: }
173: return !shortCircuitValue;
174: }
175:
176: public void validateSql(ModelEntity modelEntity, Object lhs,
177: Object rhs) throws GenericModelException {
178: validateSql(modelEntity, (EntityCondition) lhs,
179: (EntityCondition) rhs);
180: }
181:
182: public void validateSql(ModelEntity modelEntity,
183: EntityCondition lhs, EntityCondition rhs)
184: throws GenericModelException {
185: lhs.checkCondition(modelEntity);
186: rhs.checkCondition(modelEntity);
187: }
188:
189: public void validateSql(ModelEntity modelEntity, List conditionList)
190: throws GenericModelException {
191: if (conditionList == null) {
192: throw new GenericModelException("Condition list is null");
193: }
194: for (int i = 0; i < conditionList.size(); i++) {
195: Object condObj = conditionList.get(i);
196: if (!(condObj instanceof EntityCondition)) {
197: throw new GenericModelException(
198: "Object is not a valid EntityCondition ["
199: + condObj.getClass().getName() + "]");
200: }
201: EntityCondition condition = (EntityCondition) condObj;
202: condition.checkCondition(modelEntity);
203: }
204: }
205: }
|