KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.ArrayList JavaDoc;
19 import java.util.Map JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Arrays JavaDoc;
22
23 import org.jfree.data.category.CategoryDataset;
24
25 import org.jfree.data.DomainOrder;
26
27 import org.jfree.data.general.DatasetGroup;
28 import org.jfree.data.general.DatasetChangeListener;
29 import org.jfree.data.general.DatasetChangeEvent;
30
31 import org.josql.Query;
32 import org.josql.QueryResults;
33 import org.josql.QueryExecutionException;
34 import org.josql.QueryParseException;
35
36 import org.josql.internal.Utilities;
37
38 import org.josql.expressions.SelectItemExpression;
39
40 public class JoSQLFreeChartCategoryDataset extends Query implements CategoryDataset
41 {
42
43     private QueryResults results = null;
44     private int xCol = 0;
45     private List JavaDoc yCols = null;
46     private List JavaDoc listeners = new ArrayList JavaDoc ();
47     private DatasetGroup group = null;
48
49     public JoSQLFreeChartCategoryDataset ()
50     {
51
52     }
53
54     public void addChangeListener (DatasetChangeListener l)
55     {
56
57     this.listeners.add (l);
58
59     }
60
61     public void removeChangeListener (DatasetChangeListener l)
62     {
63
64     this.listeners.remove (l);
65
66     }
67
68     public DatasetGroup getGroup ()
69     {
70
71     return this.group;
72
73     }
74
75     /**
76      * Get any results, will be null unless {@link #execute(List)} has been called.
77      *
78      * @return The results.
79      */

80     public QueryResults getResults ()
81     {
82
83     return this.results;
84
85     }
86
87     /**
88      * Clear any results.
89      */

90     public void clearResults ()
91     {
92
93     this.results = null;
94
95     }
96
97     public void setGroup (DatasetGroup g)
98     {
99
100     this.group = g;
101
102     }
103
104     public void define (int xCol,
105             Object JavaDoc[] yCols)
106                         throws IllegalArgumentException JavaDoc,
107                              IllegalStateException JavaDoc,
108                              QueryParseException
109     {
110
111     this.define (xCol,
112              Arrays.asList (yCols));
113
114     }
115
116     public void define (int xCol,
117             int[] yCols)
118                         throws IllegalArgumentException JavaDoc,
119                            IllegalStateException JavaDoc,
120                            QueryParseException
121     {
122
123     List JavaDoc l = new ArrayList JavaDoc (yCols.length);
124
125     for (int i = 0; i < yCols.length; i++)
126     {
127
128         l.add (new Integer JavaDoc (yCols[i]));
129
130     }
131
132     this.define (xCol,
133              l);
134
135     }
136
137     public void define (int xCol,
138             List JavaDoc yCols)
139                         throws IllegalArgumentException JavaDoc,
140                            IllegalStateException JavaDoc,
141                            QueryParseException
142     {
143
144     if (!this.parsed ())
145     {
146
147         throw new IllegalStateException JavaDoc ("Cannot add a series until a query has been specified and parsed.");
148
149     }
150
151     if (xCol < 1)
152     {
153
154         throw new IllegalArgumentException JavaDoc ("X column index must be a minimum of 1.");
155
156     }
157
158     for (int i = 0; i < yCols.size (); i++)
159     {
160
161         Object JavaDoc o = yCols.get (i);
162
163         if (o instanceof String JavaDoc)
164         {
165
166         try
167         {
168
169             yCols.set (i,
170                    new Integer JavaDoc (Integer.parseInt ((String JavaDoc) o)));
171
172             continue;
173
174         } catch (Exception JavaDoc e) {
175
176             throw new IllegalArgumentException JavaDoc ("Unable to convert y column indicator: " +
177                             o +
178                             " to an integer.");
179
180         }
181
182         }
183
184         if (o instanceof Number JavaDoc)
185         {
186
187         yCols.set (i,
188                new Integer JavaDoc (((Number JavaDoc) o).intValue ()));
189
190         continue;
191
192         }
193
194         if (!(o instanceof Integer JavaDoc))
195         {
196
197         throw new IllegalArgumentException JavaDoc ("Expected y column indicator: " +
198                             o +
199                             " to be either a number or a string representing a number.");
200         
201         }
202
203     }
204
205     List JavaDoc cols = this.getColumns ();
206
207     for (int i = 0; i < yCols.size (); i++)
208     {
209
210         Integer JavaDoc in = (Integer JavaDoc) yCols.get (i);
211
212         if (in.intValue () < 1)
213         {
214
215         throw new IllegalArgumentException JavaDoc ("Y column index must be a minimum of 1.");
216
217         }
218
219         if (in.intValue () > cols.size ())
220         {
221
222         throw new IllegalArgumentException JavaDoc ("Y column index must be a maximum of " +
223                             cols.size () +
224                             ".");
225
226         }
227
228         SelectItemExpression yexp = (SelectItemExpression) cols.get (in.intValue () - 1);
229
230         if (yexp.getAlias () == null)
231         {
232
233         throw new IllegalArgumentException JavaDoc ("Y column: " +
234                             yexp +
235                             " must have an alias.");
236
237         }
238
239         Class JavaDoc yc = yexp.getExpectedReturnType (this);
240
241         if (!Utilities.isNumber (yc))
242         {
243
244         throw new IllegalArgumentException JavaDoc ("Y column: " +
245                             yexp +
246                             " will evaluate to an instance of type: " +
247                             yc.getName () +
248                             ", but only columns that return numbers are allowed.");
249         
250         }
251
252     }
253
254     if (xCol > cols.size ())
255     {
256
257         throw new IllegalArgumentException JavaDoc ("X column index must be a maximum of " +
258                         cols.size () +
259                         ".");
260
261     }
262
263     SelectItemExpression xexp = (SelectItemExpression) cols.get (xCol - 1);
264
265     Class JavaDoc xc = xexp.getExpectedReturnType (this);
266
267     xc = Utilities.getObjectClass (xc);
268
269     if (!(Comparable JavaDoc.class.isAssignableFrom (xc)))
270     {
271
272         throw new IllegalArgumentException JavaDoc ("X column: " +
273                         xexp +
274                         " will evaluate to an instance of type: " +
275                         xc.getName () +
276                         ", but only columns that implement: " +
277                         Comparable JavaDoc.class.getName () +
278                         " can be used.");
279
280     }
281
282     this.yCols = yCols;
283
284     }
285
286     /**
287      * Exectute the query and return the results. A reference to the results is also held to
288      * allow them to be iterated over. If you plan on re-using this data source then
289      * you should call: {@link #clearResults()} to free up the references to the results.
290      *
291      * @param l The List of objects to execute the query on.
292      * @return The results.
293      * @throws QueryExecutionException If the query cannot be executed, or if the query
294      * is set to return objects rather than "columns".
295      */

296     public QueryResults executeQuery (List JavaDoc l)
297                                   throws QueryExecutionException
298     {
299
300     if (this.isWantObjects ())
301     {
302
303         throw new QueryExecutionException ("Only SQL statements that return columns (not the objects passed in) can be used.");
304
305     }
306
307     this.results = super.execute (l);
308
309     // Notify our listeners.
310
DatasetChangeEvent dce = new DatasetChangeEvent (this,
311                              this);
312
313     for (int i = 0; i < this.listeners.size (); i++)
314     {
315
316         DatasetChangeListener d = (DatasetChangeListener) this.listeners.get (i);
317
318         d.datasetChanged (dce);
319
320     }
321
322     return this.results;
323
324     }
325
326     public DomainOrder getDomainOrder ()
327     {
328
329     return DomainOrder.ASCENDING;
330
331     }
332
333     public int getRowCount ()
334     {
335
336     return this.results.getResults ().size ();
337
338     }
339
340     public int getColumnCount ()
341     {
342
343     return this.yCols.size ();
344
345     }
346
347     public Number JavaDoc getValue (int row,
348                 int col)
349     {
350
351     List JavaDoc l = (List JavaDoc) this.results.getResults ().get (row);
352
353     return (Number JavaDoc) l.get (col);
354
355     }
356
357     public List JavaDoc getRowKeys ()
358     {
359
360     List JavaDoc ks = new ArrayList JavaDoc ();
361
362     for (int i = 0; i < this.results.getResults ().size (); i++)
363     {
364
365         List JavaDoc l = (List JavaDoc) this.results.getResults ().get (i);
366
367         ks.add (l.get (this.xCol));
368
369     }
370
371     return ks;
372
373     }
374
375     public Number JavaDoc getValue (Comparable JavaDoc row,
376                 Comparable JavaDoc col)
377     {
378
379     int rk = this.getRowIndex (row);
380     int ck = this.getColumnIndex (col);
381
382     List JavaDoc l = (List JavaDoc) this.results.getResults ().get (rk);
383
384     return (Number JavaDoc) l.get (ck);
385
386     }
387
388     public Comparable JavaDoc getColumnKey (int c)
389     {
390
391     return (Comparable JavaDoc) this.getColumnKeys ().get (c);
392
393     }
394
395     public List JavaDoc getColumnKeys ()
396     {
397
398     List JavaDoc ks = new ArrayList JavaDoc ();
399
400     for (int i = 0; i < this.yCols.size (); i++)
401     {
402
403         Integer JavaDoc in = (Integer JavaDoc) this.yCols.get (i);
404
405         SelectItemExpression sei = (SelectItemExpression) this.getColumns ().get (in.intValue () - 1);
406
407         ks.add (sei.getAlias ());
408
409     }
410
411     return ks;
412
413     }
414
415     public int getColumnIndex (Comparable JavaDoc c)
416     {
417
418     List JavaDoc ck = this.getColumnKeys ();
419
420     for (int i = 0; i < ck.size (); i++)
421     {
422
423         if (((Comparable JavaDoc) ck.get (i)).compareTo (c) == 0)
424         {
425
426         return i;
427
428         }
429
430     }
431
432     return -1;
433
434     }
435
436     public int getRowIndex (Comparable JavaDoc c)
437     {
438
439     List JavaDoc rk = this.getRowKeys ();
440
441     for (int i = 0; i < rk.size (); i++)
442     {
443
444         if (((Comparable JavaDoc) rk.get (i)).compareTo (c) == 0)
445         {
446
447         return i;
448
449         }
450
451     }
452
453     return -1;
454
455     }
456
457     public Comparable JavaDoc getRowKey (int k)
458     {
459
460     return (Comparable JavaDoc) this.getRowKeys ().get (k);
461
462     }
463
464 }
465
Popular Tags