1 23 24 25 26 31 32 package com.sun.jdo.spi.persistence.support.ejb.ejbqlc; 33 34 import java.util.*; 35 36 import com.sun.jdo.spi.persistence.utility.I18NHelper; 37 import com.sun.jdo.api.persistence.model.Model; 38 import com.sun.jdo.api.persistence.model.jdo.*; 39 import com.sun.jdo.spi.persistence.support.ejb.model.util.NameMapper; 40 41 50 public class TypeSupport 51 { 52 53 public static final Object errorType = "error"; 54 55 56 public static final Object booleanType = "boolean"; 57 58 59 public static final Object byteType = "byte"; 60 61 62 public static final Object shortType = "short"; 63 64 65 public static final Object charType = "char"; 66 67 68 public static final Object intType = "int"; 69 70 71 public static final Object longType = "long"; 72 73 74 public static final Object floatType = "float"; 75 76 77 public static final Object doubleType = "double"; 78 79 80 public static final Object booleanClassType = "java.lang.Boolean"; 81 82 83 public static final Object byteClassType = "java.lang.Byte"; 84 85 86 public static final Object shortClassType = "java.lang.Short"; 87 88 89 public static final Object characterClassType = "java.lang.Character"; 90 91 92 public static final Object integerClassType = "java.lang.Integer"; 93 94 95 public static final Object longClassType = "java.lang.Long"; 96 97 98 public static final Object floatClassType = "java.lang.Float"; 99 100 101 public static final Object doubleClassType = "java.lang.Double"; 102 103 104 public static final Object stringType = "java.lang.String"; 105 106 107 public static final Object bigDecimalType = "java.math.BigDecimal"; 108 109 110 public static final Object bigIntegerType = "java.math.BigInteger"; 111 112 113 protected static Set numericTypes = new HashSet(); 114 115 116 protected static Set numericWrapperTypes = new HashSet(); 117 118 119 protected static Set dateTimeTypes = new HashSet(); 120 121 122 protected Model model; 123 124 125 protected NameMapper nameMapper; 126 127 128 protected final static ResourceBundle msgs = I18NHelper.loadBundle( 129 TypeSupport.class); 130 131 132 static 133 { 134 numericTypes.add(byteType); 135 numericTypes.add(shortType); 136 numericTypes.add(charType); 137 numericTypes.add(intType); 138 numericTypes.add(longType); 139 numericTypes.add(floatType); 140 numericTypes.add(doubleType); 141 142 numericWrapperTypes.add(byteClassType); 143 numericWrapperTypes.add(shortClassType); 144 numericWrapperTypes.add(characterClassType); 145 numericWrapperTypes.add(integerClassType); 146 numericWrapperTypes.add(longClassType); 147 numericWrapperTypes.add(floatClassType); 148 numericWrapperTypes.add(doubleClassType); 149 150 dateTimeTypes.add("java.util.Date"); dateTimeTypes.add("java.sql.Date"); dateTimeTypes.add("java.sql.Time"); dateTimeTypes.add("java.sql.Timestamp"); } 155 156 161 public TypeSupport(Model model, NameMapper nameMapper) 162 { 163 this.model = model; 164 this.nameMapper = nameMapper; 165 } 166 167 172 public Object getTypeInfo(String name) 173 { 174 return name; 175 } 176 177 180 public Object getTypeInfo(Class clazz) 181 { 182 return getTypeInfo(clazz.getName()); 183 } 184 185 188 public static boolean isErrorType(Object type) 189 { 190 return type.equals(errorType); 191 } 192 193 196 public static boolean isBooleanType(Object type) 197 { 198 return type.equals(booleanType) || 199 type.equals(booleanClassType); 200 } 201 202 205 public static boolean isCharType(Object type) 206 { 207 return type.equals(charType) || 208 type.equals(characterClassType); 209 } 210 211 214 public static boolean isIntType(Object type) 215 { 216 return type.equals(intType) || 217 type.equals(integerClassType); 218 } 219 220 223 public static boolean isDoubleType(Object type) 224 { 225 return type.equals(doubleType) || 226 type.equals(doubleClassType); 227 } 228 229 233 public static boolean isNumericType(Object type) 234 { 235 return numericTypes.contains(type); 236 } 237 238 242 public static boolean isNumericWrapperType(Object type) 243 { 244 return numericWrapperTypes.contains(type); 245 } 246 247 251 public static boolean isNumberType(Object type) 252 { 253 return isNumericType(type) || 254 isNumericWrapperType(type) || 255 bigDecimalType.equals(type) || 256 bigIntegerType.equals(type); 257 } 258 259 263 public static boolean isFloatingPointType(Object type) 264 { 265 return doubleType.equals(type) || 266 doubleClassType.equals(type) || 267 floatType.equals(type) || 268 floatClassType.equals(type); 269 } 270 271 272 public static boolean isStringType(Object type) 273 { 274 return type.equals(stringType); 275 } 276 277 278 public boolean isCollectionType(Object type) 279 { 280 return model.isCollection((String )type); 281 } 282 283 284 public boolean isDateTimeType(Object type) 285 { 286 return dateTimeTypes.contains(getTypeName(type)); 287 } 288 289 290 public boolean isOrderableType(Object type) 291 { 292 return isNumberType(type) || isDateTimeType(type) || isStringType(type); 293 } 294 295 299 public static Object getPrimitiveType(Object type) 300 { 301 Object result = errorType; 302 if (type.equals(booleanClassType)) 303 result = booleanType; 304 else if (type.equals(integerClassType)) 305 result = intType; 306 else if (type.equals(longClassType)) 307 result = longType; 308 else if (type.equals(floatClassType)) 309 result = floatType; 310 else if (type.equals(doubleClassType)) 311 result = doubleType; 312 else if (type.equals(byteClassType)) 313 result = byteType; 314 else if (type.equals(shortClassType)) 315 result = shortType; 316 else if (type.equals(characterClassType)) 317 result = charType; 318 return result; 319 } 320 321 325 public static Object getWrapperType(Object type) 326 { 327 Object result = errorType; 328 if (type.equals(booleanType)) 329 result = booleanClassType; 330 else if (type.equals(intType)) 331 result = integerClassType; 332 else if (type.equals(longType)) 333 result = longClassType; 334 else if (type.equals(floatType)) 335 result = floatClassType; 336 else if (type.equals(doubleType)) 337 result = doubleClassType; 338 else if (type.equals(byteType)) 339 result = byteClassType; 340 else if (type.equals(shortType)) 341 result = shortClassType; 342 else if (type.equals(charType)) 343 result = characterClassType; 344 return result; 345 } 346 347 351 public static Object binaryNumericPromotion(Object left, Object right) 352 { 353 if (isNumericType(left) && isNumericType(right)) { 354 if (left.equals(doubleType) || right.equals(doubleType)) 355 return doubleType; 356 else if (left.equals(floatType) || right.equals(floatType)) 357 return floatType; 358 else if (left.equals(longType) || right.equals(longType)) 359 return longType; 360 else 361 return intType; 362 } 363 return errorType; 364 } 365 366 370 public static Object unaryNumericPromotion(Object type) 371 { 372 if (isNumericType(type)) { 373 if (type.equals(byteType) || type.equals(shortType) || 374 type.equals(charType)) { 375 return intType; 376 } 377 else { 378 return type; 379 } 380 } 381 return errorType; 382 } 383 384 390 public boolean isCompatibleWith(Object left, Object right) 391 { 392 String leftTypeName = getTypeName(left); 393 String rightTypeName = getTypeName(right); 394 395 if (nameMapper.isLocalInterface(leftTypeName) && 396 nameMapper.isEjbName(rightTypeName)) 397 rightTypeName = nameMapper.getLocalInterfaceForEjbName(rightTypeName); 398 else if (nameMapper.isRemoteInterface(leftTypeName) && 399 nameMapper.isEjbName(rightTypeName)) 400 rightTypeName = nameMapper.getRemoteInterfaceForEjbName(rightTypeName); 401 else if (nameMapper.isLocalInterface(rightTypeName) && 402 nameMapper.isEjbName(leftTypeName)) 403 leftTypeName = nameMapper.getLocalInterfaceForEjbName(leftTypeName); 404 else if (nameMapper.isRemoteInterface(rightTypeName) && 405 nameMapper.isEjbName(leftTypeName)) 406 leftTypeName = nameMapper.getRemoteInterfaceForEjbName(leftTypeName); 407 408 return leftTypeName.equals(rightTypeName); 410 } 411 412 413 public static String getTypeName(Object type) 414 { 415 return (String )type; 416 } 417 418 419 public Object getTypeInfoForAbstractSchema(String abstractSchema) 420 { 421 return nameMapper.getEjbNameForAbstractSchema(abstractSchema); 422 } 423 424 425 public String getAbstractSchemaForTypeInfo(Object typeInfo) 426 { 427 String typeName = getTypeName(typeInfo); 428 return nameMapper.isEjbName(typeName) ? 429 nameMapper.getAbstractSchemaForEjbName(typeName) : 430 typeName; 431 } 432 433 434 public Object getFieldType(Object typeInfo, String fieldName) 435 { 436 String typeName = getTypeName(typeInfo); 437 if (!nameMapper.isEjbName(typeName)) { 438 ErrorMsg.fatal(I18NHelper.getMessage( 439 msgs, "ERR_EjbNameExpected", "TypeSupport.getFieldType", typeName)); } 442 443 String fieldType = model.getFieldType(typeName, fieldName); 444 if (nameMapper.isLocalInterface(fieldType)) { 446 fieldType = nameMapper.getEjbNameForLocalInterface( 447 typeName, fieldName, fieldType); 448 } 449 else if (nameMapper.isRemoteInterface(fieldType)) { 450 451 fieldType = nameMapper.getEjbNameForRemoteInterface( 452 typeName, fieldName, fieldType); 453 } 454 return getTypeInfo(fieldType); 455 } 456 457 462 public Object getFieldInfo(Object typeInfo, String fieldName) 463 { 464 Object fieldInfo = null; 465 String typeName = getTypeName(typeInfo); 466 if (!nameMapper.isEjbName(typeName)) { 467 ErrorMsg.fatal(I18NHelper.getMessage( 468 msgs, "ERR__EjbNameExpected", "TypeSupport.getFieldInfo", typeName)); } 471 String pcClassName = nameMapper.getPersistenceClassForEjbName(typeName); 472 String pcFieldName = nameMapper.getPersistenceFieldForEjbField( 473 typeName, fieldName); 474 PersistenceClassElement pce = model.getPersistenceClass(pcClassName); 475 if (pce != null) { 476 fieldInfo = pce.getField(pcFieldName); 477 } 478 return fieldInfo; 479 } 480 481 485 public boolean isRelationship(Object fieldInfo) 486 { 487 return (fieldInfo != null) && (fieldInfo instanceof RelationshipElement); 488 } 489 490 494 public Object getElementType(Object fieldInfo) 495 { 496 if ((fieldInfo != null) && (fieldInfo instanceof RelationshipElement)) { 497 String elementClass = ((RelationshipElement)fieldInfo).getElementClass(); 498 return nameMapper.getEjbNameForPersistenceClass(elementClass); 499 } 500 else 501 return null; 502 } 503 504 510 public String getPCForTypeInfo(Object typeInfo) 511 { 512 String typeName = getTypeName(typeInfo); 513 String pcClassName = 514 nameMapper.getPersistenceClassForEjbName(typeName); 515 return (pcClassName != null) ? pcClassName : typeName; 516 } 517 518 521 public boolean isEjbName(Object typeInfo) 522 { 523 return nameMapper.isEjbName(getTypeName(typeInfo)); 524 } 525 526 530 public boolean isEjbOrInterfaceName(Object typeInfo) 531 { 532 String typeName = getTypeName(typeInfo); 533 return nameMapper.isEjbName(typeName) || 534 nameMapper.isLocalInterface(typeName) || 535 nameMapper.isRemoteInterface(typeName); 536 } 537 538 542 public boolean isRemoteInterfaceOfEjb(Object typeInfo, String ejbName) 543 { 544 String typeName = getTypeName(typeInfo); 545 String remoteInterface = nameMapper.getRemoteInterfaceForEjbName(ejbName); 546 return (remoteInterface != null) && remoteInterface.equals(typeName); 547 548 } 549 550 554 public boolean isLocalInterfaceOfEjb(Object typeInfo, String ejbName) 555 { 556 String typeName = getTypeName(typeInfo); 557 String localInterface = nameMapper.getLocalInterfaceForEjbName(ejbName); 558 return (localInterface != null) && localInterface.equals(typeName); 559 } 560 561 565 public boolean isRemoteInterface(Object typeInfo) 566 { 567 return nameMapper.isRemoteInterface(getTypeName(typeInfo)); 568 } 569 570 574 public boolean isLocalInterface(Object typeInfo) 575 { 576 return nameMapper.isLocalInterface(getTypeName(typeInfo)); 577 } 578 579 583 public boolean hasRemoteInterface(Object typeInfo) 584 { 585 return nameMapper.getRemoteInterfaceForEjbName( 586 getTypeName(typeInfo)) != null; 587 } 588 589 593 public boolean hasLocalInterface(Object typeInfo) 594 { 595 return nameMapper.getLocalInterfaceForEjbName( 596 getTypeName(typeInfo)) != null; 597 } 598 599 603 public Object getSumReturnType(Object type) { 604 if (isFloatingPointType(type)) { 605 return doubleClassType; 606 } else if (isNumericType(type) || isNumericWrapperType(type)) { 607 return longClassType; 608 } else { 609 return type; 610 } 611 } 612 613 617 public Object getAvgReturnType(Object type) { 618 if (isNumericType(type) || isNumericWrapperType(type)) { 619 return doubleClassType; 620 } else { 621 return type; 622 } 623 } 624 625 629 public Object getMinMaxReturnType(Object type) { 630 if (isFloatingPointType(type)) { 631 return doubleClassType; 632 } else if (isCharType(type)) { 633 return characterClassType; 634 } else if (isNumericType(type) || isNumericWrapperType(type)) { 635 return longClassType; 636 } else { 637 return type; 638 } 639 } 640 } 641 | Popular Tags |