Search in sources :

Example 71 with InvocableMemberBodyBuilder

use of org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder in project spring-roo by spring-projects.

the class ServiceImplMetadata method getMethodAddRemoveRel.

private MethodMetadata getMethodAddRemoveRel(MethodMetadata methodToBeImplemented, RelationInfo relationInfo, MethodMetadata operationMethod) {
    InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    // Prepare constants
    final String operation = operationMethod.getMethodName().getSymbolName();
    final JavaSymbolName param0 = methodToBeImplemented.getParameterNames().get(0);
    final JavaType childType = relationInfo.childType;
    final FieldMetadata childServideField = requiredServiceFieldByEntity.get(childType);
    final String parentFieldName = relationInfo.fieldName;
    final JavaSymbolName param1 = methodToBeImplemented.getParameterNames().get(1);
    final JavaType param1TypeWrapped = methodToBeImplemented.getParameterTypes().get(1).getJavaType().getParameters().get(0);
    final String saveMethod = serviceMetadata.getCurrentSaveMethod().getMethodName().getSymbolName();
    String childListVariable;
    if (childType.equals(param1TypeWrapped)) {
        childListVariable = param1.getSymbolName();
    } else {
        // List<{childType}> {parentFieldName} =
        // {childService}.findAll({param1});
        bodyBuilder.appendFormalLine("%s<%s> %s = %s().findAll(%s);", getNameOfJavaType(JavaType.LIST), getNameOfJavaType(childType), parentFieldName, getAccessorMethod(childServideField).getMethodName(), param1);
        childListVariable = parentFieldName;
    }
    // {param0}.{operation}({childListVariable});
    bodyBuilder.appendFormalLine("%s.%s(%s);", param0, operation, childListVariable);
    // return {repoField}.{saveMethod}({param0});
    bodyBuilder.appendFormalLine("return %s().%s(%s);", getAccessorMethod(repositoryFieldMetadata).getMethodName(), saveMethod, param0);
    return getMethod(methodToBeImplemented, true, bodyBuilder);
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) JdkJavaType(org.springframework.roo.model.JdkJavaType) SpringJavaType(org.springframework.roo.model.SpringJavaType) JavaType(org.springframework.roo.model.JavaType) FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder)

Example 72 with InvocableMemberBodyBuilder

use of org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder 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 73 with InvocableMemberBodyBuilder

use of org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder in project spring-roo by spring-projects.

the class JpaEntityFactoryMetadata method getDecimalMinAndDecimalMaxBody.

private String getDecimalMinAndDecimalMaxBody(final FieldMetadata field, final AnnotationMetadata decimalMinAnnotation, final AnnotationMetadata decimalMaxAnnotation, final String suffix) {
    final String fieldName = field.getFieldName().getSymbolName();
    final JavaType fieldType = field.getFieldType();
    final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    if (decimalMinAnnotation != null && decimalMaxAnnotation == null) {
        final String minValue = (String) decimalMinAnnotation.getAttribute(VALUE).getValue();
        if (fieldType.equals(BIG_DECIMAL)) {
            bodyBuilder.appendFormalLine("if (" + fieldName + ".compareTo(new " + BIG_DECIMAL.getSimpleTypeName() + "(\"" + minValue + "\")) == -1) {");
            bodyBuilder.indent();
            bodyBuilder.appendFormalLine(fieldName + " = new " + BIG_DECIMAL.getSimpleTypeName() + "(\"" + minValue + "\");");
        } else {
            bodyBuilder.appendFormalLine("if (" + fieldName + " < " + minValue + suffix + ") {");
            bodyBuilder.indent();
            bodyBuilder.appendFormalLine(fieldName + " = " + minValue + suffix + ";");
        }
        bodyBuilder.indentRemove();
        bodyBuilder.appendFormalLine("}");
    } else if (decimalMinAnnotation == null && decimalMaxAnnotation != null) {
        final String maxValue = (String) decimalMaxAnnotation.getAttribute(VALUE).getValue();
        if (fieldType.equals(BIG_DECIMAL)) {
            bodyBuilder.appendFormalLine("if (" + fieldName + ".compareTo(new " + BIG_DECIMAL.getSimpleTypeName() + "(\"" + maxValue + "\")) == 1) {");
            bodyBuilder.indent();
            bodyBuilder.appendFormalLine(fieldName + " = new " + BIG_DECIMAL.getSimpleTypeName() + "(\"" + maxValue + "\");");
        } else {
            bodyBuilder.appendFormalLine("if (" + fieldName + " > " + maxValue + suffix + ") {");
            bodyBuilder.indent();
            bodyBuilder.appendFormalLine(fieldName + " = " + maxValue + suffix + ";");
        }
        bodyBuilder.indentRemove();
        bodyBuilder.appendFormalLine("}");
    } else if (decimalMinAnnotation != null && decimalMaxAnnotation != null) {
        final String minValue = (String) decimalMinAnnotation.getAttribute(VALUE).getValue();
        final String maxValue = (String) decimalMaxAnnotation.getAttribute(VALUE).getValue();
        Validate.isTrue(Double.parseDouble(maxValue) >= Double.parseDouble(minValue), "The value of @DecimalMax must be greater or equal to the value of @DecimalMin for field %s", fieldName);
        if (fieldType.equals(BIG_DECIMAL)) {
            bodyBuilder.appendFormalLine("if (" + fieldName + ".compareTo(new " + BIG_DECIMAL.getSimpleTypeName() + "(\"" + minValue + "\")) == -1 || " + fieldName + ".compareTo(new " + BIG_DECIMAL.getSimpleTypeName() + "(\"" + maxValue + "\")) == 1) {");
            bodyBuilder.indent();
            bodyBuilder.appendFormalLine(fieldName + " = new " + BIG_DECIMAL.getSimpleTypeName() + "(\"" + maxValue + "\");");
        } else {
            bodyBuilder.appendFormalLine("if (" + fieldName + " < " + minValue + suffix + " || " + fieldName + " > " + maxValue + suffix + ") {");
            bodyBuilder.indent();
            bodyBuilder.appendFormalLine(fieldName + " = " + maxValue + suffix + ";");
        }
        bodyBuilder.indentRemove();
        bodyBuilder.appendFormalLine("}");
    }
    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)

Example 74 with InvocableMemberBodyBuilder

use of org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder in project spring-roo by spring-projects.

the class JpaEntityFactoryMetadata method getEmbeddedClassMutatorMethod.

private MethodMetadataBuilder getEmbeddedClassMutatorMethod(final EmbeddedHolder embeddedHolder) {
    final JavaSymbolName methodName = getEmbeddedFieldMutatorMethodName(embeddedHolder.getEmbeddedField().getFieldName());
    final JavaType[] parameterTypes = { entity, JavaType.INT_PRIMITIVE };
    // Locate user-defined method
    if (governorHasMethod(methodName, parameterTypes)) {
        // Method found in governor so do not create method in ITD
        return null;
    }
    final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    // Create constructor for embedded class
    final JavaType embeddedFieldType = embeddedHolder.getEmbeddedField().getFieldType();
    builder.getImportRegistrationResolver().addImport(embeddedFieldType);
    bodyBuilder.appendFormalLine(embeddedFieldType.getSimpleTypeName() + " embeddedClass = new " + embeddedFieldType.getSimpleTypeName() + "();");
    for (final FieldMetadata field : embeddedHolder.getFields()) {
        bodyBuilder.appendFormalLine(getEmbeddedFieldMutatorMethodName(embeddedHolder.getEmbeddedField().getFieldName(), field.getFieldName()).getSymbolName() + "(embeddedClass, " + INDEX_VAR + ");");
    }
    bodyBuilder.appendFormalLine(OBJ_VAR + "." + embeddedHolder.getEmbeddedMutatorMethodName() + "(embeddedClass);");
    final List<JavaSymbolName> parameterNames = Arrays.asList(OBJ_SYMBOL, INDEX_SYMBOL);
    return new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, JavaType.VOID_PRIMITIVE, AnnotatedJavaType.convertFromJavaTypes(parameterTypes), parameterNames, bodyBuilder);
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) JdkJavaType(org.springframework.roo.model.JdkJavaType) JavaType(org.springframework.roo.model.JavaType) FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder)

Example 75 with InvocableMemberBodyBuilder

use of org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder in project spring-roo by spring-projects.

the class JpaEntityFactoryMetadata method getEmbeddedIdMutatorMethod.

private MethodMetadataBuilder getEmbeddedIdMutatorMethod(final Set<ClassOrInterfaceTypeDetails> dataOnDemandClasses) {
    if (!hasEmbeddedIdentifier()) {
        return null;
    }
    final JavaSymbolName embeddedIdMutator = embeddedIdHolder.getEmbeddedIdMutator();
    final JavaSymbolName methodName = getEmbeddedIdMutatorMethodName();
    final JavaType[] parameterTypes = { entity, JavaType.INT_PRIMITIVE };
    // Locate user-defined method
    if (governorHasMethod(methodName, parameterTypes)) {
        // Method found in governor so do not create method in ITD
        return null;
    }
    final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    // Create constructor for embedded id class
    final JavaType embeddedIdFieldType = embeddedIdHolder.getEmbeddedIdField().getFieldType();
    builder.getImportRegistrationResolver().addImport(embeddedIdFieldType);
    final StringBuilder sb = new StringBuilder();
    final List<FieldMetadata> identifierFields = embeddedIdHolder.getIdFields();
    for (int i = 0, n = identifierFields.size(); i < n; i++) {
        if (i > 0) {
            sb.append(", ");
        }
        final FieldMetadata field = identifierFields.get(i);
        final String fieldName = field.getFieldName().getSymbolName();
        final JavaType fieldType = field.getFieldType();
        builder.getImportRegistrationResolver().addImport(fieldType);
        final String initializer = getFieldInitializer(field, null, dataOnDemandClasses);
        bodyBuilder.append(getFieldValidationBody(field, initializer, null, true));
        sb.append(fieldName);
    }
    bodyBuilder.appendFormalLine("");
    bodyBuilder.appendFormalLine(embeddedIdFieldType.getSimpleTypeName() + " embeddedIdClass = new " + embeddedIdFieldType.getSimpleTypeName() + "(" + sb.toString() + ");");
    bodyBuilder.appendFormalLine(OBJ_VAR + "." + embeddedIdMutator + "(embeddedIdClass);");
    final List<JavaSymbolName> parameterNames = Arrays.asList(OBJ_SYMBOL, INDEX_SYMBOL);
    return new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, JavaType.VOID_PRIMITIVE, AnnotatedJavaType.convertFromJavaTypes(parameterTypes), parameterNames, bodyBuilder);
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) JdkJavaType(org.springframework.roo.model.JdkJavaType) JavaType(org.springframework.roo.model.JavaType) FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) ToStringBuilder(org.apache.commons.lang3.builder.ToStringBuilder) MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder)

Aggregations

InvocableMemberBodyBuilder (org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder)152 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)121 MethodMetadataBuilder (org.springframework.roo.classpath.details.MethodMetadataBuilder)117 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)105 MethodMetadata (org.springframework.roo.classpath.details.MethodMetadata)92 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)90 ArrayList (java.util.ArrayList)85 JavaType (org.springframework.roo.model.JavaType)70 SpringJavaType (org.springframework.roo.model.SpringJavaType)46 JdkJavaType (org.springframework.roo.model.JdkJavaType)44 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)40 SpringletsJavaType (org.springframework.roo.model.SpringletsJavaType)34 Jsr303JavaType (org.springframework.roo.model.Jsr303JavaType)27 CommentStructure (org.springframework.roo.classpath.details.comments.CommentStructure)16 JavadocComment (org.springframework.roo.classpath.details.comments.JavadocComment)16 ServiceMetadata (org.springframework.roo.addon.layers.service.addon.ServiceMetadata)15 ConstructorMetadataBuilder (org.springframework.roo.classpath.details.ConstructorMetadataBuilder)15 RelationInfo (org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata.RelationInfo)14 RelationInfoExtended (org.springframework.roo.addon.web.mvc.controller.addon.RelationInfoExtended)11 JpaJavaType (org.springframework.roo.model.JpaJavaType)9