Search in sources :

Example 36 with AnnotationMetadata

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

the class JpaEntityFactoryMetadata method getFieldValidationBody.

private String getFieldValidationBody(final FieldMetadata field, final String initializer, final JavaSymbolName mutatorName, final boolean isFieldOfEmbeddableType) {
    final String fieldName = field.getFieldName().getSymbolName();
    final JavaType fieldType = field.getFieldType();
    String suffix = "";
    if (fieldType.equals(JavaType.LONG_OBJECT) || fieldType.equals(JavaType.LONG_PRIMITIVE)) {
        suffix = "L";
    } else if (fieldType.equals(JavaType.FLOAT_OBJECT) || fieldType.equals(JavaType.FLOAT_PRIMITIVE)) {
        suffix = "F";
    } else if (fieldType.equals(JavaType.DOUBLE_OBJECT) || fieldType.equals(JavaType.DOUBLE_PRIMITIVE)) {
        suffix = "D";
    }
    final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    bodyBuilder.appendFormalLine(getTypeStr(fieldType) + " " + fieldName + " = " + initializer + ";");
    if (fieldType.equals(JavaType.STRING)) {
        boolean isUnique = isFieldOfEmbeddableType;
        @SuppressWarnings("unchecked") final Map<String, Object> values = (Map<String, Object>) field.getCustomData().get(CustomDataKeys.COLUMN_FIELD);
        if (!isUnique && values != null && values.containsKey("unique")) {
            isUnique = (Boolean) values.get("unique");
        }
        // Check for @Size or @Column with length attribute
        final AnnotationMetadata sizeAnnotation = MemberFindingUtils.getAnnotationOfType(field.getAnnotations(), SIZE);
        if (sizeAnnotation != null && sizeAnnotation.getAttribute(MAX_SYMBOL) != null) {
            final Integer maxValue = (Integer) sizeAnnotation.getAttribute(MAX_SYMBOL).getValue();
            bodyBuilder.appendFormalLine("if (" + fieldName + ".length() > " + maxValue + ") {");
            bodyBuilder.indent();
            if (isUnique) {
                bodyBuilder.appendFormalLine(fieldName + " = new Random().nextInt(10) + " + fieldName + ".substring(1, " + maxValue + ");");
            } else {
                bodyBuilder.appendFormalLine(fieldName + " = " + fieldName + ".substring(0, " + maxValue + ");");
            }
            bodyBuilder.indentRemove();
            bodyBuilder.appendFormalLine("}");
        } else if (sizeAnnotation == null && values != null) {
            if (values.containsKey("length")) {
                final Integer lengthValue = (Integer) values.get("length");
                bodyBuilder.appendFormalLine("if (" + fieldName + ".length() > " + lengthValue + ") {");
                bodyBuilder.indent();
                if (isUnique) {
                    bodyBuilder.appendFormalLine(fieldName + " = new Random().nextInt(10) + " + fieldName + ".substring(1, " + lengthValue + ");");
                } else {
                    bodyBuilder.appendFormalLine(fieldName + " = " + fieldName + ".substring(0, " + lengthValue + ");");
                }
                bodyBuilder.indentRemove();
                bodyBuilder.appendFormalLine("}");
            }
        }
    } else if (JdkJavaType.isDecimalType(fieldType)) {
        // Check for @Digits, @DecimalMax, @DecimalMin
        final AnnotationMetadata digitsAnnotation = MemberFindingUtils.getAnnotationOfType(field.getAnnotations(), DIGITS);
        final AnnotationMetadata decimalMinAnnotation = MemberFindingUtils.getAnnotationOfType(field.getAnnotations(), DECIMAL_MIN);
        final AnnotationMetadata decimalMaxAnnotation = MemberFindingUtils.getAnnotationOfType(field.getAnnotations(), DECIMAL_MAX);
        if (digitsAnnotation != null) {
            bodyBuilder.append(getDigitsBody(field, digitsAnnotation, suffix));
        } else if (decimalMinAnnotation != null || decimalMaxAnnotation != null) {
            bodyBuilder.append(getDecimalMinAndDecimalMaxBody(field, decimalMinAnnotation, decimalMaxAnnotation, suffix));
        } else if (field.getCustomData().keySet().contains(CustomDataKeys.COLUMN_FIELD)) {
            @SuppressWarnings("unchecked") final Map<String, Object> values = (Map<String, Object>) field.getCustomData().get(CustomDataKeys.COLUMN_FIELD);
            bodyBuilder.append(getColumnPrecisionAndScaleBody(field, values, suffix));
        }
    } else if (JdkJavaType.isIntegerType(fieldType)) {
        // Check for @Min and @Max
        bodyBuilder.append(getMinAndMaxBody(field, suffix));
    }
    if (mutatorName != null) {
        bodyBuilder.appendFormalLine(OBJ_VAR + "." + mutatorName.getSymbolName() + "(" + fieldName + ");");
    }
    return bodyBuilder.getOutput();
}
Also used : AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) JdkJavaType(org.springframework.roo.model.JdkJavaType) JavaType(org.springframework.roo.model.JavaType) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata)

Example 37 with AnnotationMetadata

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

the class JpaEntityFactoryMetadataProviderImpl method getEntityFactoryMetadataId.

private String getEntityFactoryMetadataId(final JavaType javaType, final Iterable<ClassOrInterfaceTypeDetails> entityFactoryTypes) {
    for (final ClassOrInterfaceTypeDetails cid : entityFactoryTypes) {
        final AnnotationMetadata entityFactoryAnnotation = cid.getAnnotation(ROO_JPA_ENTITY_FACTORY);
        final AnnotationAttributeValue<JavaType> entityAttribute = entityFactoryAnnotation.getAttribute("entity");
        if (entityAttribute != null && entityAttribute.getValue().equals(javaType)) {
            // Found the DoD type for the given field's type
            return JpaDataOnDemandMetadata.createIdentifier(cid.getName(), PhysicalTypeIdentifier.getPath(cid.getDeclaredByMetadataId()));
        }
    }
    return null;
}
Also used : RooJavaType(org.springframework.roo.model.RooJavaType) JavaType(org.springframework.roo.model.JavaType) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata)

Example 38 with AnnotationMetadata

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

the class DbreDatabaseListenerImpl method updateOrDeleteManagedEntity.

private Table updateOrDeleteManagedEntity(final ClassOrInterfaceTypeDetails managedEntity, final Database database) {
    // Update the attributes of the existing JPA-related annotation
    final AnnotationMetadata jpaAnnotation = getJpaAnnotation(managedEntity);
    Validate.validState(jpaAnnotation != null, "@%s not found on existing DBRE-managed entity %s", ROO_JPA_ENTITY.getSimpleTypeName(), managedEntity.getName().getFullyQualifiedTypeName());
    // Find table in database using 'table' and 'schema' attributes from the
    // JPA annotation
    final AnnotationAttributeValue<?> tableAttribute = jpaAnnotation.getAttribute(new JavaSymbolName("table"));
    final String errMsg = "Unable to maintain database-managed entity " + managedEntity.getName().getFullyQualifiedTypeName() + " because its associated table could not be found";
    Validate.notNull(tableAttribute, errMsg);
    final String tableName = (String) tableAttribute.getValue();
    Validate.notBlank(tableName, errMsg);
    final AnnotationAttributeValue<?> schemaAttribute = jpaAnnotation.getAttribute(new JavaSymbolName("schema"));
    final String schemaName = schemaAttribute != null ? (String) schemaAttribute.getValue() : null;
    final Table table = database.getTable(tableName, schemaName);
    if (table == null) {
        // Table is missing and probably has been dropped so delete managed
        // type and its identifier if applicable
        deleteManagedType(managedEntity, "no database table called '" + tableName + "'");
        return null;
    }
    table.setIncludeNonPortableAttributes(database.isIncludeNonPortableAttributes());
    table.setDisableVersionFields(database.isDisableVersionFields());
    table.setDisableGeneratedIdentifiers(database.isDisableGeneratedIdentifiers());
    // Update the @RooJpaEntity attributes
    final AnnotationMetadataBuilder jpaAnnotationBuilder = new AnnotationMetadataBuilder(jpaAnnotation);
    final Set<JavaSymbolName> attributesToDeleteIfPresent = new LinkedHashSet<JavaSymbolName>();
    manageIdentifier(managedEntity.getName(), jpaAnnotationBuilder, attributesToDeleteIfPresent, table);
    // Manage versionField attribute
    final AnnotationAttributeValue<?> versionFieldAttribute = jpaAnnotation.getAttribute(new JavaSymbolName(VERSION_FIELD));
    if (versionFieldAttribute == null) {
        if (hasVersionField(table)) {
            attributesToDeleteIfPresent.add(new JavaSymbolName(VERSION_FIELD));
        } else {
            jpaAnnotationBuilder.addStringAttribute(VERSION_FIELD, "");
        }
    } else {
        final String versionFieldValue = (String) versionFieldAttribute.getValue();
        if (hasVersionField(table) && (StringUtils.isBlank(versionFieldValue) || VERSION.equals(versionFieldValue))) {
            attributesToDeleteIfPresent.add(new JavaSymbolName(VERSION_FIELD));
        }
    }
    final AnnotationAttributeValue<?> sequenceNameFieldAttribute = jpaAnnotation.getAttribute(new JavaSymbolName(SEQUENCE_NAME_FIELD));
    if (sequenceNameFieldAttribute == null) {
        if (!table.isDisableGeneratedIdentifiers()) {
            attributesToDeleteIfPresent.add(new JavaSymbolName(SEQUENCE_NAME_FIELD));
        } else {
            jpaAnnotationBuilder.addStringAttribute(SEQUENCE_NAME_FIELD, "");
        }
    } else {
        final String sequenceNameFieldValue = (String) sequenceNameFieldAttribute.getValue();
        if (!table.isDisableGeneratedIdentifiers() && ("".equals(sequenceNameFieldValue))) {
            attributesToDeleteIfPresent.add(new JavaSymbolName(SEQUENCE_NAME_FIELD));
        }
    }
    // Update the annotation on disk
    final ClassOrInterfaceTypeDetailsBuilder cidBuilder = new ClassOrInterfaceTypeDetailsBuilder(managedEntity);
    cidBuilder.updateTypeAnnotation(jpaAnnotationBuilder.build(), attributesToDeleteIfPresent);
    getTypeManagementService().createOrUpdateTypeOnDisk(cidBuilder.build());
    return table;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) Table(org.springframework.roo.addon.dbre.addon.model.Table) ClassOrInterfaceTypeDetailsBuilder(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Example 39 with AnnotationMetadata

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

the class DbreDatabaseListenerImpl method getIdentifierType.

/**
 * Returns the type of ID that DBRE should use for the given entity
 *
 * @param entity the entity for which to get the ID type (required)
 * @return a non-<code>null</code> ID type
 */
private JavaType getIdentifierType(final JavaType entity) {
    final PhysicalTypeMetadata governorPhysicalTypeMetadata = getPhysicalTypeMetadata(entity);
    if (governorPhysicalTypeMetadata != null) {
        final ClassOrInterfaceTypeDetails governorTypeDetails = governorPhysicalTypeMetadata.getMemberHoldingTypeDetails();
        final AnnotationMetadata jpaAnnotation = getJpaAnnotation(governorTypeDetails);
        if (jpaAnnotation != null) {
            final AnnotationAttributeValue<?> identifierTypeAttribute = jpaAnnotation.getAttribute(new JavaSymbolName(IDENTIFIER_TYPE));
            if (identifierTypeAttribute != null) {
                // The identifierType attribute exists, so get its value
                final JavaType identifierType = (JavaType) identifierTypeAttribute.getValue();
                if (identifierType != null && !JdkJavaType.isPartOfJavaLang(identifierType)) {
                    return identifierType;
                }
            }
        }
    }
    // not a simple type, so return a default
    return new JavaType(entity.getFullyQualifiedTypeName() + PRIMARY_KEY_SUFFIX);
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) JdkJavaType(org.springframework.roo.model.JdkJavaType) JavaType(org.springframework.roo.model.JavaType) PhysicalTypeMetadata(org.springframework.roo.classpath.PhysicalTypeMetadata) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata)

Example 40 with AnnotationMetadata

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

the class AbstractIdentifiableAnnotatedJavaStructureProvider method getTypeAnnotation.

public AnnotationMetadata getTypeAnnotation(final JavaType annotationType) {
    Validate.notNull(annotationType, "Annotation type required");
    IdentifiableAnnotatedJavaStructure current = this;
    while (current != null) {
        final AnnotationMetadata result = current.getAnnotation(annotationType);
        if (result != null) {
            return result;
        }
        if (current instanceof ClassOrInterfaceTypeDetails) {
            current = ((ClassOrInterfaceTypeDetails) current).getSuperclass();
        } else {
            current = null;
        }
    }
    return null;
}
Also used : AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata)

Aggregations

AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)109 JavaType (org.springframework.roo.model.JavaType)59 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)52 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)47 ArrayList (java.util.ArrayList)40 RooJavaType (org.springframework.roo.model.RooJavaType)34 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)30 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)24 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)21 MemberDetails (org.springframework.roo.classpath.scanner.MemberDetails)20 List (java.util.List)19 ClassOrInterfaceTypeDetailsBuilder (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder)19 MethodMetadata (org.springframework.roo.classpath.details.MethodMetadata)18 AnnotationAttributeValue (org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue)17 NestedAnnotationAttributeValue (org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue)16 SpringJavaType (org.springframework.roo.model.SpringJavaType)15 JdkJavaType (org.springframework.roo.model.JdkJavaType)12 JpaJavaType (org.springframework.roo.model.JpaJavaType)12 ArrayAttributeValue (org.springframework.roo.classpath.details.annotations.ArrayAttributeValue)11 LinkedHashMap (java.util.LinkedHashMap)10