KickJava   Java API By Example, From Geeks To Geeks.

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


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.repository.query.QueryException;
19 import org.outerj.daisy.repository.query.EvaluationContext;
20 import org.outerj.daisy.repository.Document;
21 import org.outerj.daisy.repository.Version;
22
23 import java.util.Iterator JavaDoc;
24 import java.sql.PreparedStatement JavaDoc;
25 import java.sql.SQLException JavaDoc;
26
27 public class In extends AbstractMultiArgPredicate {
28     private final boolean not;
29
30     public In(boolean not, Identifier identifier) {
31         super(identifier);
32         this.not = not;
33     }
34
35     public boolean evaluate(Document document, Version version, EvaluationContext evaluationContext) throws QueryException {
36         Object JavaDoc value = identifier.evaluate(valueType, document, version, evaluationContext);
37
38         if (value == null)
39             return false;
40
41         return evaluate(value, evaluationContext);
42     }
43
44     private boolean evaluate(Object JavaDoc value, EvaluationContext evaluationContext) throws QueryException {
45         Object JavaDoc[] values = value instanceof Object JavaDoc[] ? (Object JavaDoc[])value : null;
46         Iterator JavaDoc literalsIt = literals.iterator();
47         while (literalsIt.hasNext()) {
48             Literal literal = (Literal)literalsIt.next();
49             if (values == null) {
50                 if (literal.evaluate(valueType, evaluationContext).equals(value))
51                     return !not;
52             } else {
53                 Object JavaDoc literalValue = literal.evaluate(valueType, evaluationContext);
54                 for (int i = 0; i < values.length; i++)
55                     if (literalValue.equals(values[i]))
56                         return !not;
57             }
58         }
59         return not;
60     }
61
62     public void generateSql(StringBuffer JavaDoc sql, SqlGenerationContext context) throws QueryException {
63         sql.append(" (");
64         String JavaDoc preCond = identifier.getSqlPreConditions(context);
65         if (preCond != null)
66             sql.append(preCond).append(" and ");
67         identifier.generateSqlValueExpr(sql, context);
68         if (not)
69             sql.append(" NOT");
70         sql.append(" IN (");
71         final int literalCount = literals.size();
72         for (int i = 0; i < literalCount; i++) {
73             if (i < literalCount - 1)
74                 sql.append("?,");
75             else
76                 sql.append("?");
77         }
78         sql.append("))");
79     }
80
81     public int bindSql(PreparedStatement JavaDoc stmt, int bindPos, EvaluationContext evaluationContext) throws SQLException JavaDoc, QueryException {
82         bindPos = identifier.bindPreConditions(stmt, bindPos);
83         bindPos = identifier.bindValueExpr(stmt, bindPos, valueType, evaluationContext);
84         Iterator JavaDoc literalsIt = literals.iterator();
85         while (literalsIt.hasNext()) {
86             Literal literal = (Literal)literalsIt.next();
87             if (identifier.isSymbolicIdentifier()) {
88                 Object JavaDoc value = identifier.translateSymbolic(literal, evaluationContext);
89                 bindPos = Literal.bindLiteral(stmt, bindPos, valueType, value);
90             } else {
91                 bindPos = literal.bindValueExpr(stmt, bindPos, valueType, evaluationContext);
92             }
93         }
94         return bindPos;
95     }
96
97     public AclConditionViolation isAclAllowed() {
98         return identifier.isAclAllowed();
99     }
100
101     public Tristate appliesTo(Document document) throws QueryException {
102         if (identifier.canTestAppliesTo()) {
103             boolean result = evaluate(identifier.evaluate(null, document, null, new EvaluationContext()), new EvaluationContext());
104             return result ? Tristate.YES : Tristate.NO;
105         } else {
106             return Tristate.MAYBE;
107         }
108     }
109 }
110
Popular Tags