use of org.springframework.roo.addon.jpa.addon.identifier.Identifier in project spring-roo by spring-projects.
the class DbreDatabaseListenerImpl method getIdentifiers.
private List<Identifier> getIdentifiers(final Table table, final boolean usePrimaryKeys) {
final List<Identifier> result = new ArrayList<Identifier>();
// Add fields to the identifier class
final Set<Column> columns = usePrimaryKeys ? table.getPrimaryKeys() : table.getColumns();
for (final Column column : columns) {
final String columnName = column.getName();
JavaSymbolName fieldName;
try {
fieldName = new JavaSymbolName(DbreTypeUtils.suggestFieldName(columnName));
} catch (final RuntimeException e) {
throw new IllegalArgumentException("Failed to create field name for column '" + columnName + "' in table '" + table.getName() + "': " + e.getMessage());
}
final JavaType fieldType = column.getJavaType();
final String columnDefinition = table.isIncludeNonPortableAttributes() ? column.getTypeName() : "";
result.add(new Identifier(fieldName, fieldType, columnName, column.getColumnSize(), column.getScale(), columnDefinition));
}
return result;
}
use of org.springframework.roo.addon.jpa.addon.identifier.Identifier in project spring-roo by spring-projects.
the class DbreDatabaseListenerImpl method manageIdentifier.
private void manageIdentifier(final JavaType javaType, final AnnotationMetadataBuilder jpaAnnotationBuilder, final Set<JavaSymbolName> attributesToDeleteIfPresent, final Table table) {
final JavaType identifierType = getIdentifierType(javaType);
final PhysicalTypeMetadata identifierPhysicalTypeMetadata = getPhysicalTypeMetadata(identifierType);
// Process primary keys and add 'identifierType' attribute
final int pkCount = table.getPrimaryKeyCount();
if (pkCount == 1) {
// Check for redundant, managed identifier class and delete if found
if (isIdentifierDeletable(identifierType)) {
deleteJavaType(identifierType, "the " + table.getName() + " table has only one primary key");
}
attributesToDeleteIfPresent.add(new JavaSymbolName(IDENTIFIER_TYPE));
// We don't need a PK class
final List<Identifier> identifiers = getIdentifiersFromPrimaryKeys(table);
identifierResults.put(javaType, identifiers);
} else if (pkCount == 0 || pkCount > 1) {
// Check if identifier class already exists and if not, create it
if (identifierPhysicalTypeMetadata == null || !identifierPhysicalTypeMetadata.isValid() || identifierPhysicalTypeMetadata.getMemberHoldingTypeDetails() == null) {
createIdentifierClass(identifierType);
}
jpaAnnotationBuilder.addClassAttribute(IDENTIFIER_TYPE, identifierType);
// We need a PK class, so we tell the IdentifierMetadataProvider via
// IdentifierService the various column names, field types and field
// names to use
// For tables with no primary keys, create a composite key using all
// the table's columns
final List<Identifier> identifiers = pkCount == 0 ? getIdentifiersFromColumns(table) : getIdentifiersFromPrimaryKeys(table);
identifierResults.put(identifierType, identifiers);
}
}
use of org.springframework.roo.addon.jpa.addon.identifier.Identifier in project spring-roo by spring-projects.
the class JpaEntityMetadataProviderImpl method getIdentifier.
/**
* Returns the {@link Identifier} for the entity identified by the given
* metadata ID.
*
* @param metadataIdentificationString
* @return <code>null</code> if there isn't one
*/
private Identifier getIdentifier(final String metadataIdentificationString) {
final JavaType entity = getType(metadataIdentificationString);
final List<Identifier> identifiers = getIdentifiersForType(entity);
if (CollectionUtils.isEmpty(identifiers)) {
return null;
}
// We have potential identifier information from an IdentifierService.
// We only use this identifier information if the user did NOT provide
// ANY identifier-related attributes on @RooJpaEntity....
Validate.isTrue(identifiers.size() == 1, "Identifier service indicates %d fields illegally for the entity '%s' (should only be one identifier field given this is an entity, not an Identifier class)", identifiers.size(), entity.getSimpleTypeName());
return identifiers.iterator().next();
}
Aggregations