1 16 package org.outerj.daisy.query.model; 17 18 import org.outerj.daisy.query.QueryContext; 19 import org.outerj.daisy.repository.query.QueryException; 20 import org.outerj.daisy.repository.query.EvaluationContext; 21 import org.outerj.daisy.repository.*; 22 23 import java.util.ArrayList ; 24 import java.util.Iterator ; 25 import java.sql.PreparedStatement ; 26 import java.sql.SQLException ; 27 28 public class InCollection extends AbstractPredicateExpr { 29 private final ArrayList literals = new ArrayList (); 30 private DocumentCollection[] collections; 31 32 public void add(Literal literal) { 33 literals.add(literal); 34 } 35 36 public void prepare(QueryContext context) throws QueryException { 37 collections = new DocumentCollection[literals.size()]; 38 int i = 0; 39 Iterator literalsIt = literals.iterator(); 40 while (literalsIt.hasNext()) { 41 Literal literal = (Literal)literalsIt.next(); 42 String collectionName = (String )literal.evaluate(QValueType.STRING); 43 try { 44 collections[i] = context.getCollection(collectionName); 45 } catch (CollectionNotFoundException e) { 46 throw new QueryException("\"" + collectionName + "\" is not a valid collection name."); 47 } catch (RepositoryException e) { 48 throw new QueryException("Error consulting document collection information.", e); 49 } 50 i++; 51 } 52 } 53 54 public boolean evaluate(Document document, Version version, EvaluationContext evaluationContext) throws QueryException { 55 for (int i = 0; i < collections.length; i++) 56 if (document.inCollection(collections[i])) 57 return true; 58 return false; 59 } 60 61 public Tristate appliesTo(Document document) { 62 DocumentCollection[] collections = document.getCollections().getArray(); 63 if (collections.length == 0) 64 return Tristate.MAYBE; 65 66 for (int i = 0; i < collections.length; i++) 67 if (collections[i].getId() == collections[0].getId()) 68 return Tristate.YES; 69 return Tristate.NO; 70 } 71 72 public void generateSql(StringBuffer sql, SqlGenerationContext context) throws QueryException { 73 String alias = context.getNewCollectionsTableAlias(); 76 sql.append(" "); 77 sql.append(alias); 78 sql.append("."); 79 sql.append(SqlGenerationContext.DocsCollectionsTable.COLLECTION_ID); 80 sql.append(" IN("); 81 for (int i = 0; i < collections.length; i++) { 82 if (i == collections.length - 1) 83 sql.append("?"); 84 else 85 sql.append("?,"); 86 } 87 sql.append(")"); 88 } 89 90 public int bindSql(PreparedStatement stmt, int bindPos, EvaluationContext evaluationContext) throws SQLException { 91 for (int i = 0; i < collections.length; i++) { 92 stmt.setLong(bindPos, collections[i].getId()); 93 bindPos++; 94 } 95 return bindPos; 96 } 97 98 public AclConditionViolation isAclAllowed() { 99 return null; 100 } 101 } 102 | Popular Tags |