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.Document; 22 import org.outerj.daisy.repository.RepositoryException; 23 import org.outerj.daisy.repository.Version; 24 import org.outerj.daisy.repository.schema.PartType; 25 26 import java.sql.PreparedStatement ; 27 import java.sql.SQLException ; 28 29 public class HasPart extends AbstractPredicateExpr { 30 private final String partTypeName; 31 private PartType partType; 32 33 public HasPart(String partTypeName) { 34 this.partTypeName = partTypeName; 35 } 36 37 public void prepare(QueryContext context) throws QueryException { 38 try { 39 this.partType = context.getPartTypeByName(partTypeName); 40 } catch (RepositoryException e) { 41 throw new QueryException("Error retrieving PartType named \"" + partTypeName + "\".", e); 42 } 43 } 44 45 public boolean evaluate(Document document, Version version, EvaluationContext evaluationContext) throws QueryException { 46 if (version != null) 47 return version.hasPart(partType.getId()); 48 else 49 return document.hasPart(partType.getId()); 50 } 51 52 public void generateSql(StringBuffer sql, SqlGenerationContext context) throws QueryException { 53 String alias = context.getNewPartsTableAlias(); 54 55 sql.append(alias); 56 sql.append('.'); 57 sql.append(SqlGenerationContext.PartsTable.PARTTYPE_ID); 58 sql.append(" = ? "); 59 } 60 61 public int bindSql(PreparedStatement stmt, int bindPos, EvaluationContext evaluationContext) throws SQLException { 62 stmt.setLong(bindPos, partType.getId()); 63 bindPos++; 64 return bindPos; 65 } 66 67 public AclConditionViolation isAclAllowed() { 68 return new AclConditionViolation("HasPart is not allowed in ACL conditions"); 69 } 70 71 public Tristate appliesTo(Document document) { 72 throw new IllegalStateException (); 73 } 74 } 75 | Popular Tags |