Search in sources :

Example 1 with CollectionField

use of org.springframework.roo.classpath.operations.jsr303.CollectionField 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 2 with CollectionField

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

the class EmbeddableFieldCreatorProvider 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();
    }
    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()";
    }
    int modifier = Modifier.PRIVATE;
    if (transientModifier) {
        modifier += Modifier.TRANSIENT;
    }
    fieldDetails.setModifiers(modifier);
    // 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 3 with CollectionField

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

the class JpaFieldCreatorProvider method getFieldModule.

private String getFieldModule(final FieldDetails fieldDetails) {
    String module = null;
    if (fieldDetails.getFieldType() != null) {
        module = fieldDetails.getFieldType().getModule();
    }
    if (fieldDetails instanceof CollectionField) {
        final CollectionField collectionField = (CollectionField) fieldDetails;
        module = collectionField.getGenericParameterTypeName().getModule();
    }
    return module;
}
Also used : CollectionField(org.springframework.roo.classpath.operations.jsr303.CollectionField)

Example 4 with CollectionField

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

the class JpaFieldCreatorProvider method generateFieldBuilder.

/**
 * Generates Field builder based on fieldDetails
 *
 * @param fieldDetails
 * @param permitReservedWords
 * @param transientModifier
 * @return
 */
private FieldMetadataBuilder generateFieldBuilder(final FieldDetails fieldDetails, final boolean permitReservedWords, final boolean transientModifier) {
    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);
    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()";
    }
    int modifier = Modifier.PRIVATE;
    if (transientModifier) {
        modifier += Modifier.TRANSIENT;
    }
    fieldDetails.setModifiers(modifier);
    // Format the passed-in comment (if given)
    formatFieldComment(fieldDetails);
    final FieldMetadataBuilder fieldBuilder = new FieldMetadataBuilder(fieldDetails);
    fieldBuilder.setFieldInitializer(initializer);
    return fieldBuilder;
}
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 5 with CollectionField

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

CollectionField (org.springframework.roo.classpath.operations.jsr303.CollectionField)5 FieldMetadataBuilder (org.springframework.roo.classpath.details.FieldMetadataBuilder)4 DateField (org.springframework.roo.classpath.operations.jsr303.DateField)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