Search in sources :

Example 11 with AnnotationMetadataBuilder

use of org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder 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 12 with AnnotationMetadataBuilder

use of org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder in project spring-roo by spring-projects.

the class IdentifierMetadata method setDateAnnotations.

private void setDateAnnotations(final String columnDefinition, final List<AnnotationMetadataBuilder> annotations) {
    // Add JSR 220 @Temporal annotation to date fields
    String temporalType = StringUtils.defaultIfEmpty(StringUtils.upperCase(columnDefinition), "DATE");
    if ("DATETIME".equals(temporalType)) {
        // ROO-2606
        temporalType = "TIMESTAMP";
    }
    final AnnotationMetadataBuilder temporalBuilder = new AnnotationMetadataBuilder(TEMPORAL);
    temporalBuilder.addEnumAttribute("value", new EnumDetails(TEMPORAL_TYPE, new JavaSymbolName(temporalType)));
    annotations.add(temporalBuilder);
    final AnnotationMetadataBuilder dateTimeFormatBuilder = new AnnotationMetadataBuilder(DATE_TIME_FORMAT);
    dateTimeFormatBuilder.addStringAttribute("style", "M-");
    annotations.add(dateTimeFormatBuilder);
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) EnumDetails(org.springframework.roo.model.EnumDetails) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Example 13 with AnnotationMetadataBuilder

use of org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder in project spring-roo by spring-projects.

the class IdentifierMetadata method getColumnBuilder.

private AnnotationMetadataBuilder getColumnBuilder(final Identifier identifier) {
    final AnnotationMetadataBuilder columnBuilder = new AnnotationMetadataBuilder(COLUMN);
    columnBuilder.addStringAttribute("name", identifier.getColumnName());
    if (StringUtils.isNotBlank(identifier.getColumnDefinition())) {
        columnBuilder.addStringAttribute("columnDefinition", identifier.getColumnDefinition());
    }
    columnBuilder.addBooleanAttribute("nullable", false);
    // Add length attribute for Strings
    if (identifier.getColumnSize() < 4000 && identifier.getFieldType().equals(JavaType.STRING)) {
        columnBuilder.addIntegerAttribute("length", identifier.getColumnSize());
    }
    // Add precision and scale attributes for numeric fields
    if (identifier.getScale() > 0 && (identifier.getFieldType().equals(JavaType.DOUBLE_OBJECT) || identifier.getFieldType().equals(JavaType.DOUBLE_PRIMITIVE) || identifier.getFieldType().equals(BIG_DECIMAL))) {
        columnBuilder.addIntegerAttribute("precision", identifier.getColumnSize());
        columnBuilder.addIntegerAttribute("scale", identifier.getScale());
    }
    return columnBuilder;
}
Also used : AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Example 14 with AnnotationMetadataBuilder

use of org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder in project spring-roo by spring-projects.

the class IdentifierMetadata method getFieldBuilders.

/**
 * Locates declared fields.
 * <p>
 * If no parent is defined, one will be located or created. All declared
 * fields will be returned.
 *
 * @return fields (never returns null)
 */
private List<FieldMetadataBuilder> getFieldBuilders() {
    // Locate all declared fields
    final List<? extends FieldMetadata> declaredFields = governorTypeDetails.getDeclaredFields();
    // Add fields to ITD from annotation
    final List<FieldMetadata> fields = new ArrayList<FieldMetadata>();
    if (identifierServiceResult != null) {
        for (final Identifier identifier : identifierServiceResult) {
            final List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
            annotations.add(getColumnBuilder(identifier));
            if (identifier.getFieldType().equals(DATE)) {
                setDateAnnotations(identifier.getColumnDefinition(), annotations);
            }
            final FieldMetadata idField = new FieldMetadataBuilder(getId(), Modifier.PRIVATE, annotations, identifier.getFieldName(), identifier.getFieldType()).build();
            // Only add field to ITD if not declared on governor
            if (!hasField(declaredFields, idField)) {
                fields.add(idField);
            }
        }
    }
    fields.addAll(declaredFields);
    // Remove fields with static and transient modifiers
    for (final Iterator<FieldMetadata> iter = fields.iterator(); iter.hasNext(); ) {
        final FieldMetadata field = iter.next();
        if (Modifier.isStatic(field.getModifier()) || Modifier.isTransient(field.getModifier())) {
            iter.remove();
        }
    }
    // Remove fields with the @Transient annotation
    final List<FieldMetadata> transientAnnotatedFields = governorTypeDetails.getFieldsWithAnnotation(TRANSIENT);
    if (fields.containsAll(transientAnnotatedFields)) {
        fields.removeAll(transientAnnotatedFields);
    }
    final List<FieldMetadataBuilder> fieldBuilders = new ArrayList<FieldMetadataBuilder>();
    if (!fields.isEmpty()) {
        for (final FieldMetadata field : fields) {
            fieldBuilders.add(new FieldMetadataBuilder(field));
        }
        return fieldBuilders;
    }
    // We need to create a default identifier field
    final List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
    // Compute the column name, as required
    final AnnotationMetadataBuilder columnBuilder = new AnnotationMetadataBuilder(COLUMN);
    columnBuilder.addStringAttribute("name", "id");
    columnBuilder.addBooleanAttribute("nullable", false);
    annotations.add(columnBuilder);
    fieldBuilders.add(new FieldMetadataBuilder(getId(), Modifier.PRIVATE, annotations, new JavaSymbolName("id"), LONG_OBJECT));
    return fieldBuilders;
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) RooIdentifier(org.springframework.roo.addon.jpa.annotations.identifier.RooIdentifier) ArrayList(java.util.ArrayList) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder) FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder)

Example 15 with AnnotationMetadataBuilder

use of org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder in project spring-roo by spring-projects.

the class JpaTestCreator method createUnitTest.

@Override
public void createUnitTest(final JavaType entity) {
    Validate.notNull(entity, "Class to produce an unit test class for is required");
    // Check if provided JavaType is a Repository
    ClassOrInterfaceTypeDetails cid = typeLocationService.getTypeDetails(entity);
    Validate.notNull(cid.getAnnotation(RooJavaType.ROO_JPA_ENTITY), "Type must be a Roo JPA Entity type.");
    // Create JPA DataOnDemand artifacts
    List<DataOnDemandCreatorProvider> dodCreators = getValidDataOnDemandCreatorsForType(entity);
    for (DataOnDemandCreatorProvider dodCreator : dodCreators) {
        dodCreator.createDataOnDemand(entity);
    }
    final JavaType name = new JavaType(entity + "Test", entity.getModule());
    final String declaredByMetadataId = PhysicalTypeIdentifier.createIdentifier(name, Path.SRC_TEST_JAVA.getModulePathId(entity.getModule()));
    if (metadataService.get(declaredByMetadataId) != null) {
        // The file already exists
        return;
    }
    // Add @RooUnitTest to source file
    AnnotationMetadataBuilder rooUnitTestAnnotation = new AnnotationMetadataBuilder(RooJavaType.ROO_JPA_UNIT_TEST);
    rooUnitTestAnnotation.addClassAttribute("targetClass", entity);
    final ClassOrInterfaceTypeDetailsBuilder cidBuilder = new ClassOrInterfaceTypeDetailsBuilder(declaredByMetadataId, Modifier.PUBLIC, name, PhysicalTypeCategory.CLASS);
    cidBuilder.addAnnotation(rooUnitTestAnnotation);
    typeManagementService.createOrUpdateTypeOnDisk(cidBuilder.build());
}
Also used : RooJavaType(org.springframework.roo.model.RooJavaType) JavaType(org.springframework.roo.model.JavaType) ClassOrInterfaceTypeDetailsBuilder(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) DataOnDemandCreatorProvider(org.springframework.roo.addon.test.providers.DataOnDemandCreatorProvider) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Aggregations

AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)232 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)155 ArrayList (java.util.ArrayList)129 InvocableMemberBodyBuilder (org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder)90 MethodMetadataBuilder (org.springframework.roo.classpath.details.MethodMetadataBuilder)87 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)81 JavaType (org.springframework.roo.model.JavaType)78 MethodMetadata (org.springframework.roo.classpath.details.MethodMetadata)73 ClassOrInterfaceTypeDetailsBuilder (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder)54 SpringJavaType (org.springframework.roo.model.SpringJavaType)47 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)42 FieldMetadataBuilder (org.springframework.roo.classpath.details.FieldMetadataBuilder)40 JdkJavaType (org.springframework.roo.model.JdkJavaType)39 AnnotationAttributeValue (org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue)31 AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)30 RooJavaType (org.springframework.roo.model.RooJavaType)28 SpringletsJavaType (org.springframework.roo.model.SpringletsJavaType)28 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)26 Jsr303JavaType (org.springframework.roo.model.Jsr303JavaType)25 StringAttributeValue (org.springframework.roo.classpath.details.annotations.StringAttributeValue)23