Search in sources :

Example 16 with AnnotationAttributeValue

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

the class ReferenceField method decorateAnnotationsList.

@Override
public void decorateAnnotationsList(final List<AnnotationMetadataBuilder> annotations) {
    super.decorateAnnotationsList(annotations);
    final List<AnnotationAttributeValue<?>> attributes = new ArrayList<AnnotationAttributeValue<?>>();
    // Add cascade if option exists
    if (cascadeType != null) {
        List<EnumAttributeValue> cascadeValues = new ArrayList<EnumAttributeValue>();
        for (Cascade type : cascadeType) {
            cascadeValues.add(new EnumAttributeValue(new JavaSymbolName("cascade"), new EnumDetails(CASCADE_TYPE, new JavaSymbolName(type.name()))));
        }
        attributes.add(new ArrayAttributeValue<EnumAttributeValue>(new JavaSymbolName("cascade"), cascadeValues));
    }
    // Add orphanRemoval if option exists
    if (getOrphanRemoval() != null) {
        attributes.add(new BooleanAttributeValue(new JavaSymbolName("orphanRemoval"), getOrphanRemoval().booleanValue()));
    }
    if (fetch != null) {
        JavaSymbolName value = new JavaSymbolName("EAGER");
        if (fetch == Fetch.LAZY) {
            value = new JavaSymbolName("LAZY");
        }
        attributes.add(new EnumAttributeValue(new JavaSymbolName("fetch"), new EnumDetails(FETCH_TYPE, value)));
    }
    if (mappedBy != null) {
        attributes.add(new StringAttributeValue(new JavaSymbolName("mappedBy"), mappedBy.getSymbolName()));
    }
    switch(cardinality) {
        case ONE_TO_MANY:
            annotations.add(new AnnotationMetadataBuilder(ONE_TO_MANY, attributes));
            break;
        case MANY_TO_MANY:
            annotations.add(new AnnotationMetadataBuilder(MANY_TO_MANY, attributes));
            break;
        case ONE_TO_ONE:
            annotations.add(new AnnotationMetadataBuilder(ONE_TO_ONE, attributes));
            break;
        default:
            annotations.add(new AnnotationMetadataBuilder(MANY_TO_ONE, attributes));
            break;
    }
    // Add additional annotations (if any)
    if (additionaAnnotations != null) {
        annotations.addAll(additionaAnnotations);
    }
}
Also used : AnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue) NestedAnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) BooleanAttributeValue(org.springframework.roo.classpath.details.annotations.BooleanAttributeValue) ArrayList(java.util.ArrayList) EnumAttributeValue(org.springframework.roo.classpath.details.annotations.EnumAttributeValue) EnumDetails(org.springframework.roo.model.EnumDetails) StringAttributeValue(org.springframework.roo.classpath.details.annotations.StringAttributeValue) Cascade(org.springframework.roo.classpath.operations.Cascade) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Example 17 with AnnotationAttributeValue

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

the class StringField method decorateAnnotationsList.

@Override
public void decorateAnnotationsList(final List<AnnotationMetadataBuilder> annotations) {
    super.decorateAnnotationsList(annotations);
    if (sizeMin != null || sizeMax != null) {
        final List<AnnotationAttributeValue<?>> attrs = new ArrayList<AnnotationAttributeValue<?>>();
        if (sizeMin != null) {
            attrs.add(new IntegerAttributeValue(new JavaSymbolName("min"), sizeMin));
        }
        if (sizeMax != null) {
            attrs.add(new IntegerAttributeValue(new JavaSymbolName("max"), sizeMax));
        }
        annotations.add(new AnnotationMetadataBuilder(SIZE, attrs));
    }
    if (regexp != null) {
        final List<AnnotationAttributeValue<?>> attrs = new ArrayList<AnnotationAttributeValue<?>>();
        attrs.add(new StringAttributeValue(new JavaSymbolName("regexp"), regexp));
        annotations.add(new AnnotationMetadataBuilder(PATTERN, attrs));
    }
}
Also used : AnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) ArrayList(java.util.ArrayList) IntegerAttributeValue(org.springframework.roo.classpath.details.annotations.IntegerAttributeValue) StringAttributeValue(org.springframework.roo.classpath.details.annotations.StringAttributeValue) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Example 18 with AnnotationAttributeValue

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

the class UploadedFileField method decorateAnnotationsList.

@Override
public void decorateAnnotationsList(final List<AnnotationMetadataBuilder> annotations) {
    super.decorateAnnotationsList(annotations);
    final List<AnnotationAttributeValue<?>> attrs = new ArrayList<AnnotationAttributeValue<?>>();
    attrs.add(new StringAttributeValue(new JavaSymbolName("contentType"), contentType.getContentType()));
    if (autoUpload) {
        attrs.add(new BooleanAttributeValue(new JavaSymbolName("autoUpload"), autoUpload));
    }
    annotations.add(new AnnotationMetadataBuilder(ROO_UPLOADED_FILE, attrs));
    annotations.add(new AnnotationMetadataBuilder(LOB));
}
Also used : AnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) BooleanAttributeValue(org.springframework.roo.classpath.details.annotations.BooleanAttributeValue) ArrayList(java.util.ArrayList) StringAttributeValue(org.springframework.roo.classpath.details.annotations.StringAttributeValue) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Example 19 with AnnotationAttributeValue

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

the class EnumField method decorateAnnotationsList.

@Override
public void decorateAnnotationsList(final List<AnnotationMetadataBuilder> annotations) {
    super.decorateAnnotationsList(annotations);
    final List<AnnotationAttributeValue<?>> attributes = new ArrayList<AnnotationAttributeValue<?>>();
    if (enumType != null) {
        JavaSymbolName value = new JavaSymbolName("ORDINAL");
        if (enumType == EnumType.STRING) {
            value = new JavaSymbolName("STRING");
        }
        attributes.add(new EnumAttributeValue(new JavaSymbolName("value"), new EnumDetails(ENUM_TYPE, value)));
    }
    annotations.add(new AnnotationMetadataBuilder(ENUMERATED, attributes));
}
Also used : AnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) ArrayList(java.util.ArrayList) EnumAttributeValue(org.springframework.roo.classpath.details.annotations.EnumAttributeValue) EnumDetails(org.springframework.roo.model.EnumDetails) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Example 20 with AnnotationAttributeValue

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

the class JpaEntityFactoryMetadata method getFieldInitializer.

private String getFieldInitializer(final FieldMetadata field, final JpaEntityFactoryMetadata collaboratingMetadata, final Set<ClassOrInterfaceTypeDetails> dataOnDemandClasses) {
    final JavaType fieldType = field.getFieldType();
    final String fieldName = field.getFieldName().getSymbolName();
    String initializer = "null";
    final String fieldInitializer = field.getFieldInitializer();
    final Set<Object> fieldCustomDataKeys = field.getCustomData().keySet();
    // Date fields included for DataNucleus (
    if (fieldType.equals(DATE)) {
        if (MemberFindingUtils.getAnnotationOfType(field.getAnnotations(), PAST) != null) {
            builder.getImportRegistrationResolver().addImport(DATE);
            initializer = "new Date(new Date().getTime() - 10000000L)";
        } else if (MemberFindingUtils.getAnnotationOfType(field.getAnnotations(), FUTURE) != null) {
            builder.getImportRegistrationResolver().addImport(DATE);
            initializer = "new Date(new Date().getTime() + 10000000L)";
        } else {
            builder.getImportRegistrationResolver().addImports(CALENDAR, GREGORIAN_CALENDAR);
            initializer = "new GregorianCalendar(Calendar.getInstance().get(Calendar.YEAR), \n\t\t\tCalendar.getInstance().get(Calendar.MONTH), \n\t\t\tCalendar.getInstance().get(Calendar.DAY_OF_MONTH), \n\t\t\tCalendar.getInstance().get(Calendar.HOUR_OF_DAY), \n\t\t\tCalendar.getInstance().get(Calendar.MINUTE), \n\t\t\tCalendar.getInstance().get(Calendar.SECOND) + \n\t\t\tnew Double(Math.random() * 1000).intValue()).getTime()";
        }
    } else if (fieldType.equals(CALENDAR)) {
        builder.getImportRegistrationResolver().addImports(CALENDAR, GREGORIAN_CALENDAR);
        final String calendarString = "new GregorianCalendar(Calendar.getInstance().get(Calendar.YEAR), \n\t\t\tCalendar.getInstance().get(Calendar.MONTH), \n\t\t\tCalendar.getInstance().get(Calendar.DAY_OF_MONTH)";
        if (MemberFindingUtils.getAnnotationOfType(field.getAnnotations(), PAST) != null) {
            initializer = calendarString + " - 1)";
        } else if (MemberFindingUtils.getAnnotationOfType(field.getAnnotations(), FUTURE) != null) {
            initializer = calendarString + " + 1)";
        } else {
            initializer = "Calendar.getInstance()";
        }
    } else if (fieldType.equals(TIMESTAMP)) {
        builder.getImportRegistrationResolver().addImports(CALENDAR, GREGORIAN_CALENDAR, TIMESTAMP);
        initializer = "new Timestamp(new GregorianCalendar(Calendar.getInstance().get(Calendar.YEAR), \n\t\t\tCalendar.getInstance().get(Calendar.MONTH), \n\t\t\tCalendar.getInstance().get(Calendar.DAY_OF_MONTH), \n\t\t\tCalendar.getInstance().get(Calendar.HOUR_OF_DAY), \n\t\t\tCalendar.getInstance().get(Calendar.MINUTE), \n\t\t\tCalendar.getInstance().get(Calendar.SECOND) + \n\t\t\tnew Double(Math.random() * 1000).intValue()).getTime().getTime())";
    } else if (fieldType.equals(STRING)) {
        if (fieldInitializer != null && fieldInitializer.contains("\"")) {
            final int offset = fieldInitializer.indexOf("\"");
            initializer = fieldInitializer.substring(offset + 1, fieldInitializer.lastIndexOf("\""));
        } else {
            initializer = fieldName;
        }
        if (MemberFindingUtils.getAnnotationOfType(field.getAnnotations(), VALIDATOR_CONSTRAINTS_EMAIL) != null || fieldName.toLowerCase().contains("email")) {
            initializer = "\"foo\" + " + INDEX_VAR + " + \"@bar.com\"";
        } else {
            int maxLength = Integer.MAX_VALUE;
            // Check for @Size
            final AnnotationMetadata sizeAnnotation = MemberFindingUtils.getAnnotationOfType(field.getAnnotations(), SIZE);
            if (sizeAnnotation != null) {
                final AnnotationAttributeValue<?> maxValue = sizeAnnotation.getAttribute(MAX_SYMBOL);
                if (maxValue != null) {
                    validateNumericAnnotationAttribute(fieldName, "@Size", "max", maxValue.getValue());
                    maxLength = ((Integer) maxValue.getValue()).intValue();
                }
                final AnnotationAttributeValue<?> minValue = sizeAnnotation.getAttribute(MIN_SYMBOL);
                if (minValue != null) {
                    validateNumericAnnotationAttribute(fieldName, "@Size", "min", minValue.getValue());
                    final int minLength = ((Integer) minValue.getValue()).intValue();
                    Validate.isTrue(maxLength >= minLength, "@Size attribute 'max' must be greater than 'min' for field '%s' in %s", fieldName, entity.getFullyQualifiedTypeName());
                    if (initializer.length() + 2 < minLength) {
                        initializer = String.format("%1$-" + (minLength - 2) + "s", initializer).replace(' ', 'x');
                    }
                }
            } else {
                if (field.getCustomData().keySet().contains(CustomDataKeys.COLUMN_FIELD)) {
                    @SuppressWarnings("unchecked") final Map<String, Object> columnValues = (Map<String, Object>) field.getCustomData().get(CustomDataKeys.COLUMN_FIELD);
                    if (columnValues.keySet().contains("length")) {
                        final Object lengthValue = columnValues.get("length");
                        validateNumericAnnotationAttribute(fieldName, "@Column", "length", lengthValue);
                        maxLength = ((Integer) lengthValue).intValue();
                    }
                }
            }
            switch(maxLength) {
                case 0:
                    initializer = "\"\"";
                    break;
                case 1:
                    initializer = "String.valueOf(" + INDEX_VAR + ")";
                    break;
                case 2:
                    initializer = "\"" + initializer.charAt(0) + "\" + " + INDEX_VAR;
                    break;
                default:
                    if (initializer.length() + 2 > maxLength) {
                        initializer = "\"" + initializer.substring(0, maxLength - 2) + "_\" + " + INDEX_VAR;
                    } else {
                        initializer = "\"" + initializer + "_\" + " + INDEX_VAR;
                    }
            }
        }
    } else if (fieldType.equals(new JavaType(STRING.getFullyQualifiedTypeName(), 1, DataType.TYPE, null, null))) {
        initializer = StringUtils.defaultIfEmpty(fieldInitializer, "{ \"Y\", \"N\" }");
    } else if (fieldType.equals(JavaType.BOOLEAN_OBJECT)) {
        initializer = StringUtils.defaultIfEmpty(fieldInitializer, "Boolean.TRUE");
    } else if (fieldType.equals(JavaType.BOOLEAN_PRIMITIVE)) {
        initializer = StringUtils.defaultIfEmpty(fieldInitializer, "true");
    } else if (fieldType.equals(JavaType.INT_OBJECT)) {
        initializer = StringUtils.defaultIfEmpty(fieldInitializer, "new Integer(" + INDEX_VAR + ")");
    } else if (fieldType.equals(JavaType.INT_PRIMITIVE)) {
        initializer = StringUtils.defaultIfEmpty(fieldInitializer, INDEX_VAR);
    } else if (fieldType.equals(new JavaType(JavaType.INT_OBJECT.getFullyQualifiedTypeName(), 1, DataType.PRIMITIVE, null, null))) {
        initializer = StringUtils.defaultIfEmpty(fieldInitializer, "{ " + INDEX_VAR + ", " + INDEX_VAR + " }");
    } else if (fieldType.equals(JavaType.DOUBLE_OBJECT)) {
        initializer = StringUtils.defaultIfEmpty(fieldInitializer, "new Integer(" + INDEX_VAR + // Auto-boxed
        ").doubleValue()");
    } else if (fieldType.equals(JavaType.DOUBLE_PRIMITIVE)) {
        initializer = StringUtils.defaultIfEmpty(fieldInitializer, "new Integer(" + INDEX_VAR + ").doubleValue()");
    } else if (fieldType.equals(new JavaType(JavaType.DOUBLE_OBJECT.getFullyQualifiedTypeName(), 1, DataType.PRIMITIVE, null, null))) {
        initializer = StringUtils.defaultIfEmpty(fieldInitializer, "{ new Integer(" + INDEX_VAR + ").doubleValue(), new Integer(" + INDEX_VAR + ").doubleValue() }");
    } else if (fieldType.equals(JavaType.FLOAT_OBJECT)) {
        initializer = StringUtils.defaultIfEmpty(fieldInitializer, "new Integer(" + INDEX_VAR + // Auto-boxed
        ").floatValue()");
    } else if (fieldType.equals(JavaType.FLOAT_PRIMITIVE)) {
        initializer = StringUtils.defaultIfEmpty(fieldInitializer, "new Integer(" + INDEX_VAR + ").floatValue()");
    } else if (fieldType.equals(new JavaType(JavaType.FLOAT_OBJECT.getFullyQualifiedTypeName(), 1, DataType.PRIMITIVE, null, null))) {
        initializer = StringUtils.defaultIfEmpty(fieldInitializer, "{ new Integer(" + INDEX_VAR + ").floatValue(), new Integer(" + INDEX_VAR + ").floatValue() }");
    } else if (fieldType.equals(JavaType.LONG_OBJECT)) {
        initializer = StringUtils.defaultIfEmpty(fieldInitializer, // Auto-boxed
        "new Integer(" + INDEX_VAR + ").longValue()");
    } else if (fieldType.equals(JavaType.LONG_PRIMITIVE)) {
        initializer = StringUtils.defaultIfEmpty(fieldInitializer, "new Integer(" + INDEX_VAR + ").longValue()");
    } else if (fieldType.equals(new JavaType(JavaType.LONG_OBJECT.getFullyQualifiedTypeName(), 1, DataType.PRIMITIVE, null, null))) {
        initializer = StringUtils.defaultIfEmpty(fieldInitializer, "{ new Integer(" + INDEX_VAR + ").longValue(), new Integer(" + INDEX_VAR + ").longValue() }");
    } else if (fieldType.equals(JavaType.SHORT_OBJECT)) {
        initializer = StringUtils.defaultIfEmpty(fieldInitializer, "new Integer(" + INDEX_VAR + // Auto-boxed
        ").shortValue()");
    } else if (fieldType.equals(JavaType.SHORT_PRIMITIVE)) {
        initializer = StringUtils.defaultIfEmpty(fieldInitializer, "new Integer(" + INDEX_VAR + ").shortValue()");
    } else if (fieldType.equals(new JavaType(JavaType.SHORT_OBJECT.getFullyQualifiedTypeName(), 1, DataType.PRIMITIVE, null, null))) {
        initializer = StringUtils.defaultIfEmpty(fieldInitializer, "{ new Integer(" + INDEX_VAR + ").shortValue(), new Integer(" + INDEX_VAR + ").shortValue() }");
    } else if (fieldType.equals(JavaType.CHAR_OBJECT)) {
        initializer = StringUtils.defaultIfEmpty(fieldInitializer, "new Character('N')");
    } else if (fieldType.equals(JavaType.CHAR_PRIMITIVE)) {
        initializer = StringUtils.defaultIfEmpty(fieldInitializer, "'N'");
    } else if (fieldType.equals(new JavaType(JavaType.CHAR_OBJECT.getFullyQualifiedTypeName(), 1, DataType.PRIMITIVE, null, null))) {
        initializer = StringUtils.defaultIfEmpty(fieldInitializer, "{ 'Y', 'N' }");
    } else if (fieldType.equals(BIG_DECIMAL)) {
        builder.getImportRegistrationResolver().addImport(BIG_DECIMAL);
        initializer = BIG_DECIMAL.getSimpleTypeName() + ".valueOf(" + INDEX_VAR + ")";
    } else if (fieldType.equals(BIG_INTEGER)) {
        builder.getImportRegistrationResolver().addImport(BIG_INTEGER);
        initializer = BIG_INTEGER.getSimpleTypeName() + ".valueOf(" + INDEX_VAR + ")";
    } else if (fieldType.equals(JavaType.BYTE_OBJECT)) {
        initializer = "new Byte(" + StringUtils.defaultIfEmpty(fieldInitializer, "\"1\"") + ")";
    } else if (fieldType.equals(JavaType.BYTE_PRIMITIVE)) {
        initializer = "new Byte(" + StringUtils.defaultIfEmpty(fieldInitializer, "\"1\"") + ").byteValue()";
    } else if (fieldType.equals(JavaType.BYTE_ARRAY_PRIMITIVE)) {
        initializer = StringUtils.defaultIfEmpty(fieldInitializer, "String.valueOf(" + INDEX_VAR + ").getBytes()");
    } else if (fieldType.equals(entity)) {
        // Avoid circular references (ROO-562)
        initializer = OBJ_VAR;
    } else if (fieldCustomDataKeys.contains(CustomDataKeys.ENUMERATED_FIELD)) {
        builder.getImportRegistrationResolver().addImport(fieldType);
        initializer = fieldType.getSimpleTypeName() + ".class.getEnumConstants()[0]";
    } else if (collaboratingMetadata != null && collaboratingMetadata.getEntityType() != null) {
        requiredDataOnDemandCollaborators.add(fieldType);
        initializer = getFieldInitializerForRelatedEntity(field, collaboratingMetadata, fieldCustomDataKeys, dataOnDemandClasses);
    }
    return initializer;
}
Also used : AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) JdkJavaType(org.springframework.roo.model.JdkJavaType) JavaType(org.springframework.roo.model.JavaType) AnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata)

Aggregations

AnnotationAttributeValue (org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue)41 ArrayList (java.util.ArrayList)36 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)36 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)31 StringAttributeValue (org.springframework.roo.classpath.details.annotations.StringAttributeValue)23 NestedAnnotationAttributeValue (org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue)22 AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)17 ArrayAttributeValue (org.springframework.roo.classpath.details.annotations.ArrayAttributeValue)16 ClassAttributeValue (org.springframework.roo.classpath.details.annotations.ClassAttributeValue)13 JavaType (org.springframework.roo.model.JavaType)13 List (java.util.List)12 EnumAttributeValue (org.springframework.roo.classpath.details.annotations.EnumAttributeValue)11 EnumDetails (org.springframework.roo.model.EnumDetails)11 ClassOrInterfaceTypeDetailsBuilder (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder)10 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)8 IntegerAttributeValue (org.springframework.roo.classpath.details.annotations.IntegerAttributeValue)7 BooleanAttributeValue (org.springframework.roo.classpath.details.annotations.BooleanAttributeValue)6 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)4 LongAttributeValue (org.springframework.roo.classpath.details.annotations.LongAttributeValue)4 MemberDetails (org.springframework.roo.classpath.scanner.MemberDetails)4