1 21 package oracle.toplink.essentials.internal.ejb.cmp3.metadata.accessors; 23 24 import java.util.ArrayList ; 25 import java.util.List ; 26 27 import java.lang.annotation.Annotation ; 28 import java.lang.Boolean ; 29 30 import java.lang.reflect.AnnotatedElement ; 31 import java.lang.reflect.Type ; 32 33 import javax.persistence.Column; 34 import javax.persistence.FetchType; 35 import javax.persistence.PrimaryKeyJoinColumn; 36 import javax.persistence.PrimaryKeyJoinColumns; 37 import javax.persistence.UniqueConstraint; 38 39 import oracle.toplink.essentials.mappings.DatabaseMapping; 40 41 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.accessors.ClassAccessor; 42 43 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.accessors.objects.MetadataAccessibleObject; 44 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.accessors.objects.MetadataClass; 45 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.accessors.objects.MetadataMethod; 46 47 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.columns.MetadataPrimaryKeyJoinColumn; 48 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.columns.MetadataPrimaryKeyJoinColumns; 49 50 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.MetadataHelper; 51 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.MetadataLogger; 52 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.MetadataProject; 53 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.MetadataValidator; 54 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.MetadataConstants; 55 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.MetadataProcessor; 56 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.MetadataDescriptor; 57 58 import oracle.toplink.essentials.internal.ejb.cmp3.xml.XMLHelper; 59 60 import oracle.toplink.essentials.internal.helper.DatabaseField; 61 import oracle.toplink.essentials.internal.helper.DatabaseTable; 62 63 69 public abstract class MetadataAccessor { 70 private String m_fetchType; 72 private boolean m_isOptional; 73 74 private boolean m_isProcessed; 75 private Boolean m_isRelationship; 76 77 protected MetadataLogger m_logger; 78 protected MetadataProject m_project; 79 protected MetadataProcessor m_processor; 80 protected MetadataValidator m_validator; 81 protected MetadataDescriptor m_descriptor; 82 protected MetadataAccessibleObject m_accessibleObject; 83 84 87 public MetadataAccessor(MetadataAccessibleObject accessibleObject, ClassAccessor classAccessor) { 88 this(accessibleObject, classAccessor.getProcessor(), classAccessor.getDescriptor()); 89 } 90 91 94 public MetadataAccessor(MetadataAccessibleObject accessibleObject, MetadataProcessor processor, MetadataDescriptor descriptor) { 95 m_isOptional = true; 96 m_isProcessed = false; 97 98 m_processor = processor; 99 m_descriptor = descriptor; 100 m_logger = processor.getLogger(); 101 m_project = processor.getProject(); 102 m_validator = processor.getValidator(); 103 104 m_fetchType = MetadataConstants.EAGER; 105 m_accessibleObject = accessibleObject; 106 } 107 108 112 public AnnotatedElement getAnnotatedElement() { 113 return m_accessibleObject.getAnnotatedElement(); 114 } 115 116 120 protected <T extends Annotation > T getAnnotation(Class annotation) { 121 return (T) getAnnotation(annotation, getAnnotatedElement()); 122 } 123 124 128 protected <T extends Annotation > T getAnnotation(Class annotation, AnnotatedElement annotatedElement) { 129 return (T) MetadataHelper.getAnnotation(annotation, annotatedElement, m_descriptor); 130 } 131 132 136 public String getAttributeName() { 137 return m_accessibleObject.getAttributeName(); 138 } 139 140 144 public MetadataDescriptor getDescriptor() { 145 return m_descriptor; 146 } 147 148 152 public String getFetchType() { 153 return m_fetchType; 154 } 155 156 160 public Class getJavaClass() { 161 return m_descriptor.getJavaClass(); 162 } 163 164 168 protected String getJavaClassName() { 169 return getJavaClass().getName(); 170 } 171 172 176 public MetadataLogger getLogger() { 177 return m_logger; 178 } 179 180 185 public String getName() { 186 return m_accessibleObject.getName(); 187 } 188 189 197 protected String getName(DatabaseField field, String defaultName, String context) { 198 return getName(field.getName(), defaultName, context); 199 } 200 201 212 protected String getName(String name, String defaultName, String context) { 213 if (name != null && !name.equals("")) { 215 return name; 216 } else if (defaultName == null || defaultName.equals("")) { 217 return ""; 218 } else { 219 m_logger.logConfigMessage(context, getAnnotatedElement(), defaultName); 221 return defaultName; 222 } 223 } 224 225 229 protected MetadataPrimaryKeyJoinColumns getPrimaryKeyJoinColumns(String sourceTableName, String targetTableName) { 230 PrimaryKeyJoinColumn primaryKeyJoinColumn = getAnnotation(PrimaryKeyJoinColumn.class); 231 PrimaryKeyJoinColumns primaryKeyJoinColumns = getAnnotation(PrimaryKeyJoinColumns.class); 232 233 return new MetadataPrimaryKeyJoinColumns(primaryKeyJoinColumns, primaryKeyJoinColumn, sourceTableName, targetTableName); 234 } 235 236 240 public MetadataProject getProject() { 241 return m_project; 242 } 243 244 248 public MetadataProcessor getProcessor() { 249 return m_processor; 250 } 251 252 258 public Class getRawClass() { 259 return m_accessibleObject.getRawClass(); 260 } 261 262 266 protected Class getReferenceClass() { 267 return m_accessibleObject.getReferenceClass(); 268 } 269 270 274 public String getReferenceClassName() { 275 return getReferenceClass().getName(); 276 } 277 278 282 public MetadataDescriptor getReferenceDescriptor() { 283 return m_project.getDescriptor(getReferenceClass()); 284 } 285 286 290 protected Type getRelationType() { 291 return m_accessibleObject.getRelationType(); 292 } 293 294 299 protected String getSetMethodName() { 300 return ((MetadataMethod) m_accessibleObject).getSetMethodName(); 301 } 302 303 308 protected String getUpperCaseAttributeName() { 309 return getAttributeName().toUpperCase(); 310 } 311 312 316 public MetadataValidator getValidator() { 317 return m_validator; 318 } 319 320 324 protected boolean hasColumn() { 325 return isAnnotationPresent(Column.class); 326 } 327 328 332 protected boolean hasPrimaryKeyJoinColumns() { 333 return isAnnotationPresent(PrimaryKeyJoinColumns.class); 334 } 335 336 342 protected boolean isAnnotationPresent(Class <? extends Annotation > annotation) { 343 return isAnnotationPresent(annotation, getAnnotatedElement()); 344 } 345 346 352 protected boolean isAnnotationPresent(Class <? extends Annotation > annotation, AnnotatedElement annotatedElement) { 353 return MetadataHelper.isAnnotationPresent(annotation, annotatedElement, m_descriptor); 354 } 355 356 360 public boolean isBasic() { 361 return false; 362 } 363 364 368 public boolean isClass() { 369 return false; 370 } 371 372 376 public boolean isEmbedded() { 377 return false; 378 } 379 380 384 public boolean isEmbeddedId() { 385 return false; 386 } 387 388 392 public boolean isManyToMany() { 393 return false; 394 } 395 396 400 public boolean isManyToOne() { 401 return false; 402 } 403 404 408 public boolean isOneToMany() { 409 return false; 410 } 411 412 416 public boolean isOneToOne() { 417 return false; 418 } 419 420 423 public boolean isOptional() { 424 return m_isOptional; 425 } 426 427 432 public boolean isRelationship() { 433 if (m_isRelationship == null) { 434 m_isRelationship = new Boolean (isManyToOne() || isManyToMany() || isOneToMany() || isOneToOne()); 435 } 436 437 return m_isRelationship.booleanValue(); 438 } 439 440 444 public boolean isXMLAccessor() { 445 return false; 446 } 447 448 452 public boolean isProcessed() { 453 return m_isProcessed; 454 } 455 456 461 public abstract void process(); 462 463 468 protected ClassAccessor processAccessor(MetadataDescriptor descriptor) { 469 ClassAccessor accessor = new ClassAccessor(new MetadataClass(descriptor.getJavaClass()), getProcessor(), descriptor); 470 descriptor.setClassAccessor(accessor); 471 accessor.process(); 472 return accessor; 473 } 474 475 479 protected List <MetadataPrimaryKeyJoinColumn> processPrimaryKeyJoinColumns(MetadataPrimaryKeyJoinColumns primaryKeyJoinColumns) { 480 if (m_descriptor.hasCompositePrimaryKey()) { 481 if (primaryKeyJoinColumns.size() != m_descriptor.getPrimaryKeyFields().size()) { 483 m_validator.throwIncompletePrimaryKeyJoinColumnsSpecified(getJavaClass(), getAnnotatedElement()); 484 } 485 486 for (MetadataPrimaryKeyJoinColumn primaryKeyJoinColumn : primaryKeyJoinColumns.values()) { 488 if (primaryKeyJoinColumn.isPrimaryKeyFieldNotSpecified() || primaryKeyJoinColumn.isForeignKeyFieldNotSpecified()) { 489 m_validator.throwIncompletePrimaryKeyJoinColumnsSpecified(getJavaClass(), getAnnotatedElement()); 490 } 491 } 492 } else { 493 if (primaryKeyJoinColumns.size() > 1) { 494 m_validator.throwExcessivePrimaryKeyJoinColumnsSpecified(getJavaClass(), getAnnotatedElement()); 495 } 496 } 497 498 return primaryKeyJoinColumns.values(); 499 } 500 501 505 protected void setAccessorMethods(DatabaseMapping mapping) { 506 if (m_descriptor.usesPropertyAccess()) { 507 mapping.setGetMethodName(getName()); 508 mapping.setSetMethodName(getSetMethodName()); 509 } 510 } 511 512 516 public void setAnnotatedElement(AnnotatedElement annotatedElement) { 517 m_accessibleObject.setAnnotatedElement(annotatedElement); 518 } 519 520 523 public void setFetchType(FetchType fetchType) { 524 m_fetchType = fetchType.name(); 525 } 526 527 530 public void setFetchType(String fetchType) { 531 m_fetchType = fetchType; 532 } 533 534 537 public void setIsOptional(boolean isOptional) { 538 m_isOptional = isOptional; 539 } 540 541 544 public void setIsProcessed() { 545 m_isProcessed = true; 546 } 547 548 551 public boolean usesIndirection() { 552 return m_fetchType.equals(MetadataConstants.LAZY); 553 } 554 } 555 | Popular Tags |