KickJava   Java API By Example, From Geeks To Geeks.

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


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.RepositoryException;
23 import org.outerj.daisy.repository.AvailableVariant;
24 import org.outerj.daisy.repository.Version;
25
26 import java.sql.PreparedStatement JavaDoc;
27 import java.sql.SQLException JavaDoc;
28 import java.util.HashMap JavaDoc;
29
30 public class DoesNotHaveVariant extends AbstractPredicateExpr {
31     private String JavaDoc branch;
32     private String JavaDoc language;
33     private long branchId;
34     private long languageId;
35
36     public DoesNotHaveVariant(String JavaDoc branch, String JavaDoc language) {
37         this.branch = branch;
38         this.language = language;
39     }
40
41     public void prepare(QueryContext context) throws QueryException {
42         if (Character.isDigit(branch.charAt(0))) {
43             try {
44                 branchId = Long.parseLong(branch);
45             } catch (NumberFormatException JavaDoc e) {
46                 throw new QueryException("Invalid branch ID in DoesNotHaveVariant: \"" + branch + "\".");
47             }
48         } else {
49             try {
50                 branchId = context.getBranchByName(branch).getId();
51             } catch (RepositoryException e) {
52                 throw new QueryException("Problem in DoesNotHaveVariant with branch name \"" + branch + "\".");
53             }
54         }
55
56         if (Character.isDigit(language.charAt(0))) {
57             try {
58                 languageId = Long.parseLong(language);
59             } catch (NumberFormatException JavaDoc e) {
60                 throw new QueryException("Invalid language ID in DoesNotHaveVariant: \"" + language + "\".");
61             }
62         } else {
63             try {
64                 languageId = context.getLanguageByName(language).getId();
65             } catch (RepositoryException e) {
66                 throw new QueryException("Problem in DoesNotHaveVariant with language name \"" + language + "\".");
67             }
68         }
69     }
70
71     public boolean evaluate(Document document, Version version, EvaluationContext evaluationContext) throws QueryException {
72         AvailableVariant[] availableVariants;
73         try {
74             availableVariants = document.getAvailableVariants().getArray();
75         } catch (RepositoryException e) {
76             throw new QueryException("DoesNotHaveVariant: problem retrieving variants.", e);
77         }
78         for (int i = 0; i < availableVariants.length; i++) {
79             if (availableVariants[i].getBranchId() == branchId && availableVariants[i].getLanguageId() == languageId)
80                 return false;
81         }
82         return true;
83     }
84
85     private static final ParamString DOCVARIANTS_JOIN_EXPR = new ParamString(" left join document_variants {alias} on (document_variants.doc_id = {alias}.doc_id and {alias}.branch_id = {branchId} and {alias}.lang_id = {languageId})");
86
87     public void generateSql(StringBuffer JavaDoc sql, SqlGenerationContext context) throws QueryException {
88         final String JavaDoc alias = context.getNewAliasCounter();
89         final String JavaDoc docvariantsAlias = "docvariants" + alias;
90
91         SqlGenerationContext.Table fieldTypesTable = new SqlGenerationContext.Table() {
92             public String JavaDoc getName() {
93                 return "does_not_matter";
94             }
95
96             public String JavaDoc getJoinExpression(boolean searchLastVersion) {
97                 HashMap JavaDoc params = new HashMap JavaDoc();
98                 params.put("alias", docvariantsAlias);
99                 params.put("branchId", String.valueOf(branchId));
100                 params.put("languageId", String.valueOf(languageId));
101                 return DOCVARIANTS_JOIN_EXPR.toString(params);
102             }
103
104             public int bindJoin(PreparedStatement JavaDoc stmt, int bindPos) {
105                 return bindPos;
106             }
107         };
108         context.needsJoinWithTable(fieldTypesTable);
109
110         sql.append(" ").append(docvariantsAlias).append(".doc_id is null ");
111     }
112
113     public int bindSql(PreparedStatement JavaDoc stmt, int bindPos, EvaluationContext evaluationContext) throws SQLException JavaDoc {
114         return bindPos;
115     }
116
117     public AclConditionViolation isAclAllowed() {
118         return new AclConditionViolation("DoesNotHaveVariant is not allowed in ACL conditions");
119     }
120
121     public Tristate appliesTo(Document document) {
122         throw new IllegalStateException JavaDoc();
123     }
124 }
125
Popular Tags