KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectstyle > cayenne > access > jdbc > ResultDirective


1 /* ====================================================================
2  *
3  * The ObjectStyle Group Software License, version 1.1
4  * ObjectStyle Group - http://objectstyle.org/
5  *
6  * Copyright (c) 2002-2005, Andrei (Andrus) Adamchik and individual authors
7  * of the software. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if any,
22  * must include the following acknowlegement:
23  * "This product includes software developed by independent contributors
24  * and hosted on ObjectStyle Group web site (http://objectstyle.org/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The names "ObjectStyle Group" and "Cayenne" must not be used to endorse
29  * or promote products derived from this software without prior written
30  * permission. For written permission, email
31  * "andrus at objectstyle dot org".
32  *
33  * 5. Products derived from this software may not be called "ObjectStyle"
34  * or "Cayenne", nor may "ObjectStyle" or "Cayenne" appear in their
35  * names without prior written permission.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE OBJECTSTYLE GROUP OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals and hosted on ObjectStyle Group web site. For more
53  * information on the ObjectStyle Group, please see
54  * <http://objectstyle.org/>.
55  */

56
57 package org.objectstyle.cayenne.access.jdbc;
58
59 import java.io.IOException JavaDoc;
60 import java.io.Writer JavaDoc;
61 import java.math.BigDecimal JavaDoc;
62 import java.math.BigInteger JavaDoc;
63 import java.sql.Date JavaDoc;
64 import java.sql.Time JavaDoc;
65 import java.sql.Timestamp JavaDoc;
66 import java.util.Collection JavaDoc;
67 import java.util.HashMap JavaDoc;
68 import java.util.Map JavaDoc;
69
70 import org.apache.velocity.context.InternalContextAdapter;
71 import org.apache.velocity.exception.MethodInvocationException;
72 import org.apache.velocity.exception.ParseErrorException;
73 import org.apache.velocity.exception.ResourceNotFoundException;
74 import org.apache.velocity.runtime.directive.Directive;
75 import org.apache.velocity.runtime.parser.node.Node;
76 import org.objectstyle.cayenne.util.Util;
77
78 /**
79  * A custom Velocity directive to describe a ResultSet column.
80  * There are the following possible invocation formats inside the template:
81  *
82  * <pre>
83  * #result(column_name) - e.g. #result('ARTIST_ID')
84  * #result(column_name java_type) - e.g. #result('ARTIST_ID' 'String')
85  * #result(column_name java_type column_alias) - e.g. #result('ARTIST_ID' 'String' 'ID')
86  * #result(column_name java_type column_alias data_row_key) - e.g. #result('ARTIST_ID' 'String' 'ID' 'toArtist.ID')
87  * </pre>
88  *
89  * <p>'data_row_key' is needed if SQL 'column_alias' is not appropriate as a DataRow key on the Cayenne
90  * side. One common case when this happens is when a DataRow retrieved from a query is mapped
91  * using joint prefetch keys. In this case DataRow must use DB_PATH expressions for joint column keys,
92  * and their format is incompatible with most databases alias format.</p>
93  *
94  * <p>Most common Java types used in JDBC can be specified without
95  * a package. This includes all numeric types, primitives, String, SQL dates, BigDecimal
96  * and BigInteger.
97  * </p>
98  *
99  * @author Andrei Adamchik
100  * @since 1.1
101  */

102 public class ResultDirective extends Directive {
103
104     private static final Map JavaDoc typesGuess = new HashMap JavaDoc();
105
106     static {
107         // init default types
108

109         // primitives
110
typesGuess.put("long", Long JavaDoc.class.getName());
111         typesGuess.put("double", Double JavaDoc.class.getName());
112         typesGuess.put("byte", Byte JavaDoc.class.getName());
113         typesGuess.put("boolean", Boolean JavaDoc.class.getName());
114         typesGuess.put("float", Float JavaDoc.class.getName());
115         typesGuess.put("short", Short JavaDoc.class.getName());
116         typesGuess.put("int", Integer JavaDoc.class.getName());
117
118         // numeric
119
typesGuess.put("Long", Long JavaDoc.class.getName());
120         typesGuess.put("Double", Double JavaDoc.class.getName());
121         typesGuess.put("Byte", Byte JavaDoc.class.getName());
122         typesGuess.put("Boolean", Boolean JavaDoc.class.getName());
123         typesGuess.put("Float", Float JavaDoc.class.getName());
124         typesGuess.put("Short", Short JavaDoc.class.getName());
125         typesGuess.put("Integer", Integer JavaDoc.class.getName());
126
127         // other
128
typesGuess.put("String", String JavaDoc.class.getName());
129         typesGuess.put("Date", Date JavaDoc.class.getName());
130         typesGuess.put("Time", Time JavaDoc.class.getName());
131         typesGuess.put("Timestamp", Timestamp JavaDoc.class.getName());
132         typesGuess.put("BigDecimal", BigDecimal JavaDoc.class.getName());
133         typesGuess.put("BigInteger", BigInteger JavaDoc.class.getName());
134     }
135
136     public String JavaDoc getName() {
137         return "result";
138     }
139
140     public int getType() {
141         return LINE;
142     }
143
144     public boolean render(InternalContextAdapter context, Writer JavaDoc writer, Node node)
145         throws
146             IOException JavaDoc,
147             ResourceNotFoundException,
148             ParseErrorException,
149             MethodInvocationException {
150
151         String JavaDoc column = getChildAsString(context, node, 0);
152         if (column == null) {
153             throw new ParseErrorException("Column name expected at line "
154                     + node.getLine()
155                     + ", column "
156                     + node.getColumn());
157         }
158
159         String JavaDoc alias = getChildAsString(context, node, 2);
160         String JavaDoc dataRowKey = getChildAsString(context, node, 3);
161         
162         // determine what we want to name this column in a resulting DataRow...
163
String JavaDoc label = (!Util.isEmptyString(dataRowKey)) ? dataRowKey : (!Util
164                 .isEmptyString(alias)) ? alias : null;
165   
166
167         ColumnDescriptor columnDescriptor = new ColumnDescriptor();
168         columnDescriptor.setName(column);
169         columnDescriptor.setLabel(label);
170   
171         String JavaDoc type = getChildAsString(context, node, 1);
172         if (type != null) {
173             columnDescriptor.setJavaClass(guessType(type.toString()));
174         }
175
176         writer.write(column);
177         
178         // append alias if needed
179
if (!Util.isEmptyString(alias) && !alias.equals(column)) {
180             writer.write(" AS ");
181             writer.write(alias);
182         }
183
184         bindResult(context, columnDescriptor);
185         return true;
186     }
187
188     protected Object JavaDoc getChild(InternalContextAdapter context, Node node, int i)
189         throws MethodInvocationException {
190         return (i >= 0 && i < node.jjtGetNumChildren())
191             ? node.jjtGetChild(i).value(context)
192             : null;
193     }
194     
195     /**
196      * Returns a directive argument at a given index converted to String.
197      *
198      * @since 1.2
199      */

200     protected String JavaDoc getChildAsString(InternalContextAdapter context, Node node, int i)
201             throws MethodInvocationException {
202         Object JavaDoc value = getChild(context, node, i);
203         return (value != null) ? value.toString() : null;
204     }
205
206     /**
207      * Converts "short" type notation to the fully qualified class name.
208      * Right now supports all major standard SQL types, including primitives.
209      * All other types are expected to be fully qualified, and are not converted.
210      */

211     protected String JavaDoc guessType(String JavaDoc type) {
212         String JavaDoc guessed = (String JavaDoc) typesGuess.get(type);
213         return guessed != null ? guessed : type;
214     }
215
216     /**
217      * Adds value to the list of result columns in the context.
218      */

219     protected void bindResult(
220         InternalContextAdapter context,
221         ColumnDescriptor columnDescriptor) {
222
223         Collection JavaDoc resultColumns =
224             (Collection JavaDoc) context.getInternalUserContext().get(
225                 SQLTemplateProcessor.RESULT_COLUMNS_LIST_KEY);
226
227         if (resultColumns != null) {
228             resultColumns.add(columnDescriptor);
229         }
230     }
231 }
232
Popular Tags