KickJava   Java API By Example, From Geeks To Geeks.

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


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.AbstractFunction;
19 import org.outerj.daisy.query.model.SqlGenerationContext;
20 import org.outerj.daisy.query.model.QValueType;
21 import org.outerj.daisy.query.QueryContext;
22 import org.outerj.daisy.repository.query.QueryException;
23 import org.outerj.daisy.repository.query.EvaluationContext;
24 import org.outerj.daisy.repository.Document;
25 import org.outerj.daisy.repository.Version;
26
27 import java.sql.PreparedStatement JavaDoc;
28 import java.sql.SQLException JavaDoc;
29 import java.util.Locale JavaDoc;
30
31 public class UserIdFunction extends AbstractFunction {
32     public static final String JavaDoc NAME = "UserId";
33
34     public String JavaDoc getFunctionName() {
35         return NAME;
36     }
37
38     public void prepare(QueryContext context) throws QueryException {
39         super.prepare(context);
40         if (params.size() != 0)
41             throw new QueryException(NAME + " function takes no arguments.");
42     }
43
44     public void generateSqlValueExpr(StringBuffer JavaDoc sql, SqlGenerationContext context) throws QueryException {
45         sql.append(" ? ");
46     }
47
48     public int bindValueExpr(PreparedStatement JavaDoc stmt, int bindPos, QValueType valueType, EvaluationContext evaluationContext) throws SQLException JavaDoc, QueryException {
49         Long JavaDoc userId = evaluationContext.getUserId();
50         if (userId == null)
51             throw new QueryException(NAME + " function: user ID is not available.");
52
53         stmt.setLong(bindPos, userId.longValue());
54         return ++bindPos;
55     }
56
57     public Object JavaDoc evaluate(QValueType valueType, EvaluationContext evaluationContext) throws QueryException {
58         return evaluationContext.getUserId();
59     }
60
61     public Object JavaDoc evaluate(QValueType valueType, Document document, Version version, EvaluationContext evaluationContext) throws QueryException {
62         return evaluate(null, evaluationContext);
63     }
64
65     public QValueType getValueType() {
66         return QValueType.LONG;
67     }
68
69     public String JavaDoc getTitle(Locale JavaDoc locale) {
70         return getExpression();
71     }
72
73     public QValueType getOutputValueType() {
74         return QValueType.LONG;
75     }
76
77     public Object JavaDoc getOutputValue(Document document, Version version, EvaluationContext evaluationContext) throws QueryException {
78         return evaluate(null, evaluationContext);
79     }
80 }
81
Popular Tags