Improved: General refactoring and code improvements, package

org.apache.ofbiz.entity.condition.
(OFBIZ-9956)

Thanks Dennis Balkir for reporting and providing the patches.

git-svn-id: https://svn.apache.org/repos/asf/ofbiz/ofbiz-framework/trunk@1818469 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael Brohl
2017-12-17 10:37:49 +00:00
parent 1cca5dc166
commit f48b282587
16 changed files with 105 additions and 66 deletions
@@ -77,7 +77,9 @@ public class EntityClause {
}
public Object getValue() {
if (value == null) value = new Object();
if (value == null) {
value = new Object();
}
return value;
}
@@ -85,7 +85,6 @@ public abstract class EntityComparisonOperator<L, R> extends EntityOperator<L, R
@Override
public void addSqlValue(StringBuilder sql, ModelEntity entity, List<EntityConditionParam> entityConditionParams, boolean compat, L lhs, R rhs, Datasource datasourceInfo) {
//Debug.logInfo("EntityComparisonOperator.addSqlValue field=" + lhs + ", value=" + rhs + ", value type=" + (rhs == null ? "null object" : rhs.getClass().getName()), module);
// if this is an IN operator and the rhs Object isEmpty, add "1=0" instead of the normal SQL. Note that "FALSE" does not work with all databases.
if (this.idInt == EntityOperator.ID_IN && UtilValidate.isEmpty(rhs)) {
@@ -160,7 +159,9 @@ public abstract class EntityComparisonOperator<L, R> extends EntityOperator<L, R
rightValue = rhs;
}
if (leftValue == WILDCARD || rightValue == WILDCARD) return true;
if (leftValue == WILDCARD || rightValue == WILDCARD) {
return true;
}
return compare(UtilGenerics.<L>cast(leftValue), UtilGenerics.<R>cast(rightValue));
}
@@ -173,9 +174,8 @@ public abstract class EntityComparisonOperator<L, R> extends EntityOperator<L, R
if (item instanceof EntityConditionValue) {
EntityConditionValue ecv = (EntityConditionValue) item;
return ecv.freeze();
} else {
return item;
}
return item;
}
public EntityComparisonOperator(int id, String code) {
@@ -252,15 +252,13 @@ public abstract class EntityComparisonOperator<L, R> extends EntityOperator<L, R
if (lhs == null) {
if (rhs != null) {
return false;
} else {
return true;
}
return true;
} else if (rhs instanceof Collection<?>) {
if (((Collection<?>) rhs).contains(lhs)) {
return true;
} else {
return false;
}
return false;
} else if (lhs.equals(rhs)) {
return true;
} else {
@@ -57,19 +57,19 @@ public abstract class EntityCondition extends EntityConditionBase implements IsE
}
public static <T extends EntityCondition> EntityConditionList<T> makeCondition(EntityJoinOperator operator, T... conditionList) {
return new EntityConditionList<T>(Arrays.<T>asList(conditionList), operator);
return new EntityConditionList<>(Arrays.<T>asList(conditionList), operator);
}
public static <T extends EntityCondition> EntityConditionList<T> makeCondition(T... conditionList) {
return new EntityConditionList<T>(Arrays.<T>asList(conditionList), EntityOperator.AND);
return new EntityConditionList<>(Arrays.<T>asList(conditionList), EntityOperator.AND);
}
public static <T extends EntityCondition> EntityConditionList<T> makeCondition(List<T> conditionList, EntityJoinOperator operator) {
return new EntityConditionList<T>(conditionList, operator);
return new EntityConditionList<>(conditionList, operator);
}
public static <T extends EntityCondition> EntityConditionList<T> makeCondition(List<T> conditionList) {
return new EntityConditionList<T>(conditionList, EntityOperator.AND);
return new EntityConditionList<>(conditionList, EntityOperator.AND);
}
public static <L,R> EntityFieldMap makeCondition(Map<String, ? extends Object> fieldMap, EntityComparisonOperator<L,R> compOp, EntityJoinOperator joinOp) {
@@ -58,7 +58,9 @@ public abstract class EntityConditionBase implements Serializable {
}
protected String getColName(Map<String, String> tableAliases, ModelEntity modelEntity, String fieldName, boolean includeTableNamePrefix, Datasource datasourceInfo) {
if (modelEntity == null) return fieldName;
if (modelEntity == null) {
return fieldName;
}
return getColName(tableAliases, modelEntity, getField(modelEntity, fieldName), fieldName, includeTableNamePrefix, datasourceInfo);
}
@@ -73,7 +75,9 @@ public abstract class EntityConditionBase implements Serializable {
}
protected String getColName(Map<String, String> tableAliases, ModelEntity modelEntity, ModelField modelField, String fieldName, boolean includeTableNamePrefix, Datasource datasourceInfo) {
if (modelEntity == null || modelField == null) return fieldName;
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 (ie table.column) field name
if (datasourceInfo != null && datasourceInfo.getAliasViewColumns() && modelEntity instanceof ModelViewEntity) {
@@ -69,8 +69,9 @@ public abstract class EntityConditionFunction extends EntityCondition {
}
public String getCode() {
if (codeString == null)
if (codeString == null) {
return "null";
}
return codeString;
}
@@ -85,7 +86,9 @@ public abstract class EntityConditionFunction extends EntityCondition {
@Override
public boolean equals(Object obj) {
if (!(obj instanceof EntityConditionFunction)) return false;
if (!(obj instanceof EntityConditionFunction)) {
return false;
}
EntityConditionFunction otherFunc = (EntityConditionFunction) obj;
return this.idInt.equals(otherFunc.idInt) && (this.condition != null ? condition.equals(otherFunc.condition) : otherFunc.condition != null);
}
@@ -72,7 +72,6 @@ public abstract class EntityConditionListBase<T extends EntityCondition> extends
@Override
public String makeWhereString(ModelEntity modelEntity, List<EntityConditionParam> entityConditionParams, Datasource datasourceInfo) {
// if (Debug.verboseOn()) Debug.logVerbose("makeWhereString for entity " + modelEntity.getEntityName(), module);
StringBuilder sql = new StringBuilder();
operator.addSqlValue(sql, modelEntity, entityConditionParams, conditionList, datasourceInfo);
return sql.toString();
@@ -80,7 +79,6 @@ public abstract class EntityConditionListBase<T extends EntityCondition> extends
@Override
public void checkCondition(ModelEntity modelEntity) throws GenericModelException {
// if (Debug.verboseOn()) Debug.logVerbose("checkCondition for entity " + modelEntity.getEntityName(), module);
operator.validateSql(modelEntity, conditionList);
}
@@ -96,15 +94,12 @@ public abstract class EntityConditionListBase<T extends EntityCondition> extends
@Override
public boolean equals(Object obj) {
if (!(obj instanceof EntityConditionListBase<?>)) return false;
if (!(obj instanceof EntityConditionListBase<?>)) {
return false;
}
EntityConditionListBase<?> other = UtilGenerics.cast(obj);
boolean isEqual = conditionList.equals(other.conditionList) && operator.equals(other.operator);
//if (!isEqual) {
// Debug.logWarning("EntityConditionListBase.equals is false:\n this.operator=" + this.operator + "; other.operator=" + other.operator +
// "\nthis.conditionList=" + this.conditionList +
// "\nother.conditionList=" + other.conditionList, module);
//}
return isEqual;
}
@@ -149,6 +149,8 @@ public class EntityConditionSubSelect extends EntityConditionValue {
@Override
public void visit(EntityConditionVisitor visitor) {
if (whereCond != null) whereCond.visit(visitor);
if (whereCond != null) {
whereCond.visit(visitor);
}
}
}
@@ -30,7 +30,7 @@ import org.apache.ofbiz.entity.config.model.Datasource;
import org.apache.ofbiz.entity.model.ModelEntity;
/**
* Date-range condition.
* Date-range condition.
*
*/
@SuppressWarnings("serial")
@@ -69,7 +69,9 @@ public final class EntityDateFilterCondition extends EntityCondition {
@Override
public boolean equals(Object obj) {
if (!(obj instanceof EntityDateFilterCondition)) return false;
if (!(obj instanceof EntityDateFilterCondition)) {
return false;
}
EntityDateFilterCondition other = (EntityDateFilterCondition) obj;
return equals(fromDateName, other.fromDateName) && equals(thruDateName, other.thruDateName);
}
@@ -115,15 +117,15 @@ public final class EntityDateFilterCondition extends EntityCondition {
}
/**
* Creates an EntityCondition representing a date range filter query to be used against
* entities that themselves represent a date range. When used the resulting entities
* Creates an EntityCondition representing a date range filter query to be used against
* entities that themselves represent a date range. When used the resulting entities
* will meet at least one of the following criteria:
* - fromDate is equal to or after rangeStart but before rangeEnd
* - thruDate is equal to or after rangeStart but before rangeEnd
* - fromDate is null and thruDate is equal to or after rangeStart
* - thruDate is null and fromDate is before rangeEnd
* - fromDate is null and thruDate is null
*
*
* @param rangeStart The start of the range to filter against
* @param rangeEnd The end of the range to filter against
* @param fromDateName The name of the field containing the entity's "fromDate"
@@ -75,8 +75,6 @@ public final class EntityExpr extends EntityCondition {
}
this.operator = UtilGenerics.cast(operator);
this.rhs = rhs;
//Debug.logInfo("new EntityExpr internal field=" + lhs + ", value=" + rhs + ", value type=" + (rhs == null ? "null object" : rhs.getClass().getName()), module);
}
public EntityExpr(EntityCondition lhs, EntityJoinOperator operator, EntityCondition rhs) {
@@ -114,7 +112,6 @@ public final class EntityExpr extends EntityCondition {
@Override
public String makeWhereString(ModelEntity modelEntity, List<EntityConditionParam> entityConditionParams, Datasource datasourceInfo) {
// if (Debug.verboseOn()) Debug.logVerbose("makeWhereString for entity " + modelEntity.getEntityName(), module);
this.checkRhsType(modelEntity, null);
@@ -130,7 +127,6 @@ public final class EntityExpr extends EntityCondition {
@Override
public void checkCondition(ModelEntity modelEntity) throws GenericModelException {
// if (Debug.verboseOn()) Debug.logVerbose("checkCondition for entity " + modelEntity.getEntityName(), module);
if (lhs instanceof EntityCondition) {
((EntityCondition) lhs).checkCondition(modelEntity);
((EntityCondition) rhs).checkCondition(modelEntity);
@@ -163,7 +159,9 @@ public final class EntityExpr extends EntityCondition {
}
public void checkRhsType(ModelEntity modelEntity, Delegator delegator) {
if (this.rhs == null || this.rhs == GenericEntity.NULL_FIELD || modelEntity == null) return;
if (this.rhs == null || this.rhs == GenericEntity.NULL_FIELD || modelEntity == null) {
return;
}
Object value = this.rhs;
if (this.rhs instanceof EntityFunction<?>) {
@@ -267,16 +265,13 @@ public final class EntityExpr extends EntityCondition {
@Override
public boolean equals(Object obj) {
if (!(obj instanceof EntityExpr)) return false;
if (!(obj instanceof EntityExpr)) {
return false;
}
EntityExpr other = (EntityExpr) obj;
boolean isEqual = equals(lhs, other.lhs) &&
equals(operator, other.operator) &&
equals(rhs, other.rhs);
//if (!isEqual) {
// Debug.logWarning("EntityExpr.equals is false for: \n-this.lhs=" + this.lhs + "; other.lhs=" + other.lhs +
// "\nthis.operator=" + this.operator + "; other.operator=" + other.operator +
// "\nthis.rhs=" + this.rhs + "other.rhs=" + other.rhs, module);
//}
return isEqual;
}
@@ -93,19 +93,33 @@ public class EntityFieldValue extends EntityConditionValue {
@Override
public int hashCode() {
int hash = fieldName.hashCode();
if (this.entityAlias != null) hash |= this.entityAlias.hashCode();
if (this.entityAliasStack != null) hash |= this.entityAliasStack.hashCode();
if (this.modelViewEntity != null) hash |= this.modelViewEntity.hashCode();
if (this.entityAlias != null) {
hash |= this.entityAlias.hashCode();
}
if (this.entityAliasStack != null) {
hash |= this.entityAliasStack.hashCode();
}
if (this.modelViewEntity != null) {
hash |= this.modelViewEntity.hashCode();
}
return hash;
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof EntityFieldValue)) return false;
if (!(obj instanceof EntityFieldValue)) {
return false;
}
EntityFieldValue otherValue = (EntityFieldValue) obj;
if (!fieldName.equals(otherValue.fieldName)) return false;
if (UtilMisc.compare(this.entityAlias, otherValue.entityAlias) != 0) return false;
if (UtilMisc.compare(this.entityAliasStack, otherValue.entityAliasStack) != 0) return false;
if (!fieldName.equals(otherValue.fieldName)) {
return false;
}
if (UtilMisc.compare(this.entityAlias, otherValue.entityAlias) != 0) {
return false;
}
if (UtilMisc.compare(this.entityAliasStack, otherValue.entityAliasStack) != 0) {
return false;
}
return true;
}
@@ -180,7 +180,9 @@ public abstract class EntityFunction<T extends Comparable<?>> extends EntityCond
@Override
public boolean equals(Object obj) {
if (!(obj instanceof EntityFunction<?>)) return false;
if (!(obj instanceof EntityFunction<?>)) {
return false;
}
EntityFunction<?> otherFunc = UtilGenerics.cast(obj);
return (this.function == otherFunc.function &&
(this.nested != null ? nested.equals(otherFunc.nested) : otherFunc.nested == null) &&
@@ -130,8 +130,12 @@ public class EntityJoinOperator extends EntityOperator<EntityCondition, EntityCo
@Override
public boolean entityMatches(GenericEntity entity, EntityCondition lhs, EntityCondition rhs) {
if (lhs.entityMatches(entity) == shortCircuitValue) return shortCircuitValue;
if (rhs.entityMatches(entity) == shortCircuitValue) return shortCircuitValue;
if (lhs.entityMatches(entity) == shortCircuitValue) {
return shortCircuitValue;
}
if (rhs.entityMatches(entity) == shortCircuitValue) {
return shortCircuitValue;
}
return !shortCircuitValue;
}
@@ -145,8 +149,12 @@ public class EntityJoinOperator extends EntityOperator<EntityCondition, EntityCo
@Override
public boolean mapMatches(Delegator delegator, Map<String, ? extends Object> map, EntityCondition lhs, EntityCondition rhs) {
if (lhs.mapMatches(delegator, map) == shortCircuitValue) return shortCircuitValue;
if (rhs.mapMatches(delegator, map) == shortCircuitValue) return shortCircuitValue;
if (lhs.mapMatches(delegator, map) == shortCircuitValue) {
return shortCircuitValue;
}
if (rhs.mapMatches(delegator, map) == shortCircuitValue) {
return shortCircuitValue;
}
return !shortCircuitValue;
}
@@ -157,7 +165,9 @@ public class EntityJoinOperator extends EntityOperator<EntityCondition, EntityCo
public boolean mapMatches(Delegator delegator, Map<String, ? extends Object> map, List<? extends EntityCondition> conditionList) {
if (UtilValidate.isNotEmpty(conditionList)) {
for (EntityCondition condition: conditionList) {
if (condition.mapMatches(delegator, map) == shortCircuitValue) return shortCircuitValue;
if (condition.mapMatches(delegator, map) == shortCircuitValue) {
return shortCircuitValue;
}
}
}
return !shortCircuitValue;
@@ -58,7 +58,7 @@ public abstract class EntityOperator<L, R, T> extends EntityConditionBase {
public static final int ID_NOT_LIKE = 14;
private static final AtomicInteger dynamicId = new AtomicInteger();
private static HashMap<String, EntityOperator<?,?,?>> registry = new HashMap<String, EntityOperator<?,?,?>>();
private static HashMap<String, EntityOperator<?,?,?>> registry = new HashMap<>();
private static <L,R,T> void registerCase(String name, EntityOperator<L,R,T> operator) {
registry.put(name.toLowerCase(Locale.getDefault()), operator);
@@ -77,15 +77,17 @@ public abstract class EntityOperator<L, R, T> extends EntityConditionBase {
public static <L,R> EntityComparisonOperator<L,R> lookupComparison(String name) {
EntityOperator<?,?,Boolean> operator = lookup(name);
if (!(operator instanceof EntityComparisonOperator<?,?>))
if (!(operator instanceof EntityComparisonOperator<?,?>)) {
throw new IllegalArgumentException(name + " is not a comparison operator");
}
return UtilGenerics.cast(operator);
}
public static EntityJoinOperator lookupJoin(String name) {
EntityOperator<?,?,Boolean> operator = lookup(name);
if (!(operator instanceof EntityJoinOperator))
if (!(operator instanceof EntityJoinOperator)) {
throw new IllegalArgumentException(name + " is not a join operator");
}
return UtilGenerics.cast(operator);
}
@@ -99,10 +101,8 @@ public abstract class EntityOperator<L, R, T> extends EntityConditionBase {
@Override
protected void makeRHSWhereString(ModelEntity entity, List<EntityConditionParam> entityConditionParams, StringBuilder sb, ModelField field, Object rhs, Datasource datasourceInfo) {
if (rhs == null || rhs == GenericEntity.NULL_FIELD) {
//Debug.logInfo("makeRHSWhereString: field IS NULL: " + field.getName(), module);
sb.append(" IS NULL");
} else {
//Debug.logInfo("makeRHSWhereString: field not null, doing super: " + field.getName() + ", type: " + rhs.getClass().getName() + ", value: " + rhs, module);
super.makeRHSWhereString(entity, entityConditionParams, sb, field, rhs, datasourceInfo);
}
}
@@ -300,7 +300,9 @@ public abstract class EntityOperator<L, R, T> extends EntityConditionBase {
public static final Comparable<?> WILDCARD = new Comparable<Object>() {
public int compareTo(Object obj) {
if (obj != WILDCARD) throw new ClassCastException();
if (obj != WILDCARD) {
throw new ClassCastException();
}
return 0;
}
@@ -88,7 +88,9 @@ public final class EntityWhereString extends EntityCondition {
@Override
public boolean equals(Object obj) {
if (!(obj instanceof EntityWhereString)) return false;
if (!(obj instanceof EntityWhereString)) {
return false;
}
EntityWhereString other = (EntityWhereString) obj;
return equals(sqlString, other.sqlString);
}
@@ -206,7 +206,9 @@ public class OrderByItem implements Comparator<GenericEntity> {
@Override
public boolean equals(java.lang.Object obj) {
if (!(obj instanceof OrderByItem)) return false;
if (!(obj instanceof OrderByItem)) {
return false;
}
OrderByItem that = (OrderByItem) obj;
return getValue().equals(that.getValue()) && getDescending() == that.getDescending();
@@ -88,7 +88,9 @@ public class OrderByList implements Comparator<GenericEntity> {
sb.append(" ORDER BY ");
}
for (int i = 0; i < orderByList.size(); i++) {
if (i != 0) sb.append(", ");
if (i != 0) {
sb.append(", ");
}
OrderByItem orderByItem = orderByList.get(i);
orderByItem.makeOrderByString(sb, modelEntity, includeTablenamePrefix, datasourceInfo);
}
@@ -98,7 +100,9 @@ public class OrderByList implements Comparator<GenericEntity> {
int result = 0;
for (OrderByItem orderByItem: orderByList) {
result = orderByItem.compare(entity1, entity2);
if (result != 0) break;
if (result != 0) {
break;
}
}
return result;
}
@@ -113,7 +117,9 @@ public class OrderByList implements Comparator<GenericEntity> {
@Override
public boolean equals(java.lang.Object obj) {
if (!(obj instanceof OrderByList)) return false;
if (!(obj instanceof OrderByList)) {
return false;
}
OrderByList that = (OrderByList) obj;
return orderByList.equals(that.orderByList);
}