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.Collection;
021: import java.util.List;
022: import java.util.Map;
023:
024: import org.ofbiz.base.util.Debug;
025: import org.ofbiz.entity.EntityCryptoException;
026: import org.ofbiz.entity.GenericDelegator;
027: import org.ofbiz.entity.GenericEntity;
028: import org.ofbiz.entity.GenericModelException;
029: import org.ofbiz.entity.config.DatasourceInfo;
030: import org.ofbiz.entity.model.ModelEntity;
031: import org.ofbiz.entity.model.ModelField;
032:
033: /**
034: * Encapsulates simple expressions used for specifying queries
035: *
036: */
037: public class EntityExpr extends EntityCondition {
038: public static final String module = EntityExpr.class.getName();
039:
040: private Object lhs;
041: private EntityOperator operator;
042: private Object rhs;
043:
044: protected EntityExpr() {
045: }
046:
047: public EntityExpr(Object lhs, EntityComparisonOperator operator,
048: Object rhs) {
049: if (lhs == null) {
050: throw new IllegalArgumentException(
051: "The field value cannot be null");
052: }
053: if (lhs instanceof String) {
054: Debug
055: .logError(
056: new Exception(),
057: "EntityExpr called with lhs as a String; consider recompiling",
058: module);
059: }
060: if (operator == null) {
061: throw new IllegalArgumentException(
062: "The operator argument cannot be null");
063: }
064:
065: if (rhs == null || rhs == GenericEntity.NULL_FIELD) {
066: if (!EntityOperator.NOT_EQUAL.equals(operator)
067: && !EntityOperator.EQUALS.equals(operator)) {
068: throw new IllegalArgumentException(
069: "Operator must be EQUALS or NOT_EQUAL when right/rhs argument is NULL ");
070: }
071: }
072:
073: if (EntityOperator.BETWEEN.equals(operator)) {
074: if (!(rhs instanceof Collection)
075: || (((Collection) rhs).size() != 2)) {
076: throw new IllegalArgumentException(
077: "BETWEEN Operator requires a Collection with 2 elements for the right/rhs argument");
078: }
079: }
080:
081: this .lhs = lhs;
082: this .operator = operator;
083: this .rhs = rhs;
084:
085: //Debug.logInfo("new EntityExpr internal field=" + lhs + ", value=" + rhs + ", value type=" + (rhs == null ? "null object" : rhs.getClass().getName()), module);
086: }
087:
088: public EntityExpr(String lhs, EntityComparisonOperator operator,
089: Object rhs) {
090: this (new EntityFieldValue(lhs), operator, rhs);
091: //Debug.logInfo("new EntityExpr field=" + lhs + ", value=" + rhs + ", value type=" + (rhs == null ? "null object" : rhs.getClass().getName()), module);
092: }
093:
094: public EntityExpr(String lhs, boolean leftUpper,
095: EntityComparisonOperator operator, Object rhs,
096: boolean rightUpper) {
097: if (lhs == null) {
098: throw new IllegalArgumentException(
099: "The field value cannot be null");
100: }
101: if (operator == null) {
102: throw new IllegalArgumentException(
103: "The operator argument cannot be null");
104: }
105: this .lhs = new EntityFieldValue(lhs);
106: if (leftUpper)
107: this .lhs = new EntityFunction.UPPER(this .lhs);
108: this .operator = operator;
109: if (rhs instanceof EntityConditionValue) {
110: if (rightUpper)
111: rhs = new EntityFunction.UPPER(
112: (EntityConditionValue) rhs);
113: this .rhs = rhs;
114: } else {
115: if (rightUpper)
116: rhs = new EntityFunction.UPPER(rhs);
117: this .rhs = rhs;
118: }
119: }
120:
121: public EntityExpr(EntityCondition lhs, EntityJoinOperator operator,
122: EntityCondition rhs) {
123: if (lhs == null) {
124: throw new IllegalArgumentException(
125: "The left EntityCondition argument cannot be null");
126: }
127: if (rhs == null) {
128: throw new IllegalArgumentException(
129: "The right EntityCondition argument cannot be null");
130: }
131: if (operator == null) {
132: throw new IllegalArgumentException(
133: "The operator argument cannot be null");
134: }
135:
136: this .lhs = lhs;
137: this .operator = operator;
138: this .rhs = rhs;
139: }
140:
141: /** @deprecated */
142: public void setLUpper(boolean upper) {
143: }
144:
145: /** @deprecated */
146: public boolean isLUpper() {
147: return lhs instanceof EntityFunction.UPPER;
148: }
149:
150: /** @deprecated */
151: public boolean isRUpper() {
152: return rhs instanceof EntityFunction.UPPER;
153: }
154:
155: /** @deprecated */
156: public void setRUpper(boolean upper) {
157: }
158:
159: public Object getLhs() {
160: return lhs;
161: }
162:
163: public EntityOperator getOperator() {
164: return operator;
165: }
166:
167: public Object getRhs() {
168: return rhs;
169: }
170:
171: public String makeWhereString(ModelEntity modelEntity,
172: List entityConditionParams, DatasourceInfo datasourceInfo) {
173: // if (Debug.verboseOn()) Debug.logVerbose("makeWhereString for entity " + modelEntity.getEntityName(), module);
174: StringBuffer sql = new StringBuffer();
175: operator.addSqlValue(sql, modelEntity, entityConditionParams,
176: true, lhs, rhs, datasourceInfo);
177: return sql.toString();
178: }
179:
180: public boolean mapMatches(GenericDelegator delegator, Map map) {
181: return operator.mapMatches(delegator, map, lhs, rhs);
182: }
183:
184: public void checkCondition(ModelEntity modelEntity)
185: throws GenericModelException {
186: // if (Debug.verboseOn()) Debug.logVerbose("checkCondition for entity " + modelEntity.getEntityName(), module);
187: if (lhs instanceof EntityCondition) {
188: ((EntityCondition) lhs).checkCondition(modelEntity);
189: ((EntityCondition) rhs).checkCondition(modelEntity);
190: }
191: }
192:
193: protected void addValue(StringBuffer buffer, ModelField field,
194: Object value, List params) {
195: if (rhs instanceof EntityFunction.UPPER) {
196: if (value instanceof String) {
197: value = ((String) value).toUpperCase();
198: }
199: }
200: super .addValue(buffer, field, value, params);
201: }
202:
203: public EntityCondition freeze() {
204: return operator.freeze(lhs, rhs);
205: }
206:
207: public void encryptConditionFields(ModelEntity modelEntity,
208: GenericDelegator delegator) {
209: if (this .lhs instanceof String) {
210: ModelField modelField = modelEntity
211: .getField((String) this .lhs);
212: if (modelField != null && modelField.getEncrypt()) {
213: if (!(rhs instanceof EntityConditionValue)) {
214: try {
215: this .rhs = delegator.encryptFieldValue(
216: modelEntity.getEntityName(), this .rhs);
217: } catch (EntityCryptoException e) {
218: Debug.logWarning(e, "Error encrypting field ["
219: + modelEntity.getEntityName() + "."
220: + modelField.getName()
221: + "] with value: " + this .rhs, module);
222: }
223: }
224: }
225: }
226: }
227:
228: public void visit(EntityConditionVisitor visitor) {
229: visitor.acceptEntityOperator(operator, lhs, rhs);
230: }
231:
232: public void accept(EntityConditionVisitor visitor) {
233: visitor.acceptEntityExpr(this );
234: }
235:
236: public boolean equals(Object obj) {
237: if (!(obj instanceof EntityExpr))
238: return false;
239: EntityExpr other = (EntityExpr) obj;
240: boolean isEqual = equals(lhs, other.lhs)
241: && equals(operator, other.operator)
242: && equals(rhs, other.rhs);
243: //if (!isEqual) {
244: // Debug.logWarning("EntityExpr.equals is false for: \n-this.lhs=" + this.lhs + "; other.lhs=" + other.lhs +
245: // "\nthis.operator=" + this.operator + "; other.operator=" + other.operator +
246: // "\nthis.rhs=" + this.rhs + "other.rhs=" + other.rhs, module);
247: //}
248: return isEqual;
249: }
250:
251: public int hashCode() {
252: return hashCode(lhs) + hashCode(operator) + hashCode(rhs);
253: }
254: }
|