Search in sources :

Example 1 with Identifier

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;
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) JdkJavaType(org.springframework.roo.model.JdkJavaType) JavaType(org.springframework.roo.model.JavaType) Identifier(org.springframework.roo.addon.jpa.addon.identifier.Identifier) PhysicalTypeIdentifier(org.springframework.roo.classpath.PhysicalTypeIdentifier) Column(org.springframework.roo.addon.dbre.addon.model.Column) ArrayList(java.util.ArrayList)

Example 2 with Identifier

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);
    }
}
Also used : JdkJavaType(org.springframework.roo.model.JdkJavaType) JavaType(org.springframework.roo.model.JavaType) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) Identifier(org.springframework.roo.addon.jpa.addon.identifier.Identifier) PhysicalTypeIdentifier(org.springframework.roo.classpath.PhysicalTypeIdentifier) PhysicalTypeMetadata(org.springframework.roo.classpath.PhysicalTypeMetadata) List(java.util.List) ArrayList(java.util.ArrayList)

Example 3 with Identifier

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();
}
Also used : RooJavaType(org.springframework.roo.model.RooJavaType) JpaJavaType(org.springframework.roo.model.JpaJavaType) JavaType(org.springframework.roo.model.JavaType) Identifier(org.springframework.roo.addon.jpa.addon.identifier.Identifier) PhysicalTypeIdentifier(org.springframework.roo.classpath.PhysicalTypeIdentifier)

Aggregations

Identifier (org.springframework.roo.addon.jpa.addon.identifier.Identifier)3 PhysicalTypeIdentifier (org.springframework.roo.classpath.PhysicalTypeIdentifier)3 JavaType (org.springframework.roo.model.JavaType)3 ArrayList (java.util.ArrayList)2 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)2 JdkJavaType (org.springframework.roo.model.JdkJavaType)2 List (java.util.List)1 Column (org.springframework.roo.addon.dbre.addon.model.Column)1 PhysicalTypeMetadata (org.springframework.roo.classpath.PhysicalTypeMetadata)1 JpaJavaType (org.springframework.roo.model.JpaJavaType)1 RooJavaType (org.springframework.roo.model.RooJavaType)1