Author: pgil
Date: Fri Jan 4 14:50:13 2019 New Revision: 1850372 URL: http://svn.apache.org/viewvc?rev=1850372&view=rev Log: Refactoring ‘EntityCondition’ - Remove EntityConditionBase class (OFBIZ-10691) This is removing an abuse of inheritance for code reuse which was· breaking Liskov substitution principle. This has been achieved with the following changes: * The ‘Serializable’ interface which was implemented by ‘EntityConditionBase’ is now implemented directly by the· ‘EntityCondition’, ‘EntityConditionValue’, and ‘EntityOperator’ classes. * ‘emptyList’ and ‘_emptyMap’ useless static members has been removed and ‘emptyAliases’ has been move down to the ‘EntityConditionValue’ subclass. * The ‘castBoolean’ method has been removed due to the automatic boxing/unboxing of boolean and Boolean values which make it useless. * ‘equals’ and ‘hashCode’ pseudo abstract methods (meaning methods throwing an unsupported operation exception) has been moved down to the ‘EntityCondition’ and ‘EntityConditionValue’ sub classes. ‘Objects#equals’ and ‘Objects#hashCode’ standard methods has been used in the EntityCondition class hierarchy instead. * The ‘getColName’ methods has been moved down to the· ‘EntityConditionValue’ class which is the unique class using those methods. * The remaining static methods ‘getField’ and ‘addValue’ has been moved to a new ‘EntityConditionUtils’ class. The unused override of ‘addValue’ has been removed which allowed us to make this method static. Thanks Mathieu for the contribution Added: ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityConditionUtils.java (with props) Removed: ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityConditionBase.java Modified: ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityComparisonOperator.java ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityCondition.java ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityConditionValue.java ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityDateFilterCondition.java ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityExpr.java ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityFieldValue.java ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityFunction.java ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityJoinOperator.java ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityOperator.java ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityWhereString.java Modified: ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityComparisonOperator.java URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityComparisonOperator.java?rev=1850372&r1=1850371&r2=1850372&view=diff ============================================================================== --- ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityComparisonOperator.java (original) +++ ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityComparisonOperator.java Fri Jan 4 14:50:13 2019 @@ -93,14 +93,14 @@ public abstract class EntityComparisonOp ecv.addSqlValue(sql, entity, entityConditionParams, false, datasourceInfo); field = ecv.getModelField(entity); } else if (compat && lhs instanceof String) { - field = getField(entity, (String) lhs); + field = EntityConditionUtils.getField(entity, (String) lhs); if (field == null) { sql.append(lhs); } else { sql.append(field.getColName()); } } else { - addValue(sql, null, lhs, entityConditionParams); + EntityConditionUtils.addValue(sql, null, lhs, entityConditionParams); field = null; } @@ -125,7 +125,7 @@ public abstract class EntityComparisonOp } ecv.addSqlValue(sql, entity, entityConditionParams, false, datasourceInfo); } else { - addValue(sql, field, rhs, entityConditionParams); + EntityConditionUtils.addValue(sql, field, rhs, entityConditionParams); } } Modified: ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityCondition.java URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityCondition.java?rev=1850372&r1=1850371&r2=1850372&view=diff ============================================================================== --- ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityCondition.java (original) +++ ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityCondition.java Fri Jan 4 14:50:13 2019 @@ -18,6 +18,7 @@ *******************************************************************************/ package org.apache.ofbiz.entity.condition; +import java.io.Serializable; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -42,7 +43,7 @@ import org.apache.ofbiz.entity.model.Mod * */ @SuppressWarnings("serial") -public abstract class EntityCondition extends EntityConditionBase implements IsEmpty { +public abstract class EntityCondition implements IsEmpty, Serializable { public static <L,R,LL,RR> EntityExpr makeCondition(L lhs, EntityComparisonOperator<LL,RR> operator, R rhs) { return new EntityExpr(lhs, operator, rhs); @@ -137,4 +138,15 @@ public abstract class EntityCondition ex abstract public boolean mapMatches(Delegator delegator, Map<String, ? extends Object> map); abstract public EntityCondition freeze(); + + @Override + public boolean equals(Object obj) { + throw new UnsupportedOperationException("equals:" + getClass().getName()); + } + + @Override + public int hashCode() { + throw new UnsupportedOperationException("hashCode: " + getClass().getName()); + } + } Added: ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityConditionUtils.java URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityConditionUtils.java?rev=1850372&view=auto ============================================================================== --- ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityConditionUtils.java (added) +++ ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityConditionUtils.java Fri Jan 4 14:50:13 2019 @@ -0,0 +1,55 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.ofbiz.entity.condition; + +import java.util.List; + +import org.apache.ofbiz.entity.jdbc.SqlJdbcUtil; +import org.apache.ofbiz.entity.model.ModelEntity; +import org.apache.ofbiz.entity.model.ModelField; + +/** + * Auxiliary methods used by condition expressions. + */ +final class EntityConditionUtils { + + /** + * Calls {@link ModelEntity#getField(String)} if the entity model is not null. + * + * @param modelEntity the entity model to query + * @param fieldName the name of the field to get from {@code ModelEntity} + * @return the field corresponding to {@code fieldName} in {@code ModelEntity} + */ + static ModelField getField(ModelEntity modelEntity, String fieldName) { + return (modelEntity == null) ? null : modelEntity.getField(fieldName); + } + + /** + * Calls {@link SqlJdbcUtil#addValue(StringBuilder, ModelField, Object, List)} + * if the condition parameters are not null. + * + * @param buffer the buffer that will receive the SQL dump + * @param field the field to dump + * @param value the value to dump + * @param params the condition parameters + */ + static void addValue(StringBuilder buffer, ModelField field, Object value, List<EntityConditionParam> params) { + SqlJdbcUtil.addValue(buffer, params == null ? null : field, value, params); + } +} Propchange: ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityConditionUtils.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityConditionUtils.java ------------------------------------------------------------------------------ svn:keywords = Date Rev Author URL Id Propchange: ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityConditionUtils.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Modified: ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityConditionValue.java URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityConditionValue.java?rev=1850372&r1=1850371&r2=1850372&view=diff ============================================================================== --- ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityConditionValue.java (original) +++ ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityConditionValue.java Fri Jan 4 14:50:13 2019 @@ -18,7 +18,10 @@ *******************************************************************************/ package org.apache.ofbiz.entity.condition; +import java.io.Serializable; import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; import java.util.List; import java.util.Map; @@ -34,8 +37,9 @@ import org.apache.ofbiz.entity.model.Mod * */ @SuppressWarnings("serial") -public abstract class EntityConditionValue extends EntityConditionBase { +public abstract class EntityConditionValue implements Serializable { + private static final Map<String, String> emptyAliases = Collections.unmodifiableMap(new HashMap<>()); public static EntityConditionValue CONSTANT_NUMBER(Number value) { return new ConstantNumberValue(value); } public static final class ConstantNumberValue extends EntityConditionValue { private Number value; @@ -109,4 +113,14 @@ public abstract class EntityConditionVal toString(sql); return sql.toString(); } + + @Override + public boolean equals(Object obj) { + throw new UnsupportedOperationException("equals:" + getClass().getName()); + } + + @Override + public int hashCode() { + throw new UnsupportedOperationException("hashCode: " + getClass().getName()); + } } Modified: ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityDateFilterCondition.java URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityDateFilterCondition.java?rev=1850372&r1=1850371&r2=1850372&view=diff ============================================================================== --- ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityDateFilterCondition.java (original) +++ ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityDateFilterCondition.java Fri Jan 4 14:50:13 2019 @@ -22,6 +22,7 @@ import java.sql.Timestamp; import java.util.LinkedList; import java.util.List; import java.util.Map; +import java.util.Objects; import org.apache.ofbiz.base.util.UtilDateTime; import org.apache.ofbiz.entity.Delegator; @@ -73,12 +74,12 @@ public final class EntityDateFilterCondi return false; } EntityDateFilterCondition other = (EntityDateFilterCondition) obj; - return equals(fromDateName, other.fromDateName) && equals(thruDateName, other.thruDateName); + return Objects.equals(fromDateName, other.fromDateName) && Objects.equals(thruDateName, other.thruDateName); } @Override public int hashCode() { - return hashCode(fromDateName) ^ hashCode(thruDateName); + return Objects.hashCode(fromDateName) ^ Objects.hashCode(thruDateName); } @Override Modified: ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityExpr.java URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityExpr.java?rev=1850372&r1=1850371&r2=1850372&view=diff ============================================================================== --- ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityExpr.java (original) +++ ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityExpr.java Fri Jan 4 14:50:13 2019 @@ -22,6 +22,7 @@ import java.util.Collection; import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.Objects; import org.apache.ofbiz.base.util.Debug; import org.apache.ofbiz.base.util.ObjectType; @@ -134,16 +135,6 @@ public final class EntityExpr extends En } @Override - protected void addValue(StringBuilder buffer, ModelField field, Object value, List<EntityConditionParam> params) { - if (rhs instanceof EntityFunction.UPPER) { - if (value instanceof String) { - value = ((String) value).toUpperCase(Locale.getDefault()); - } - } - super.addValue(buffer, field, value, params); - } - - @Override public EntityCondition freeze() { return operator.freeze(lhs, rhs); } @@ -263,15 +254,13 @@ public final class EntityExpr extends En if (!(obj instanceof EntityExpr)) { return false; } - EntityExpr other = (EntityExpr) obj; - return equals(lhs, other.lhs) && equals(operator, other.operator) - && equals(rhs, other.rhs); + EntityExpr ee = (EntityExpr) obj; + return Objects.equals(lhs, ee.lhs) && Objects.equals(operator, ee.operator) && Objects.equals(rhs, ee.rhs); + } @Override public int hashCode() { - return hashCode(lhs) + - hashCode(operator) + - hashCode(rhs); + return Objects.hashCode(lhs) + Objects.hashCode(operator) + Objects.hashCode(rhs); } } Modified: ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityFieldValue.java URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityFieldValue.java?rev=1850372&r1=1850371&r2=1850372&view=diff ============================================================================== --- ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityFieldValue.java (original) +++ ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityFieldValue.java Fri Jan 4 14:50:13 2019 @@ -19,6 +19,8 @@ package org.apache.ofbiz.entity.condition; +import static org.apache.ofbiz.entity.condition.EntityConditionUtils.getField; + import java.util.LinkedList; import java.util.List; import java.util.Map; @@ -171,6 +173,46 @@ public class EntityFieldValue extends En } } + private String getColName(Map<String, String> tableAliases, ModelEntity modelEntity, String fieldName, + boolean includeTableNamePrefix, Datasource datasourceInfo) { + if (modelEntity == null) { + return fieldName; + } + return getColName(tableAliases, modelEntity, getField(modelEntity, fieldName), fieldName, + includeTableNamePrefix, datasourceInfo); + } + + private String getColName(Map<String, String> tableAliases, ModelEntity modelEntity, ModelField modelField, + String fieldName, boolean includeTableNamePrefix, Datasource datasourceInfo) { + if (modelEntity == null || modelField == null) { + return fieldName; + } + + // If this is a view entity and we are configured to alias the views, use the alias here + // instead of the composite (i.e. table.column) field name. + if (datasourceInfo != null && datasourceInfo.getAliasViewColumns() && modelEntity instanceof ModelViewEntity) { + ModelViewEntity modelViewEntity = (ModelViewEntity) modelEntity; + ModelAlias modelAlias = modelViewEntity.getAlias(fieldName); + if (modelAlias != null) { + return modelAlias.getColAlias(); + } + } + + String colName = getColName(modelField, fieldName); + if (includeTableNamePrefix && datasourceInfo != null) { + String tableName = modelEntity.getTableName(datasourceInfo); + if (tableAliases.containsKey(tableName)) { + tableName = tableAliases.get(tableName); + } + colName = tableName + "." + colName; + } + return colName; + } + + private String getColName(ModelField modelField, String fieldName) { + return (modelField == null) ? fieldName : modelField.getColValue(); + } + @Override public void validateSql(ModelEntity modelEntity) throws GenericModelException { ModelField field = getModelField(modelEntity); Modified: ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityFunction.java URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityFunction.java?rev=1850372&r1=1850371&r2=1850372&view=diff ============================================================================== --- ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityFunction.java (original) +++ ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityFunction.java Fri Jan 4 14:50:13 2019 @@ -195,7 +195,7 @@ public abstract class EntityFunction<T e if (nested != null) { nested.addSqlValue(sql, tableAliases, modelEntity, entityConditionParams, includeTableNamePrefix, datasourceinfo); } else { - addValue(sql, null, value, entityConditionParams); + EntityConditionUtils.addValue(sql, null, value, entityConditionParams); } sql.append(')'); } Modified: ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityJoinOperator.java URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityJoinOperator.java?rev=1850372&r1=1850371&r2=1850372&view=diff ============================================================================== --- ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityJoinOperator.java (original) +++ ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityJoinOperator.java Fri Jan 4 14:50:13 2019 @@ -127,7 +127,7 @@ public class EntityJoinOperator extends } public Boolean eval(Delegator delegator, Map<String, ? extends Object> map, EntityCondition lhs, EntityCondition rhs) { - return castBoolean(mapMatches(delegator, map, lhs, rhs)); + return mapMatches(delegator, map, lhs, rhs); } @Override @@ -139,7 +139,7 @@ public class EntityJoinOperator extends } public Boolean eval(Delegator delegator, Map<String, ? extends Object> map, List<? extends EntityCondition> conditionList) { - return castBoolean(mapMatches(delegator, map, conditionList)); + return mapMatches(delegator, map, conditionList); } public boolean mapMatches(Delegator delegator, Map<String, ? extends Object> map, List<? extends EntityCondition> conditionList) { Modified: ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityOperator.java URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityOperator.java?rev=1850372&r1=1850371&r2=1850372&view=diff ============================================================================== --- ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityOperator.java (original) +++ ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityOperator.java Fri Jan 4 14:50:13 2019 @@ -19,6 +19,9 @@ package org.apache.ofbiz.entity.condition; +import static org.apache.ofbiz.entity.condition.EntityConditionUtils.addValue; + +import java.io.Serializable; import java.util.Collection; import java.util.HashMap; import java.util.Iterator; @@ -40,7 +43,7 @@ import org.apache.ofbiz.entity.model.Mod * */ @SuppressWarnings("serial") -public abstract class EntityOperator<L, R, T> extends EntityConditionBase { +public abstract class EntityOperator<L, R, T> implements Serializable { public static final int ID_EQUALS = 1; public static final int ID_NOT_EQUAL = 2; Modified: ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityWhereString.java URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityWhereString.java?rev=1850372&r1=1850371&r2=1850372&view=diff ============================================================================== --- ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityWhereString.java (original) +++ ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityWhereString.java Fri Jan 4 14:50:13 2019 @@ -20,6 +20,7 @@ package org.apache.ofbiz.entity.conditio import java.util.List; import java.util.Map; +import java.util.Objects; import org.apache.ofbiz.base.util.UtilValidate; import org.apache.ofbiz.entity.Delegator; @@ -92,11 +93,11 @@ public final class EntityWhereString ext return false; } EntityWhereString other = (EntityWhereString) obj; - return equals(sqlString, other.sqlString); + return Objects.equals(sqlString, other.sqlString); } @Override public int hashCode() { - return hashCode(sqlString); + return Objects.hashCode(sqlString); } } |
Free forum by Nabble | Edit this page |