KickJava   Java API By Example, From Geeks To Geeks.

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


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 Or extends AbstractPredicateExpr {
30     private final ArrayList JavaDoc exprs = new ArrayList JavaDoc(5);
31
32     public Or() {
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 OR conditions is succesful, we don't need to look any further
51
if (result)
52                 return true;
53         }
54         return false;
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 void generateSql(StringBuffer JavaDoc sql, SqlGenerationContext context) throws QueryException {
68         sql.append(" (");
69         Iterator JavaDoc exprsIt = exprs.iterator();
70         while (exprsIt.hasNext()) {
71             PredicateExpr expr = (PredicateExpr)exprsIt.next();
72             expr.generateSql(sql, context);
73             if (exprsIt.hasNext())
74                 sql.append(" OR ");
75         }
76         sql.append(")");
77     }
78
79     public int bindSql(PreparedStatement JavaDoc stmt, int bindPos, EvaluationContext evaluationContext) throws SQLException JavaDoc, QueryException {
80         Iterator JavaDoc exprsIt = exprs.iterator();
81         while (exprsIt.hasNext()) {
82             PredicateExpr expr = (PredicateExpr)exprsIt.next();
83             bindPos = expr.bindSql(stmt, bindPos, evaluationContext);
84         }
85         return bindPos;
86     }
87
88     public Tristate appliesTo(Document document) throws QueryException {
89         Tristate highest = Tristate.NO;
90         Iterator JavaDoc exprsIt = exprs.iterator();
91         while (exprsIt.hasNext()) {
92             Tristate result = ((PredicateExpr)exprsIt.next()).appliesTo(document);
93             if (result == Tristate.YES)
94                 return Tristate.YES;
95             else if (result == Tristate.MAYBE)
96                 highest = Tristate.MAYBE;
97         }
98         return highest;
99     }
100 }
101
Popular Tags