1 15 package org.josql.contrib; 16 17 import java.util.List ; 18 import java.util.ArrayList ; 19 import java.util.Map ; 20 import java.util.HashMap ; 21 import java.util.Arrays ; 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 yCols = null; 46 private List listeners = new ArrayList (); 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 80 public QueryResults getResults () 81 { 82 83 return this.results; 84 85 } 86 87 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 [] yCols) 106 throws IllegalArgumentException , 107 IllegalStateException , 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 , 119 IllegalStateException , 120 QueryParseException 121 { 122 123 List l = new ArrayList (yCols.length); 124 125 for (int i = 0; i < yCols.length; i++) 126 { 127 128 l.add (new Integer (yCols[i])); 129 130 } 131 132 this.define (xCol, 133 l); 134 135 } 136 137 public void define (int xCol, 138 List yCols) 139 throws IllegalArgumentException , 140 IllegalStateException , 141 QueryParseException 142 { 143 144 if (!this.parsed ()) 145 { 146 147 throw new IllegalStateException ("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 ("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 o = yCols.get (i); 162 163 if (o instanceof String ) 164 { 165 166 try 167 { 168 169 yCols.set (i, 170 new Integer (Integer.parseInt ((String ) o))); 171 172 continue; 173 174 } catch (Exception e) { 175 176 throw new IllegalArgumentException ("Unable to convert y column indicator: " + 177 o + 178 " to an integer."); 179 180 } 181 182 } 183 184 if (o instanceof Number ) 185 { 186 187 yCols.set (i, 188 new Integer (((Number ) o).intValue ())); 189 190 continue; 191 192 } 193 194 if (!(o instanceof Integer )) 195 { 196 197 throw new IllegalArgumentException ("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 cols = this.getColumns (); 206 207 for (int i = 0; i < yCols.size (); i++) 208 { 209 210 Integer in = (Integer ) yCols.get (i); 211 212 if (in.intValue () < 1) 213 { 214 215 throw new IllegalArgumentException ("Y column index must be a minimum of 1."); 216 217 } 218 219 if (in.intValue () > cols.size ()) 220 { 221 222 throw new IllegalArgumentException ("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 ("Y column: " + 234 yexp + 235 " must have an alias."); 236 237 } 238 239 Class yc = yexp.getExpectedReturnType (this); 240 241 if (!Utilities.isNumber (yc)) 242 { 243 244 throw new IllegalArgumentException ("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 ("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 xc = xexp.getExpectedReturnType (this); 266 267 xc = Utilities.getObjectClass (xc); 268 269 if (!(Comparable .class.isAssignableFrom (xc))) 270 { 271 272 throw new IllegalArgumentException ("X column: " + 273 xexp + 274 " will evaluate to an instance of type: " + 275 xc.getName () + 276 ", but only columns that implement: " + 277 Comparable .class.getName () + 278 " can be used."); 279 280 } 281 282 this.yCols = yCols; 283 284 } 285 286 296 public QueryResults executeQuery (List 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 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 getValue (int row, 348 int col) 349 { 350 351 List l = (List ) this.results.getResults ().get (row); 352 353 return (Number ) l.get (col); 354 355 } 356 357 public List getRowKeys () 358 { 359 360 List ks = new ArrayList (); 361 362 for (int i = 0; i < this.results.getResults ().size (); i++) 363 { 364 365 List l = (List ) 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 getValue (Comparable row, 376 Comparable col) 377 { 378 379 int rk = this.getRowIndex (row); 380 int ck = this.getColumnIndex (col); 381 382 List l = (List ) this.results.getResults ().get (rk); 383 384 return (Number ) l.get (ck); 385 386 } 387 388 public Comparable getColumnKey (int c) 389 { 390 391 return (Comparable ) this.getColumnKeys ().get (c); 392 393 } 394 395 public List getColumnKeys () 396 { 397 398 List ks = new ArrayList (); 399 400 for (int i = 0; i < this.yCols.size (); i++) 401 { 402 403 Integer in = (Integer ) 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 c) 416 { 417 418 List ck = this.getColumnKeys (); 419 420 for (int i = 0; i < ck.size (); i++) 421 { 422 423 if (((Comparable ) 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 c) 437 { 438 439 List rk = this.getRowKeys (); 440 441 for (int i = 0; i < rk.size (); i++) 442 { 443 444 if (((Comparable ) 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 getRowKey (int k) 458 { 459 460 return (Comparable ) this.getRowKeys ().get (k); 461 462 } 463 464 } 465 | Popular Tags |