KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scriptella > driver > janino > JaninoQuery


1 /*
2  * Copyright 2006-2007 The Scriptella Project Team.
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 scriptella.driver.janino;
17
18 import scriptella.spi.ParametersCallback;
19 import scriptella.spi.QueryCallback;
20 import scriptella.util.CollectionUtils;
21
22 import java.util.Map JavaDoc;
23
24 /**
25  * A base class for Janino <query> elements.
26  * <p>This class exposes a simplified query API. The concept of this API is the following:
27  * <ul>
28  * <li>A virtual row is produced by calling {@link #set(String, Object)}
29  * for each virtual column.
30  * <li>{@link #next()} is called to process this row by nested scripting elements.
31  * </ul>
32  * Additionally a {@link #next(String[], Object[]) helper method} exists to make iterating even simpler.
33  * <p>Virtual rows may also be {@link #set(java.util.Map) constructed} from {@link Map} or {@link java.util.Properties}.
34  * <p>Public members of this class are available in Janino scripting elements.
35  * <p>Examples:
36  * <p>The following query produces two virtual rows:</p>
37  * <table border=1>
38  * <tr><th>id</th><th>name</th><th>age</th></tr>
39  * <tr><td>123</td><td>John</td><td>20</td></tr>
40  * <tr><td>200</td><td>Mary</td><td></td></tr>
41  * </table>
42  *
43  * <code><pre>
44  * &lt;query&gt;
45  * set("id", "123"); //set row column
46  * set("name", "John");
47  * set("age", new Integer(20));
48  * next();//sends a virtual row for processing
49  * set("id", "200");
50  * set("name", "Mary");
51  * next();
52  * &lt;/query&gt;
53  * </pre></code>
54  * The same effect may achieved using the following code:
55  * <code><pre>
56  * &lt;query&gt;
57  * String[] names = new String[] {"id", "name", "age"};
58  * next(names, new Object[] {"123", "John", new Integer(20)};
59  * next(names, new Object[] {"200", "Mary", null)};
60  * &lt;/query&gt;
61  * </pre></code>
62  * <p>Assume you have a map(or Properties) with the following mapping:
63  * <br><code>id->123, name->John, age->20</code>
64  * <br>A virtual row is produced using this code:
65  * <code><pre>
66  * &lt;query&gt;
67  * //fill a map or load properties file
68  * next(map};
69  * &lt;/query&gt;
70  * </pre></code>
71  * @author Fyodor Kupolov
72  * @version 1.0
73  */

74 public abstract class JaninoQuery extends JaninoScript implements ParametersCallback {
75     private QueryCallback queryCallback;
76     private Map JavaDoc<String JavaDoc, Object JavaDoc> row;
77
78     /**
79      * This method in not a part of the public API.
80      */

81     final void setQueryCallback(QueryCallback queryCallback) {
82         this.queryCallback = queryCallback;
83     }
84
85     public final QueryCallback getQueryCallback() {
86         return queryCallback;
87     }
88
89     public final Object JavaDoc getParameter(final String JavaDoc name) {
90         if (row != null) {
91             Object JavaDoc v = row.get(name);
92             if (v != null) {
93                 return v;
94             }
95         }
96         return super.get(name);
97     }
98
99
100     /**
101      * Sets a value for specified parameter name.
102      * <p>This parameter becomes visible to nested scripting elements
103      * after {@link #next()} method is called.
104      * <p>This method is available inside Janino &lt;query&gt; element.
105      * @param name parameter name
106      * @param value parameter value.
107      */

108     public final void set(String JavaDoc name, Object JavaDoc value) {
109         initRow();
110         row.put(name, value);
111     }
112
113     /**
114      * Fills the virtual row using parameters from specified map.
115      * <p>This method is available inside Janino &lt;query&gt; element.
116      * @param parametersMap map of parameters, where key is a variable name
117      */

118     public final void set(Map JavaDoc<String JavaDoc,?> parametersMap) {
119         initRow();
120         row.putAll(parametersMap);
121     }
122
123     private void initRow() {
124         if (row == null) {
125             row = CollectionUtils.newCaseInsensitiveAsciiMap();
126         }
127     }
128
129     /**
130      * Moves to the next virtual row.
131      * <p>Nested scripting elements are evaluated and
132      * the parameters set by {@link #set(String, Object)} method are available to them.
133      * <p><em>Note:</em> The values of all parameters set via {@link #set(String, Object)} method are
134      * cleared(or restored).
135      * <p>This method is available inside Janino &lt;query&gt; element.
136      */

137     public final void next() {
138         queryCallback.processRow(this);
139         if (row != null) {
140             row.clear();
141         }
142     }
143
144     /**
145      * Produces a virtual row based on the specified columns.
146      * <p>A serie of {@link #set(String, Object) parameter setters} is performed.
147      * After parameters for the current row have been set, {@link #next()} method is invoked.
148      * <p>This method is available inside Janino &lt;query&gt; element.
149      * @param parameterNames array of parameter names.
150      * @param values array of parameter values, i.e. value[i] specifies a value for parameter[i].
151      */

152     public final void next(String JavaDoc[] parameterNames, Object JavaDoc[] values) {
153         for (int i = 0; i < parameterNames.length; i++) {
154             set(parameterNames[i], values[i]);
155         }
156         next();
157     }
158
159     /**
160      * Produces a virtual row based on the specified columns.
161      * <p>Parameters are set via {@link #set(java.util.Map)} method.
162      * After parameters for the current row have been set, {@link #next()} method is invoked.
163      * <p>This method is available inside Janino &lt;query&gt; element.
164      * @param parametersMap map of parameters.
165      */

166     public final void next(Map JavaDoc<String JavaDoc,?> parametersMap) {
167         set(parametersMap);
168         next();
169     }
170
171
172
173     public String JavaDoc toString() {
174         return "Query";
175     }
176
177
178 }
179
Popular Tags