KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > query > model > And


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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.Version;
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.sql.PreparedStatement JavaDoc;
27 import java.sql.SQLException JavaDoc;
28
29 public class And extends AbstractPredicateExpr {
30     private final ArrayList JavaDoc exprs = new ArrayList JavaDoc(5);
31
32     public And() {
33     }
34
35     public void add(PredicateExpr expr) {
36         exprs.add(expr);
37     }
38
39     public void prepare(QueryContext context) throws QueryException {
40         Iterator JavaDoc exprsIt = exprs.iterator();
41         while (exprsIt.hasNext()) {
42             ((PredicateExpr)exprsIt.next()).prepare(context);
43         }
44     }
45
46     public boolean evaluate(Document document, Version version, EvaluationContext evaluationContext) throws QueryException {
47         Iterator JavaDoc exprsIt = exprs.iterator();
48         while (exprsIt.hasNext()) {
49             boolean result = ((PredicateExpr)exprsIt.next()).evaluate(document, version, evaluationContext);
50             // from the moment one of the AND conditions fails, we don't need to look any further
51
if (!result)
52                 return false;
53         }
54         return true;
55     }
56
57     public AclConditionViolation isAclAllowed() {
58         Iterator JavaDoc exprsIt = exprs.iterator();
59         while (exprsIt.hasNext()) {
60             AclConditionViolation result = ((PredicateExpr)exprsIt.next()).isAclAllowed();
61             if (result != null)
62                 return result;
63         }
64         return null;
65     }
66
67     public Tristate appliesTo(Document document) throws QueryException {
68         Tristate highest = Tristate.MAYBE;
69         Iterator JavaDoc exprsIt = exprs.iterator();
70         while (exprsIt.hasNext()) {
71             Tristate result = ((PredicateExpr)exprsIt.next()).appliesTo(document);
72             if (result == Tristate.NO)
73                 return Tristate.NO;
74             else if (result == Tristate.YES)
75                 highest = Tristate.YES;
76         }
77         return highest;
78     }
79
80     public void generateSql(StringBuffer JavaDoc sql, SqlGenerationContext context) throws QueryException {
81         sql.append(" (");
82         Iterator JavaDoc exprsIt = exprs.iterator();
83         while (exprsIt.hasNext()) {
84             PredicateExpr expr = (PredicateExpr)exprsIt.next();
85             expr.generateSql(sql, context);
86             if (exprsIt.hasNext())
87                 sql.append(" AND ");
88         }
89         sql.append(")");
90     }
91
92     public int bindSql(PreparedStatement JavaDoc stmt, int bindPos, EvaluationContext evaluationContext) throws SQLException JavaDoc, QueryException {
93         Iterator JavaDoc exprsIt = exprs.iterator();
94         while (exprsIt.hasNext()) {
95             PredicateExpr expr = (PredicateExpr)exprsIt.next();
96             bindPos = expr.bindSql(stmt, bindPos, evaluationContext);
97         }
98         return bindPos;
99     }
100 }
101
Popular Tags