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.query.QueryContext; 21 import org.outerj.daisy.repository.Document; 22 import org.outerj.daisy.repository.Version; 23 24 import java.util.Iterator ; 25 import java.util.HashMap ; 26 import java.sql.PreparedStatement ; 27 import java.sql.SQLException ; 28 29 public class HasNone extends AbstractMultiArgPredicate { 30 public HasNone(Identifier identifier) { 31 super(identifier); 32 } 33 34 public void prepare(QueryContext context) throws QueryException { 35 super.prepare(context); 36 if (!identifier.isMultiValue()) 37 throw new QueryException("The HAS NONE condition can only be used with multivalue fields, which " + identifier.getExpression() + " is not."); 38 } 39 40 public boolean evaluate(Document document, Version version, EvaluationContext evaluationContext) throws QueryException { 41 Object [] values = (Object [])identifier.evaluate(valueType, document, version, evaluationContext); 42 if (values == null) 43 return true; 44 Iterator literalIt = literals.iterator(); 45 while (literalIt.hasNext()) { 46 Literal literal = (Literal)literalIt.next(); 47 Object value = literal.evaluate(valueType, evaluationContext); 48 for (int i = 0; i < values.length; i++) { 49 if (value.equals(values[i])) { 50 return false; 51 } 52 } 53 } 54 return true; 55 } 56 57 private static final ParamString FIELD_JOIN_EXPR = new ParamString(" left outer join thefields {thefieldsAlias} on (document_variants.doc_id = {thefieldsAlias}.doc_id and document_variants.branch_id = {thefieldsAlias}.branch_id and document_variants.lang_id = {thefieldsAlias}.lang_id and document_variants.{versionColumn} = {thefieldsAlias}.version_id and {thefieldsAlias}.fieldtype_id = {fieldTypeId} and {thefieldsAlias}.{valueColumn} IN ({inParameters}) )"); 58 private static final ParamString COLLECTIONS_JOIN_EXPR = new ParamString(" left outer join document_collections {collectionsAlias} on (document_variants.doc_id = {collectionsAlias}.document_id and document_variants.branch_id = {collectionsAlias}.branch_id and document_variants.lang_id = {collectionsAlias}.lang_id and {collectionsAlias}.collection_id IN ({inParameters}) )"); 59 60 public void generateSql(StringBuffer sql, SqlGenerationContext context) throws QueryException { 61 if (identifier.getDelegate() instanceof Identifier.FieldIdentifier) { 62 final String alias = context.getNewAliasCounter(); 63 final String thefieldsAlias = "fields" + alias; 64 65 SqlGenerationContext.Table fieldTypesTable = new SqlGenerationContext.Table() { 66 public String getName() { 67 return "does_not_matter"; 68 } 69 70 public String getJoinExpression(boolean searchLastVersion) { 71 StringBuffer inParameters = new StringBuffer (); 72 for (int i = 0; i < literals.size(); i++) { 73 if (i > 0) 74 inParameters.append(","); 75 inParameters.append("?"); 76 } 77 78 HashMap params = new HashMap (); 79 params.put("thefieldsAlias", thefieldsAlias); 80 params.put("fieldTypeId", String.valueOf(((Identifier.FieldIdentifier)identifier.getDelegate()).getfieldTypeId())); 81 params.put("inParameters", inParameters.toString()); 82 params.put("valueColumn", SqlGenerationContext.FieldsTable.getValueColumn(identifier.getValueType())); 83 params.put("versionColumn", SqlGenerationContext.getVersionField(searchLastVersion)); 84 return FIELD_JOIN_EXPR.toString(params); 85 } 86 87 public int bindJoin(PreparedStatement stmt, int bindPos) throws SQLException , QueryException { 88 Iterator literalsIt = literals.iterator(); 89 while (literalsIt.hasNext()) { 90 Literal literal = (Literal)literalsIt.next(); 91 if (identifier.isSymbolicIdentifier()) { 92 Object value = identifier.translateSymbolic(literal, new EvaluationContext()); 93 bindPos = Literal.bindLiteral(stmt, bindPos, valueType, value); 94 } else { 95 bindPos = literal.bindValueExpr(stmt, bindPos, valueType, new EvaluationContext()); 96 } 97 } 98 return bindPos; 99 } 100 }; 101 context.needsJoinWithTable(fieldTypesTable); 102 103 sql.append(" ").append(thefieldsAlias).append(".fieldtype_id is null "); 104 } else if (identifier.getDelegate() instanceof Identifier.CollectionsIdentifier) { 105 final String alias = context.getNewAliasCounter(); 106 final String collectionsAlias = "collections" + alias; 107 108 SqlGenerationContext.Table collectionsTable = new SqlGenerationContext.Table() { 109 public String getName() { 110 return "does_not_matter"; 111 } 112 113 public String getJoinExpression(boolean searchLastVersion) { 114 StringBuffer inParameters = new StringBuffer (); 115 for (int i = 0; i < literals.size(); i++) { 116 if (i > 0) 117 inParameters.append(","); 118 inParameters.append("?"); 119 } 120 121 HashMap params = new HashMap (); 122 params.put("collectionsAlias", collectionsAlias); 123 params.put("inParameters", inParameters.toString()); 124 return COLLECTIONS_JOIN_EXPR.toString(params); 125 } 126 127 public int bindJoin(PreparedStatement stmt, int bindPos) throws SQLException , QueryException { 128 Iterator literalsIt = literals.iterator(); 129 while (literalsIt.hasNext()) { 130 Literal literal = (Literal)literalsIt.next(); 131 if (identifier.isSymbolicIdentifier()) { 132 Object value = identifier.translateSymbolic(literal, new EvaluationContext()); 133 bindPos = Literal.bindLiteral(stmt, bindPos, valueType, value); 134 } else { 135 bindPos = literal.bindValueExpr(stmt, bindPos, valueType, new EvaluationContext()); 136 } 137 } 138 return bindPos; 139 } 140 }; 141 context.needsJoinWithTable(collectionsTable); 142 143 sql.append(" ").append(collectionsAlias).append(".collection_id is null "); 144 } else { 145 throw new RuntimeException ("Unsupported identifier type in HAS NONE: " + identifier.getExpression()); 146 } 147 } 148 149 public int bindSql(PreparedStatement stmt, int bindPos, EvaluationContext evaluationContext) throws SQLException { 150 return bindPos; 151 } 152 153 public AclConditionViolation isAclAllowed() { 154 return null; 155 } 156 157 public Tristate appliesTo(Document document) { 158 return Tristate.MAYBE; 159 } 160 161 } 162 | Popular Tags |