KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: EntityConditionBase.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2002-2005 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 package org.ofbiz.entity.condition;
25
26 import java.io.Serializable JavaDoc;
27 import java.util.Collections JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Map JavaDoc;
30
31 import javolution.util.FastList;
32 import javolution.util.FastMap;
33
34 import org.ofbiz.entity.config.DatasourceInfo;
35 import org.ofbiz.entity.jdbc.SqlJdbcUtil;
36 import org.ofbiz.entity.model.ModelEntity;
37 import org.ofbiz.entity.model.ModelField;
38 import org.ofbiz.entity.model.ModelViewEntity;
39 import org.ofbiz.entity.model.ModelViewEntity.ModelAlias;
40
41 /**
42  * Represents the conditions to be used to constrain a query
43  * <br/>An EntityCondition can represent various type of constraints, including:
44  * <ul>
45  * <li>EntityConditionList: a list of EntityConditions, combined with the operator specified
46  * <li>EntityExpr: for simple expressions or expressions that combine EntityConditions
47  * <li>EntityFieldMap: a map of fields where the field (key) equals the value, combined with the operator specified
48  * </ul>
49  * These can be used in various combinations using the EntityConditionList and EntityExpr objects.
50  *
51  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
52  * @version $Rev: 5462 $
53  * @since 2.0
54  */

55 public abstract class EntityConditionBase implements Serializable JavaDoc {
56
57     public static final List JavaDoc emptyList = Collections.unmodifiableList(FastList.newInstance());
58     public static final Map JavaDoc emptyMap = Collections.unmodifiableMap(FastMap.newInstance());
59
60     protected ModelField getField(ModelEntity modelEntity, String JavaDoc fieldName) {
61         ModelField modelField = null;
62         if (modelEntity != null) {
63             modelField = (ModelField) modelEntity.getField(fieldName);
64         }
65         return modelField;
66     }
67
68     protected String JavaDoc getColName(Map JavaDoc tableAliases, ModelEntity modelEntity, String JavaDoc fieldName, boolean includeTableNamePrefix, DatasourceInfo datasourceInfo) {
69         if (modelEntity == null) return fieldName;
70         return getColName(tableAliases, modelEntity, getField(modelEntity, fieldName), fieldName, includeTableNamePrefix, datasourceInfo);
71     }
72
73     protected String JavaDoc getColName(ModelField modelField, String JavaDoc fieldName) {
74         String JavaDoc colName = null;
75         if (modelField != null) {
76             colName = modelField.getColName();
77         } else {
78             colName = (String JavaDoc) fieldName;
79         }
80         return colName;
81     }
82
83     protected String JavaDoc getColName(Map JavaDoc tableAliases, ModelEntity modelEntity, ModelField modelField, String JavaDoc fieldName, boolean includeTableNamePrefix, DatasourceInfo datasourceInfo) {
84         if (modelEntity == null || modelField == null) return fieldName;
85
86         // if this is a view entity and we are configured to alias the views, use the alias here instead of the composite (ie table.column) field name
87
if (datasourceInfo != null && datasourceInfo.aliasViews && modelEntity instanceof ModelViewEntity) {
88             ModelViewEntity modelViewEntity = (ModelViewEntity) modelEntity;
89             ModelAlias modelAlias = modelViewEntity.getAlias(fieldName);
90             if (modelAlias != null) {
91                 return modelAlias.getColAlias();
92             }
93         }
94         
95         String JavaDoc colName = getColName(modelField, fieldName);
96         if (includeTableNamePrefix && datasourceInfo != null) {
97             String JavaDoc tableName = modelEntity.getTableName(datasourceInfo);
98             if (tableAliases.containsKey(tableName)) {
99                 tableName = (String JavaDoc) tableAliases.get(tableName);
100             }
101             colName = tableName + "." + colName;
102         }
103         return colName;
104     }
105
106     protected void addValue(StringBuffer JavaDoc buffer, ModelField field, Object JavaDoc value, List JavaDoc params) {
107         SqlJdbcUtil.addValue(buffer, params == null ? null : field, value, params);
108     }
109
110     public boolean equals(Object JavaDoc obj) {
111         throw new UnsupportedOperationException JavaDoc("equals:" + getClass().getName());
112     }
113
114     public int hashCode() {
115         throw new UnsupportedOperationException JavaDoc("hashCode: " + getClass().getName());
116     }
117
118     protected static boolean equals(Object JavaDoc o1, Object JavaDoc o2) {
119         return o1 == null ? o2 == null : o1.equals(o2);
120     }
121
122     protected static int hashCode(Object JavaDoc o) {
123         return o != null ? o.hashCode() : 0;
124     }
125
126     public static Boolean JavaDoc castBoolean(boolean result) {
127         return result ? Boolean.TRUE : Boolean.FALSE;
128     }
129 }
130
Popular Tags