001: /*
002: * $Id: EntityFunction.java,v 1.1 2003/11/05 12:08:00 jonesde Exp $
003: *
004: * Copyright (c) 2002 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: */
024:
025: package org.ofbiz.entity.condition;
026:
027: import java.util.List;
028:
029: import org.ofbiz.entity.GenericEntity;
030: import org.ofbiz.entity.GenericModelException;
031: import org.ofbiz.entity.model.ModelEntity;
032: import org.ofbiz.entity.model.ModelField;
033:
034: /**
035: * Encapsulates operations between entities and entity fields. This is a immutable class.
036: *
037: *@author <a href='mailto:chris_maurer@altavista.com'>Chris Maurer</a>
038: *@author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
039: *@author <a href="mailto:jaz@jflow.net">Andy Zeneski</a>
040: *@created Nov 5, 2001
041: *@version 1.0
042: */
043: public abstract class EntityFunction extends EntityCondition {
044:
045: public static final int ID_LENGTH = 1;
046: public static final int ID_TRIM = 2;
047: public static final int ID_UPPER = 3;
048: public static final int ID_LOWER = 4;
049:
050: public static class LENGTH extends EntityFunction {
051: public LENGTH(EntityFunction nested) {
052: super (ID_LENGTH, "LENGTH", nested);
053: }
054:
055: public LENGTH(Object value, boolean asEntity) {
056: super (ID_LENGTH, "LENGTH", value, asEntity);
057: }
058:
059: public Object doEval(Object value) {
060: return new Integer(value.toString().length());
061: }
062: };
063:
064: public static class TRIM extends EntityFunction {
065: public TRIM(EntityFunction nested) {
066: super (ID_TRIM, "TRIM", nested);
067: }
068:
069: public TRIM(Object value, boolean asEntity) {
070: super (ID_TRIM, "TRIM", value, asEntity);
071: }
072:
073: public Object doEval(Object value) {
074: return value.toString().trim();
075: }
076: };
077:
078: public static class UPPER extends EntityFunction {
079: public UPPER(EntityFunction nested) {
080: super (ID_UPPER, "UPPER", nested);
081: }
082:
083: public UPPER(Object value, boolean asEntity) {
084: super (ID_UPPER, "UPPER", value, asEntity);
085: }
086:
087: public Object doEval(Object value) {
088: return new Integer(value.toString().toUpperCase());
089: }
090: };
091:
092: public static class LOWER extends EntityFunction {
093: public LOWER(EntityFunction nested) {
094: super (ID_LOWER, "LOWER", nested);
095: }
096:
097: public LOWER(Object value, boolean asEntity) {
098: super (ID_LOWER, "LOWER", value, asEntity);
099: }
100:
101: public Object doEval(Object value) {
102: return new Integer(value.toString().toLowerCase());
103: }
104: };
105:
106: protected int idInt;
107: protected String codeString;
108: protected EntityFunction nested;
109: protected Object value;
110: protected boolean asEntity;
111:
112: protected EntityFunction(int id, String code, EntityFunction nested) {
113: idInt = id;
114: codeString = code;
115: this .nested = nested;
116: }
117:
118: protected EntityFunction(int id, String code, Object value,
119: boolean asEntity) {
120: idInt = id;
121: codeString = code;
122: this .value = value;
123: this .asEntity = asEntity;
124: }
125:
126: public String getCode() {
127: if (codeString == null)
128: return "null";
129: else
130: return codeString;
131: }
132:
133: public int getId() {
134: return idInt;
135: }
136:
137: public boolean equals(Object obj) {
138: EntityFunction otherFunc = (EntityFunction) obj;
139: return this .idInt == otherFunc.idInt
140: && (this .nested != null ? nested
141: .equals(otherFunc.nested)
142: : otherFunc.nested != null)
143: && (this .value != null ? value.equals(otherFunc.value)
144: : otherFunc.value != null)
145: && this .asEntity == otherFunc.asEntity;
146: }
147:
148: protected abstract Object doEval(Object value);
149:
150: public String makeWhereString(ModelEntity modelEntity,
151: List entityConditionParams) {
152: StringBuffer sb = new StringBuffer();
153: sb.append(codeString).append('(');
154: if (nested != null) {
155: sb.append(nested.makeWhereString(modelEntity,
156: entityConditionParams));
157: } else {
158: if (asEntity) {
159: ModelField field = getField(modelEntity, (String) value);
160: String colName = getColName(field, (String) value);
161: sb.append(colName);
162: } else {
163: addValue(sb, null, value, entityConditionParams);
164: }
165: }
166: sb.append(')');
167: return sb.toString();
168: }
169:
170: public void checkCondition(ModelEntity modelEntity)
171: throws GenericModelException {
172: if (nested != null) {
173: nested.checkCondition(modelEntity);
174: }
175: }
176:
177: public Object eval(GenericEntity entity) {
178: if (nested != null) {
179: return doEval(nested.eval(entity));
180: } else {
181: if (asEntity) {
182: return doEval(entity.get(value.toString()));
183: } else {
184: return doEval(value);
185: }
186: }
187: }
188:
189: public Object eval(Object value) {
190: if (nested != null) {
191: return doEval(nested.eval(value));
192: } else if (value != null) {
193: return doEval(value);
194: } else {
195: return null;
196: }
197: }
198:
199: public boolean entityMatches(GenericEntity entity) {
200: Object result = eval(entity);
201: if (result == null)
202: return false;
203: if (result instanceof Boolean)
204: return ((Boolean) result).booleanValue();
205: return result.toString().length() > 0;
206: }
207: }
|