KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > josql > contrib > JoSQLJRDataSource


1 /*
2  * Copyright 2004-2005 Gary Bentley
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may
5  * not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */

15 package org.josql.contrib;
16
17 import java.util.List JavaDoc;
18
19 import net.sf.jasperreports.engine.JRRewindableDataSource;
20 import net.sf.jasperreports.engine.JRField;
21
22 import org.josql.QueryExecutionException;
23 import org.josql.QueryParseException;
24 import org.josql.Query;
25 import org.josql.QueryResults;
26
27 /**
28  * A data source suitable for use with <a HREF="http://jasperreports.sourceforge.net/">JasperReports</a>.
29  * This is basically just an extension to {@link Query} that allows the
30  * results to be iterated over, thereby providing the ability for objects to be reported on
31  * that are held in memory.
32  * <p>
33  * One limitation here is that the SQL query must return columns rather than the objects
34  * since the values need to be mapped by JasperReports. For example:
35  * <pre>
36  * SELECT lastModified,
37  * name
38  * FROM java.io.File
39  * WHERE name LIKE '%.html'
40  * </pre>
41  * <p>
42  * This query would work but it should be noted that the select "columns" (since they do not have
43  * aliases assigned) will be labeled 1, 2, X and so on.
44  * You can assign aliases to the "columns" and then use them in the report definition file.
45  * <p>
46  * Please note: due to my bewilderment (and the fact that I can't get the examples to work ;)
47  * I haven't been able to adequately test this implementation, in the rudementary tests I
48  * performed it seemed to work. If it doesn't please send me an example so that I can try
49  * it!
50  * <p>
51  * Last Modified By: $Author: barrygently $<br />
52  * Last Modified On: $Date: 2005/01/20 15:27:25 $<br />
53  * Current Revision: $Revision: 1.1 $<br />
54  */

55 public class JoSQLJRDataSource extends Query implements JRRewindableDataSource
56 {
57
58     private int row = 0;
59     private List JavaDoc results = null;
60
61     public JoSQLJRDataSource ()
62     {
63
64     }
65
66     /**
67      * Exectute the query and return the results. A reference to the results is also held to
68      * allow them to be iterated over. If you plan on re-using this data source then
69      * you should call: {@link #clearResults()} to free up the references to the results.
70      *
71      * @param l The List of objects to execute the query on.
72      * @return The results.
73      * @throws QueryExecutionException If the query cannot be executed, or if the query
74      * is set to return objects rather than "columns".
75      */

76     public QueryResults executeQuery (List JavaDoc l)
77                                   throws QueryExecutionException
78     {
79
80     if (this.isWantObjects ())
81     {
82
83         throw new QueryExecutionException ("Only SQL statements that return columns (not the objects passed in) can be used.");
84
85     }
86
87     QueryResults qr = super.execute (l);
88
89     this.results = qr.getResults ();
90
91     return qr;
92
93     }
94
95     public List JavaDoc getResults ()
96     {
97
98     return this.results;
99
100     }
101
102     public void clearResults ()
103     {
104
105     this.results = null;
106
107     }
108
109     public Object JavaDoc getFieldValue (JRField field)
110     {
111
112     // Get the row of objects.
113
List JavaDoc res = (List JavaDoc) this.results.get (this.row);
114
115     Integer JavaDoc ind = (Integer JavaDoc) this.getAliases ().get (field.getName ());
116
117     int i = -1;
118
119     if (ind != null)
120     {
121
122         i = ind.intValue ();
123
124         if (i > (res.size () - 1))
125         {
126
127         return null;
128
129         }
130
131     }
132
133     // Get the index for the field name.
134
return res.get (i);
135
136     }
137
138     public boolean next ()
139     {
140
141     if (this.row < (this.results.size () - 1))
142     {
143
144         this.row++;
145
146         return true;
147
148     }
149
150     return false;
151
152     }
153
154     public void moveFirst ()
155     {
156
157     this.row = 0;
158
159     }
160
161 }
162
Popular Tags