1 15 package org.josql.functions; 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.TreeMap ; 22 import java.util.LinkedHashMap ; 23 import java.util.Collections ; 24 25 import com.gentlyweb.utils.Getter; 26 import com.gentlyweb.utils.GeneralComparator; 27 28 import org.josql.Query; 29 import org.josql.QueryExecutionException; 30 31 import org.josql.expressions.Expression; 32 33 import org.josql.internal.Utilities; 34 35 38 public class CollectionFunctions extends AbstractFunctionHandler 39 { 40 41 private Map foreachQueryCache = null; 42 43 47 public static final String HANDLER_ID = "_internal_collection"; 48 49 55 public List sort (List objs) 56 { 57 58 Collections.sort (objs); 59 60 return objs; 61 62 } 63 64 71 public List sort (Map m) 72 { 73 74 return this.sort (m, 75 "key", 76 GeneralComparator.ASC); 77 78 } 79 80 89 public List sort (Map m, 90 String type) 91 { 92 93 return this.sort (m, 94 type, 95 GeneralComparator.ASC); 96 97 } 98 99 109 public List sort (Map m, 110 String type, 111 String dir) 112 { 113 114 boolean key = true; 115 116 String acc = "key"; 117 118 if ((type != null) 119 && 120 (type.equalsIgnoreCase ("value")) 121 ) 122 { 123 124 acc = "value"; 125 126 } 127 128 String d = GeneralComparator.ASC; 129 130 if (dir != null) 131 { 132 133 dir = dir.toUpperCase (); 134 135 if (dir.equals (GeneralComparator.DESC)) 136 { 137 138 d = GeneralComparator.DESC; 139 140 } 141 142 } 143 144 GeneralComparator gc = new GeneralComparator (Map.Entry .class); 145 146 gc.addField (acc, 147 d); 148 149 List l = new ArrayList (m.entrySet ()); 150 151 Collections.sort (l, 152 gc); 153 154 return l; 155 156 } 157 158 167 public Object get (Map m, 168 Expression exp) 169 throws QueryExecutionException 170 { 171 172 return m.get (exp.getValue (this.q.getCurrentObject (), 175 this.q)); 176 177 } 178 179 187 public Object get (List l, 188 Number n) 189 { 190 191 int i = n.intValue (); 192 193 if ((i > l.size ()) 194 || 195 (i < 0) 196 ) 197 { 198 199 return null; 200 201 } 202 203 return l.get (i); 204 205 } 206 207 221 public int count (List objs, 222 Expression exp, 223 Object value) 224 throws QueryExecutionException 225 { 226 227 Boolean b = Boolean.FALSE; 228 229 if (value != null) 230 { 231 232 b = Boolean.valueOf (value.toString ()); 233 234 } 235 236 int count = 0; 237 238 List retVals = new ArrayList (); 239 240 int size = objs.size (); 241 242 for (int i = 0; i < size; i++) 243 { 244 245 Object o = objs.get (i); 246 247 Object v = null; 248 249 try 250 { 251 252 if (Utilities.compare (exp.getValue (o, 253 this.q), 254 b) == 0) 255 { 256 257 count++; 258 259 } 260 261 } catch (Exception e) { 262 263 throw new QueryExecutionException ("Unable to get value from expression: " + 264 exp + 265 " for item: " + 266 i + 267 " from the list of objects.", 268 e); 269 270 } 271 272 } 273 274 return count; 275 276 } 277 278 public int count (Expression exp) 279 throws QueryExecutionException 280 { 281 282 return this.count ((List ) this.q.getVariable (Query.ALL_OBJS_VAR_NAME), 283 exp); 284 285 } 286 287 public int count (List allobjs, 288 Expression exp) 289 throws QueryExecutionException 290 { 291 292 int count = 0; 293 294 List retVals = new ArrayList (); 295 296 int size = allobjs.size (); 297 298 for (int i = 0; i < size; i++) 299 { 300 301 Object o = allobjs.get (i); 302 303 Object v = null; 304 305 try 306 { 307 308 if (exp.isTrue (o, 309 this.q)) 310 { 311 312 count++; 313 314 } 315 316 } catch (Exception e) { 317 318 throw new QueryExecutionException ("Unable to determine whether expression: \"" + 319 exp + 320 "\" is true for object at index: " + 321 i + 322 " from the list of objects.", 323 e); 324 325 } 326 327 } 328 329 return count; 330 331 } 332 333 public List toList (List allobjs, 334 Expression exp, 335 String saveValueName) 336 throws QueryExecutionException 337 { 338 339 return this.collect (allobjs, 340 exp, 341 saveValueName); 342 343 } 344 345 public List unique (List objs) 346 { 347 348 352 353 Map m = new LinkedHashMap (); 354 355 int s = objs.size (); 356 357 for (int i = 0; i < s; i++) 358 { 359 360 m.put (objs.get (i), 361 null); 362 363 } 364 365 return new ArrayList (m.keySet ()); 366 367 } 368 369 public List unique (Expression exp) 370 throws QueryExecutionException 371 { 372 373 return this.unique ((List ) this.q.getVariable (Query.ALL_OBJS_VAR_NAME), 374 exp); 375 376 } 377 378 public List unique (List objs, 379 Expression exp) 380 throws QueryExecutionException 381 { 382 383 387 388 Map m = new HashMap (); 389 390 int s = objs.size (); 391 392 for (int i = 0; i < s; i++) 393 { 394 395 Object o = objs.get (i); 396 397 o = exp.getValue (o, 398 this.q); 399 400 m.put (o, 401 null); 402 403 } 404 405 return new ArrayList (m.keySet ()); 406 407 } 408 409 public List collect (List objs, 410 Expression exp, 411 String saveValueName) 412 throws QueryExecutionException 413 { 414 415 if (saveValueName != null) 416 { 417 418 Object o = this.q.getSaveValue (saveValueName); 419 420 if (o != null) 421 { 422 423 return (List ) o; 424 425 } 426 427 } 428 429 List retVals = new ArrayList (); 430 431 int s = objs.size (); 432 433 Object co = this.q.getCurrentObject (); 434 435 for (int i = 0; i < s; i++) 436 { 437 438 Object o = objs.get (i); 439 440 this.q.setCurrentObject (o); 441 442 Object v = null; 443 444 try 446 { 447 448 retVals.add (exp.getValue (o, 449 this.q)); 450 451 } catch (Exception e) { 452 453 throw new QueryExecutionException ("Unable to execute expression: \"" + 454 exp + 455 " on object at index: " + 456 i + 457 " from the list of objects.", 458 e); 459 460 } 461 462 } 463 464 if (saveValueName != null) 465 { 466 467 this.q.setSaveValue (saveValueName, 468 retVals); 469 470 } 471 472 this.q.setCurrentObject (co); 474 475 return retVals; 476 477 } 478 479 public List collect (Expression exp) 480 throws QueryExecutionException 481 { 482 483 return this.collect ((List ) this.q.getVariable (Query.ALL_OBJS_VAR_NAME), 484 exp); 485 486 } 487 488 public List collect (List allobjs, 489 Expression exp) 490 throws QueryExecutionException 491 { 492 493 return this.collect (allobjs, 494 exp, 495 null); 496 497 } 498 499 public List toList (Expression exp) 500 throws QueryExecutionException 501 { 502 503 return this.toList ((List ) this.q.getVariable (Query.ALL_OBJS_VAR_NAME), 504 exp); 505 506 } 507 508 public List toList (List allobjs, 509 Expression exp) 510 throws QueryExecutionException 511 { 512 513 return this.collect (allobjs, 514 exp, 515 null); 516 517 } 518 519 public List foreach (Expression exp) 520 throws QueryExecutionException 521 { 522 523 return this.foreach ((List ) this.q.getVariable (Query.ALL_OBJS_VAR_NAME), 524 exp); 525 526 } 527 528 public List foreach (List allobjs, 529 Expression exp) 530 throws QueryExecutionException 531 { 532 533 if (allobjs == null) 534 { 535 536 return null; 537 538 } 539 540 List res = new ArrayList (); 541 542 int s = allobjs.size (); 543 544 for (int i = 0; i < s; i++) 545 { 546 547 Object o = allobjs.get (i); 548 549 this.q.setCurrentObject (o); 550 551 res.add (exp.getValue (o, 552 this.q)); 553 554 } 555 556 return res; 557 558 } 559 560 private void fillMap (Object o, 561 Getter k, 562 Getter v) 563 throws QueryExecutionException 564 { 565 566 567 568 } 569 570 592 593 606 public List foreach (List objs, 607 String exp) 608 throws QueryExecutionException 609 { 610 611 List l = new ArrayList (); 612 613 if ((objs == null) 614 || 615 (objs.size () == 0) 616 ) 617 { 618 619 return l; 620 621 } 622 623 Query q = null; 624 625 if (this.foreachQueryCache != null) 627 { 628 629 q = (Query) this.foreachQueryCache.get (exp); 630 631 } 632 633 if (q == null) 634 { 635 636 Class c = null; 638 639 Object o = objs.get (0); 640 641 if (o == null) 642 { 643 644 int s = objs.size () - 1; 645 646 for (int i = s; s > -1; i--) 648 { 649 650 o = objs.get (i); 651 652 if (o != null) 653 { 654 655 c = o.getClass (); 656 break; 657 658 } 659 660 } 661 662 } else { 663 664 c = o.getClass (); 665 666 } 667 668 if (exp.toLowerCase ().trim ().startsWith ("where")) 669 { 670 671 exp = exp.trim ().substring (5); 672 673 } 674 675 String query = "SELECT * FROM " + c.getName () + " WHERE " + exp; 676 677 q = new Query (); 678 679 try 680 { 681 682 q.parse (query); 683 684 } catch (Exception e) { 685 686 throw new QueryExecutionException ("Unable to create statement using WHERE clause: " + 687 exp + 688 " and class: " + 689 c.getName () + 690 " (gained from objects in list passed in)", 691 e); 692 693 } 694 695 if (this.foreachQueryCache == null) 697 { 698 699 this.foreachQueryCache = new HashMap (); 700 701 } 702 703 this.foreachQueryCache.put (exp, 704 q); 705 706 } 707 708 return q.execute (objs).getResults (); 709 710 } 711 712 public List foreach (Expression listFunction, 713 Expression exp) 714 throws QueryExecutionException 715 { 716 717 Object o = listFunction.getValue (this.q.getCurrentObject (), 719 this.q); 720 721 if (!(o instanceof List )) 722 { 723 724 throw new QueryExecutionException ("Expected expression: " + 725 listFunction + 726 " to return instance of: " + 727 List .class.getName () + 728 " but returned instance of: " + 729 o.getClass ().getName ()); 730 731 } 732 733 List l = (List ) o; 734 735 return this.foreach (l, 736 exp); 737 738 } 739 740 755 public List find (List objs, 756 Expression exp) 757 throws QueryExecutionException 758 { 759 760 Object currobj = this.q.getCurrentObject (); 763 List allobjs = (List ) this.q.getVariable (Query.ALL_OBJS_VAR_NAME); 764 765 this.q.setAllObjects (objs); 766 767 List r = new ArrayList (); 768 769 int s = objs.size (); 770 771 for (int i = 0; i < s; i++) 772 { 773 774 Object o = objs.get (i); 775 776 this.q.setCurrentObject (o); 777 778 try 779 { 780 781 if (exp.isTrue (o, 782 this.q)) 783 { 784 785 r.add (o); 786 787 } 788 789 } catch (Exception e) { 790 791 throw new QueryExecutionException ("Unable to evaulate expression: " + 792 exp + 793 " against object: " + 794 i + 795 " (class: " + 796 o.getClass ().getName () + 797 ")", 798 e); 799 800 } 801 802 } 803 804 this.q.setCurrentObject (currobj); 806 this.q.setAllObjects (allobjs); 807 808 return r; 809 810 } 811 812 831 public Map grp (List objs, 832 Expression exp) 833 throws QueryExecutionException 834 { 835 836 Object currobj = this.q.getCurrentObject (); 839 List allobjs = (List ) this.q.getVariable (Query.ALL_OBJS_VAR_NAME); 840 841 this.q.setAllObjects (objs); 842 843 Map r = new LinkedHashMap (); 844 845 int s = objs.size (); 846 847 for (int i = 0; i < s; i++) 848 { 849 850 Object o = objs.get (i); 851 852 this.q.setCurrentObject (o); 853 854 try 855 { 856 857 Object v = exp.getValue (o, 858 this.q); 859 860 List vs = (List ) r.get (v); 861 862 if (vs == null) 863 { 864 865 vs = new ArrayList (); 866 867 r.put (v, 868 vs); 869 870 } 871 872 vs.add (v); 873 874 } catch (Exception e) { 875 876 throw new QueryExecutionException ("Unable to evaulate expression: " + 877 exp + 878 " against object: " + 879 i + 880 " (class: " + 881 o.getClass ().getName () + 882 ")", 883 e); 884 885 } 886 887 } 888 889 this.q.setCurrentObject (currobj); 891 this.q.setAllObjects (allobjs); 892 893 return r; 894 895 } 896 897 } 898 | Popular Tags |