Search in sources :

Example 1 with SetField

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

the class JpaFieldCreatorProvider method createParentFieldOfToManyRelation.

/**
 * Create parent field of a *ToMany relation
 *
 * @param parentCid
 * @param childCid
 * @param fieldName
 * @param fieldType
 * @param cardinality
 * @param permitReservedWords
 * @param sizeMin
 * @param sizeMax
 * @param comment
 * @param notNull
 * @param mappedBy
 * @param fetch
 * @param aggregation
 * @param orphanRemoval
 * @param cascadeType
 * @param isList
 */
private void createParentFieldOfToManyRelation(ClassOrInterfaceTypeDetails parentCid, ClassOrInterfaceTypeDetails childCid, JavaSymbolName fieldName, JavaType fieldType, Cardinality cardinality, boolean permitReservedWords, Integer sizeMin, Integer sizeMax, String comment, boolean notNull, JavaSymbolName mappedBy, Fetch fetch, Boolean aggregation, Boolean orphanRemoval, Cascade[] cascadeType, boolean isList) {
    if (cascadeType == null) {
        // prepare cascadType
        if (aggregation) {
            cascadeType = Cascade.MERGE_PERSIST;
        } else {
            // Composition
            cascadeType = Cascade.ALL_ARRAY;
        }
    }
    // Check if child field exist
    checkFieldExists(mappedBy, false, childCid, "mappedBy");
    SetField parentFieldDetails;
    if (isList) {
        parentFieldDetails = new ListField(parentCid.getDeclaredByMetadataId(), new JavaType(LIST.getFullyQualifiedTypeName(), 0, DataType.TYPE, null, Arrays.asList(fieldType)), fieldName, fieldType, cardinality, cascadeType, false);
    } else {
        parentFieldDetails = new SetField(parentCid.getDeclaredByMetadataId(), new JavaType(SET.getFullyQualifiedTypeName(), 0, DataType.TYPE, null, Arrays.asList(fieldType)), fieldName, fieldType, cardinality, cascadeType, false);
    }
    parentFieldDetails.setNotNull(notNull);
    if (comment != null) {
        parentFieldDetails.setComment(comment);
    }
    if (sizeMin != null) {
        parentFieldDetails.setSizeMin(sizeMin);
    }
    if (sizeMax != null) {
        parentFieldDetails.setSizeMax(sizeMax);
    }
    parentFieldDetails.setMappedBy(mappedBy);
    parentFieldDetails.setFetch(fetch);
    if (orphanRemoval == null) {
        if (aggregation) {
            orphanRemoval = false;
        } else {
            orphanRemoval = true;
        }
    }
    AnnotationMetadataBuilder rooJpaRelationAnnotation = new AnnotationMetadataBuilder(RooJavaType.ROO_JPA_RELATION);
    if (aggregation) {
        rooJpaRelationAnnotation.addEnumAttribute("type", RooEnumDetails.RELATION_TYPE_AGGREGATION);
    } else {
        rooJpaRelationAnnotation.addEnumAttribute("type", RooEnumDetails.RELATION_TYPE_COMPOSITION);
    }
    parentFieldDetails.addAdditionaAnnotation(rooJpaRelationAnnotation);
    // insert parent field
    insertField(parentFieldDetails, permitReservedWords, false, true);
}
Also used : JdkJavaType(org.springframework.roo.model.JdkJavaType) RooJavaType(org.springframework.roo.model.RooJavaType) SpringletsJavaType(org.springframework.roo.model.SpringletsJavaType) JpaJavaType(org.springframework.roo.model.JpaJavaType) JavaType(org.springframework.roo.model.JavaType) ListField(org.springframework.roo.classpath.operations.jsr303.ListField) SetField(org.springframework.roo.classpath.operations.jsr303.SetField) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Example 2 with SetField

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

the class JpaFieldCreatorProvider method createChildFieldOfManyToManyRelation.

/**
 * Create child field of a ManyToMany relation
 *
 * @param childCid
 * @param parentType
 * @param permitReservedWords
 * @param mappedBy
 * @param fetch
 * @param joinTable
 * @param joinColumns
 * @param referencedColumns
 * @param inverseJoinColumns
 * @param inverseReferencedColumns
 * @param isList
 */
private void createChildFieldOfManyToManyRelation(ClassOrInterfaceTypeDetails childCid, JavaType parentType, boolean permitReservedWords, JavaSymbolName mappedBy, Fetch fetch, String joinTable, String joinColumns, String referencedColumns, String inverseJoinColumns, String inverseReferencedColumns, boolean isList) {
    SetField childFieldDetails;
    if (isList) {
        childFieldDetails = new ListField(childCid.getDeclaredByMetadataId(), new JavaType(LIST.getFullyQualifiedTypeName(), 0, DataType.TYPE, null, Arrays.asList(parentType)), mappedBy, parentType, Cardinality.MANY_TO_MANY, null, false);
    } else {
        childFieldDetails = new SetField(childCid.getDeclaredByMetadataId(), new JavaType(SET.getFullyQualifiedTypeName(), 0, DataType.TYPE, null, Arrays.asList(parentType)), mappedBy, parentType, Cardinality.MANY_TO_MANY, null, false);
    }
    childFieldDetails.setFetch(fetch);
    if (StringUtils.isNotBlank(joinTable) || StringUtils.isNotBlank(inverseJoinColumns) || StringUtils.isNotBlank(joinColumns)) {
        if (StringUtils.isNotBlank(inverseJoinColumns)) {
        }
        // Create strings arrays and set @JoinTable annotation
        String[] joinColumnsArray = null;
        String[] referencedColumnsArray = null;
        String[] inverseJoinColumnsArray = null;
        String[] inverseReferencedColumnsArray = null;
        if (joinColumns != null) {
            joinColumnsArray = joinColumns.replace(" ", "").split(",");
        }
        if (referencedColumns != null) {
            referencedColumnsArray = referencedColumns.replace(" ", "").split(",");
        }
        if (inverseJoinColumns != null) {
            inverseJoinColumnsArray = inverseJoinColumns.replace(" ", "").split(",");
        }
        if (inverseReferencedColumns != null) {
            inverseReferencedColumnsArray = inverseReferencedColumns.replace(" ", "").split(",");
        }
        // Validate same number of elements
        if (joinColumnsArray != null && referencedColumnsArray != null) {
            Validate.isTrue(joinColumnsArray.length == referencedColumnsArray.length, "--joinColumns and --referencedColumns must have same number of column values");
        }
        if (inverseJoinColumnsArray != null && inverseReferencedColumnsArray != null) {
            Validate.isTrue(inverseJoinColumnsArray.length == inverseReferencedColumnsArray.length, "--inverseJoinColumns and --inverseReferencedColumns must have same number of column values");
        }
        // JoinTable set
        childFieldDetails.setJoinAnnotations(joinTable, joinColumnsArray, referencedColumnsArray, inverseJoinColumnsArray, inverseReferencedColumnsArray);
    }
    insertField(childFieldDetails, permitReservedWords, false, true);
}
Also used : JdkJavaType(org.springframework.roo.model.JdkJavaType) RooJavaType(org.springframework.roo.model.RooJavaType) SpringletsJavaType(org.springframework.roo.model.SpringletsJavaType) JpaJavaType(org.springframework.roo.model.JpaJavaType) JavaType(org.springframework.roo.model.JavaType) ListField(org.springframework.roo.classpath.operations.jsr303.ListField) SetField(org.springframework.roo.classpath.operations.jsr303.SetField)

Example 3 with SetField

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

the class JpaFieldCreatorProvider method createCollectionEnumeration.

/**
 * Create a field for a List or Set of a enumeration
 *
 * @param parentCid
 * @param fieldType
 * @param fieldName
 * @param permitReservedWords
 * @param sizeMin
 * @param sizeMax
 * @param comment
 * @param notNull
 * @param isList
 */
private void createCollectionEnumeration(final ClassOrInterfaceTypeDetails parentCid, JavaType fieldType, JavaSymbolName fieldName, boolean permitReservedWords, Integer sizeMin, Integer sizeMax, String comment, boolean notNull, boolean isList) {
    SetField parentFieldDetails;
    if (isList) {
        parentFieldDetails = new SetField(parentCid.getDeclaredByMetadataId(), new JavaType(LIST.getFullyQualifiedTypeName(), 0, DataType.TYPE, null, Arrays.asList(fieldType)), fieldName, fieldType, null, null, false);
    } else {
        parentFieldDetails = new ListField(parentCid.getDeclaredByMetadataId(), new JavaType(SET.getFullyQualifiedTypeName(), 0, DataType.TYPE, null, Arrays.asList(fieldType)), fieldName, fieldType, null, null, false);
    }
    parentFieldDetails.setNotNull(notNull);
    if (comment != null) {
        parentFieldDetails.setComment(comment);
    }
    if (sizeMin != null) {
        parentFieldDetails.setSizeMin(sizeMin);
    }
    if (sizeMax != null) {
        parentFieldDetails.setSizeMax(sizeMax);
    }
    // Handle enumeration Set
    insertField(parentFieldDetails, permitReservedWords, false, true);
}
Also used : JdkJavaType(org.springframework.roo.model.JdkJavaType) RooJavaType(org.springframework.roo.model.RooJavaType) SpringletsJavaType(org.springframework.roo.model.SpringletsJavaType) JpaJavaType(org.springframework.roo.model.JpaJavaType) JavaType(org.springframework.roo.model.JavaType) ListField(org.springframework.roo.classpath.operations.jsr303.ListField) SetField(org.springframework.roo.classpath.operations.jsr303.SetField)

Example 4 with SetField

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

the class UpdateCompilationUnitTest method testSimpleClass3AddField.

@Test
public void testSimpleClass3AddField() throws Exception {
    // Set up
    final File file = getResource(SIMPLE_CLASS3_FILE_PATH);
    final String fileContents = getResourceContents(file);
    final ClassOrInterfaceTypeDetails simpleInterfaceDetails = typeParsingService.getTypeFromString(fileContents, SIMPLE_CLASS3_DECLARED_BY_MID, SIMPLE_CLASS3_TYPE);
    final SetField fieldDetails = new SetField(SIMPLE_CLASS3_DECLARED_BY_MID, new JavaType(SET.getFullyQualifiedTypeName(), 0, DataType.TYPE, null, Arrays.asList(SIMPLE_CLASS3_TYPE)), new JavaSymbolName("children"), SIMPLE_CLASS3_TYPE, Cardinality.ONE_TO_MANY, new Cascade[] { Cascade.REMOVE }, false);
    final FieldMetadataBuilder fieldBuilder = new FieldMetadataBuilder(fieldDetails.getPhysicalTypeIdentifier(), Modifier.PRIVATE, new ArrayList<AnnotationMetadataBuilder>(), fieldDetails.getFieldName(), fieldDetails.getFieldType());
    fieldBuilder.setFieldInitializer("new HashSet<SimpleClass3>()");
    final ClassOrInterfaceTypeDetails newClassDetails = addField(simpleInterfaceDetails, fieldBuilder.build());
    // Invoke
    final String result = typeParsingService.getCompilationUnitContents(newClassDetails);
    saveResult(file, result, "-addField");
    checkSimple3Class(result);
    assertTrue(result.contains("private Set<SimpleClass3> children = new HashSet<SimpleClass3>();"));
    // Add another
    final ClassOrInterfaceTypeDetails simpleInterfaceDetails2 = typeParsingService.getTypeFromString(result, SIMPLE_CLASS3_DECLARED_BY_MID, SIMPLE_CLASS3_TYPE);
    final ReferenceField fieldDetails2 = new ReferenceField(SIMPLE_CLASS3_DECLARED_BY_MID, SIMPLE_CLASS2_TYPE, new JavaSymbolName("referenceField"), Cardinality.MANY_TO_ONE, new Cascade[] { Cascade.REFRESH });
    final FieldMetadataBuilder fieldBuilder2 = new FieldMetadataBuilder(fieldDetails2.getPhysicalTypeIdentifier(), Modifier.PRIVATE, new ArrayList<AnnotationMetadataBuilder>(), fieldDetails2.getFieldName(), fieldDetails2.getFieldType());
    final ClassOrInterfaceTypeDetails newClassDetails2 = addField(simpleInterfaceDetails2, fieldBuilder2.build());
    // Invoke
    final String result2 = typeParsingService.getCompilationUnitContents(newClassDetails2);
    // Save to file for debug
    saveResult(file, result2, "-addField2");
    checkSimple3Class(result2);
    assertTrue(result.contains("private Set<SimpleClass3> children = new HashSet<SimpleClass3>();"));
    assertTrue(result2.contains("private SimpleClass2 referenceField;"));
}
Also used : JavaType(org.springframework.roo.model.JavaType) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) SetField(org.springframework.roo.classpath.operations.jsr303.SetField) File(java.io.File) ReferenceField(org.springframework.roo.classpath.operations.jsr303.ReferenceField) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder) FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder) Test(org.junit.Test)

Example 5 with SetField

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

SetField (org.springframework.roo.classpath.operations.jsr303.SetField)5 JavaType (org.springframework.roo.model.JavaType)5 ListField (org.springframework.roo.classpath.operations.jsr303.ListField)4 JdkJavaType (org.springframework.roo.model.JdkJavaType)4 JpaJavaType (org.springframework.roo.model.JpaJavaType)4 RooJavaType (org.springframework.roo.model.RooJavaType)4 SpringletsJavaType (org.springframework.roo.model.SpringletsJavaType)3 FieldMetadataBuilder (org.springframework.roo.classpath.details.FieldMetadataBuilder)2 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)2 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)2 File (java.io.File)1 Entry (java.util.Map.Entry)1 Test (org.junit.Test)1 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)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 CollectionField (org.springframework.roo.classpath.operations.jsr303.CollectionField)1 DateField (org.springframework.roo.classpath.operations.jsr303.DateField)1 ReferenceField (org.springframework.roo.classpath.operations.jsr303.ReferenceField)1