private Table updateOrDeleteManagedEntity(
final ClassOrInterfaceTypeDetails managedEntity,
final Database database) {
// Update the attributes of the existing JPA-related annotation
final AnnotationMetadata jpaAnnotation = getJpaAnnotation(managedEntity);
Validate.validState(jpaAnnotation != null,
"Neither @%s nor @%s found on existing DBRE-managed entity %s",
ROO_JPA_ACTIVE_RECORD.getSimpleTypeName(), ROO_JPA_ENTITY
.getSimpleTypeName(), managedEntity.getName()
.getFullyQualifiedTypeName());
// Find table in database using 'table' and 'schema' attributes from the
// JPA annotation
final AnnotationAttributeValue<?> tableAttribute = jpaAnnotation
.getAttribute(new JavaSymbolName("table"));
final String errMsg = "Unable to maintain database-managed entity "
+ managedEntity.getName().getFullyQualifiedTypeName()
+ " because its associated table could not be found";
Validate.notNull(tableAttribute, errMsg);
final String tableName = (String) tableAttribute.getValue();
Validate.notBlank(tableName, errMsg);
final AnnotationAttributeValue<?> schemaAttribute = jpaAnnotation
.getAttribute(new JavaSymbolName("schema"));
final String schemaName = schemaAttribute != null ? (String) schemaAttribute
.getValue() : null;
final Table table = database.getTable(tableName, schemaName);
if (table == null) {
// Table is missing and probably has been dropped so delete managed
// type and its identifier if applicable
deleteManagedType(managedEntity, "no database table called '"
+ tableName + "'");
return null;
}
table.setIncludeNonPortableAttributes(database
.isIncludeNonPortableAttributes());
table.setDisableVersionFields(database.isDisableVersionFields());
table.setDisableGeneratedIdentifiers(database
.isDisableGeneratedIdentifiers());
// Update the @RooJpaEntity/@RooJpaActiveRecord attributes
final AnnotationMetadataBuilder jpaAnnotationBuilder = new AnnotationMetadataBuilder(
jpaAnnotation);
final Set<JavaSymbolName> attributesToDeleteIfPresent = new LinkedHashSet<JavaSymbolName>();
manageIdentifier(managedEntity.getName(), jpaAnnotationBuilder,
attributesToDeleteIfPresent, table);
// Manage versionField attribute
final AnnotationAttributeValue<?> versionFieldAttribute = jpaAnnotation
.getAttribute(new JavaSymbolName(VERSION_FIELD));
if (versionFieldAttribute == null) {
if (hasVersionField(table)) {
attributesToDeleteIfPresent.add(new JavaSymbolName(
VERSION_FIELD));
}
else {
jpaAnnotationBuilder.addStringAttribute(VERSION_FIELD, "");
}
}
else {
final String versionFieldValue = (String) versionFieldAttribute
.getValue();
if (hasVersionField(table)
&& (StringUtils.isBlank(versionFieldValue) || VERSION
.equals(versionFieldValue))) {
attributesToDeleteIfPresent.add(new JavaSymbolName(
VERSION_FIELD));
}
}
final AnnotationAttributeValue<?> sequenceNameFieldAttribute = jpaAnnotation
.getAttribute(new JavaSymbolName(SEQUENCE_NAME_FIELD));
if (sequenceNameFieldAttribute == null) {
if (!table.isDisableGeneratedIdentifiers()) {
attributesToDeleteIfPresent.add(new JavaSymbolName(
SEQUENCE_NAME_FIELD));