| 1 16 package org.outerj.daisy.query.model; 17 18 import org.outerj.daisy.repository.query.QueryException; 19 import org.outerj.daisy.repository.query.EvaluationContext; 20 import org.outerj.daisy.repository.Document; 21 import org.outerj.daisy.repository.Version; 22 23 import java.util.Iterator ; 24 import java.sql.PreparedStatement ; 25 import java.sql.SQLException ; 26 27 public class In extends AbstractMultiArgPredicate { 28 private final boolean not; 29 30 public In(boolean not, Identifier identifier) { 31 super(identifier); 32 this.not = not; 33 } 34 35 public boolean evaluate(Document document, Version version, EvaluationContext evaluationContext) throws QueryException { 36 Object value = identifier.evaluate(valueType, document, version, evaluationContext); 37 38 if (value == null) 39 return false; 40 41 return evaluate(value, evaluationContext); 42 } 43 44 private boolean evaluate(Object value, EvaluationContext evaluationContext) throws QueryException { 45 Object [] values = value instanceof Object [] ? (Object [])value : null; 46 Iterator literalsIt = literals.iterator(); 47 while (literalsIt.hasNext()) { 48 Literal literal = (Literal)literalsIt.next(); 49 if (values == null) { 50 if (literal.evaluate(valueType, evaluationContext).equals(value)) 51 return !not; 52 } else { 53 Object literalValue = literal.evaluate(valueType, evaluationContext); 54 for (int i = 0; i < values.length; i++) 55 if (literalValue.equals(values[i])) 56 return !not; 57 } 58 } 59 return not; 60 } 61 62 public void generateSql(StringBuffer sql, SqlGenerationContext context) throws QueryException { 63 sql.append(" ("); 64 String preCond = identifier.getSqlPreConditions(context); 65 if (preCond != null) 66 sql.append(preCond).append(" and "); 67 identifier.generateSqlValueExpr(sql, context); 68 if (not) 69 sql.append(" NOT"); 70 sql.append(" IN ("); 71 final int literalCount = literals.size(); 72 for (int i = 0; i < literalCount; i++) { 73 if (i < literalCount - 1) 74 sql.append("?,"); 75 else 76 sql.append("?"); 77 } 78 sql.append("))"); 79 } 80 81 public int bindSql(PreparedStatement stmt, int bindPos, EvaluationContext evaluationContext) throws SQLException , QueryException { 82 bindPos = identifier.bindPreConditions(stmt, bindPos); 83 bindPos = identifier.bindValueExpr(stmt, bindPos, valueType, evaluationContext); 84 Iterator literalsIt = literals.iterator(); 85 while (literalsIt.hasNext()) { 86 Literal literal = (Literal)literalsIt.next(); 87 if (identifier.isSymbolicIdentifier()) { 88 Object value = identifier.translateSymbolic(literal, evaluationContext); 89 bindPos = Literal.bindLiteral(stmt, bindPos, valueType, value); 90 } else { 91 bindPos = literal.bindValueExpr(stmt, bindPos, valueType, evaluationContext); 92 } 93 } 94 return bindPos; 95 } 96 97 public AclConditionViolation isAclAllowed() { 98 return identifier.isAclAllowed(); 99 } 100 101 public Tristate appliesTo(Document document) throws QueryException { 102 if (identifier.canTestAppliesTo()) { 103 boolean result = evaluate(identifier.evaluate(null, document, null, new EvaluationContext()), new EvaluationContext()); 104 return result ? Tristate.YES : Tristate.NO; 105 } else { 106 return Tristate.MAYBE; 107 } 108 } 109 } 110 | Popular Tags |