Search in sources :

Example 1 with Reference

use of org.springframework.roo.addon.dbre.addon.model.Reference in project spring-roo by spring-projects.

the class DbreMetadata method addOneToManyFields.

private void addOneToManyFields(final Table table) {
    Validate.notNull(table, "Table required");
    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-many 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 was referenced by 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(getInflectorPlural(DbreTypeUtils.suggestFieldName(foreignTableName)) + fieldSuffix);
        JavaSymbolName mappedByFieldName = null;
        if (exportedKey.getReferenceCount() == 1) {
            final Reference reference = exportedKey.getReferences().iterator().next();
            mappedByFieldName = new JavaSymbolName(DbreTypeUtils.suggestFieldName(reference.getForeignColumnName()));
        } else {
            mappedByFieldName = new JavaSymbolName(DbreTypeUtils.suggestFieldName(table) + fieldSuffix);
        }
        // Check for existence of same field - ROO-1691
        while (true) {
            if (!hasFieldInItd(fieldName)) {
                break;
            }
            fieldName = new JavaSymbolName(fieldName.getSymbolName() + "_");
        }
        final FieldMetadataBuilder fieldBuilder = getOneToManyMappedByField(fieldName, mappedByFieldName, foreignTableName, foreignSchemaName, 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) Table(org.springframework.roo.addon.dbre.addon.model.Table) Reference(org.springframework.roo.addon.dbre.addon.model.Reference) ForeignKey(org.springframework.roo.addon.dbre.addon.model.ForeignKey) FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder)

Example 2 with Reference

use of org.springframework.roo.addon.dbre.addon.model.Reference in project spring-roo by spring-projects.

the class DbreMetadata method getManyToManyOwningSideField.

private FieldMetadataBuilder getManyToManyOwningSideField(final JavaSymbolName fieldName, final Table joinTable, final Table inverseSideTable, final CascadeAction onUpdate, final CascadeAction onDelete) {
    final JavaType element = DbreTypeUtils.findTypeForTable(managedEntities, inverseSideTable);
    Validate.notNull(element, "Attempted to create many-to-many owning-side field '%s' in '%s' %s", fieldName, destination.getFullyQualifiedTypeName(), getErrorMsg(inverseSideTable.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>();
    // Add @ManyToMany annotation
    final AnnotationMetadataBuilder manyToManyBuilder = new AnnotationMetadataBuilder(MANY_TO_MANY);
    annotations.add(manyToManyBuilder);
    // Add @JoinTable annotation
    final AnnotationMetadataBuilder joinTableBuilder = new AnnotationMetadataBuilder(JOIN_TABLE);
    final List<AnnotationAttributeValue<?>> joinTableAnnotationAttributes = new ArrayList<AnnotationAttributeValue<?>>();
    joinTableAnnotationAttributes.add(new StringAttributeValue(new JavaSymbolName(NAME), joinTable.getName()));
    final Iterator<ForeignKey> iter = joinTable.getImportedKeys().iterator();
    // Add "joinColumns" attribute containing nested @JoinColumn annotations
    final List<NestedAnnotationAttributeValue> joinColumnArrayValues = new ArrayList<NestedAnnotationAttributeValue>();
    final Set<Reference> firstKeyReferences = iter.next().getReferences();
    for (final Reference reference : firstKeyReferences) {
        final AnnotationMetadataBuilder joinColumnBuilder = getJoinColumnAnnotation(reference, firstKeyReferences.size() > 1);
        joinColumnArrayValues.add(new NestedAnnotationAttributeValue(new JavaSymbolName(VALUE), joinColumnBuilder.build()));
    }
    joinTableAnnotationAttributes.add(new ArrayAttributeValue<NestedAnnotationAttributeValue>(new JavaSymbolName("joinColumns"), joinColumnArrayValues));
    // Add "inverseJoinColumns" attribute containing nested @JoinColumn
    // annotations
    final List<NestedAnnotationAttributeValue> inverseJoinColumnArrayValues = new ArrayList<NestedAnnotationAttributeValue>();
    final Set<Reference> lastKeyReferences = iter.next().getReferences();
    for (final Reference reference : lastKeyReferences) {
        final AnnotationMetadataBuilder joinColumnBuilder = getJoinColumnAnnotation(reference, lastKeyReferences.size() > 1);
        inverseJoinColumnArrayValues.add(new NestedAnnotationAttributeValue(new JavaSymbolName(VALUE), joinColumnBuilder.build()));
    }
    joinTableAnnotationAttributes.add(new ArrayAttributeValue<NestedAnnotationAttributeValue>(new JavaSymbolName("inverseJoinColumns"), inverseJoinColumnArrayValues));
    // Add attributes to a @JoinTable annotation builder
    joinTableBuilder.setAttributes(joinTableAnnotationAttributes);
    annotations.add(joinTableBuilder);
    return new FieldMetadataBuilder(getId(), Modifier.PRIVATE, annotations, fieldName, fieldType);
}
Also used : AnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue) NestedAnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue) Reference(org.springframework.roo.addon.dbre.addon.model.Reference) ArrayList(java.util.ArrayList) ForeignKey(org.springframework.roo.addon.dbre.addon.model.ForeignKey) FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder) JdkJavaType(org.springframework.roo.model.JdkJavaType) JavaType(org.springframework.roo.model.JavaType) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) StringAttributeValue(org.springframework.roo.classpath.details.annotations.StringAttributeValue) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder) NestedAnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue)

Example 3 with Reference

use of org.springframework.roo.addon.dbre.addon.model.Reference in project spring-roo by spring-projects.

the class DbreMetadata method addManyToOneFields.

private void addManyToOneFields(final Table table) {
    // Add unique many-to-one fields
    final Map<JavaSymbolName, FieldMetadataBuilder> uniqueFields = new LinkedHashMap<JavaSymbolName, FieldMetadataBuilder>();
    for (final ForeignKey foreignKey : table.getImportedKeys()) {
        final Table foreignTable = foreignKey.getForeignTable();
        if (foreignTable == null || isOneToOne(table, foreignKey)) {
            continue;
        }
        // Assume many-to-one multiplicity
        JavaSymbolName fieldName = null;
        final String foreignTableName = foreignTable.getName();
        final String foreignSchemaName = foreignTable.getSchema().getName();
        if (foreignKey.getReferenceCount() == 1) {
            final Reference reference = foreignKey.getReferences().iterator().next();
            fieldName = new JavaSymbolName(DbreTypeUtils.suggestFieldName(reference.getLocalColumnName()));
        } else {
            final Short keySequence = foreignKey.getKeySequence();
            final String fieldSuffix = keySequence != null && keySequence > 0 ? String.valueOf(keySequence) : "";
            fieldName = new JavaSymbolName(DbreTypeUtils.suggestFieldName(foreignTableName) + fieldSuffix);
        }
        final JavaType fieldType = DbreTypeUtils.findTypeForTableName(managedEntities, foreignTableName, foreignSchemaName);
        Validate.notNull(fieldType, "Attempted to create many-to-one field '%s' in '%s' %s", fieldName, destination.getFullyQualifiedTypeName(), getErrorMsg(foreignTable.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, MANY_TO_ONE, true);
        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());
    }
}
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) Reference(org.springframework.roo.addon.dbre.addon.model.Reference) ForeignKey(org.springframework.roo.addon.dbre.addon.model.ForeignKey) FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder) LinkedHashMap(java.util.LinkedHashMap)

Example 4 with Reference

use of org.springframework.roo.addon.dbre.addon.model.Reference in project spring-roo by spring-projects.

the class DbreMetadata method getOneToOneOrManyToOneField.

private FieldMetadataBuilder getOneToOneOrManyToOneField(final JavaSymbolName fieldName, final JavaType fieldType, final ForeignKey foreignKey, final JavaType annotationType, final boolean referencedColumn) {
    // Add annotations to field
    final List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
    // Add annotation
    final AnnotationMetadataBuilder annotationBuilder = new AnnotationMetadataBuilder(annotationType);
    if (foreignKey.isExported()) {
        addCascadeType(annotationBuilder, foreignKey.getOnUpdate(), foreignKey.getOnDelete());
    }
    annotations.add(annotationBuilder);
    final Set<Reference> references = foreignKey.getReferences();
    if (references.size() == 1) {
        // Add @JoinColumn annotation
        annotations.add(getJoinColumnAnnotation(references.iterator().next(), referencedColumn, fieldType));
    } else {
        // Add @JoinColumns annotation
        annotations.add(getJoinColumnsAnnotation(references, fieldType));
    }
    return new FieldMetadataBuilder(getId(), Modifier.PRIVATE, annotations, fieldName, fieldType);
}
Also used : Reference(org.springframework.roo.addon.dbre.addon.model.Reference) ArrayList(java.util.ArrayList) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder) FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder)

Example 5 with Reference

use of org.springframework.roo.addon.dbre.addon.model.Reference in project spring-roo by spring-projects.

the class DbreMetadata method getJoinColumnsAnnotation.

private AnnotationMetadataBuilder getJoinColumnsAnnotation(final Set<Reference> references, final JavaType fieldType) {
    final List<NestedAnnotationAttributeValue> arrayValues = new ArrayList<NestedAnnotationAttributeValue>();
    // Nullable attribute will have same value for each
    // If some column not required, all JoinColumn will be nullable
    boolean nullable = false;
    for (final Reference reference : references) {
        if (!reference.getLocalColumn().isRequired()) {
            nullable = true;
        }
    }
    for (final Reference reference : references) {
        final AnnotationMetadataBuilder joinColumnAnnotation = getJoinColumnAnnotation(reference, true, fieldType, nullable);
        arrayValues.add(new NestedAnnotationAttributeValue(new JavaSymbolName(VALUE), joinColumnAnnotation.build()));
    }
    final List<AnnotationAttributeValue<?>> attributes = new ArrayList<AnnotationAttributeValue<?>>();
    attributes.add(new ArrayAttributeValue<NestedAnnotationAttributeValue>(new JavaSymbolName(VALUE), arrayValues));
    return new AnnotationMetadataBuilder(JOIN_COLUMNS, attributes);
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) AnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue) NestedAnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue) Reference(org.springframework.roo.addon.dbre.addon.model.Reference) ArrayList(java.util.ArrayList) NestedAnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Aggregations

Reference (org.springframework.roo.addon.dbre.addon.model.Reference)5 FieldMetadataBuilder (org.springframework.roo.classpath.details.FieldMetadataBuilder)4 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)4 ArrayList (java.util.ArrayList)3 ForeignKey (org.springframework.roo.addon.dbre.addon.model.ForeignKey)3 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)3 Table (org.springframework.roo.addon.dbre.addon.model.Table)2 AnnotationAttributeValue (org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue)2 NestedAnnotationAttributeValue (org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue)2 JavaType (org.springframework.roo.model.JavaType)2 JdkJavaType (org.springframework.roo.model.JdkJavaType)2 LinkedHashMap (java.util.LinkedHashMap)1 StringAttributeValue (org.springframework.roo.classpath.details.annotations.StringAttributeValue)1