1 24 package org.ofbiz.base.util; 25 26 import java.lang.reflect.Constructor ; 27 import java.lang.reflect.InvocationTargetException ; 28 import java.math.BigDecimal ; 29 import java.text.DateFormat ; 30 import java.text.NumberFormat ; 31 import java.text.ParseException ; 32 import java.text.SimpleDateFormat ; 33 import java.util.Collection ; 34 import java.util.Date ; 35 import java.util.List ; 36 import java.util.Locale ; 37 import java.util.Map ; 38 39 import javolution.util.FastMap; 40 41 51 public class ObjectType { 52 53 public static final String module = ObjectType.class.getName(); 54 55 public static final Object NULL = new NullObject(); 56 57 protected static Map classCache = new FastMap(); 58 59 public static final String LANG_PACKAGE = "java.lang."; public static final String SQL_PACKAGE = "java.sql."; 62 68 public static Class loadClass(String className) throws ClassNotFoundException { 69 Class theClass = (Class ) CachedClassLoader.globalClassNameClassMap.get(className); 71 72 if (theClass != null) return theClass; 73 74 return loadClass(className, null); 75 } 76 77 84 public static Class loadClass(String className, ClassLoader loader) throws ClassNotFoundException { 85 Class theClass = (Class ) CachedClassLoader.globalClassNameClassMap.get(className); 87 88 if (theClass != null) return theClass; 89 90 if (loader == null) loader = Thread.currentThread().getContextClassLoader(); 91 92 try { 93 theClass = loader.loadClass(className); 94 } catch (Exception e) { 95 theClass = (Class ) classCache.get(className); 96 if (theClass == null) { 97 synchronized (ObjectType.class) { 98 theClass = (Class ) classCache.get(className); 99 if (theClass == null) { 100 theClass = Class.forName(className); 101 if (theClass != null) { 102 if (Debug.verboseOn()) Debug.logVerbose("Loaded Class: " + theClass.getName(), module); 103 classCache.put(className, theClass); 104 } 105 } 106 } 107 } 108 } 109 110 return theClass; 111 } 112 113 121 public static Object getInstance(String className) throws ClassNotFoundException , 122 InstantiationException , IllegalAccessException { 123 Class c = loadClass(className); 124 Object o = c.newInstance(); 125 126 if (Debug.verboseOn()) Debug.logVerbose("Instantiated object: " + o.toString(), module); 127 return o; 128 } 129 130 137 public static boolean interfaceOf(Class objectClass, String interfaceName) throws ClassNotFoundException { 138 Class interfaceClass = loadClass(interfaceName); 139 140 return interfaceOf(objectClass, interfaceClass); 141 } 142 143 149 public static boolean interfaceOf(Class objectClass, Object interfaceObject) { 150 Class interfaceClass = interfaceObject.getClass(); 151 152 return interfaceOf(objectClass, interfaceClass); 153 } 154 155 164 public static Object getInstance(String className, Object [] parameters) throws ClassNotFoundException , 165 InstantiationException , IllegalAccessException , NoSuchMethodException , InvocationTargetException { 166 Class [] sig = new Class [parameters.length]; 167 for (int i = 0; i < sig.length; i++) { 168 sig[i] = parameters[i].getClass(); 169 } 170 Class c = loadClass(className); 171 Constructor con = c.getConstructor(sig); 172 Object o = con.newInstance(parameters); 173 174 if (Debug.verboseOn()) Debug.logVerbose("Instantiated object: " + o.toString(), module); 175 return o; 176 } 177 178 185 public static boolean interfaceOf(Object obj, String interfaceName) throws ClassNotFoundException { 186 Class interfaceClass = loadClass(interfaceName); 187 188 return interfaceOf(obj, interfaceClass); 189 } 190 191 197 public static boolean interfaceOf(Object obj, Object interfaceObject) { 198 Class interfaceClass = interfaceObject.getClass(); 199 200 return interfaceOf(obj, interfaceClass); 201 } 202 203 209 public static boolean interfaceOf(Object obj, Class interfaceClass) { 210 Class objectClass = obj.getClass(); 211 212 return interfaceOf(objectClass, interfaceClass); 213 } 214 215 221 public static boolean interfaceOf(Class objectClass, Class interfaceClass) { 222 while (objectClass != null) { 223 Class [] ifaces = objectClass.getInterfaces(); 224 225 for (int i = 0; i < ifaces.length; i++) { 226 if (ifaces[i] == interfaceClass) return true; 227 } 228 objectClass = objectClass.getSuperclass(); 229 } 230 return false; 231 } 232 233 240 public static boolean isOrSubOf(Class objectClass, String parentName) throws ClassNotFoundException { 241 Class parentClass = loadClass(parentName); 242 243 return isOrSubOf(objectClass, parentClass); 244 } 245 246 252 public static boolean isOrSubOf(Class objectClass, Object parentObject) { 253 Class parentClass = parentObject.getClass(); 254 255 return isOrSubOf(objectClass, parentClass); 256 } 257 258 265 public static boolean isOrSubOf(Object obj, String parentName) throws ClassNotFoundException { 266 Class parentClass = loadClass(parentName); 267 268 return isOrSubOf(obj, parentClass); 269 } 270 271 277 public static boolean isOrSubOf(Object obj, Object parentObject) { 278 Class parentClass = parentObject.getClass(); 279 280 return isOrSubOf(obj, parentClass); 281 } 282 283 289 public static boolean isOrSubOf(Object obj, Class parentClass) { 290 Class objectClass = obj.getClass(); 291 292 return isOrSubOf(objectClass, parentClass); 293 } 294 295 301 public static boolean isOrSubOf(Class objectClass, Class parentClass) { 302 303 while (objectClass != null) { 304 if (objectClass == parentClass) return true; 305 objectClass = objectClass.getSuperclass(); 306 } 307 return false; 308 } 309 310 316 public static boolean instanceOf(Class objectClass, Object typeObject) { 317 Class typeClass = typeObject.getClass(); 318 319 return instanceOf(objectClass, typeClass); 320 } 321 322 328 public static boolean instanceOf(Class objectClass, String typeName) { 329 return instanceOf(objectClass, typeName, null); 330 } 331 332 338 public static boolean instanceOf(Object obj, Object typeObject) { 339 Class typeClass = typeObject.getClass(); 340 341 return instanceOf(obj, typeClass); 342 } 343 344 350 public static boolean instanceOf(Object obj, String typeName) { 351 return instanceOf(obj, typeName, null); 352 } 353 354 361 public static boolean instanceOf(Class objectClass, String typeName, ClassLoader loader) { 362 Class infoClass = loadInfoClass(typeName, loader); 363 364 if (infoClass == null) 365 throw new IllegalArgumentException ("Illegal type found in info map (could not load class for specified type)"); 366 367 return instanceOf(objectClass, infoClass); 368 } 369 370 377 public static boolean instanceOf(Object obj, String typeName, ClassLoader loader) { 378 Class infoClass = loadInfoClass(typeName, loader); 379 380 if (infoClass == null) 381 throw new IllegalArgumentException ("Illegal type found in info map (could not load class for specified type)"); 382 383 return instanceOf(obj, infoClass); 384 } 385 386 public static Class loadInfoClass(String typeName, ClassLoader loader) { 387 try { 389 return ObjectType.loadClass(typeName, loader); 390 } catch (SecurityException se1) { 391 throw new IllegalArgumentException ("Problems with classloader: security exception (" + 392 se1.getMessage() + ")"); 393 } catch (ClassNotFoundException e1) { 394 try { 395 return ObjectType.loadClass(LANG_PACKAGE + typeName, loader); 396 } catch (SecurityException se2) { 397 throw new IllegalArgumentException ("Problems with classloader: security exception (" + 398 se2.getMessage() + ")"); 399 } catch (ClassNotFoundException e2) { 400 try { 401 return ObjectType.loadClass(SQL_PACKAGE + typeName, loader); 402 } catch (SecurityException se3) { 403 throw new IllegalArgumentException ("Problems with classloader: security exception (" + 404 se3.getMessage() + ")"); 405 } catch (ClassNotFoundException e3) { 406 throw new IllegalArgumentException ("Cannot find and load the class of type: " + typeName + 407 " or of type: " + LANG_PACKAGE + typeName + " or of type: " + SQL_PACKAGE + typeName + 408 ": (" + e3.getMessage() + ")"); 409 } 410 } 411 } 412 } 413 414 420 public static boolean instanceOf(Object obj, Class typeClass) { 421 if (obj == null) return true; 422 Class objectClass = obj.getClass(); 423 return instanceOf(objectClass, typeClass); 424 } 425 426 432 public static boolean instanceOf(Class objectClass, Class typeClass) { 433 if (typeClass.isInterface()) { 434 return interfaceOf(objectClass, typeClass); 435 } else { 436 return isOrSubOf(objectClass, typeClass); 437 } 438 } 439 440 452 public static Object simpleTypeConvert(Object obj, String type, String format, Locale locale, boolean noTypeFail) throws GeneralException { 453 if (obj == null) { 454 return null; 455 } 456 457 if (obj.getClass().getName().equals(type)) { 458 return obj; 459 } 460 if ("PlainString".equals(type)) { 461 return obj.toString(); 462 } 463 if ("Object".equals(type) || "java.lang.Object".equals(type)) { 464 return obj; 465 } 466 467 String fromType = null; 468 469 if (obj instanceof java.lang.String ) { 470 fromType = "String"; 471 String str = (String ) obj; 472 if ("String".equals(type) || "java.lang.String".equals(type)) { 473 return obj; 474 } 475 if (str.length() == 0) { 476 return null; 477 } 478 479 if ("Boolean".equals(type) || "java.lang.Boolean".equals(type)) { 480 Boolean value = null; 481 if (str.equalsIgnoreCase("TRUE")) 482 value = new Boolean (true); 483 else 484 value = new Boolean (false); 485 return value; 486 } else if ("Locale".equals(type) || "java.util.Locale".equals(type)) { 487 Locale loc = UtilMisc.parseLocale(str); 488 if (loc != null) { 489 return loc; 490 } else { 491 throw new GeneralException("Could not convert " + str + " to " + type + ": "); 492 } 493 } else if ("BigDecimal".equals(type) || "java.math.BigDecimal".equals(type)) { 494 try { 495 NumberFormat nf = null; 496 if (locale == null) { 497 nf = NumberFormat.getNumberInstance(); 498 } else { 499 nf = NumberFormat.getNumberInstance(locale); 500 } 501 Number tempNum = nf.parse(str); 502 return new BigDecimal (tempNum.toString()); 503 } catch (ParseException e) { 504 throw new GeneralException("Could not convert " + str + " to " + type + ": ", e); 505 } 506 } else if ("Double".equals(type) || "java.lang.Double".equals(type)) { 507 try { 508 NumberFormat nf = null; 509 if (locale == null) { 510 nf = NumberFormat.getNumberInstance(); 511 } else { 512 nf = NumberFormat.getNumberInstance(locale); 513 } 514 Number tempNum = nf.parse(str); 515 516 return new Double (tempNum.doubleValue()); 517 } catch (ParseException e) { 518 throw new GeneralException("Could not convert " + str + " to " + type + ": ", e); 519 } 520 } else if ("Float".equals(type) || "java.lang.Float".equals(type)) { 521 try { 522 NumberFormat nf = null; 523 if (locale == null) { 524 nf = NumberFormat.getNumberInstance(); 525 } else { 526 nf = NumberFormat.getNumberInstance(locale); 527 } 528 Number tempNum = nf.parse(str); 529 530 return new Float (tempNum.floatValue()); 531 } catch (ParseException e) { 532 throw new GeneralException("Could not convert " + str + " to " + type + ": ", e); 533 } 534 } else if ("Long".equals(type) || "java.lang.Long".equals(type)) { 535 try { 536 NumberFormat nf = null; 537 if (locale == null) { 538 nf = NumberFormat.getNumberInstance(); 539 } else { 540 nf = NumberFormat.getNumberInstance(locale); 541 } 542 nf.setMaximumFractionDigits(0); 543 Number tempNum = nf.parse(str); 544 545 return new Long (tempNum.longValue()); 546 } catch (ParseException e) { 547 throw new GeneralException("Could not convert " + str + " to " + type + ": ", e); 548 } 549 } else if ("Integer".equals(type) || "java.lang.Integer".equals(type)) { 550 try { 551 NumberFormat nf = null; 552 if (locale == null) { 553 nf = NumberFormat.getNumberInstance(); 554 } else { 555 nf = NumberFormat.getNumberInstance(locale); 556 } 557 nf.setMaximumFractionDigits(0); 558 Number tempNum = nf.parse(str); 559 560 return new Integer (tempNum.intValue()); 561 } catch (ParseException e) { 562 throw new GeneralException("Could not convert " + str + " to " + type + ": ", e); 563 } 564 } else if ("Date".equals(type) || "java.sql.Date".equals(type)) { 565 if (format == null || format.length() == 0) { 566 try { 567 return java.sql.Date.valueOf(str); 568 } catch (Exception e) { 569 try { 570 DateFormat df = null; 571 if (locale != null) { 572 df = DateFormat.getDateInstance(DateFormat.SHORT, locale); 573 } else { 574 df = DateFormat.getDateInstance(DateFormat.SHORT); 575 } 576 Date fieldDate = df.parse(str); 577 578 return new java.sql.Date (fieldDate.getTime()); 579 } catch (ParseException e1) { 580 throw new GeneralException("Could not convert " + str + " to " + type + ": ", e); 581 } 582 } 583 } else { 584 try { 585 SimpleDateFormat sdf = new SimpleDateFormat (format); 586 java.util.Date fieldDate = sdf.parse(str); 587 return new java.sql.Date (fieldDate.getTime()); 588 } catch (ParseException e) { 589 throw new GeneralException("Could not convert " + str + " to " + type + ": ", e); 590 } 591 } 592 } else if ("Time".equals(type) || "java.sql.Time".equals(type)) { 593 if (format == null || format.length() == 0) { 594 try { 595 return java.sql.Time.valueOf(str); 596 } catch (Exception e) { 597 try { 598 DateFormat df = null; 599 if (locale != null) { 600 df = DateFormat.getTimeInstance(DateFormat.SHORT, locale); 601 } else { 602 df = DateFormat.getTimeInstance(DateFormat.SHORT); 603 } 604 Date fieldDate = df.parse(str); 605 606 return new java.sql.Time (fieldDate.getTime()); 607 } catch (ParseException e1) { 608 throw new GeneralException("Could not convert " + str + " to " + type + ": ", e); 609 } 610 } 611 } else { 612 try { 613 SimpleDateFormat sdf = new SimpleDateFormat (format); 614 java.util.Date fieldDate = sdf.parse(str); 615 return new java.sql.Time (fieldDate.getTime()); 616 } catch (ParseException e) { 617 throw new GeneralException("Could not convert " + str + " to " + type + ": ", e); 618 } 619 } 620 } else if ("Timestamp".equals(type) || "java.sql.Timestamp".equals(type)) { 621 if (format == null || format.length() == 0) { 622 try { 623 return java.sql.Timestamp.valueOf(str); 624 } catch (Exception e) { 625 try { 626 DateFormat df = null; 627 if (locale != null) { 628 df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, locale); 629 } else { 630 df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT); 631 } 632 Date fieldDate = df.parse(str); 633 return new java.sql.Timestamp (fieldDate.getTime()); 634 } catch (ParseException e1) { 635 throw new GeneralException("Could not convert " + str + " to " + type + ": ", e); 636 } 637 } 638 } else { 639 try { 640 SimpleDateFormat sdf = new SimpleDateFormat (format); 641 java.util.Date fieldDate = sdf.parse(str); 642 return new java.sql.Timestamp (fieldDate.getTime()); 643 } catch (ParseException e) { 644 throw new GeneralException("Could not convert " + str + " to " + type + ": ", e); 645 } 646 } 647 } else { 648 throw new GeneralException("Conversion from " + fromType + " to " + type + " not currently supported"); 649 } 650 } else if (obj instanceof Double ) { 651 fromType = "Double"; 652 Double dbl = (Double ) obj; 653 654 if ("String".equals(type) || "java.lang.String".equals(type)) { 655 NumberFormat nf = null; 656 657 if (locale == null) { 658 nf = NumberFormat.getNumberInstance(); 659 } else { 660 nf = NumberFormat.getNumberInstance(locale); 661 } 662 return nf.format(dbl.doubleValue()); 663 } else if ("BigDecimal".equals(type) || "java.math.BigDecimal".equals(type)) { 664 return new BigDecimal (dbl.doubleValue()); 665 } else if ("Double".equals(type) || "java.lang.Double".equals(type)) { 666 return obj; 667 } else if ("Float".equals(type) || "java.lang.Float".equals(type)) { 668 return new Float (dbl.floatValue()); 669 } else if ("Long".equals(type) || "java.lang.Long".equals(type)) { 670 return new Long (Math.round(dbl.doubleValue())); 671 } else if ("Integer".equals(type) || "java.lang.Integer".equals(type)) { 672 return new Integer ((int) Math.round(dbl.doubleValue())); 673 } else { 674 throw new GeneralException("Conversion from " + fromType + " to " + type + " not currently supported"); 675 } 676 } else if (obj instanceof Float ) { 677 fromType = "Float"; 678 Float flt = (Float ) obj; 679 680 if ("String".equals(type)) { 681 NumberFormat nf = null; 682 683 if (locale == null) 684 nf = NumberFormat.getNumberInstance(); 685 else 686 nf = NumberFormat.getNumberInstance(locale); 687 return nf.format(flt.doubleValue()); 688 } else if ("BigDecimal".equals(type) || "java.math.BigDecimal".equals(type)) { 689 return new BigDecimal (flt.doubleValue()); 690 } else if ("Double".equals(type)) { 691 return new Double (flt.doubleValue()); 692 } else if ("Float".equals(type)) { 693 return obj; 694 } else if ("Long".equals(type)) { 695 return new Long (Math.round(flt.doubleValue())); 696 } else if ("Integer".equals(type)) { 697 return new Integer ((int) Math.round(flt.doubleValue())); 698 } else { 699 throw new GeneralException("Conversion from " + fromType + " to " + type + " not currently supported"); 700 } 701 } else if (obj instanceof Long ) { 702 fromType = "Long"; 703 Long lng = (Long ) obj; 704 705 if ("String".equals(type) || "java.lang.String".equals(type)) { 706 NumberFormat nf = null; 707 if (locale == null) { 708 nf = NumberFormat.getNumberInstance(); 709 } else { 710 nf = NumberFormat.getNumberInstance(locale); 711 } 712 return nf.format(lng.longValue()); 713 } else if ("BigDecimal".equals(type) || "java.math.BigDecimal".equals(type)) { 714 return BigDecimal.valueOf(lng.longValue()); 715 } else if ("Double".equals(type) || "java.lang.Double".equals(type)) { 716 return new Double (lng.doubleValue()); 717 } else if ("Float".equals(type) || "java.lang.Float".equals(type)) { 718 return new Float (lng.floatValue()); 719 } else if ("Long".equals(type) || "java.lang.Long".equals(type)) { 720 return obj; 721 } else if ("Integer".equals(type) || "java.lang.Integer".equals(type)) { 722 return new Integer (lng.intValue()); 723 } else { 724 throw new GeneralException("Conversion from " + fromType + " to " + type + " not currently supported"); 725 } 726 } else if (obj instanceof Integer ) { 727 fromType = "Integer"; 728 Integer intgr = (Integer ) obj; 729 if ("String".equals(type) || "java.lang.String".equals(type)) { 730 NumberFormat nf = null; 731 if (locale == null) { 732 nf = NumberFormat.getNumberInstance(); 733 } else { 734 nf = NumberFormat.getNumberInstance(locale); 735 } 736 return nf.format(intgr.longValue()); 737 } else if ("BigDecimal".equals(type) || "java.math.BigDecimal".equals(type)) { 738 return BigDecimal.valueOf(intgr.longValue()); 739 } else if ("Double".equals(type) || "java.lang.Double".equals(type)) { 740 return new Double (intgr.doubleValue()); 741 } else if ("Float".equals(type) || "java.lang.Float".equals(type)) { 742 return new Float (intgr.floatValue()); 743 } else if ("Long".equals(type) || "java.lang.Long".equals(type)) { 744 return new Long (intgr.longValue()); 745 } else if ("Integer".equals(type) || "java.lang.Integer".equals(type)) { 746 return obj; 747 } else { 748 throw new GeneralException("Conversion from " + fromType + " to " + type + " not currently supported"); 749 } 750 } else if (obj instanceof BigDecimal ) { 751 fromType = "BigDecimal"; 752 BigDecimal bigDec = (BigDecimal ) obj; 753 if ("String".equals(type) || "java.lang.String".equals(type)) { 754 NumberFormat nf = null; 755 if (locale == null) { 756 nf = NumberFormat.getNumberInstance(); 757 } else { 758 nf = NumberFormat.getNumberInstance(locale); 759 } 760 return nf.format(bigDec.doubleValue()); 761 } else if ("BigDecimal".equals(type) || "java.math.BigDecimal".equals(type)) { 762 return obj; 763 } else if ("Double".equals(type) || "java.lang.Double".equals(type)) { 764 return new Double (bigDec.doubleValue()); 765 } else if ("Float".equals(type) || "java.lang.Float".equals(type)) { 766 return new Float (bigDec.floatValue()); 767 } else if ("Long".equals(type) || "java.lang.Long".equals(type)) { 768 return new Long (bigDec.longValue()); 769 } else if ("Integer".equals(type) || "java.lang.Integer".equals(type)) { 770 return new Integer (bigDec.intValue()); 771 } else { 772 throw new GeneralException("Conversion from " + fromType + " to " + type + " not currently supported"); 773 } 774 } else if (obj instanceof java.sql.Date ) { 775 fromType = "Date"; 776 java.sql.Date dte = (java.sql.Date ) obj; 777 if ("String".equals(type) || "java.lang.String".equals(type)) { 778 if (format == null || format.length() == 0) { 779 return dte.toString(); 780 } else { 781 SimpleDateFormat sdf = new SimpleDateFormat (format); 782 return sdf.format(new java.util.Date (dte.getTime())); 783 } 784 } else if ("Date".equals(type) || "java.sql.Date".equals(type)) { 785 return obj; 786 } else if ("Time".equals(type) || "java.sql.Time".equals(type)) { 787 throw new GeneralException("Conversion from " + fromType + " to " + type + " not currently supported"); 788 } else if ("Timestamp".equals(type) || "java.sql.Timestamp".equals(type)) { 789 return new java.sql.Timestamp (dte.getTime()); 790 } else { 791 throw new GeneralException("Conversion from " + fromType + " to " + type + " not currently supported"); 792 } 793 } else if (obj instanceof java.sql.Time ) { 794 fromType = "Time"; 795 java.sql.Time tme = (java.sql.Time ) obj; 796 797 if ("String".equals(type) || "java.lang.String".equals(type)) { 798 if (format == null || format.length() == 0) { 799 return tme.toString(); 800 } else { 801 SimpleDateFormat sdf = new SimpleDateFormat (format); 802 803 return sdf.format(new java.util.Date (tme.getTime())); 804 } 805 } else if ("Date".equals(type) || "java.sql.Date".equals(type)) { 806 throw new GeneralException("Conversion from " + fromType + " to " + type + " not currently supported"); 807 } else if ("Time".equals(type) || "java.sql.Time".equals(type)) { 808 return obj; 809 } else if ("Timestamp".equals(type) || "java.sql.Timestamp".equals(type)) { 810 return new java.sql.Timestamp (tme.getTime()); 811 } else { 812 throw new GeneralException("Conversion from " + fromType + " to " + type + " not currently supported"); 813 } 814 } else if (obj instanceof java.sql.Timestamp ) { 815 fromType = "Timestamp"; 816 java.sql.Timestamp tme = (java.sql.Timestamp ) obj; 817 818 if ("String".equals(type) || "java.lang.String".equals(type)) { 819 if (format == null || format.length() == 0) { 820 return tme.toString(); 821 } else { 822 SimpleDateFormat sdf = new SimpleDateFormat (format); 823 824 return sdf.format(new java.util.Date (tme.getTime())); 825 } 826 } else if ("Date".equals(type) || "java.sql.Date".equals(type)) { 827 return new java.sql.Date (tme.getTime()); 828 } else if ("Time".equals(type) || "java.sql.Time".equals(type)) { 829 return new java.sql.Time (tme.getTime()); 830 } else if ("Timestamp".equals(type) || "java.sql.Timestamp".equals(type)) { 831 return obj; 832 } else { 833 throw new GeneralException("Conversion from " + fromType + " to " + type + " not currently supported"); 834 } 835 } else if (obj instanceof java.lang.Boolean ) { 836 fromType = "Boolean"; 837 Boolean bol = (Boolean ) obj; 838 if ("Boolean".equals(type) || "java.lang.Boolean".equals(type)) { 839 return bol; 840 } else if ("String".equals(type) || "java.lang.String".equals(type)) { 841 return bol.toString(); 842 } else if ("Integer".equals(type) || "java.lang.Integer".equals(type)) { 843 if (bol.booleanValue()) 844 return new Integer (1); 845 else 846 return new Integer (0); 847 } else { 848 throw new GeneralException("Conversion from " + fromType + " to " + type + " not currently supported"); 849 } 850 } else if (obj instanceof java.util.Locale ) { 851 fromType = "Locale"; 852 Locale loc = (Locale ) obj; 853 if ("Locale".equals(type) || "java.util.Locale".equals(type)) { 854 return loc; 855 } else if ("String".equals(type) || "java.lang.String".equals(type)) { 856 return loc.toString(); 857 } else { 858 throw new GeneralException("Conversion from " + fromType + " to " + type + " not currently supported"); 859 } 860 } else if (obj.getClass().getName().equals("org.ofbiz.entity.GenericValue")) { 861 fromType = "GenericValue"; 862 if ("GenericValue".equals(type) || "org.ofbiz.entity.GenericValue".equals(type)) { 863 return obj; 864 } else if ("Map".equals(type) || "java.util.Map".equals(type)) { 865 return obj; 866 } else if ("String".equals(type) || "java.lang.String".equals(type)) { 867 return obj.toString(); 868 } else { 869 throw new GeneralException("Conversion from " + fromType + " to " + type + " not currently supported"); 870 } 871 } else if (obj instanceof java.util.Map ) { 872 fromType = "Map"; 873 Map map = (Map ) obj; 874 if ("Map".equals(type) || "java.util.Map".equals(type)) { 875 return map; 876 } else if ("String".equals(type) || "java.lang.String".equals(type)) { 877 return map.toString(); 878 } else { 879 throw new GeneralException("Conversion from " + fromType + " to " + type + " not currently supported"); 880 } 881 } else if (obj instanceof java.util.List ) { 882 fromType = "List"; 883 List list = (List ) obj; 884 if ("List".equals(type) || "java.util.List".equals(type)) { 885 return list; 886 } else if ("String".equals(type) || "java.lang.String".equals(type)) { 887 return list.toString(); 888 } else { 889 throw new GeneralException("Conversion from " + fromType + " to " + type + " not currently supported"); 890 } 891 } else { 892 if ("String".equals(type) || "java.lang.String".equals(type)) { 894 Debug.logWarning("No special conversion available for " + obj.getClass().getName() + " to String, returning object.toString().", module); 895 return obj.toString(); 896 } 897 898 if (noTypeFail) { 899 throw new GeneralException("Conversion from " + obj.getClass().getName() + " to " + type + " not currently supported"); 900 } else { 901 Debug.logWarning("No type conversion available for " + obj.getClass().getName() + " to " + type + ", returning original object.", module); 902 return obj; 903 } 904 } 905 } 906 907 public static Object simpleTypeConvert(Object obj, String type, String format, Locale locale) throws GeneralException { 908 return simpleTypeConvert(obj, type, format, locale, true); 909 } 910 911 public static Boolean doRealCompare(Object value1, Object value2, String operator, String type, String format, 912 List messages, Locale locale, ClassLoader loader) { 913 boolean verboseOn = Debug.verboseOn(); 914 915 if (verboseOn) Debug.logVerbose("Comparing value1: \"" + value1 + "\" " + operator + " value2:\"" + value2 + "\"", module); 916 917 try { 918 if (!"PlainString".equals(type)) { 919 Class clz = ObjectType.loadClass(type, loader); 920 type = clz.getName(); 921 } 922 } catch (ClassNotFoundException e) { 923 Debug.logWarning("The specified type [" + type + "] is not a valid class or a known special type, may see more errors later because of this: " + e.getMessage(), module); 924 } 925 926 if ("is-null".equals(operator) && value1 == null) { 928 return Boolean.TRUE; 929 } else if ("is-not-null".equals(operator) && value1 == null) { 930 return Boolean.FALSE; 931 } else if ("is-empty".equals(operator) && value1 == null) { 932 return Boolean.TRUE; 933 } else if ("is-not-empty".equals(operator) && value1 == null) { 934 return Boolean.FALSE; 935 } else if ("contains".equals(operator) && value1 == null) { 936 return Boolean.FALSE; 937 } 938 939 int result = 0; 940 941 Object convertedValue2 = null; 942 if (value2 != null) { 943 try { 944 convertedValue2 = ObjectType.simpleTypeConvert(value2, type, format, locale); 945 } catch (GeneralException e) { 946 Debug.logError(e, module); 947 messages.add("Could not convert value2 for comparison: " + e.getMessage()); 948 return null; 949 } 950 } 951 952 if ("contains".equals(operator) && value1 instanceof Collection ) { 954 Collection col1 = (Collection ) value1; 955 if (col1.contains(convertedValue2)) { 956 return Boolean.TRUE; 957 } else { 958 return Boolean.FALSE; 959 } 960 } 961 962 Object convertedValue1 = null; 963 try { 964 convertedValue1 = ObjectType.simpleTypeConvert(value1, type, format, locale); 965 } catch (GeneralException e) { 966 Debug.logError(e, module); 967 messages.add("Could not convert value1 for comparison: " + e.getMessage()); 968 return null; 969 } 970 971 if (convertedValue1 == null || convertedValue2 == null) { 973 if ("equals".equals(operator)) { 974 if (convertedValue1 == null && convertedValue2 == null) { 975 return Boolean.TRUE; 976 } else { 977 return Boolean.FALSE; 978 } 979 } else if ("not-equals".equals(operator)) { 980 if (convertedValue1 == null && convertedValue2 == null) { 981 return Boolean.FALSE; 982 } else { 983 return Boolean.TRUE; 984 } 985 } else if ("is-not-empty".equals(operator) || "is-empty".equals(operator)) { 986 } else { 988 if (convertedValue1 == null) { 989 messages.add("Left value is null, cannot complete compare for the operator " + operator); 990 return null; 991 } 992 if (convertedValue2 == null) { 993 messages.add("Right value is null, cannot complete compare for the operator " + operator); 994 return null; 995 } 996 } 997 } 998 999 if ("contains".equals(operator)) { 1000 if ("java.lang.String".equals(type) || "PlainString".equals(type)) { 1001 String str1 = (String ) convertedValue1; 1002 String str2 = (String ) convertedValue2; 1003 1004 if (str1.indexOf(str2) < 0) { 1005 return Boolean.FALSE; 1006 } else { 1007 return Boolean.TRUE; 1008 } 1009 } else { 1010 messages.add("Error in XML file: cannot do a contains compare between a String and a non-String type"); 1011 return null; 1012 } 1013 } else if ("is-empty".equals(operator)) { 1014 if (convertedValue1 == null) 1015 return Boolean.TRUE; 1016 if (convertedValue1 instanceof String && ((String ) convertedValue1).length() == 0) 1017 return Boolean.TRUE; 1018 if (convertedValue1 instanceof List && ((List ) convertedValue1).size() == 0) 1019 return Boolean.TRUE; 1020 if (convertedValue1 instanceof Map && ((Map ) convertedValue1).size() == 0) 1021 return Boolean.TRUE; 1022 return Boolean.FALSE; 1023 } else if ("is-not-empty".equals(operator)) { 1024 if (convertedValue1 == null) 1025 return Boolean.FALSE; 1026 if (convertedValue1 instanceof String && ((String ) convertedValue1).length() == 0) 1027 return Boolean.FALSE; 1028 if (convertedValue1 instanceof List && ((List ) convertedValue1).size() == 0) 1029 return Boolean.FALSE; 1030 if (convertedValue1 instanceof Map && ((Map ) convertedValue1).size() == 0) 1031 return Boolean.FALSE; 1032 return Boolean.TRUE; 1033 } 1034 1035 if ("java.lang.String".equals(type) || "PlainString".equals(type)) { 1036 String str1 = (String ) convertedValue1; 1037 String str2 = (String ) convertedValue2; 1038 1039 if (str1.length() == 0 || str2.length() == 0) { 1040 if ("equals".equals(operator)) { 1041 if (str1.length() == 0 && str2.length() == 0) { 1042 return Boolean.TRUE; 1043 } else { 1044 return Boolean.FALSE; 1045 } 1046 } else if ("not-equals".equals(operator)) { 1047 if (str1.length() == 0 && str2.length() == 0) { 1048 return Boolean.FALSE; 1049 } else { 1050 return Boolean.TRUE; 1051 } 1052 } else { 1053 messages.add("ERROR: Could not do a compare between strings with one empty string for the operator " + operator); 1054 return null; 1055 } 1056 } 1057 result = str1.compareTo(str2); 1058 } else if ("java.lang.Double".equals(type) || "java.lang.Float".equals(type) || "java.lang.Long".equals(type) || "java.lang.Integer".equals(type) || "java.math.BigDecimal".equals(type)) { 1059 Number tempNum = (Number ) convertedValue1; 1060 double value1Double = tempNum.doubleValue(); 1061 1062 tempNum = (Number ) convertedValue2; 1063 double value2Double = tempNum.doubleValue(); 1064 1065 if (value1Double < value2Double) 1066 result = -1; 1067 else if (value1Double > value2Double) 1068 result = 1; 1069 else 1070 result = 0; 1071 } else if ("java.sql.Date".equals(type)) { 1072 java.sql.Date value1Date = (java.sql.Date ) convertedValue1; 1073 java.sql.Date value2Date = (java.sql.Date ) convertedValue2; 1074 result = value1Date.compareTo(value2Date); 1075 } else if ("java.sql.Time".equals(type)) { 1076 java.sql.Time value1Time = (java.sql.Time ) convertedValue1; 1077 java.sql.Time value2Time = (java.sql.Time ) convertedValue2; 1078 result = value1Time.compareTo(value2Time); 1079 } else if ("java.sql.Timestamp".equals(type)) { 1080 java.sql.Timestamp value1Timestamp = (java.sql.Timestamp ) convertedValue1; 1081 java.sql.Timestamp value2Timestamp = (java.sql.Timestamp ) convertedValue2; 1082 result = value1Timestamp.compareTo(value2Timestamp); 1083 } else if ("java.lang.Boolean".equals(type)) { 1084 Boolean value1Boolean = (Boolean ) convertedValue1; 1085 Boolean value2Boolean = (Boolean ) convertedValue2; 1086 if ("equals".equals(operator)) { 1087 if ((value1Boolean.booleanValue() && value2Boolean.booleanValue()) || (!value1Boolean.booleanValue() && !value2Boolean.booleanValue())) 1088 result = 0; 1089 else 1090 result = 1; 1091 } else if ("not-equals".equals(operator)) { 1092 if ((!value1Boolean.booleanValue() && value2Boolean.booleanValue()) || (value1Boolean.booleanValue() && !value2Boolean.booleanValue())) 1093 result = 0; 1094 else 1095 result = 1; 1096 } else { 1097 messages.add("Can only compare Booleans using the operators 'equals' or 'not-equals'"); 1098 return null; 1099 } 1100 } else if ("java.lang.Object".equals(type)) { 1101 if (convertedValue1.equals(convertedValue2)) { 1102 result = 0; 1103 } else { 1104 result = 1; 1105 } 1106 } else { 1107 messages.add("Type \"" + type + "\" specified for compare not supported."); 1108 return null; 1109 } 1110 1111 if (verboseOn) Debug.logVerbose("Got Compare result: " + result + ", operator: " + operator, module); 1112 if ("less".equals(operator)) { 1113 if (result >= 0) 1114 return Boolean.FALSE; 1115 } else if ("greater".equals(operator)) { 1116 if (result <= 0) 1117 return Boolean.FALSE; 1118 } else if ("less-equals".equals(operator)) { 1119 if (result > 0) 1120 return Boolean.FALSE; 1121 } else if ("greater-equals".equals(operator)) { 1122 if (result < 0) 1123 return Boolean.FALSE; 1124 } else if ("equals".equals(operator)) { 1125 if (result != 0) 1126 return Boolean.FALSE; 1127 } else if ("not-equals".equals(operator)) { 1128 if (result == 0) 1129 return Boolean.FALSE; 1130 } else { 1131 messages.add("Specified compare operator \"" + operator + "\" not known."); 1132 return null; 1133 } 1134 1135 if (verboseOn) Debug.logVerbose("Returning true", module); 1136 return Boolean.TRUE; 1137 } 1138 1139 public static boolean isEmpty(Object value) { 1140 if (value == null) return true; 1141 1142 if (value instanceof String ) { 1143 if (((String ) value).length() == 0) { 1144 return true; 1145 } 1146 } else if (value instanceof Collection ) { 1147 if (((Collection ) value).size() == 0) { 1148 return true; 1149 } 1150 } else if (value instanceof Map ) { 1151 if (((Map ) value).size() == 0) { 1152 return true; 1153 } 1154 } 1155 return false; 1156 } 1157 1158 public static final class NullObject { 1159 public NullObject() { } 1160 1161 public String toString() { 1162 return "ObjectType.NullObject"; 1163 } 1164 1165 public boolean equals(Object other) { 1166 if (other instanceof NullObject) { 1167 return true; 1169 } else { 1170 return false; 1171 } 1172 } 1173 } 1174} 1175 | Popular Tags |