1 24 25 package org.ofbiz.entity.condition; 26 27 import java.util.ArrayList ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 import java.util.Map ; 31 32 import org.ofbiz.entity.GenericDelegator; 33 import org.ofbiz.entity.GenericEntity; 34 import org.ofbiz.entity.GenericModelException; 35 import org.ofbiz.entity.model.ModelEntity; 36 37 46 public class EntityJoinOperator extends EntityOperator { 47 48 protected boolean shortCircuitValue; 49 50 protected EntityJoinOperator(int id, String code, boolean shortCircuitValue) { 51 super(id, code); 52 this.shortCircuitValue = shortCircuitValue; 53 } 54 55 public void addSqlValue(StringBuffer sql, ModelEntity modelEntity, List entityConditionParams, boolean compat, Object lhs, Object rhs) { 56 sql.append('('); 57 sql.append(((EntityCondition) lhs).makeWhereString(modelEntity, entityConditionParams)); 58 sql.append(' '); 59 sql.append(getCode()); 60 sql.append(' '); 61 if (rhs instanceof EntityCondition) { 62 sql.append(((EntityCondition) rhs).makeWhereString(modelEntity, entityConditionParams)); 63 } else { 64 addValue(sql, null, rhs, entityConditionParams); 65 } 66 sql.append(')'); 67 } 68 69 public void addSqlValue(StringBuffer sql, ModelEntity modelEntity, List entityConditionParams, List conditionList) { 70 if (conditionList != null && conditionList.size() > 0) { 71 sql.append('('); 72 Iterator conditionIter = conditionList.iterator(); 73 while (conditionIter.hasNext()) { 74 EntityCondition condition = (EntityCondition) conditionIter.next(); 75 sql.append(condition.makeWhereString(modelEntity, entityConditionParams)); 76 if (conditionIter.hasNext()) { 77 sql.append(' '); 78 sql.append(getCode()); 79 sql.append(' '); 80 } 81 } 82 sql.append(')'); 83 } 84 } 85 86 protected EntityCondition freeze(Object item) { 87 return ((EntityCondition) item).freeze(); 88 } 89 90 public EntityCondition freeze(Object lhs, Object rhs) { 91 return new EntityExpr(freeze(lhs), this, freeze(rhs)); 92 } 93 94 public EntityCondition freeze(List conditionList) { 95 List newList = new ArrayList (conditionList.size()); 96 for (int i = 0; i < conditionList.size(); i++) { 97 EntityCondition condition = (EntityCondition) conditionList.get(i); 98 newList.add(condition.freeze()); 99 } 100 return new EntityConditionList(newList, this); 101 } 102 103 public void visit(EntityConditionVisitor visitor, List conditionList) { 104 if (conditionList != null && conditionList.size() > 0) { 105 for (int i = 0; i < conditionList.size(); i++) { 106 visitor.visit(conditionList.get(i)); 107 } 108 } 109 } 110 111 public void visit(EntityConditionVisitor visitor, Object lhs, Object rhs) { 112 ((EntityCondition) lhs).visit(visitor); 113 visitor.visit(rhs); 114 } 115 116 public boolean entityMatches(GenericEntity entity, Object lhs, Object rhs) { 117 return entityMatches(entity, (EntityCondition) lhs, (EntityCondition) rhs); 118 } 119 120 public Object eval(GenericEntity entity, EntityCondition lhs, EntityCondition rhs) { 121 return entityMatches(entity, lhs, rhs) ? Boolean.TRUE : Boolean.FALSE; 122 } 123 124 public boolean entityMatches(GenericEntity entity, EntityCondition lhs, EntityCondition rhs) { 125 if (lhs.entityMatches(entity)) return shortCircuitValue; 126 if (rhs.entityMatches(entity)) return shortCircuitValue; 127 return !shortCircuitValue; 128 } 129 130 public boolean entityMatches(GenericEntity entity, List conditionList) { 131 return mapMatches(entity.getDelegator(), entity, conditionList); 132 } 133 134 public Object eval(GenericDelegator delegator, Map map, Object lhs, Object rhs) { 135 return castBoolean(mapMatches(delegator, map, lhs, rhs)); 136 } 137 138 public boolean mapMatches(GenericDelegator delegator, Map map, Object lhs, Object rhs) { 139 if (((EntityCondition) lhs).mapMatches(delegator, map)) return shortCircuitValue; 140 if (((EntityCondition) rhs).mapMatches(delegator, map)) return shortCircuitValue; 141 return !shortCircuitValue; 142 } 143 144 public Object eval(GenericDelegator delegator, Map map, List conditionList) { 145 return castBoolean(mapMatches(delegator, map, conditionList)); 146 } 147 148 public boolean mapMatches(GenericDelegator delegator, Map map, List conditionList) { 149 if (conditionList != null && conditionList.size() > 0) { 150 for (int i = 0; i < conditionList.size(); i++) { 151 EntityCondition condition = (EntityCondition) conditionList.get(i); 152 if (condition.mapMatches(delegator, map) == shortCircuitValue) return shortCircuitValue; 153 } 154 } 155 return !shortCircuitValue; 156 } 157 158 public void validateSql(ModelEntity modelEntity, Object lhs, Object rhs) throws GenericModelException { 159 validateSql(modelEntity, (EntityCondition) lhs, (EntityCondition) rhs); 160 } 161 162 public void validateSql(ModelEntity modelEntity, EntityCondition lhs, EntityCondition rhs) throws GenericModelException { 163 lhs.checkCondition(modelEntity); 164 rhs.checkCondition(modelEntity); 165 } 166 167 public void validateSql(ModelEntity modelEntity, List conditionList) throws GenericModelException { 168 if (conditionList == null && conditionList.size() == 0) 169 throw new GenericModelException("Empty list for joining"); 170 for (int i = 0; i < conditionList.size(); i++) { 171 Object condObj = conditionList.get(i); 172 if (!(condObj instanceof EntityCondition)) { 173 throw new GenericModelException("Object is not a valid EntityCondition [" + condObj.getClass().getName() + "]"); 174 } 175 EntityCondition condition = (EntityCondition) condObj; 176 condition.checkCondition(modelEntity); 177 } 178 } 179 } 180 | Popular Tags |