Search in sources :

Example 1 with ReferenceField

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

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

the class JpaFieldCreatorProvider method createChildFieldOfOneToManyRelation.

/**
 * Create child field of an OneToMany relation
 *
 * @param childCid
 * @param parentType
 * @param permitReservedWords
 * @param mappedBy
 * @param fetch
 * @param joinColumnName
 * @param referencedColumnName
 * @param joinTable
 * @param joinColumns
 * @param referencedColumns
 * @param inverseJoinColumns
 * @param inverseReferencedColumns
 */
private void createChildFieldOfOneToManyRelation(ClassOrInterfaceTypeDetails childCid, JavaType parentType, boolean permitReservedWords, JavaSymbolName mappedBy, Fetch fetch, String joinColumnName, String referencedColumnName, String joinTable, String joinColumns, String referencedColumns, String inverseJoinColumns, String inverseReferencedColumns, String formatExpression, String formatMessage) {
    final ReferenceField childFieldDetails = new ReferenceField(childCid.getDeclaredByMetadataId(), parentType, mappedBy, Cardinality.MANY_TO_ONE, null);
    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);
    } else if (StringUtils.isNotBlank(joinColumnName)) {
        if (StringUtils.isNotBlank(referencedColumnName)) {
            childFieldDetails.setJoinColumn(joinColumnName, referencedColumnName);
        } else {
            childFieldDetails.setJoinColumn(joinColumnName);
        }
    }
    childFieldDetails.addAdditionaAnnotation(buildEntityFormatAnnotation(formatExpression, formatMessage, mappedBy.getSymbolName()));
    insertField(childFieldDetails, permitReservedWords, false, true);
}
Also used : ReferenceField(org.springframework.roo.classpath.operations.jsr303.ReferenceField)

Example 3 with ReferenceField

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

the class JpaFieldCreatorProvider method createReferenceField.

@Override
public void createReferenceField(JavaType typeName, JavaType fieldType, JavaSymbolName fieldName, boolean aggregation, JavaSymbolName mappedBy, Cascade[] cascadeType, boolean notNull, String joinColumnName, String referencedColumnName, Fetch fetch, String comment, boolean permitReservedWords, Boolean orphanRemoval, boolean isForce, String formatExpression, String formatMessage) {
    final ClassOrInterfaceTypeDetails childCid = typeLocationService.getTypeDetails(fieldType);
    final ClassOrInterfaceTypeDetails parentCid = typeLocationService.getTypeDetails(typeName);
    Validate.notNull(parentCid, "The type specified, '%s', doesn't exist", typeName);
    Validate.notNull(childCid, "The specified target '--type' does not exist or can not be found. Please create this type first.", fieldType);
    // Check if parent field exist
    checkFieldExists(fieldName, isForce, parentCid, "fieldName");
    if (mappedBy == null) {
        // generate mappedBy name from uncapitalized parentClass name
        mappedBy = new JavaSymbolName(StringUtils.uncapitalize(typeName.getSimpleTypeName()));
    }
    // Check that child 'mappedBy' field isn't equal to child type name uncapitalized
    Validate.isTrue(!StringUtils.uncapitalize(fieldType.getSimpleTypeName()).equals(mappedBy.getSymbolName()), "Child entity field can not have the same name as the referenced entity ('%s') name in " + "lower camel case ('%s'). Please assign it other value using '--mappedBy' option.", fieldType.getSimpleTypeName(), mappedBy.getSymbolName());
    // Check if child field exist
    checkFieldExists(mappedBy, false, childCid, "mappedBy");
    // Check if the requested entity is a JPA @Entity
    final MemberDetails childMemberDetails = memberDetailsScanner.getMemberDetails(this.getClass().getName(), childCid);
    final AnnotationMetadata entityAnnotation = childMemberDetails.getAnnotation(ENTITY);
    final AnnotationMetadata persistentAnnotation = childMemberDetails.getAnnotation(PERSISTENT);
    Validate.isTrue(entityAnnotation != null || persistentAnnotation != null, "The field reference command is only applicable to JPA @Entity or Spring Data @Persistent target types.");
    // Prepare parent field
    if (cascadeType == null) {
        // prepare cascadType
        if (aggregation) {
            cascadeType = Cascade.MERGE_PERSIST;
        } else {
            // Compsition
            cascadeType = Cascade.ALL_ARRAY;
        }
    }
    if (fetch == null) {
        fetch = Fetch.LAZY;
    }
    final ReferenceField parentFieldDetails = new ReferenceField(parentCid.getDeclaredByMetadataId(), fieldType, fieldName, Cardinality.ONE_TO_ONE, cascadeType);
    parentFieldDetails.setFetch(fetch);
    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);
    if (comment != null) {
        parentFieldDetails.setComment(comment);
    }
    if (orphanRemoval == null && !aggregation) {
        // composition
        orphanRemoval = true;
    }
    parentFieldDetails.setOrphanRemoval(orphanRemoval);
    parentFieldDetails.setMappedBy(mappedBy);
    // ROO-3868: New entity visualization support using a new format annotation
    parentFieldDetails.addAdditionaAnnotation(buildEntityFormatAnnotation(formatExpression, formatMessage, fieldName.getSymbolName()));
    // Prepare child files
    final ReferenceField childFieldDetails = new ReferenceField(childCid.getDeclaredByMetadataId(), typeName, mappedBy, Cardinality.ONE_TO_ONE, null);
    childFieldDetails.setFetch(fetch);
    if (joinColumnName != null) {
        if (referencedColumnName != null) {
            Validate.notNull(joinColumnName, "@JoinColumn name is required if specifying a referencedColumnName");
            childFieldDetails.setJoinColumn(joinColumnName, referencedColumnName);
        } else {
            childFieldDetails.setJoinColumn(joinColumnName);
        }
    }
    childFieldDetails.setNotNull(notNull);
    // ROO-3868: New entity visualization support using a new format annotation
    childFieldDetails.addAdditionaAnnotation(buildEntityFormatAnnotation(formatExpression, formatMessage, fieldName.getSymbolName()));
    // insert child field
    insertField(childFieldDetails, permitReservedWords, false, true);
    // insert parent field
    insertField(parentFieldDetails, permitReservedWords, false, true);
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) MemberDetails(org.springframework.roo.classpath.scanner.MemberDetails) ReferenceField(org.springframework.roo.classpath.operations.jsr303.ReferenceField) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Aggregations

ReferenceField (org.springframework.roo.classpath.operations.jsr303.ReferenceField)3 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)2 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)2 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)2 File (java.io.File)1 Test (org.junit.Test)1 FieldMetadataBuilder (org.springframework.roo.classpath.details.FieldMetadataBuilder)1 AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)1 SetField (org.springframework.roo.classpath.operations.jsr303.SetField)1 MemberDetails (org.springframework.roo.classpath.scanner.MemberDetails)1 JavaType (org.springframework.roo.model.JavaType)1