Search in sources :

Example 56 with FieldMetadataBuilder

use of org.springframework.roo.classpath.details.FieldMetadataBuilder in project spring-roo by spring-projects.

the class DbreMetadata method addOneToOneFields.

private void addOneToOneFields(final Table table) {
    // Add unique one-to-one fields
    final Map<JavaSymbolName, FieldMetadataBuilder> uniqueFields = new LinkedHashMap<JavaSymbolName, FieldMetadataBuilder>();
    for (final ForeignKey foreignKey : table.getImportedKeys()) {
        if (!isOneToOne(table, foreignKey)) {
            continue;
        }
        final Table importedKeyForeignTable = foreignKey.getForeignTable();
        Validate.notNull(importedKeyForeignTable, "Foreign key table for foreign key '%s' in table '%s' does not exist. One-to-one relationship not created", foreignKey.getName(), table.getFullyQualifiedTableName());
        final String foreignTableName = importedKeyForeignTable.getName();
        final String foreignSchemaName = importedKeyForeignTable.getSchema().getName();
        final Short keySequence = foreignKey.getKeySequence();
        final String fieldSuffix = keySequence != null && keySequence > 0 ? String.valueOf(keySequence) : "";
        final JavaSymbolName fieldName = new JavaSymbolName(DbreTypeUtils.suggestFieldName(foreignTableName) + fieldSuffix);
        final JavaType fieldType = DbreTypeUtils.findTypeForTableName(managedEntities, foreignTableName, foreignSchemaName);
        Validate.notNull(fieldType, "Attempted to create one-to-one field '%s' in '%s' %s", fieldName, destination.getFullyQualifiedTypeName(), getErrorMsg(importedKeyForeignTable.getFullyQualifiedTableName(), table.getFullyQualifiedTableName()));
        // Fields are stored in a field-keyed map first before adding them
        // to the builder.
        // This ensures the fields from foreign keys with multiple columns
        // will only get created once.
        final FieldMetadataBuilder fieldBuilder = getOneToOneOrManyToOneField(fieldName, fieldType, foreignKey, ONE_TO_ONE, false);
        uniqueFields.put(fieldName, fieldBuilder);
    }
    for (final FieldMetadataBuilder fieldBuilder : uniqueFields.values()) {
        addToBuilder(fieldBuilder);
        // Exclude these fields in @RooToString to avoid circular references
        // - ROO-1399
        excludeFieldsInToStringAnnotation(fieldBuilder.getFieldName().getSymbolName());
    }
    // Add one-to-one mapped-by fields
    if (table.isJoinTable()) {
        return;
    }
    for (final ForeignKey exportedKey : table.getExportedKeys()) {
        final Table exportedKeyForeignTable = exportedKey.getForeignTable();
        Validate.notNull(exportedKeyForeignTable, "Foreign key table for foreign key '%s' in table '%s' does not exist. One-to-one relationship not created", exportedKey.getName(), table.getFullyQualifiedTableName());
        if (exportedKeyForeignTable.isJoinTable()) {
            continue;
        }
        final String foreignTableName = exportedKeyForeignTable.getName();
        final String foreignSchemaName = exportedKeyForeignTable.getSchema().getName();
        final Table foreignTable = database.getTable(foreignTableName, foreignSchemaName);
        Validate.notNull(foreignTable, "Related table '%s' could not be found but has a foreign-key reference to table '%s'", exportedKeyForeignTable.getFullyQualifiedTableName(), table.getFullyQualifiedTableName());
        if (!isOneToOne(foreignTable, foreignTable.getImportedKey(exportedKey.getName()))) {
            continue;
        }
        final Short keySequence = exportedKey.getKeySequence();
        final String fieldSuffix = keySequence != null && keySequence > 0 ? String.valueOf(keySequence) : "";
        JavaSymbolName fieldName = new JavaSymbolName(DbreTypeUtils.suggestFieldName(foreignTableName) + fieldSuffix);
        final JavaType fieldType = DbreTypeUtils.findTypeForTableName(managedEntities, foreignTableName, foreignSchemaName);
        Validate.notNull(fieldType, "Attempted to create one-to-one mapped-by field '%s' in '%s' %s", fieldName, destination.getFullyQualifiedTypeName(), getErrorMsg(foreignTable.getFullyQualifiedTableName()));
        // Check for existence of same field - ROO-1691
        while (true) {
            if (!hasFieldInItd(fieldName)) {
                break;
            }
            fieldName = new JavaSymbolName(fieldName.getSymbolName() + "_");
        }
        final JavaSymbolName mappedByFieldName = new JavaSymbolName(DbreTypeUtils.suggestFieldName(table.getName()) + fieldSuffix);
        final FieldMetadataBuilder fieldBuilder = getOneToOneMappedByField(fieldName, fieldType, mappedByFieldName, exportedKey.getOnUpdate(), exportedKey.getOnDelete());
        addToBuilder(fieldBuilder);
        // Exclude these fields in @RooToString to avoid circular references
        // - ROO-1399
        excludeFieldsInToStringAnnotation(fieldBuilder.getFieldName().getSymbolName());
    }
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) JdkJavaType(org.springframework.roo.model.JdkJavaType) JavaType(org.springframework.roo.model.JavaType) Table(org.springframework.roo.addon.dbre.addon.model.Table) ForeignKey(org.springframework.roo.addon.dbre.addon.model.ForeignKey) FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder) LinkedHashMap(java.util.LinkedHashMap)

Example 57 with FieldMetadataBuilder

use of org.springframework.roo.classpath.details.FieldMetadataBuilder in project spring-roo by spring-projects.

the class DbreMetadata method getManyToManyInverseSideField.

private FieldMetadataBuilder getManyToManyInverseSideField(final JavaSymbolName fieldName, final JavaSymbolName mappedByFieldName, final Table owningSideTable, final CascadeAction onUpdate, final CascadeAction onDelete) {
    final JavaType element = DbreTypeUtils.findTypeForTable(managedEntities, owningSideTable);
    Validate.notNull(element, "Attempted to create many-to-many inverse-side field '%s' in '%s' %s", fieldName, destination.getFullyQualifiedTypeName(), getErrorMsg(owningSideTable.getFullyQualifiedTableName()));
    final List<JavaType> params = Arrays.asList(element);
    final JavaType fieldType = new JavaType(SET.getFullyQualifiedTypeName(), 0, DataType.TYPE, null, params);
    // Add annotations to field
    final List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
    final AnnotationMetadataBuilder manyToManyBuilder = new AnnotationMetadataBuilder(MANY_TO_MANY);
    manyToManyBuilder.addStringAttribute(MAPPED_BY, mappedByFieldName.getSymbolName());
    addCascadeType(manyToManyBuilder, onUpdate, onDelete);
    annotations.add(manyToManyBuilder);
    return new FieldMetadataBuilder(getId(), Modifier.PRIVATE, annotations, fieldName, fieldType);
}
Also used : JdkJavaType(org.springframework.roo.model.JdkJavaType) JavaType(org.springframework.roo.model.JavaType) ArrayList(java.util.ArrayList) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder) FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder)

Example 58 with FieldMetadataBuilder

use of org.springframework.roo.classpath.details.FieldMetadataBuilder in project spring-roo by spring-projects.

the class DbreMetadata method addOtherFields.

private void addOtherFields(final Table table) {
    final Map<JavaSymbolName, FieldMetadataBuilder> uniqueFields = new LinkedHashMap<JavaSymbolName, FieldMetadataBuilder>();
    for (final Column column : table.getColumns()) {
        final String columnName = column.getName();
        JavaSymbolName fieldName = new JavaSymbolName(DbreTypeUtils.suggestFieldName(columnName));
        final boolean isIdField = isIdField(fieldName) || column.isPrimaryKey();
        final boolean isVersionField = isVersionField(fieldName) || (columnName.equals("version") && !database.isDisableVersionFields());
        final boolean isCompositeKeyField = isCompositeKeyField(fieldName);
        final boolean isForeignKey = table.findImportedKeyByLocalColumnName(columnName) != null;
        if (isIdField || isVersionField || isCompositeKeyField || isForeignKey) {
            continue;
        }
        final boolean hasEmbeddedIdField = isEmbeddedIdField(fieldName) && !isCompositeKeyField;
        if (hasEmbeddedIdField) {
            fieldName = governorTypeDetails.getUniqueFieldName(fieldName.getSymbolName());
        }
        final FieldMetadataBuilder fieldBuilder = getField(fieldName, column, table.getName(), table.isIncludeNonPortableAttributes());
        if (fieldBuilder.getFieldType().equals(DATE) && fieldName.getSymbolName().equals(CREATED)) {
            fieldBuilder.setFieldInitializer("new Date()");
        }
        uniqueFields.put(fieldName, fieldBuilder);
    }
    for (final FieldMetadataBuilder fieldBuilder : uniqueFields.values()) {
        addToBuilder(fieldBuilder);
    }
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) Column(org.springframework.roo.addon.dbre.addon.model.Column) FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder) LinkedHashMap(java.util.LinkedHashMap)

Example 59 with FieldMetadataBuilder

use of org.springframework.roo.classpath.details.FieldMetadataBuilder in project spring-roo by spring-projects.

the class DbreMetadata method addManyToManyFields.

private void addManyToManyFields(final Table table) {
    final Map<Table, Integer> owningSideTables = new LinkedHashMap<Table, Integer>();
    final Map<JavaSymbolName, FieldMetadataBuilder> uniqueOwningSideFields = new LinkedHashMap<JavaSymbolName, FieldMetadataBuilder>();
    final Map<JavaSymbolName, FieldMetadataBuilder> uniqueInverseSideFields = new LinkedHashMap<JavaSymbolName, FieldMetadataBuilder>();
    for (final Table joinTable : database.getTables()) {
        if (!joinTable.isJoinTable()) {
            continue;
        }
        final String errMsg = "table in join table '" + joinTable.getName() + "' for many-to-many relationship could not be found. Note that table names are case sensitive in some databases such as MySQL.";
        final Iterator<ForeignKey> iter = joinTable.getImportedKeys().iterator();
        // First foreign key in set
        final ForeignKey foreignKey1 = iter.next();
        // Second and last foreign key in set
        final ForeignKey foreignKey2 = iter.next();
        final Table owningSideTable = foreignKey1.getForeignTable();
        Validate.notNull(owningSideTable, "Owning-side %s", errMsg);
        final Table inverseSideTable = foreignKey2.getForeignTable();
        Validate.notNull(inverseSideTable, "Inverse-side %s", errMsg);
        final Integer tableCount = owningSideTables.containsKey(owningSideTable) ? owningSideTables.get(owningSideTable) + 1 : 0;
        owningSideTables.put(owningSideTable, tableCount);
        final String fieldSuffix = owningSideTables.get(owningSideTable) > 0 ? String.valueOf(owningSideTables.get(owningSideTable)) : "";
        final boolean sameTable = owningSideTable.equals(inverseSideTable);
        if (owningSideTable.equals(table)) {
            final JavaSymbolName fieldName = new JavaSymbolName(getInflectorPlural(DbreTypeUtils.suggestFieldName(inverseSideTable)) + (sameTable ? "1" : fieldSuffix));
            final FieldMetadataBuilder fieldBuilder = getManyToManyOwningSideField(fieldName, joinTable, inverseSideTable, foreignKey1.getOnUpdate(), foreignKey1.getOnDelete());
            uniqueOwningSideFields.put(fieldName, fieldBuilder);
        }
        if (inverseSideTable.equals(table)) {
            final JavaSymbolName fieldName = new JavaSymbolName(getInflectorPlural(DbreTypeUtils.suggestFieldName(owningSideTable)) + (sameTable ? "2" : fieldSuffix));
            final JavaSymbolName mappedByFieldName = new JavaSymbolName(getInflectorPlural(DbreTypeUtils.suggestFieldName(inverseSideTable)) + (sameTable ? "1" : fieldSuffix));
            final FieldMetadataBuilder fieldBuilder = getManyToManyInverseSideField(fieldName, mappedByFieldName, owningSideTable, foreignKey2.getOnUpdate(), foreignKey2.getOnDelete());
            uniqueInverseSideFields.put(fieldName, fieldBuilder);
        }
    }
    // Add unique owning-side many-to-one fields
    for (final FieldMetadataBuilder fieldBuilder : uniqueOwningSideFields.values()) {
        addToBuilder(fieldBuilder);
        // Exclude these fields in @RooToString to avoid circular references
        // - ROO-1399
        excludeFieldsInToStringAnnotation(fieldBuilder.getFieldName().getSymbolName());
    }
    // Add unique inverse-side many-to-one fields
    for (final FieldMetadataBuilder fieldBuilder : uniqueInverseSideFields.values()) {
        addToBuilder(fieldBuilder);
        // Exclude these fields in @RooToString to avoid circular references
        // - ROO-1399
        excludeFieldsInToStringAnnotation(fieldBuilder.getFieldName().getSymbolName());
    }
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) Table(org.springframework.roo.addon.dbre.addon.model.Table) ForeignKey(org.springframework.roo.addon.dbre.addon.model.ForeignKey) LinkedHashMap(java.util.LinkedHashMap) FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder)

Example 60 with FieldMetadataBuilder

use of org.springframework.roo.classpath.details.FieldMetadataBuilder in project spring-roo by spring-projects.

the class RepositoryJpaLayerProvider method getMethodAdditions.

/**
 * Returns the additions that the caller needs to make in order to invoke
 * the given method
 *
 * @param callerMID the caller's metadata ID (required)
 * @param method the method being called (required)
 * @param repositoryType the type of repository being called
 * @param parameterNames the parameter names used by the caller
 * @return a non-<code>null</code> set of additions
 */
private MemberTypeAdditions getMethodAdditions(final String callerMID, final RepositoryJpaLayerMethod method, final JavaType repositoryType, final List<MethodParameter> parameters) {
    // Create a builder to hold the repository field to be copied into the
    // caller
    final ClassOrInterfaceTypeDetailsBuilder cidBuilder = new ClassOrInterfaceTypeDetailsBuilder(callerMID);
    final AnnotationMetadataBuilder autowiredAnnotation = new AnnotationMetadataBuilder(AUTOWIRED);
    final String repositoryFieldName = StringUtils.uncapitalize(repositoryType.getSimpleTypeName());
    cidBuilder.addField(new FieldMetadataBuilder(callerMID, 0, Arrays.asList(autowiredAnnotation), new JavaSymbolName(repositoryFieldName), repositoryType));
    // Create the additions to invoke the given method on this field
    final String methodCall = repositoryFieldName + "." + method.getCall(parameters);
    return new MemberTypeAdditions(cidBuilder, method.getName(), methodCall, false, parameters);
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) ClassOrInterfaceTypeDetailsBuilder(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder) MemberTypeAdditions(org.springframework.roo.classpath.layers.MemberTypeAdditions) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder) FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder)

Aggregations

FieldMetadataBuilder (org.springframework.roo.classpath.details.FieldMetadataBuilder)66 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)48 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)40 ArrayList (java.util.ArrayList)21 JavaType (org.springframework.roo.model.JavaType)19 JdkJavaType (org.springframework.roo.model.JdkJavaType)10 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)8 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)7 FieldDetails (org.springframework.roo.classpath.details.FieldDetails)7 ForeignKey (org.springframework.roo.addon.dbre.addon.model.ForeignKey)5 CommentStructure (org.springframework.roo.classpath.details.comments.CommentStructure)5 JavadocComment (org.springframework.roo.classpath.details.comments.JavadocComment)5 LinkedHashMap (java.util.LinkedHashMap)4 Reference (org.springframework.roo.addon.dbre.addon.model.Reference)4 Table (org.springframework.roo.addon.dbre.addon.model.Table)4 ClassOrInterfaceTypeDetailsBuilder (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder)4 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)4 CollectionField (org.springframework.roo.classpath.operations.jsr303.CollectionField)4 DateField (org.springframework.roo.classpath.operations.jsr303.DateField)4 EnumDetails (org.springframework.roo.model.EnumDetails)4