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.List;
021: import java.util.Map;
022:
023: import org.ofbiz.entity.GenericDelegator;
024: import org.ofbiz.entity.GenericModelException;
025: import org.ofbiz.entity.config.DatasourceInfo;
026: import org.ofbiz.entity.model.ModelEntity;
027: import org.ofbiz.entity.model.ModelField;
028:
029: /**
030: * Encapsulates operations between entities and entity fields. This is a immutable class.
031: *
032: */
033: public abstract class EntityFunction extends EntityConditionValue {
034:
035: public static interface Fetcher {
036: Object getValue(Object value);
037: }
038:
039: public static final int ID_LENGTH = 1;
040: public static final int ID_TRIM = 2;
041: public static final int ID_UPPER = 3;
042: public static final int ID_LOWER = 4;
043:
044: public static class LENGTH extends EntityFunction {
045: public static Fetcher FETCHER = new Fetcher() {
046: public Object getValue(Object value) {
047: return new Integer(value.toString().length());
048: }
049: };
050:
051: public LENGTH(EntityConditionValue nested) {
052: super (FETCHER, ID_LENGTH, "LENGTH", nested);
053: }
054:
055: public LENGTH(Object value) {
056: super (FETCHER, ID_LENGTH, "LENGTH", value);
057: }
058: };
059:
060: public static class TRIM extends EntityFunction {
061: public static Fetcher FETCHER = new Fetcher() {
062: public Object getValue(Object value) {
063: return value.toString().trim();
064: }
065: };
066:
067: public TRIM(EntityConditionValue nested) {
068: super (FETCHER, ID_TRIM, "TRIM", nested);
069: }
070:
071: public TRIM(Object value) {
072: super (FETCHER, ID_TRIM, "TRIM", value);
073: }
074: };
075:
076: public static class UPPER extends EntityFunction {
077: public static Fetcher FETCHER = new Fetcher() {
078: public Object getValue(Object value) {
079: return value.toString().toUpperCase();
080: }
081: };
082:
083: public UPPER(EntityConditionValue nested) {
084: super (FETCHER, ID_UPPER, "UPPER", nested);
085: }
086:
087: public UPPER(Object value) {
088: super (FETCHER, ID_UPPER, "UPPER", value);
089: }
090: };
091:
092: public static class LOWER extends EntityFunction {
093: public static Fetcher FETCHER = new Fetcher() {
094: public Object getValue(Object value) {
095: return value.toString().toLowerCase();
096: }
097: };
098:
099: public LOWER(EntityConditionValue nested) {
100: super (FETCHER, ID_LOWER, "LOWER", nested);
101: }
102:
103: public LOWER(Object value) {
104: super (FETCHER, ID_LOWER, "LOWER", value);
105: }
106: };
107:
108: protected int idInt;
109: protected String codeString;
110: protected EntityConditionValue nested;
111: protected Object value;
112: protected Fetcher fetcher;
113:
114: protected EntityFunction(Fetcher fetcher, int id, String code,
115: EntityConditionValue nested) {
116: this .fetcher = fetcher;
117: idInt = id;
118: codeString = code;
119: this .nested = nested;
120: }
121:
122: protected EntityFunction(Fetcher fetcher, int id, String code,
123: Object value) {
124: this .fetcher = fetcher;
125: idInt = id;
126: codeString = code;
127: if (value instanceof EntityConditionValue) {
128: this .nested = (EntityConditionValue) value;
129: } else if (value instanceof String) {
130: this .value = ((String) value).replaceAll("'", "''");
131: } else {
132: this .value = value;
133: }
134: }
135:
136: public EntityConditionValue freeze() {
137: if (nested != null) {
138: return new EntityFunction(fetcher, idInt, codeString,
139: nested.freeze()) {
140: };
141: } else {
142: return new EntityFunction(fetcher, idInt, codeString, value) {
143: };
144: }
145: }
146:
147: public String getCode() {
148: if (codeString == null)
149: return "null";
150: else
151: return codeString;
152: }
153:
154: public int getId() {
155: return idInt;
156: }
157:
158: public int hashCode() {
159: return codeString.hashCode();
160: }
161:
162: public boolean equals(Object obj) {
163: if (!(obj instanceof EntityFunction))
164: return false;
165: EntityFunction otherFunc = (EntityFunction) obj;
166: return (this .idInt == otherFunc.idInt
167: && (this .nested != null ? nested
168: .equals(otherFunc.nested)
169: : otherFunc.nested == null) && (this .value != null ? value
170: .equals(otherFunc.value)
171: : otherFunc.value == null));
172: }
173:
174: public void addSqlValue(StringBuffer sql, Map tableAliases,
175: ModelEntity modelEntity, List entityConditionParams,
176: boolean includeTableNamePrefix,
177: DatasourceInfo datasourceinfo) {
178: sql.append(codeString).append('(');
179: if (nested != null) {
180: nested.addSqlValue(sql, tableAliases, modelEntity,
181: entityConditionParams, includeTableNamePrefix,
182: datasourceinfo);
183: } else {
184: addValue(sql, null, value, entityConditionParams);
185: }
186: sql.append(')');
187: }
188:
189: public void visit(EntityConditionVisitor visitor) {
190: if (nested != null) {
191: visitor.acceptEntityConditionValue(nested);
192: } else {
193: visitor.acceptObject(value);
194: }
195: }
196:
197: public void accept(EntityConditionVisitor visitor) {
198: visitor.acceptEntityFunction(this );
199: }
200:
201: public ModelField getModelField(ModelEntity modelEntity) {
202: if (nested != null) {
203: return nested.getModelField(modelEntity);
204: }
205: return null;
206: }
207:
208: public void validateSql(ModelEntity modelEntity)
209: throws GenericModelException {
210: if (nested != null) {
211: nested.validateSql(modelEntity);
212: }
213: }
214:
215: public Object getValue(GenericDelegator delegator, Map map) {
216: Object value = nested != null ? nested.getValue(delegator, map)
217: : this.value;
218: return value != null ? fetcher.getValue(value) : null;
219: }
220: }
|