KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
22
23 import java.util.ArrayList JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.sql.PreparedStatement JavaDoc;
26 import java.sql.SQLException JavaDoc;
27
28 public class InCollection extends AbstractPredicateExpr {
29     private final ArrayList JavaDoc literals = new ArrayList JavaDoc();
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 JavaDoc literalsIt = literals.iterator();
40         while (literalsIt.hasNext()) {
41             Literal literal = (Literal)literalsIt.next();
42             String JavaDoc collectionName = (String JavaDoc)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 JavaDoc sql, SqlGenerationContext context) throws QueryException {
73         // Note: for each InCollection condition, a new join with the document_collections table
74
// is created, otherwise and conditions wouldn't work.
75
String JavaDoc 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 JavaDoc stmt, int bindPos, EvaluationContext evaluationContext) throws SQLException JavaDoc {
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