Search in sources :

Example 31 with FieldMetadataBuilder

use of org.springframework.roo.classpath.details.FieldMetadataBuilder 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 32 with FieldMetadataBuilder

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

the class DbreMetadata method getField.

private FieldMetadataBuilder getField(final JavaSymbolName fieldName, final Column column, final String tableName, final boolean includeNonPortable) {
    JavaType fieldType = column.getJavaType();
    Validate.notNull(fieldType, "Field type for column '%s' in table '%s' is null", column.getName(), tableName);
    // boolean primitive
    if (fieldType.equals(JavaType.BOOLEAN_OBJECT) && column.isRequired()) {
        fieldType = JavaType.BOOLEAN_PRIMITIVE;
    }
    // Add annotations to field
    final List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
    // Add @Column annotation
    final AnnotationMetadataBuilder columnBuilder = new AnnotationMetadataBuilder(COLUMN);
    columnBuilder.addStringAttribute(NAME, column.getEscapedName());
    if (includeNonPortable) {
        columnBuilder.addStringAttribute("columnDefinition", column.getTypeName());
    }
    // Add length attribute for Strings
    int columnSize = column.getColumnSize();
    if (columnSize < 4000 && fieldType.equals(JavaType.STRING)) {
        columnBuilder.addIntegerAttribute("length", columnSize);
    }
    // Add precision and scale attributes for numeric fields
    if (columnSize > 0 && JdkJavaType.isDecimalType(fieldType)) {
        columnBuilder.addIntegerAttribute("precision", columnSize);
        int scale = column.getScale();
        if (scale > 0) {
            columnBuilder.addIntegerAttribute("scale", scale);
        }
    }
    // Add unique = true to @Column if applicable
    if (column.isUnique()) {
        columnBuilder.addBooleanAttribute("unique", true);
    }
    annotations.add(columnBuilder);
    // Add @NotNull if applicable
    if (column.isRequired()) {
        annotations.add(new AnnotationMetadataBuilder(NOT_NULL));
    }
    // Add JSR 220 @Temporal annotation to date fields
    if (fieldType.equals(DATE) || fieldType.equals(CALENDAR)) {
        final AnnotationMetadataBuilder temporalBuilder = new AnnotationMetadataBuilder(TEMPORAL);
        temporalBuilder.addEnumAttribute(VALUE, new EnumDetails(TEMPORAL_TYPE, new JavaSymbolName(column.getJdbcType())));
        annotations.add(temporalBuilder);
        final AnnotationMetadataBuilder dateTimeFormatBuilder = new AnnotationMetadataBuilder(DATE_TIME_FORMAT);
        if (fieldType.equals(DATE)) {
            dateTimeFormatBuilder.addStringAttribute("style", "M-");
        } else {
            dateTimeFormatBuilder.addStringAttribute("style", "MM");
        }
        if (fieldName.getSymbolName().equals(CREATED)) {
            columnBuilder.addBooleanAttribute("updatable", false);
        }
        annotations.add(dateTimeFormatBuilder);
    }
    // Add @Lob for CLOB fields if applicable
    if (column.getJdbcType().equals("CLOB")) {
        annotations.add(new AnnotationMetadataBuilder(LOB));
    }
    final FieldMetadataBuilder fieldBuilder = new FieldMetadataBuilder(getId(), Modifier.PRIVATE, annotations, fieldName, fieldType);
    if (fieldName.getSymbolName().equals(CREATED)) {
        if (fieldType.equals(DATE)) {
            fieldBuilder.setFieldInitializer("new java.util.Date()");
        } else {
            fieldBuilder.setFieldInitializer("java.util.Calendar.getInstance()");
        }
    }
    return fieldBuilder;
}
Also used : JdkJavaType(org.springframework.roo.model.JdkJavaType) JavaType(org.springframework.roo.model.JavaType) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) ArrayList(java.util.ArrayList) EnumDetails(org.springframework.roo.model.EnumDetails) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder) FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder)

Example 33 with FieldMetadataBuilder

use of org.springframework.roo.classpath.details.FieldMetadataBuilder 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 34 with FieldMetadataBuilder

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

the class DtoFieldCreatorProvider method insertField.

public void insertField(final FieldDetails fieldDetails, final boolean permitReservedWords, final boolean transientModifier) {
    String module = null;
    if (!permitReservedWords) {
        ReservedWords.verifyReservedWordsNotPresent(fieldDetails.getFieldName());
        if (fieldDetails.getColumn() != null) {
            ReservedWords.verifyReservedWordsNotPresent(fieldDetails.getColumn());
        }
    }
    final List<AnnotationMetadataBuilder> annotations = fieldDetails.getInitedAnnotations();
    fieldDetails.decorateAnnotationsList(annotations);
    fieldDetails.setAnnotations(annotations);
    if (fieldDetails.getFieldType() != null) {
        module = fieldDetails.getFieldType().getModule();
    }
    fieldDetails.setModifiers(Modifier.PRIVATE);
    String initializer = null;
    if (fieldDetails instanceof CollectionField) {
        final CollectionField collectionField = (CollectionField) fieldDetails;
        module = collectionField.getGenericParameterTypeName().getModule();
        initializer = "new " + collectionField.getInitializer() + "()";
    } else if (fieldDetails instanceof DateField && fieldDetails.getFieldName().getSymbolName().equals("created")) {
        initializer = "new Date()";
    }
    // Format the passed-in comment (if given)
    formatFieldComment(fieldDetails);
    final FieldMetadataBuilder fieldBuilder = new FieldMetadataBuilder(fieldDetails);
    fieldBuilder.setFieldInitializer(initializer);
    typeManagementService.addField(fieldBuilder.build());
    if (module != null) {
        projectOperations.addModuleDependency(module);
    }
}
Also used : DateField(org.springframework.roo.classpath.operations.jsr303.DateField) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder) CollectionField(org.springframework.roo.classpath.operations.jsr303.CollectionField) FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder)

Example 35 with FieldMetadataBuilder

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

the class JpaAuditOperationsImpl method getCreatedDateField.

/**
 * Builds createdDate field for storing entity's created date
 *
 * @return FieldMetadataBuilder
 */
private FieldMetadataBuilder getCreatedDateField(ClassOrInterfaceTypeDetails entityDetails, String columnName) {
    // Create field annotations
    List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
    // Only add @Column if required by annotation @RooJpaAudit
    if (StringUtils.isNotBlank(columnName)) {
        AnnotationMetadataBuilder columnAnnotation = new AnnotationMetadataBuilder(JpaJavaType.COLUMN);
        columnAnnotation.addStringAttribute("name", columnName);
        annotations.add(columnAnnotation);
    }
    // Add @CreatedDate
    AnnotationMetadataBuilder createdDateAnnotation = new AnnotationMetadataBuilder(SpringJavaType.CREATED_DATE);
    annotations.add(createdDateAnnotation);
    // Add @Temporal
    AnnotationMetadataBuilder temporalAnnotation = new AnnotationMetadataBuilder(JpaJavaType.TEMPORAL);
    temporalAnnotation.addEnumAttribute("value", new EnumDetails(JpaJavaType.TEMPORAL_TYPE, new JavaSymbolName("TIMESTAMP")));
    annotations.add(temporalAnnotation);
    // Add @DateTimeFormat
    AnnotationMetadataBuilder dateTimeFormatAnnotation = new AnnotationMetadataBuilder(SpringJavaType.DATE_TIME_FORMAT);
    dateTimeFormatAnnotation.addStringAttribute("style", "M-");
    annotations.add(dateTimeFormatAnnotation);
    // Create field
    FieldDetails fieldDetails = new FieldDetails(PhysicalTypeIdentifier.createIdentifier(entityDetails), JdkJavaType.CALENDAR, new JavaSymbolName("createdDate"));
    fieldDetails.setModifiers(Modifier.PRIVATE);
    fieldDetails.setAnnotations(annotations);
    FieldMetadataBuilder fieldBuilder = new FieldMetadataBuilder(fieldDetails);
    return fieldBuilder;
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) ArrayList(java.util.ArrayList) FieldDetails(org.springframework.roo.classpath.details.FieldDetails) EnumDetails(org.springframework.roo.model.EnumDetails) 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