KickJava   Java API By Example, From Geeks To Geeks.

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


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.sql.PreparedStatement JavaDoc;
25 import java.sql.SQLException JavaDoc;
26 import java.util.HashMap JavaDoc;
27
28 public class IsNull extends AbstractPredicateExpr {
29     private final Identifier identifier;
30
31     public IsNull(Identifier identifier) {
32         this.identifier = identifier;
33     }
34
35     public void prepare(QueryContext context) throws QueryException {
36         identifier.prepare(context);
37         // Note: reason of this limitation: see generateSql method
38
if (!(identifier.getDelegate() instanceof Identifier.FieldIdentifier || identifier.getDelegate() instanceof Identifier.CustomFieldIdentifier)) {
39             throw new QueryException("Operator IS NULL is only allowed on fields.");
40         }
41     }
42
43     public boolean evaluate(Document document, Version version, EvaluationContext evaluationContext) throws QueryException {
44         Object JavaDoc value = identifier.evaluate(null, document, version, new EvaluationContext());
45         return evaluate(value);
46     }
47
48     public boolean evaluate(Object JavaDoc value) {
49         return value == null;
50     }
51
52     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}) ");
53
54     private static final ParamString CUSTOMFIELDS_JOIN_EXPR = new ParamString(" left outer join customfields {customFieldsAlias1} on (document_variants.doc_id = {customFieldsAlias1}.doc_id and document_variants.branch_id = {customFieldsAlias1}.branch_id and document_variants.lang_id = {customFieldsAlias1}.lang_id and {customFieldsAlias1}.name = '{name}')");
55
56     public void generateSql(StringBuffer JavaDoc sql, SqlGenerationContext context) throws QueryException {
57         // IS NULL is a bit of a special case. It is (currently) only supported for
58
// fields and custom fields (see check in prepare method). It doesn't depend on the SQL generation by
59
// the identifier but replaces it.
60
if (identifier.getDelegate() instanceof Identifier.FieldIdentifier) {
61             final String JavaDoc alias = context.getNewAliasCounter();
62             final String JavaDoc thefieldsAlias = "fields" + alias;
63
64             SqlGenerationContext.Table fieldTypesTable = new SqlGenerationContext.Table() {
65                 public String JavaDoc getName() {
66                     return "does_not_matter";
67                 }
68
69                 public String JavaDoc getJoinExpression(boolean searchLastVersion) {
70                     HashMap JavaDoc params = new HashMap JavaDoc();
71                     params.put("thefieldsAlias", thefieldsAlias);
72                     params.put("fieldTypeId", String.valueOf(((Identifier.FieldIdentifier)identifier.getDelegate()).getfieldTypeId()));
73                     params.put("versionColumn", SqlGenerationContext.getVersionField(searchLastVersion));
74                     return FIELD_JOIN_EXPR.toString(params);
75                 }
76
77                 public int bindJoin(PreparedStatement JavaDoc stmt, int bindPos) {
78                     return bindPos;
79                 }
80             };
81             context.needsJoinWithTable(fieldTypesTable);
82
83             sql.append(" ").append(thefieldsAlias).append('.').append(SqlGenerationContext.FieldsTable.FIELDTYPE_ID).append(" IS NULL");
84         } else if (identifier.getDelegate() instanceof Identifier.CustomFieldIdentifier) {
85             final String JavaDoc alias = context.getNewAliasCounter();
86             final String JavaDoc customFieldsAlias1 = "customfields" + alias;
87
88             SqlGenerationContext.Table extraCustomFieldsTable = new SqlGenerationContext.Table() {
89                 public String JavaDoc getName() {
90                     return "does_not_matter";
91                 }
92
93                 public String JavaDoc getJoinExpression(boolean searchLastVersion) {
94                     HashMap JavaDoc params = new HashMap JavaDoc();
95                     params.put("customFieldsAlias1", customFieldsAlias1);
96                     params.put("name", SqlUtils.escapeString(identifier.getExpression().substring(1)));
97                     return CUSTOMFIELDS_JOIN_EXPR.toString(params);
98                 }
99
100                 public int bindJoin(PreparedStatement JavaDoc stmt, int bindPos) {
101                     return bindPos;
102                 }
103             };
104             context.needsJoinWithTable(extraCustomFieldsTable);
105
106             sql.append(customFieldsAlias1).append(".name IS NULL");
107         }
108     }
109
110     public int bindSql(PreparedStatement JavaDoc stmt, int bindPos, EvaluationContext evaluationContext) throws SQLException JavaDoc, QueryException {
111         if (identifier.getDelegate() instanceof Identifier.CustomFieldIdentifier) {
112             // do nothing
113
} else if (identifier.getDelegate() instanceof Identifier.FieldIdentifier) {
114             // do nothing
115
} else {
116             bindPos = identifier.bindPreConditions(stmt, bindPos);
117             bindPos = identifier.bindValueExpr(stmt, bindPos, null, evaluationContext);
118         }
119         return bindPos;
120     }
121
122     public AclConditionViolation isAclAllowed() {
123         return identifier.isAclAllowed();
124     }
125
126     public Tristate appliesTo(Document document) throws QueryException {
127         if (identifier.canTestAppliesTo()) {
128             boolean result = evaluate(identifier.evaluate(null, document, null, new EvaluationContext()));
129             return result ? Tristate.YES : Tristate.NO;
130         } else {
131             return Tristate.MAYBE;
132         }
133     }
134 }
135
Popular Tags