KickJava   Java API By Example, From Geeks To Geeks.

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


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.repository.Document;
19 import org.outerj.daisy.repository.Version;
20 import org.outerj.daisy.repository.query.QueryException;
21 import org.outerj.daisy.repository.query.EvaluationContext;
22 import org.outerj.daisy.query.QueryContext;
23 import org.outerj.daisy.query.model.*;
24
25 import java.util.Locale JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.sql.PreparedStatement JavaDoc;
28 import java.sql.SQLException JavaDoc;
29
30 public class ConcatFunction extends AbstractFunction {
31     public static final String JavaDoc NAME = "Concat";
32
33     public String JavaDoc getFunctionName() {
34         return NAME;
35     }
36
37     public void prepare(QueryContext context) throws QueryException {
38         super.prepare(context);
39         if (params.size() < 1)
40             throw new QueryException(NAME + " requires at least one argument.");
41
42         Iterator JavaDoc paramsIt = params.iterator();
43         while (paramsIt.hasNext()) {
44             ValueExpr valueExpr = (ValueExpr)paramsIt.next();
45             if ((valueExpr.getValueType() != QValueType.STRING && valueExpr.getValueType() != QValueType.UNDEFINED) || valueExpr.isSymbolicIdentifier() || valueExpr.isMultiValue())
46                 throw new QueryException(NAME + " has invalid arguments (only strings allowed).");
47         }
48     }
49
50     public void generateSqlValueExpr(StringBuffer JavaDoc sql, SqlGenerationContext context) throws QueryException {
51         String JavaDoc[] sqlFunction = context.getJdbcHelper().getStringConcatFunction();
52         sql.append(' ').append(sqlFunction[0]).append(' ');
53         super.generateSqlValueExpr(sql, context);
54         sql.append(sqlFunction[1]).append(' ');
55     }
56
57     public int bindValueExpr(PreparedStatement JavaDoc stmt, int bindPos, QValueType valueType, EvaluationContext evaluationContext) throws SQLException JavaDoc, QueryException {
58         return super.bindValueExpr(stmt, bindPos, QValueType.STRING, evaluationContext);
59     }
60
61     public Object JavaDoc evaluate(QValueType valueType, EvaluationContext evaluationContext) throws QueryException {
62         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
63         Iterator JavaDoc paramsIt = params.iterator();
64         while (paramsIt.hasNext()) {
65             ValueExpr valueExpr = (ValueExpr)paramsIt.next();
66             result.append(valueExpr.evaluate(QValueType.STRING, evaluationContext));
67         }
68         return result.toString();
69     }
70
71     public Object JavaDoc evaluate(QValueType valueType, Document document, Version version, EvaluationContext evaluationContext) throws QueryException {
72         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
73         Iterator JavaDoc paramsIt = params.iterator();
74         while (paramsIt.hasNext()) {
75             ValueExpr valueExpr = (ValueExpr)paramsIt.next();
76             result.append(valueExpr.evaluate(QValueType.STRING, document, version, evaluationContext));
77         }
78         return result.toString();
79     }
80
81     public QValueType getValueType() {
82         return QValueType.STRING;
83     }
84
85     public String JavaDoc getTitle(Locale JavaDoc locale) {
86         return getExpression();
87     }
88
89     public QValueType getOutputValueType() {
90         return QValueType.STRING;
91     }
92
93     public Object JavaDoc getOutputValue(Document document, Version version, EvaluationContext evaluationContext) throws QueryException {
94         return evaluate(null, document, version, evaluationContext);
95     }
96 }
97
Popular Tags