Search in sources :

Example 16 with JavaType

use of org.springframework.roo.model.JavaType in project spring-roo by spring-projects.

the class JsonControllerTestCreator method createIntegrationTest.

@Override
public void createIntegrationTest(JavaType type, Pom module) {
    Validate.notNull(type, "Class to produce an integration test class for is required");
    // Check if provided JavaType is a JSON Controller
    ClassOrInterfaceTypeDetails cid = typeLocationService.getTypeDetails(type);
    Validate.notNull(cid.getAnnotation(RooJavaType.ROO_CONTROLLER), "Type must be a Roo controller.");
    Validate.notNull(cid.getAnnotation(RooJavaType.ROO_JSON), "Type must be a Roo JSON controller.");
    // Add springlets-boot-starter-test dependency
    projectOperations.addProperty("", SPRINGLETS_VERSION_PROPERTY);
    projectOperations.addDependency(module.getModuleName(), SPRINGLETS_BOOT_STARTER_TEST_DEPENDENCY);
    // Get the controller managed entity
    ControllerAnnotationValues controllerAnnotationValues = new ControllerAnnotationValues(cid);
    JavaType managedEntity = controllerAnnotationValues.getEntity();
    // Workaround to get a JavaType with not null module when recovering it
    // from a ClassAttributeValue
    managedEntity = new JavaType(managedEntity.getFullyQualifiedTypeName(), managedEntity.getArray(), managedEntity.getDataType(), managedEntity.getArgName(), managedEntity.getParameters(), typeLocationService.getTypeDetails(managedEntity).getType().getModule());
    // Create Data On Demand artifacts for managed entity
    List<DataOnDemandCreatorProvider> dodCreators = getValidDataOnDemandCreatorsForType(managedEntity);
    Validate.isTrue(!dodCreators.isEmpty(), "Couldn't find any 'DataOnDemandCreatorProvider' for JSON controllers.");
    Validate.isTrue(dodCreators.size() == 1, "More than 1 valid 'DataOnDemandCreatorProvider' found for JSON controllers. %s can't decide which one to use.", this.getClass().getName());
    DataOnDemandCreatorProvider creator = dodCreators.get(0);
    creator.createDataOnDemand(managedEntity);
    // Add module dependency with test-jar dependency
    if (projectOperations.isMultimoduleProject()) {
        String managedEntityModuleName = managedEntity.getModule();
        Pom managedEntityModule = projectOperations.getPomFromModuleName(managedEntityModuleName);
        projectOperations.addDependency(module.getModuleName(), new Dependency(managedEntityModule.getGroupId(), managedEntityModule.getArtifactId(), "${project.version}", DependencyType.valueOfTypeCode("test-jar"), DependencyScope.TEST), true, true);
    }
    // Create integration test class
    final JavaType name = new JavaType(type + "IT", module.getModuleName());
    final String declaredByMetadataId = PhysicalTypeIdentifier.createIdentifier(name, Path.SRC_TEST_JAVA.getModulePathId(module.getModuleName()));
    if (metadataService.get(declaredByMetadataId) != null) {
        // The file already exists
        return;
    }
    // Add @RooJsonControllerIntegrationTest to source file
    AnnotationMetadataBuilder rooIntegrationTestAnnotation = new AnnotationMetadataBuilder(RooJavaType.ROO_JSON_CONTROLLER_INTEGRATION_TEST);
    rooIntegrationTestAnnotation.addClassAttribute("targetClass", type);
    // Create integration test class
    final ClassOrInterfaceTypeDetailsBuilder cidBuilder = new ClassOrInterfaceTypeDetailsBuilder(declaredByMetadataId, Modifier.PUBLIC, name, PhysicalTypeCategory.CLASS);
    cidBuilder.addAnnotation(rooIntegrationTestAnnotation);
    // Write changes to disk
    typeManagementService.createOrUpdateTypeOnDisk(cidBuilder.build());
}
Also used : RooJavaType(org.springframework.roo.model.RooJavaType) JavaType(org.springframework.roo.model.JavaType) ControllerAnnotationValues(org.springframework.roo.addon.web.mvc.controller.addon.ControllerAnnotationValues) ClassOrInterfaceTypeDetailsBuilder(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) Dependency(org.springframework.roo.project.Dependency) DataOnDemandCreatorProvider(org.springframework.roo.addon.test.providers.DataOnDemandCreatorProvider) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder) Pom(org.springframework.roo.project.maven.Pom)

Example 17 with JavaType

use of org.springframework.roo.model.JavaType in project spring-roo by spring-projects.

the class EmbeddableFieldCreatorProvider method createNumericField.

@Override
public void createNumericField(ClassOrInterfaceTypeDetails javaTypeDetails, JavaType fieldType, boolean primitive, Set<String> legalNumericPrimitives, JavaSymbolName fieldName, boolean notNull, boolean nullRequired, String decimalMin, String decimalMax, Integer digitsInteger, Integer digitsFraction, Long min, Long max, String column, String comment, boolean unique, String value, boolean permitReservedWords, boolean transientModifier, List<AnnotationMetadataBuilder> extraAnnotations) {
    final String physicalTypeIdentifier = javaTypeDetails.getDeclaredByMetadataId();
    if (primitive && legalNumericPrimitives.contains(fieldType.getFullyQualifiedTypeName())) {
        fieldType = new JavaType(fieldType.getFullyQualifiedTypeName(), 0, DataType.PRIMITIVE, null, null);
    }
    final NumericField fieldDetails = new NumericField(physicalTypeIdentifier, fieldType, fieldName);
    fieldDetails.setNotNull(notNull);
    fieldDetails.setNullRequired(nullRequired);
    if (decimalMin != null) {
        fieldDetails.setDecimalMin(decimalMin);
    }
    if (decimalMax != null) {
        fieldDetails.setDecimalMax(decimalMax);
    }
    if (digitsInteger != null) {
        fieldDetails.setDigitsInteger(digitsInteger);
    }
    if (digitsFraction != null) {
        fieldDetails.setDigitsFraction(digitsFraction);
    }
    if (min != null) {
        fieldDetails.setMin(min);
    }
    if (max != null) {
        fieldDetails.setMax(max);
    }
    if (column != null) {
        fieldDetails.setColumn(column);
    }
    if (comment != null) {
        fieldDetails.setComment(comment);
    }
    if (unique) {
        fieldDetails.setUnique(true);
    }
    if (value != null) {
        fieldDetails.setValue(value);
    }
    if (extraAnnotations != null && !extraAnnotations.isEmpty()) {
        fieldDetails.addAnnotations(extraAnnotations);
    }
    Validate.isTrue(fieldDetails.isDigitsSetCorrectly(), "Must specify both --digitsInteger and --digitsFraction for @Digits to be added");
    insertField(fieldDetails, permitReservedWords, transientModifier);
}
Also used : JdkJavaType(org.springframework.roo.model.JdkJavaType) RooJavaType(org.springframework.roo.model.RooJavaType) JpaJavaType(org.springframework.roo.model.JpaJavaType) JavaType(org.springframework.roo.model.JavaType) NumericField(org.springframework.roo.classpath.operations.jsr303.NumericField)

Example 18 with JavaType

use of org.springframework.roo.model.JavaType 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 19 with JavaType

use of org.springframework.roo.model.JavaType in project spring-roo by spring-projects.

the class JpaFieldCreatorProvider method createNumericField.

@Override
public void createNumericField(ClassOrInterfaceTypeDetails javaTypeDetails, JavaType fieldType, boolean primitive, Set<String> legalNumericPrimitives, JavaSymbolName fieldName, boolean notNull, boolean nullRequired, String decimalMin, String decimalMax, Integer digitsInteger, Integer digitsFraction, Long min, Long max, String column, String comment, boolean unique, String value, boolean permitReservedWords, boolean transientModifier, List<AnnotationMetadataBuilder> extraAnnotations) {
    final String physicalTypeIdentifier = javaTypeDetails.getDeclaredByMetadataId();
    if (primitive && legalNumericPrimitives.contains(fieldType.getFullyQualifiedTypeName())) {
        fieldType = new JavaType(fieldType.getFullyQualifiedTypeName(), 0, DataType.PRIMITIVE, null, null);
    }
    final NumericField fieldDetails = new NumericField(physicalTypeIdentifier, fieldType, fieldName);
    fieldDetails.setNotNull(notNull);
    fieldDetails.setNullRequired(nullRequired);
    if (decimalMin != null) {
        fieldDetails.setDecimalMin(decimalMin);
    }
    if (decimalMax != null) {
        fieldDetails.setDecimalMax(decimalMax);
    }
    if (digitsInteger != null) {
        fieldDetails.setDigitsInteger(digitsInteger);
    }
    if (digitsFraction != null) {
        fieldDetails.setDigitsFraction(digitsFraction);
    }
    if (min != null) {
        fieldDetails.setMin(min);
    }
    if (max != null) {
        fieldDetails.setMax(max);
    }
    if (column != null) {
        fieldDetails.setColumn(column);
    }
    if (comment != null) {
        fieldDetails.setComment(comment);
    }
    if (unique) {
        fieldDetails.setUnique(true);
    }
    if (value != null) {
        fieldDetails.setValue(value);
    }
    if (extraAnnotations != null && !extraAnnotations.isEmpty()) {
        fieldDetails.addAnnotations(extraAnnotations);
    }
    Validate.isTrue(fieldDetails.isDigitsSetCorrectly(), "Must specify both --digitsInteger and --digitsFraction for @Digits to be added");
    insertField(fieldDetails, permitReservedWords, transientModifier);
}
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) NumericField(org.springframework.roo.classpath.operations.jsr303.NumericField)

Example 20 with JavaType

use of org.springframework.roo.model.JavaType 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)

Aggregations

JavaType (org.springframework.roo.model.JavaType)411 RooJavaType (org.springframework.roo.model.RooJavaType)212 ArrayList (java.util.ArrayList)142 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)133 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)129 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)114 JdkJavaType (org.springframework.roo.model.JdkJavaType)110 SpringJavaType (org.springframework.roo.model.SpringJavaType)101 JpaJavaType (org.springframework.roo.model.JpaJavaType)83 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)78 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)76 MethodMetadata (org.springframework.roo.classpath.details.MethodMetadata)72 InvocableMemberBodyBuilder (org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder)70 MethodMetadataBuilder (org.springframework.roo.classpath.details.MethodMetadataBuilder)65 LogicalPath (org.springframework.roo.project.LogicalPath)62 SpringletsJavaType (org.springframework.roo.model.SpringletsJavaType)60 AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)59 Jsr303JavaType (org.springframework.roo.model.Jsr303JavaType)38 MemberDetails (org.springframework.roo.classpath.scanner.MemberDetails)35 ClassOrInterfaceTypeDetailsBuilder (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder)30