Search in sources :

Example 6 with FieldDetails

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

the class DtoFieldCreatorProvider method createOtherField.

@Override
public void createOtherField(ClassOrInterfaceTypeDetails cid, JavaType fieldType, JavaSymbolName fieldName, boolean notNull, boolean nullRequired, String comment, String column, boolean permitReservedWords, boolean transientModifier, List<AnnotationMetadataBuilder> extraAnnotations) {
    final String physicalTypeIdentifier = cid.getDeclaredByMetadataId();
    final FieldDetails fieldDetails = new FieldDetails(physicalTypeIdentifier, fieldType, fieldName);
    fieldDetails.setNotNull(notNull);
    fieldDetails.setNullRequired(nullRequired);
    if (comment != null) {
        fieldDetails.setComment(comment);
    }
    if (column != null) {
        fieldDetails.setColumn(column);
    }
    if (extraAnnotations != null && !extraAnnotations.isEmpty()) {
        fieldDetails.addAnnotations(extraAnnotations);
    }
    insertField(fieldDetails, permitReservedWords, false);
}
Also used : FieldDetails(org.springframework.roo.classpath.details.FieldDetails)

Example 7 with FieldDetails

use of org.springframework.roo.classpath.details.FieldDetails 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)

Example 8 with FieldDetails

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

the class JpaAuditOperationsImpl method getModifiedDateField.

/**
 * Builds modifiedDate field for storing entity's last modified date
 *
 * @return FieldMetadataBuilder
 */
private FieldMetadataBuilder getModifiedDateField(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 @LastModifiedDate
    AnnotationMetadataBuilder createdDateAnnotation = new AnnotationMetadataBuilder(SpringJavaType.LAST_MODIFIED_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("modifiedDate"));
    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)

Example 9 with FieldDetails

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

the class JpaOperationsImpl method getIdentifierField.

/**
 * This method generates the identifier field using the provided values.
 *
 * @param entity
 * @param identifierField
 * @param identifierType
 * @param identifierColumn
 * @param sequenceName
 * @param identifierStrategy
 * @param inheritanceType
 * @return
 */
private FieldMetadata getIdentifierField(final JavaType entity, String identifierField, final JavaType identifierType, final String identifierColumn, final String sequenceName, IdentifierStrategy identifierStrategy, InheritanceType inheritanceType) {
    final List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
    final boolean hasIdClass = !(identifierType.isCoreType());
    final JavaType annotationType = hasIdClass ? EMBEDDED_ID : ID;
    annotations.add(new AnnotationMetadataBuilder(annotationType));
    if (StringUtils.isEmpty(identifierField)) {
        identifierField = "id";
    }
    // Compute the column name, as required
    if (!hasIdClass) {
        if (!"".equals(sequenceName)) {
            // ROO-3719: Add SEQUENCE as @GeneratedValue strategy
            // Check if provided identifierStrategy is valid
            boolean isValidIdentifierStrategy = false;
            if (identifierStrategy != null) {
                for (IdentifierStrategy identifierStrategyType : IdentifierStrategy.values()) {
                    if (identifierStrategyType.name().equals(identifierStrategy.name())) {
                        isValidIdentifierStrategy = true;
                        break;
                    }
                }
            }
            if (!isValidIdentifierStrategy) {
                identifierStrategy = IdentifierStrategy.AUTO;
            }
            // InheritanceType.TABLE_PER_CLASS)
            if (IdentifierStrategy.AUTO.name().equals(identifierStrategy.name())) {
                if (inheritanceType != null) {
                    if ("TABLE_PER_CLASS".equals(inheritanceType.name())) {
                        identifierStrategy = IdentifierStrategy.TABLE;
                    }
                }
            }
            final AnnotationMetadataBuilder generatedValueBuilder = new AnnotationMetadataBuilder(GENERATED_VALUE);
            generatedValueBuilder.addEnumAttribute("strategy", new EnumDetails(GENERATION_TYPE, new JavaSymbolName(identifierStrategy.name())));
            if (StringUtils.isNotBlank(sequenceName)) {
                final String sequenceKey = StringUtils.uncapitalize(entity.getSimpleTypeName()) + "Gen";
                generatedValueBuilder.addStringAttribute("generator", sequenceKey);
                final AnnotationMetadataBuilder sequenceGeneratorBuilder = new AnnotationMetadataBuilder(SEQUENCE_GENERATOR);
                sequenceGeneratorBuilder.addStringAttribute("name", sequenceKey);
                sequenceGeneratorBuilder.addStringAttribute("sequenceName", sequenceName);
                annotations.add(sequenceGeneratorBuilder);
            }
            annotations.add(generatedValueBuilder);
        }
        // User has specified alternative columnName
        if (StringUtils.isNotBlank(identifierColumn)) {
            final AnnotationMetadataBuilder columnBuilder = new AnnotationMetadataBuilder(COLUMN);
            columnBuilder.addStringAttribute("name", identifierColumn);
            annotations.add(columnBuilder);
        }
    }
    FieldDetails identifierFieldDetails = new FieldDetails(getTypeLocationService().getPhysicalTypeIdentifier(entity), identifierType, new JavaSymbolName(identifierField));
    identifierFieldDetails.setModifiers(Modifier.PRIVATE);
    identifierFieldDetails.addAnnotations(annotations);
    return new FieldMetadataBuilder(identifierFieldDetails).build();
}
Also used : JdkJavaType(org.springframework.roo.model.JdkJavaType) RooJavaType(org.springframework.roo.model.RooJavaType) JavaType(org.springframework.roo.model.JavaType) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) ArrayList(java.util.ArrayList) FieldDetails(org.springframework.roo.classpath.details.FieldDetails) EnumDetails(org.springframework.roo.model.EnumDetails) IdentifierStrategy(org.springframework.roo.addon.jpa.addon.entity.IdentifierStrategy) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder) FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder)

Example 10 with FieldDetails

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

the class DtoOperationsImpl method addFieldsToProjection.

/**
 * Removes persistence annotations of provided fields and adds them to a
 * ClassOrInterfaceTypeDetailsBuilder representing a Projection in construction.
 * Also adds final modifier to fields if required.
 *
 * @param projectionBuilder the ClassOrInterfaceTypeDetailsBuilder for building the
 *            Projection class.
 * @param fieldsToAdd the List<FieldMetadata> to add.
 */
private void addFieldsToProjection(ClassOrInterfaceTypeDetailsBuilder projectionBuilder, Map<String, FieldMetadata> fieldsToAdd) {
    Iterator<Entry<String, FieldMetadata>> iterator = fieldsToAdd.entrySet().iterator();
    while (iterator.hasNext()) {
        Entry<String, FieldMetadata> entry = iterator.next();
        FieldMetadata field = entry.getValue();
        // List and Set types require special management
        FieldMetadataBuilder fieldBuilder = null;
        FieldDetails fieldDetails = null;
        if (field.getFieldType().getFullyQualifiedTypeName().equals("java.util.Set")) {
            JavaType fieldType = field.getFieldType().getParameters().get(0);
            fieldDetails = new SetField(projectionBuilder.getDeclaredByMetadataId(), new JavaType(JdkJavaType.SET.getFullyQualifiedTypeName(), 0, DataType.TYPE, null, Arrays.asList(fieldType)), field.getFieldName(), fieldType, null, null, true);
            fieldBuilder = new FieldMetadataBuilder(fieldDetails);
            fieldBuilder.setModifier(field.getModifier());
            fieldBuilder.setAnnotations(field.getAnnotations());
        } else if (field.getFieldType().getFullyQualifiedTypeName().equals("java.util.List")) {
            JavaType fieldType = field.getFieldType().getParameters().get(0);
            fieldDetails = new ListField(projectionBuilder.getDeclaredByMetadataId(), new JavaType(JdkJavaType.LIST.getFullyQualifiedTypeName(), 0, DataType.TYPE, null, Arrays.asList(fieldType)), field.getFieldName(), fieldType, null, null, true);
            fieldBuilder = new FieldMetadataBuilder(fieldDetails);
            fieldBuilder.setModifier(field.getModifier());
            fieldBuilder.setAnnotations(field.getAnnotations());
        } else {
            // Can't just copy FieldMetadata because field.declaredByMetadataId will be the same
            fieldBuilder = new FieldMetadataBuilder(projectionBuilder.getDeclaredByMetadataId(), field);
        }
        // Add dependency between modules
        typeLocationService.addModuleDependency(projectionBuilder.getName().getModule(), field.getFieldType());
        // Set new fieldName
        fieldBuilder.setFieldName(new JavaSymbolName(entry.getKey()));
        // If it is a CollectionField it needs an initializer
        String initializer = null;
        if (fieldDetails instanceof CollectionField) {
            final CollectionField collectionField = (CollectionField) fieldDetails;
            initializer = "new " + collectionField.getInitializer() + "()";
        } else if (fieldDetails instanceof DateField && fieldDetails.getFieldName().getSymbolName().equals("created")) {
            initializer = "new Date()";
        }
        fieldBuilder.setFieldInitializer(initializer);
        // Remove persistence annotations
        List<AnnotationMetadata> annotations = field.getAnnotations();
        for (AnnotationMetadata annotation : annotations) {
            if (annotation.getAnnotationType().getFullyQualifiedTypeName().contains("javax.persistence")) {
                fieldBuilder.removeAnnotation(annotation.getAnnotationType());
            } else if (annotation.getAnnotationType().getFullyQualifiedTypeName().startsWith("javax.validation")) {
                // Add validation dependency
                projectOperations.addDependency(projectionBuilder.getName().getModule(), new Dependency("javax.validation", "validation-api", null));
            } else if (annotation.getAnnotationType().equals(RooJavaType.ROO_JPA_RELATION)) {
                fieldBuilder.removeAnnotation(annotation.getAnnotationType());
            }
        }
        fieldBuilder.setModifier(Modifier.PRIVATE);
        // Build field
        FieldMetadata projectionField = fieldBuilder.build();
        // Add field to DTO
        projectionBuilder.addField(projectionField);
    }
}
Also used : FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) FieldDetails(org.springframework.roo.classpath.details.FieldDetails) ListField(org.springframework.roo.classpath.operations.jsr303.ListField) Dependency(org.springframework.roo.project.Dependency) SetField(org.springframework.roo.classpath.operations.jsr303.SetField) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata) FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder) Entry(java.util.Map.Entry) JdkJavaType(org.springframework.roo.model.JdkJavaType) RooJavaType(org.springframework.roo.model.RooJavaType) JpaJavaType(org.springframework.roo.model.JpaJavaType) JavaType(org.springframework.roo.model.JavaType) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) DateField(org.springframework.roo.classpath.operations.jsr303.DateField) CollectionField(org.springframework.roo.classpath.operations.jsr303.CollectionField)

Aggregations

FieldDetails (org.springframework.roo.classpath.details.FieldDetails)10 FieldMetadataBuilder (org.springframework.roo.classpath.details.FieldMetadataBuilder)7 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)7 ArrayList (java.util.ArrayList)6 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)6 EnumDetails (org.springframework.roo.model.EnumDetails)3 JavaType (org.springframework.roo.model.JavaType)2 JdkJavaType (org.springframework.roo.model.JdkJavaType)2 RooJavaType (org.springframework.roo.model.RooJavaType)2 Entry (java.util.Map.Entry)1 IdentifierStrategy (org.springframework.roo.addon.jpa.addon.entity.IdentifierStrategy)1 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)1 AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)1 CollectionField (org.springframework.roo.classpath.operations.jsr303.CollectionField)1 DateField (org.springframework.roo.classpath.operations.jsr303.DateField)1 ListField (org.springframework.roo.classpath.operations.jsr303.ListField)1 SetField (org.springframework.roo.classpath.operations.jsr303.SetField)1 JpaJavaType (org.springframework.roo.model.JpaJavaType)1 Dependency (org.springframework.roo.project.Dependency)1