Search in sources :

Example 6 with DateField

use of org.springframework.roo.classpath.operations.jsr303.DateField 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)

Example 7 with DateField

use of org.springframework.roo.classpath.operations.jsr303.DateField in project spring-roo by spring-projects.

the class DtoFieldCreatorProvider method createDateField.

@Override
public void createDateField(ClassOrInterfaceTypeDetails javaTypeDetails, JavaType fieldType, JavaSymbolName fieldName, boolean notNull, boolean nullRequired, boolean future, boolean past, DateFieldPersistenceType persistenceType, String column, String comment, DateTime dateFormat, DateTime timeFormat, String pattern, String value, boolean permitReservedWords, boolean transientModifier, List<AnnotationMetadataBuilder> extraAnnotations) {
    final String physicalTypeIdentifier = javaTypeDetails.getDeclaredByMetadataId();
    final DateField fieldDetails = new DateField(physicalTypeIdentifier, fieldType, fieldName);
    fieldDetails.setNotNull(notNull);
    fieldDetails.setNullRequired(nullRequired);
    fieldDetails.setFuture(future);
    fieldDetails.setPast(past);
    if (JdkJavaType.isDateField(fieldType)) {
        fieldDetails.setPersistenceType(persistenceType);
    }
    if (column != null) {
        fieldDetails.setColumn(column);
    }
    if (comment != null) {
        fieldDetails.setComment(comment);
    }
    if (dateFormat != null) {
        fieldDetails.setDateFormat(dateFormat);
    }
    if (timeFormat != null) {
        fieldDetails.setTimeFormat(timeFormat);
    }
    if (pattern != null) {
        fieldDetails.setPattern(pattern);
    }
    if (value != null) {
        fieldDetails.setValue(value);
    }
    if (extraAnnotations != null && !extraAnnotations.isEmpty()) {
        fieldDetails.addAnnotations(extraAnnotations);
    }
    insertField(fieldDetails, permitReservedWords, false);
}
Also used : DateField(org.springframework.roo.classpath.operations.jsr303.DateField)

Aggregations

DateField (org.springframework.roo.classpath.operations.jsr303.DateField)7 FieldMetadataBuilder (org.springframework.roo.classpath.details.FieldMetadataBuilder)4 CollectionField (org.springframework.roo.classpath.operations.jsr303.CollectionField)4 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)3 Entry (java.util.Map.Entry)1 FieldDetails (org.springframework.roo.classpath.details.FieldDetails)1 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)1 AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)1 ListField (org.springframework.roo.classpath.operations.jsr303.ListField)1 SetField (org.springframework.roo.classpath.operations.jsr303.SetField)1 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)1 JavaType (org.springframework.roo.model.JavaType)1 JdkJavaType (org.springframework.roo.model.JdkJavaType)1 JpaJavaType (org.springframework.roo.model.JpaJavaType)1 RooJavaType (org.springframework.roo.model.RooJavaType)1 Dependency (org.springframework.roo.project.Dependency)1