KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > query > model > functions > ContextDocFunction


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.functions;
17
18 import org.outerj.daisy.query.model.*;
19 import org.outerj.daisy.query.QueryContext;
20 import org.outerj.daisy.repository.query.QueryException;
21 import org.outerj.daisy.repository.query.EvaluationContext;
22 import org.outerj.daisy.repository.Document;
23 import org.outerj.daisy.repository.Version;
24
25 import java.util.Locale JavaDoc;
26 import java.sql.PreparedStatement JavaDoc;
27 import java.sql.SQLException JavaDoc;
28
29 public class ContextDocFunction extends AbstractFunction {
30     public static final String JavaDoc NAME = "ContextDoc";
31
32     public String JavaDoc getFunctionName() {
33         return NAME;
34     }
35
36     public void prepare(QueryContext context) throws QueryException {
37         super.prepare(context);
38         if (params.size() != 1)
39             throw new QueryException(NAME + " function expects exactly one parameter.");
40     }
41
42     public void generateSqlValueExpr(StringBuffer JavaDoc sql, SqlGenerationContext context) throws QueryException {
43         sql.append(" ? ");
44     }
45
46     public String JavaDoc getSqlPreConditions(SqlGenerationContext context) throws QueryException {
47         // the parameter of this function is evaluated statically, no SQL should be generated.
48
return null;
49     }
50
51     public int bindPreConditions(PreparedStatement JavaDoc stmt, int bindPos) throws SQLException JavaDoc, QueryException {
52         // the parameter of this function is evaluated statically, no SQL should be generated.
53
return bindPos;
54     }
55
56     public int bindValueExpr(PreparedStatement JavaDoc stmt, int bindPos, QValueType valueType, EvaluationContext evaluationContext) throws SQLException JavaDoc, QueryException {
57         if (evaluationContext.getContextDocument() == null)
58             throw new QueryException(NAME + " function is used but there is no context document available.");
59
60         valueType = getParam(0).getValueType() != null ? getParam(0).getValueType() : valueType;
61         Object JavaDoc value = evaluate(valueType, evaluationContext.getContextDocument(), evaluationContext.getContextVersion(), evaluationContext);
62         Literal.bindLiteral(stmt, bindPos, valueType, value);
63         return ++bindPos;
64     }
65
66     public boolean isMultiValue() {
67         return getParam(0).isMultiValue();
68     }
69
70     public Object JavaDoc evaluate(QValueType valueType, EvaluationContext evaluationContext) throws QueryException {
71         return getOutputValue(null, null, evaluationContext);
72     }
73
74     public Object JavaDoc evaluate(QValueType valueType, Document document, Version version, EvaluationContext evaluationContext) throws QueryException {
75         return getOutputValue(document, version, evaluationContext);
76     }
77
78     public QValueType getValueType() {
79         return getParam(0).getOutputValueType();
80     }
81
82     public String JavaDoc getTitle(Locale JavaDoc locale) {
83         return getExpression();
84     }
85
86     public QValueType getOutputValueType() {
87         return getParam(0).getOutputValueType();
88     }
89
90     public Object JavaDoc getOutputValue(Document document, Version version, EvaluationContext evaluationContext) throws QueryException {
91         if (evaluationContext.getContextDocument() == null)
92             throw new QueryException(NAME + " function is used but there is no context document available.");
93         return getParam(0).getOutputValue(evaluationContext.getContextDocument(), evaluationContext.getContextVersion(), evaluationContext);
94     }
95 }
96
Popular Tags