KickJava   Java API By Example, From Geeks To Geeks.

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


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.QValueType;
20 import org.outerj.daisy.query.model.ValueExpr;
21 import org.outerj.daisy.query.model.SqlGenerationContext;
22 import org.outerj.daisy.query.QueryContext;
23 import org.outerj.daisy.repository.query.EvaluationContext;
24 import org.outerj.daisy.repository.query.QueryException;
25 import org.outerj.daisy.repository.Document;
26 import org.outerj.daisy.repository.Version;
27
28 import java.util.Locale JavaDoc;
29 import java.util.Date JavaDoc;
30 import java.util.Calendar JavaDoc;
31 import java.util.GregorianCalendar JavaDoc;
32 import java.sql.PreparedStatement JavaDoc;
33 import java.sql.SQLException JavaDoc;
34
35 public abstract class DateComponentFunction extends AbstractFunction {
36     public void prepare(QueryContext context) throws QueryException {
37         super.prepare(context);
38         if (params.size() != 1)
39             throw new QueryException(getFunctionName() + " takes exactly 1 parameter.");
40         ValueExpr param = getParam(0);
41         if ((param.getValueType() != QValueType.DATE && param.getValueType() != QValueType.DATETIME
42                 && param.getValueType() != QValueType.UNDEFINED)
43                 || param.isMultiValue() || param.isSymbolicIdentifier())
44             throw new QueryException("Invalid argument for " + getFunctionName() + " function: " + param.getExpression());
45     }
46
47     protected abstract int getCalendarField();
48
49     public Object JavaDoc evaluate(QValueType valueType, EvaluationContext evaluationContext) throws QueryException {
50         Date JavaDoc date = (Date JavaDoc)getParam(0).evaluate(QValueType.DATE, evaluationContext);
51         Calendar JavaDoc calendar = new GregorianCalendar JavaDoc();
52         calendar.setTime(date);
53         return new Long JavaDoc(calendar.get(getCalendarField()) + getAdjustment());
54     }
55
56     public Object JavaDoc evaluate(QValueType valueType, Document document, Version version, EvaluationContext evaluationContext) throws QueryException {
57         Date JavaDoc date = (Date JavaDoc)getParam(0).evaluate(QValueType.DATE, document, version, evaluationContext);
58         Calendar JavaDoc calendar = new GregorianCalendar JavaDoc(Locale.US);
59         calendar.setTime(date);
60         return new Long JavaDoc(calendar.get(getCalendarField()) + getAdjustment());
61     }
62
63     protected abstract String JavaDoc[] getSqlFunction(SqlGenerationContext context);
64
65     protected int getAdjustment() {
66         return 0;
67     }
68
69     public void generateSqlValueExpr(StringBuffer JavaDoc sql, SqlGenerationContext context) throws QueryException {
70         String JavaDoc[] sqlFunction = getSqlFunction(context);
71         sql.append(' ').append(sqlFunction[0]);
72         super.generateSqlValueExpr(sql, context);
73         sql.append(sqlFunction[1]).append(' ');
74     }
75
76     public int bindValueExpr(PreparedStatement JavaDoc stmt, int bindPos, QValueType valueType, EvaluationContext evaluationContext) throws SQLException JavaDoc, QueryException {
77         return super.bindValueExpr(stmt, bindPos, QValueType.DATE, evaluationContext);
78     }
79
80     public QValueType getValueType() {
81         return QValueType.LONG;
82     }
83
84     public String JavaDoc getTitle(Locale JavaDoc locale) {
85         return getExpression();
86     }
87
88     public QValueType getOutputValueType() {
89         return QValueType.LONG;
90     }
91
92     public Object JavaDoc getOutputValue(Document document, Version version, EvaluationContext evaluationContext) throws QueryException {
93         return evaluate(null, document, version, evaluationContext);
94     }
95 }
96
Popular Tags