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.io.Serializable;
021: import java.util.Collections;
022: import java.util.List;
023: import java.util.Map;
024:
025: import javolution.util.FastList;
026: import javolution.util.FastMap;
027:
028: import org.ofbiz.entity.config.DatasourceInfo;
029: import org.ofbiz.entity.jdbc.SqlJdbcUtil;
030: import org.ofbiz.entity.model.ModelEntity;
031: import org.ofbiz.entity.model.ModelField;
032: import org.ofbiz.entity.model.ModelViewEntity;
033: import org.ofbiz.entity.model.ModelViewEntity.ModelAlias;
034:
035: /**
036: * Represents the conditions to be used to constrain a query
037: * <br/>An EntityCondition can represent various type of constraints, including:
038: * <ul>
039: * <li>EntityConditionList: a list of EntityConditions, combined with the operator specified
040: * <li>EntityExpr: for simple expressions or expressions that combine EntityConditions
041: * <li>EntityFieldMap: a map of fields where the field (key) equals the value, combined with the operator specified
042: * </ul>
043: * These can be used in various combinations using the EntityConditionList and EntityExpr objects.
044: *
045: */
046: public abstract class EntityConditionBase implements Serializable {
047:
048: public static final List emptyList = Collections
049: .unmodifiableList(FastList.newInstance());
050: public static final Map emptyMap = Collections
051: .unmodifiableMap(FastMap.newInstance());
052:
053: protected ModelField getField(ModelEntity modelEntity,
054: String fieldName) {
055: ModelField modelField = null;
056: if (modelEntity != null) {
057: modelField = modelEntity.getField(fieldName);
058: }
059: return modelField;
060: }
061:
062: protected String getColName(Map tableAliases,
063: ModelEntity modelEntity, String fieldName,
064: boolean includeTableNamePrefix,
065: DatasourceInfo datasourceInfo) {
066: if (modelEntity == null)
067: return fieldName;
068: return getColName(tableAliases, modelEntity, getField(
069: modelEntity, fieldName), fieldName,
070: includeTableNamePrefix, datasourceInfo);
071: }
072:
073: protected String getColName(ModelField modelField, String fieldName) {
074: String colName = null;
075: if (modelField != null) {
076: colName = modelField.getColName();
077: } else {
078: colName = fieldName;
079: }
080: return colName;
081: }
082:
083: protected String getColName(Map tableAliases,
084: ModelEntity modelEntity, ModelField modelField,
085: String fieldName, boolean includeTableNamePrefix,
086: DatasourceInfo datasourceInfo) {
087: if (modelEntity == null || modelField == null)
088: return fieldName;
089:
090: // 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
091: if (datasourceInfo != null && datasourceInfo.aliasViews
092: && modelEntity instanceof ModelViewEntity) {
093: ModelViewEntity modelViewEntity = (ModelViewEntity) modelEntity;
094: ModelAlias modelAlias = modelViewEntity.getAlias(fieldName);
095: if (modelAlias != null) {
096: return modelAlias.getColAlias();
097: }
098: }
099:
100: String colName = getColName(modelField, fieldName);
101: if (includeTableNamePrefix && datasourceInfo != null) {
102: String tableName = modelEntity.getTableName(datasourceInfo);
103: if (tableAliases.containsKey(tableName)) {
104: tableName = (String) tableAliases.get(tableName);
105: }
106: colName = tableName + "." + colName;
107: }
108: return colName;
109: }
110:
111: protected void addValue(StringBuffer buffer, ModelField field,
112: Object value, List params) {
113: SqlJdbcUtil.addValue(buffer, params == null ? null : field,
114: value, params);
115: }
116:
117: public boolean equals(Object obj) {
118: throw new UnsupportedOperationException("equals:"
119: + getClass().getName());
120: }
121:
122: public int hashCode() {
123: throw new UnsupportedOperationException("hashCode: "
124: + getClass().getName());
125: }
126:
127: protected static boolean equals(Object o1, Object o2) {
128: return o1 == null ? o2 == null : o1.equals(o2);
129: }
130:
131: protected static int hashCode(Object o) {
132: return o != null ? o.hashCode() : 0;
133: }
134:
135: public static Boolean castBoolean(boolean result) {
136: return result ? Boolean.TRUE : Boolean.FALSE;
137: }
138: }
|