KickJava   Java API By Example, From Geeks To Geeks.

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


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

45 public abstract class EntityFunction extends EntityConditionValue {
46
47     public static interface Fetcher {
48         Object JavaDoc getValue(Object JavaDoc value);
49     }
50
51     public static final int ID_LENGTH = 1;
52     public static final int ID_TRIM = 2;
53     public static final int ID_UPPER = 3;
54     public static final int ID_LOWER = 4;
55
56     public static class LENGTH extends EntityFunction {
57         public static Fetcher FETCHER = new Fetcher() {
58             public Object JavaDoc getValue(Object JavaDoc value) { return new Integer JavaDoc(value.toString().length()); }
59         };
60         public LENGTH(EntityConditionValue nested) { super(FETCHER, ID_LENGTH, "LENGTH", nested); }
61         public LENGTH(Object JavaDoc value) { super(FETCHER, ID_LENGTH, "LENGTH", value); }
62     };
63
64     public static class TRIM extends EntityFunction {
65         public static Fetcher FETCHER = new Fetcher() {
66             public Object JavaDoc getValue(Object JavaDoc value) { return value.toString().trim(); }
67         };
68         public TRIM(EntityConditionValue nested) { super(FETCHER, ID_TRIM, "TRIM", nested); }
69         public TRIM(Object JavaDoc value) { super(FETCHER, ID_TRIM, "TRIM", value); }
70     };
71
72     public static class UPPER extends EntityFunction {
73         public static Fetcher FETCHER = new Fetcher() {
74             public Object JavaDoc getValue(Object JavaDoc value) { return value.toString().toUpperCase(); }
75         };
76         public UPPER(EntityConditionValue nested) { super(FETCHER, ID_UPPER, "UPPER", nested); }
77         public UPPER(Object JavaDoc value) { super(FETCHER, ID_UPPER, "UPPER", value); }
78     };
79
80     public static class LOWER extends EntityFunction {
81         public static Fetcher FETCHER = new Fetcher() {
82             public Object JavaDoc getValue(Object JavaDoc value) { return value.toString().toLowerCase(); }
83         };
84         public LOWER(EntityConditionValue nested) { super(FETCHER, ID_LOWER, "LOWER", nested); }
85         public LOWER(Object JavaDoc value) { super(FETCHER, ID_LOWER, "LOWER", value); }
86     };
87
88     protected int idInt;
89     protected String JavaDoc codeString;
90     protected EntityConditionValue nested;
91     protected Object JavaDoc value;
92     protected Fetcher fetcher;
93
94     protected EntityFunction(Fetcher fetcher, int id, String JavaDoc code, EntityConditionValue nested) {
95         this.fetcher = fetcher;
96         idInt = id;
97         codeString = code;
98         this.nested = nested;
99     }
100
101     protected EntityFunction(Fetcher fetcher, int id, String JavaDoc code, Object JavaDoc value) {
102         this.fetcher = fetcher;
103         idInt = id;
104         codeString = code;
105         if (value instanceof EntityConditionValue) {
106             this.nested = (EntityConditionValue) value;
107         } else {
108             this.value = value;
109         }
110     }
111
112     public EntityConditionValue freeze() {
113         if (nested != null) {
114             return new EntityFunction(fetcher, idInt, codeString, nested.freeze()) {};
115         } else {
116             return new EntityFunction(fetcher, idInt, codeString, value) {};
117         }
118     }
119
120     public String JavaDoc getCode() {
121         if (codeString == null)
122             return "null";
123         else
124             return codeString;
125     }
126
127     public int getId() {
128         return idInt;
129     }
130
131     public int hashCode() {
132         return codeString.hashCode();
133     }
134
135     public boolean equals(Object JavaDoc obj) {
136         if (!(obj instanceof EntityFunction)) return false;
137         EntityFunction otherFunc = (EntityFunction) obj;
138         return (this.idInt == otherFunc.idInt &&
139             (this.nested != null ? nested.equals(otherFunc.nested) : otherFunc.nested == null) &&
140             (this.value != null ? value.equals(otherFunc.value) : otherFunc.value == null));
141     }
142
143     public void addSqlValue(StringBuffer JavaDoc sql, Map JavaDoc tableAliases, ModelEntity modelEntity, List JavaDoc entityConditionParams, boolean includeTableNamePrefix, DatasourceInfo datasourceinfo) {
144         sql.append(codeString).append('(');
145         if (nested != null) {
146             nested.addSqlValue(sql, tableAliases, modelEntity, entityConditionParams, includeTableNamePrefix, datasourceinfo);
147         } else {
148             addValue(sql, null, value, entityConditionParams);
149         }
150         sql.append(')');
151     }
152
153     public void visit(EntityConditionVisitor visitor) {
154         if (nested != null) {
155             visitor.acceptEntityConditionValue(nested);
156         } else {
157             visitor.acceptObject(value);
158         }
159     }
160
161     public void accept(EntityConditionVisitor visitor) {
162         visitor.acceptEntityFunction(this);
163     }
164
165     public ModelField getModelField(ModelEntity modelEntity) {
166         if (nested != null) {
167             return nested.getModelField(modelEntity);
168         }
169         return null;
170     }
171
172     public void validateSql(ModelEntity modelEntity) throws GenericModelException {
173         if (nested != null) {
174             nested.validateSql(modelEntity);
175         }
176     }
177
178     public Object JavaDoc getValue(GenericDelegator delegator, Map JavaDoc map) {
179         Object JavaDoc value = nested != null ? nested.getValue(delegator, map) : this.value;
180         return value != null ? fetcher.getValue(value) : null;
181     }
182 }
183
Popular Tags