KickJava   Java API By Example, From Geeks To Geeks.

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


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.query.QueryContext;
21 import org.outerj.daisy.repository.Document;
22 import org.outerj.daisy.repository.Version;
23
24 import java.util.Iterator JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.sql.PreparedStatement JavaDoc;
27 import java.sql.SQLException JavaDoc;
28
29 public class HasNone extends AbstractMultiArgPredicate {
30     public HasNone(Identifier identifier) {
31         super(identifier);
32     }
33
34     public void prepare(QueryContext context) throws QueryException {
35         super.prepare(context);
36         if (!identifier.isMultiValue())
37             throw new QueryException("The HAS NONE condition can only be used with multivalue fields, which " + identifier.getExpression() + " is not.");
38     }
39
40     public boolean evaluate(Document document, Version version, EvaluationContext evaluationContext) throws QueryException {
41         Object JavaDoc[] values = (Object JavaDoc[])identifier.evaluate(valueType, document, version, evaluationContext);
42         if (values == null)
43             return true;
44         Iterator JavaDoc literalIt = literals.iterator();
45         while (literalIt.hasNext()) {
46             Literal literal = (Literal)literalIt.next();
47             Object JavaDoc value = literal.evaluate(valueType, evaluationContext);
48             for (int i = 0; i < values.length; i++) {
49                 if (value.equals(values[i])) {
50                     return false;
51                 }
52             }
53         }
54         return true;
55     }
56
57     private static final ParamString FIELD_JOIN_EXPR = new ParamString(" left outer join thefields {thefieldsAlias} on (document_variants.doc_id = {thefieldsAlias}.doc_id and document_variants.branch_id = {thefieldsAlias}.branch_id and document_variants.lang_id = {thefieldsAlias}.lang_id and document_variants.{versionColumn} = {thefieldsAlias}.version_id and {thefieldsAlias}.fieldtype_id = {fieldTypeId} and {thefieldsAlias}.{valueColumn} IN ({inParameters}) )");
58     private static final ParamString COLLECTIONS_JOIN_EXPR = new ParamString(" left outer join document_collections {collectionsAlias} on (document_variants.doc_id = {collectionsAlias}.document_id and document_variants.branch_id = {collectionsAlias}.branch_id and document_variants.lang_id = {collectionsAlias}.lang_id and {collectionsAlias}.collection_id IN ({inParameters}) )");
59
60     public void generateSql(StringBuffer JavaDoc sql, SqlGenerationContext context) throws QueryException {
61         if (identifier.getDelegate() instanceof Identifier.FieldIdentifier) {
62             final String JavaDoc alias = context.getNewAliasCounter();
63             final String JavaDoc thefieldsAlias = "fields" + alias;
64
65             SqlGenerationContext.Table fieldTypesTable = new SqlGenerationContext.Table() {
66                 public String JavaDoc getName() {
67                     return "does_not_matter";
68                 }
69
70                 public String JavaDoc getJoinExpression(boolean searchLastVersion) {
71                     StringBuffer JavaDoc inParameters = new StringBuffer JavaDoc();
72                     for (int i = 0; i < literals.size(); i++) {
73                         if (i > 0)
74                             inParameters.append(",");
75                         inParameters.append("?");
76                     }
77
78                     HashMap JavaDoc params = new HashMap JavaDoc();
79                     params.put("thefieldsAlias", thefieldsAlias);
80                     params.put("fieldTypeId", String.valueOf(((Identifier.FieldIdentifier)identifier.getDelegate()).getfieldTypeId()));
81                     params.put("inParameters", inParameters.toString());
82                     params.put("valueColumn", SqlGenerationContext.FieldsTable.getValueColumn(identifier.getValueType()));
83                     params.put("versionColumn", SqlGenerationContext.getVersionField(searchLastVersion));
84                     return FIELD_JOIN_EXPR.toString(params);
85                 }
86
87                 public int bindJoin(PreparedStatement JavaDoc stmt, int bindPos) throws SQLException JavaDoc, QueryException {
88                     Iterator JavaDoc literalsIt = literals.iterator();
89                     while (literalsIt.hasNext()) {
90                         Literal literal = (Literal)literalsIt.next();
91                         if (identifier.isSymbolicIdentifier()) {
92                             Object JavaDoc value = identifier.translateSymbolic(literal, new EvaluationContext());
93                             bindPos = Literal.bindLiteral(stmt, bindPos, valueType, value);
94                         } else {
95                             bindPos = literal.bindValueExpr(stmt, bindPos, valueType, new EvaluationContext());
96                         }
97                     }
98                     return bindPos;
99                 }
100             };
101             context.needsJoinWithTable(fieldTypesTable);
102
103             sql.append(" ").append(thefieldsAlias).append(".fieldtype_id is null ");
104         } else if (identifier.getDelegate() instanceof Identifier.CollectionsIdentifier) {
105             final String JavaDoc alias = context.getNewAliasCounter();
106             final String JavaDoc collectionsAlias = "collections" + alias;
107
108             SqlGenerationContext.Table collectionsTable = new SqlGenerationContext.Table() {
109                 public String JavaDoc getName() {
110                     return "does_not_matter";
111                 }
112
113                 public String JavaDoc getJoinExpression(boolean searchLastVersion) {
114                     StringBuffer JavaDoc inParameters = new StringBuffer JavaDoc();
115                     for (int i = 0; i < literals.size(); i++) {
116                         if (i > 0)
117                             inParameters.append(",");
118                         inParameters.append("?");
119                     }
120
121                     HashMap JavaDoc params = new HashMap JavaDoc();
122                     params.put("collectionsAlias", collectionsAlias);
123                     params.put("inParameters", inParameters.toString());
124                     return COLLECTIONS_JOIN_EXPR.toString(params);
125                 }
126
127                 public int bindJoin(PreparedStatement JavaDoc stmt, int bindPos) throws SQLException JavaDoc, QueryException {
128                     Iterator JavaDoc literalsIt = literals.iterator();
129                     while (literalsIt.hasNext()) {
130                         Literal literal = (Literal)literalsIt.next();
131                         if (identifier.isSymbolicIdentifier()) {
132                             Object JavaDoc value = identifier.translateSymbolic(literal, new EvaluationContext());
133                             bindPos = Literal.bindLiteral(stmt, bindPos, valueType, value);
134                         } else {
135                             bindPos = literal.bindValueExpr(stmt, bindPos, valueType, new EvaluationContext());
136                         }
137                     }
138                     return bindPos;
139                 }
140             };
141             context.needsJoinWithTable(collectionsTable);
142
143             sql.append(" ").append(collectionsAlias).append(".collection_id is null ");
144         } else {
145             throw new RuntimeException JavaDoc("Unsupported identifier type in HAS NONE: " + identifier.getExpression());
146         }
147     }
148
149     public int bindSql(PreparedStatement JavaDoc stmt, int bindPos, EvaluationContext evaluationContext) throws SQLException JavaDoc {
150         return bindPos;
151     }
152
153     public AclConditionViolation isAclAllowed() {
154         return null;
155     }
156
157     public Tristate appliesTo(Document document) {
158         return Tristate.MAYBE;
159     }
160
161 }
162
Popular Tags