KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.outerj.daisy.linkextraction.LinkType;
24
25 import java.sql.PreparedStatement JavaDoc;
26 import java.sql.SQLException JavaDoc;
27
28 public class LinksToOrFrom extends AbstractPredicateExpr {
29     private final long documentId;
30     private long branchId = -1;
31     private long languageId = -1;
32     private String JavaDoc branch;
33     private String JavaDoc language;
34     private final boolean inLastVersion;
35     private final boolean inLiveVersion;
36     private final LinkType[] linkTypes;
37     private final boolean fromMode;
38
39     public LinksToOrFrom(long documentId, boolean inLastVersion, boolean inLiveVersion, LinkType[] linkTypes, boolean fromMode) {
40         this.documentId = documentId;
41         this.inLastVersion = inLastVersion;
42         this.inLiveVersion = inLiveVersion;
43         this.linkTypes = linkTypes;
44         this.fromMode = fromMode;
45     }
46
47     public LinksToOrFrom(long documentId, String JavaDoc branch, String JavaDoc language, boolean inLastVersion, boolean inLiveVersion, LinkType[] linkTypes, boolean fromMode) {
48         this.documentId = documentId;
49         this.inLastVersion = inLastVersion;
50         this.inLiveVersion = inLiveVersion;
51         this.branch = branch;
52         this.language = language;
53         this.linkTypes = linkTypes;
54         this.fromMode = fromMode;
55     }
56
57     public void prepare(QueryContext context) throws QueryException {
58         if (branch != null)
59             branchId = SqlUtils.parseBranch(branch, context);
60         if (language != null)
61             languageId = SqlUtils.parseLanguage(language, context);
62     }
63
64     public boolean evaluate(Document document, Version version, EvaluationContext evaluationContext) throws QueryException {
65         throw new RuntimeException JavaDoc("LinksTo/LinksToVariant/LinksFrom/LinksFromVariant cannot be dynamically evaluated.");
66     }
67
68     public void generateSql(StringBuffer JavaDoc sql, SqlGenerationContext context) throws QueryException {
69         if (fromMode)
70             context.needsJoinWithTable(SqlGenerationContext.EXTRACTED_LINKS_TABLE_INVERSE);
71         else
72             context.needsJoinWithTable(SqlGenerationContext.EXTRACTED_LINKS_TABLE);
73         sql.append(" (");
74         sql.append(SqlGenerationContext.EXTRACTED_LINKS_TABLE.getName());
75         sql.append(".");
76         if (fromMode)
77             sql.append(SqlGenerationContext.ExtractedLinksTable.SOURCE_DOC_ID);
78         else
79             sql.append(SqlGenerationContext.ExtractedLinksTable.TARGET_DOC_ID);
80         sql.append(" = ? ");
81         if (branchId != -1) {
82             sql.append(" and ");
83             sql.append(SqlGenerationContext.EXTRACTED_LINKS_TABLE.getName());
84             sql.append(".");
85             if (fromMode)
86                 sql.append(SqlGenerationContext.ExtractedLinksTable.SOURCE_BRANCH_ID);
87             else
88                 sql.append(SqlGenerationContext.ExtractedLinksTable.TARGET_BRANCH_ID);
89             sql.append(" = ? ");
90         }
91         if (languageId != -1) {
92             sql.append(" and ");
93             sql.append(SqlGenerationContext.EXTRACTED_LINKS_TABLE.getName());
94             sql.append(".");
95             if (fromMode)
96                 sql.append(SqlGenerationContext.ExtractedLinksTable.SOURCE_LANG_ID);
97             else
98                 sql.append(SqlGenerationContext.ExtractedLinksTable.TARGET_LANG_ID);
99             sql.append(" = ? ");
100         }
101         // if both inLastVersion and inLiveVersion, we don't need to add any extra conditions
102
if (!(inLastVersion && inLiveVersion)) {
103             if (inLastVersion) {
104                 sql.append(" and ");
105                 sql.append(SqlGenerationContext.EXTRACTED_LINKS_TABLE.getName());
106                 sql.append(".");
107                 sql.append(SqlGenerationContext.ExtractedLinksTable.IN_LAST_VERSION);
108                 sql.append(" = ?");
109             }
110             if (inLiveVersion) {
111                 sql.append(" and ");
112                 sql.append(SqlGenerationContext.EXTRACTED_LINKS_TABLE.getName());
113                 sql.append(".");
114                 sql.append(SqlGenerationContext.ExtractedLinksTable.IN_LIVE_VERSION);
115                 sql.append(" = ?");
116             }
117         }
118         if (linkTypes != null && linkTypes.length > 0) {
119             sql.append(" and ");
120             sql.append(SqlGenerationContext.EXTRACTED_LINKS_TABLE.getName());
121             sql.append(".");
122             sql.append(SqlGenerationContext.ExtractedLinksTable.LINKTYPE);
123             sql.append(" IN ( ");
124             for (int i = 0; i < linkTypes.length; i++) {
125                 if (i > 0)
126                     sql.append(", ");
127                 sql.append("?");
128             }
129             sql.append(")");
130         }
131         sql.append(")");
132     }
133
134     public int bindSql(PreparedStatement JavaDoc stmt, int bindPos, EvaluationContext evaluationContext) throws SQLException JavaDoc {
135         stmt.setLong(bindPos++, documentId);
136         if (branchId != -1)
137             stmt.setLong(bindPos++, branchId);
138         if (languageId != -1)
139             stmt.setLong(bindPos++, languageId);
140         if (!(inLastVersion && inLiveVersion)) {
141             if (inLastVersion) {
142                 stmt.setBoolean(bindPos, inLastVersion);
143                 bindPos++;
144             }
145             if (inLiveVersion) {
146                 stmt.setBoolean(bindPos, inLiveVersion);
147                 bindPos++;
148             }
149         }
150         if (linkTypes != null) {
151             for (int i = 0; i < linkTypes.length; i++)
152                 stmt.setString(bindPos++, linkTypes[i].getCode());
153         }
154
155         return bindPos;
156     }
157
158     public AclConditionViolation isAclAllowed() {
159         return new AclConditionViolation("LinksTo/LinksToVariant/LinksFrom/LinksFromVariant is not allowed in ACL conditions");
160     }
161
162     public Tristate appliesTo(Document document) {
163         throw new IllegalStateException JavaDoc();
164     }
165 }
166
Popular Tags