1 21 package oracle.toplink.essentials.internal.ejb.cmp3.metadata.accessors; 23 24 import java.io.Serializable ; 25 26 import javax.persistence.Column; 27 import javax.persistence.Enumerated; 28 import javax.persistence.EnumType; 29 import javax.persistence.GeneratedValue; 30 import javax.persistence.Id; 31 import javax.persistence.Lob; 32 import javax.persistence.Temporal; 33 import javax.persistence.Version; 34 35 import oracle.toplink.essentials.descriptors.TimestampLockingPolicy; 36 import oracle.toplink.essentials.descriptors.VersionLockingPolicy; 37 38 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.accessors.ClassAccessor; 39 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.accessors.objects.MetadataAccessibleObject; 40 41 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.columns.MetadataColumn; 42 43 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.MetadataConstants; 44 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.MetadataHelper; 45 46 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.sequencing.MetadataGeneratedValue; 47 48 import oracle.toplink.essentials.internal.helper.Helper; 49 import oracle.toplink.essentials.internal.helper.DatabaseField; 50 51 import oracle.toplink.essentials.mappings.converters.EnumTypeConverter; 52 import oracle.toplink.essentials.mappings.converters.SerializedObjectConverter; 53 import oracle.toplink.essentials.mappings.converters.TypeConversionConverter; 54 55 import oracle.toplink.essentials.mappings.DirectToFieldMapping; 56 57 import oracle.toplink.essentials.internal.helper.DatabaseField; 58 59 65 public class BasicAccessor extends NonRelationshipAccessor { 66 69 public BasicAccessor(MetadataAccessibleObject accessibleObject, ClassAccessor classAccessor) { 70 super(accessibleObject, classAccessor); 71 } 72 73 77 protected MetadataColumn getColumn() { 78 Column column = getAnnotation(Column.class); 79 return new MetadataColumn(column, this); 80 } 81 82 91 public DatabaseField getDatabaseField() { 92 MetadataColumn column; 95 96 if (m_descriptor.hasAttributeOverrideFor(getAttributeName())) { 97 column = m_descriptor.getAttributeOverrideFor(getAttributeName()); 98 } else { 99 column = getColumn(); 100 } 101 102 DatabaseField field = column.getDatabaseField(); 104 105 field.setName(getName(field.getName(), column.getUpperCaseAttributeName(), m_logger.COLUMN)); 107 108 if (field.getTableName().equals("")) { 110 field.setTableName(m_descriptor.getPrimaryTableName()); 111 } 112 113 return field; 114 } 115 116 119 public String getEnumeratedType() { 120 Enumerated enumerated = getAnnotation(Enumerated.class); 121 122 if (enumerated == null) { 123 return EnumType.ORDINAL.name(); 124 } else { 125 return enumerated.value().name(); 126 } 127 } 128 129 133 public String getTemporalType() { 134 Temporal temporal = getAnnotation(Temporal.class); 135 return temporal.value().name(); 136 } 137 138 142 public boolean hasEnumerated() { 143 return isAnnotationPresent(Enumerated.class); 144 } 145 146 150 public boolean hasTemporal() { 151 return isAnnotationPresent(Temporal.class); 152 } 153 154 157 public boolean isBasic() { 158 return true; 159 } 160 161 167 public boolean isEnumerated() { 168 return hasEnumerated() || MetadataHelper.isValidEnumeratedType(getReferenceClass()); 169 } 170 171 175 public boolean isId() { 176 return isAnnotationPresent(Id.class); 177 } 178 179 183 public boolean isLob() { 184 return isAnnotationPresent(Lob.class); 185 } 186 187 191 public boolean isSerialized() { 192 return MetadataHelper.isValidSerializedType(getReferenceClass()); 193 } 194 195 199 public boolean isTemporal() { 200 return hasTemporal() || MetadataHelper.isValidTemporalType(getReferenceClass()); 201 } 202 203 207 public boolean isVersion() { 208 return isAnnotationPresent(Version.class); 209 } 210 211 215 public void process() { 216 DatabaseField field = getDatabaseField(); 218 219 if (isVersion()) { 221 if (m_descriptor.usesOptimisticLocking()) { 222 m_logger.logWarningMessage(m_logger.IGNORE_VERSION_LOCKING, this); 224 } else { 225 processVersion(field); 226 } 227 } else if (isId()) { 228 processId(field); 230 } 231 232 if (m_descriptor.hasMappingForAttributeName(getAttributeName())) { 233 m_logger.logWarningMessage(m_logger.IGNORE_MAPPING, this); 235 } else { 236 processDirectToFieldMapping(field); 240 } 241 } 242 243 248 protected void processDirectToFieldMapping(DatabaseField field) { 249 DirectToFieldMapping mapping = new DirectToFieldMapping(); 250 mapping.setField(field); 251 mapping.setIsReadOnly(field.isReadOnly()); 252 mapping.setAttributeName(getAttributeName()); 253 mapping.setIsOptional(isOptional()); 254 255 if (usesIndirection()) { 256 m_logger.logWarningMessage(m_logger.IGNORE_BASIC_FETCH_LAZY, this); 257 } 258 259 setAccessorMethods(mapping); 261 262 if (isEnumerated()) { 265 processEnumerated(mapping); 266 } else if (isLob()) { 267 processLob(mapping); 268 } else if (isTemporal()) { 269 processTemporal(mapping); 270 } else if (isSerialized()) { 271 processSerialized(mapping); 272 } 273 274 m_descriptor.addMapping(mapping); 276 } 277 278 284 protected void processEnumerated(DirectToFieldMapping mapping) { 285 if (hasEnumerated()) { 288 if (! MetadataHelper.isValidEnumeratedType(getReferenceClass())) { 289 m_validator.throwInvalidTypeForEnumeratedAttribute(getJavaClass(), mapping.getAttributeName(), getReferenceClass()); 290 } 291 } 292 293 mapping.setConverter(new EnumTypeConverter(mapping, getReferenceClassName(), getEnumeratedType().equals(EnumType.ORDINAL.name()))); 295 } 296 297 301 protected void processGeneratedValue(DatabaseField field) { 302 GeneratedValue generatedValue = getAnnotation(GeneratedValue.class); 303 304 if (generatedValue != null) { 305 processGeneratedValue(new MetadataGeneratedValue(generatedValue), field); 306 } 307 } 308 309 312 protected void processGeneratedValue(MetadataGeneratedValue generatedValue, DatabaseField sequenceNumberField) { 313 DatabaseField existingSequenceNumberField = m_descriptor.getSequenceNumberField(); 315 316 if (existingSequenceNumberField == null) { 317 m_descriptor.setSequenceNumberField(sequenceNumberField); 318 getProject().addGeneratedValue(generatedValue, getJavaClass()); 319 } else { 320 m_validator.throwOnlyOneGeneratedValueIsAllowed(getJavaClass(), existingSequenceNumberField.getQualifiedName(), sequenceNumberField.getQualifiedName()); 321 } 322 } 323 324 328 protected void processId(DatabaseField field) { 329 if (m_descriptor.ignoreIDs()) { 330 m_logger.logWarningMessage(m_logger.IGNORE_PRIMARY_KEY, this); 332 } else { 333 String attributeName = getAttributeName(); 334 335 if (m_descriptor.hasEmbeddedIdAttribute()) { 336 m_validator.throwEmbeddedIdAndIdFound(getJavaClass(), m_descriptor.getEmbeddedIdAttributeName(), attributeName); 338 } 339 340 m_descriptor.validatePKClassId(attributeName, getReferenceClass()); 342 343 m_descriptor.addIdAttributeName(attributeName); 345 346 m_descriptor.addPrimaryKeyField(field); 348 349 processGeneratedValue(field); 351 352 processTableGenerator(); 354 355 processSequenceGenerator(); 357 } 358 } 359 360 365 protected void processLob(DirectToFieldMapping mapping) { 366 if (MetadataHelper.isValidClobType(getReferenceClass())) { 369 mapping.setFieldClassification(java.sql.Clob .class); 370 } else if (MetadataHelper.isValidBlobType(getReferenceClass())) { 371 mapping.setFieldClassification(java.sql.Blob .class); 372 } else { 373 m_validator.throwInvalidTypeForLOBAttribute(getJavaClass(), mapping.getAttributeName(), getReferenceClass()); 375 } 376 377 mapping.setConverter(new TypeConversionConverter(mapping)); 379 } 380 381 387 protected void processSerialized(DirectToFieldMapping mapping) { 388 if (Helper.classImplementsInterface(getReferenceClass(), Serializable .class)) { 389 mapping.setConverter(new SerializedObjectConverter(mapping)); 390 } else { 391 m_validator.throwInvalidTypeForSerializedAttribute(getJavaClass(), mapping.getAttributeName(), getReferenceClass()); 392 } 393 } 394 395 399 protected void processTemporal(DirectToFieldMapping mapping) { 400 if (hasTemporal()) { 401 if (MetadataHelper.isValidTemporalType(getReferenceClass())) { 402 mapping.setConverter(new TypeConversionConverter(mapping)); 404 mapping.setFieldClassification(MetadataHelper.getFieldClassification(getTemporalType())); 405 } else { 406 m_validator.throwInvalidTypeForTemporalAttribute(getJavaClass(), getAttributeName(), getReferenceClass()); 407 } 408 } else { 409 m_validator.throwNoTemporalTypeSpecified(getJavaClass(), getAttributeName()); 410 } 411 } 412 413 416 protected void processVersion(DatabaseField field) { 417 Class lockType = getRawClass(); 418 field.setType(lockType); 419 420 if (MetadataHelper.isValidVersionLockingType(lockType)) { 421 m_descriptor.setOptimisticLockingPolicy(new VersionLockingPolicy(field)); 422 } else if (MetadataHelper.isValidTimstampVersionLockingType(lockType)) { 423 m_descriptor.setOptimisticLockingPolicy(new TimestampLockingPolicy(field)); 424 } else { 425 m_validator.throwInvalidTypeForVersionAttribute(getJavaClass(), getAttributeName(), lockType); 426 } 427 } 428 } 429 | Popular Tags |