use of org.springframework.roo.addon.web.mvc.views.components.FieldItem in project spring-roo by spring-projects.
the class AbstractViewGenerationService method createFieldItem.
protected FieldItem createFieldItem(JpaEntityMetadata entityMetadata, FieldMetadata entityField, String entityName, String suffixId, ViewContext<T> ctx, String referencedFieldName) {
// Getting current identifier field
FieldMetadata identifierField = entityMetadata.getCurrentIndentifierField();
FieldMetadata versionField = entityMetadata.getCurrentVersionField();
// Exclude id and version fields
if (entityField.getAnnotation(JpaJavaType.ID) != null || entityField.getAnnotation(JpaJavaType.VERSION) != null || identifierField.getFieldName().equals(entityField.getFieldName()) || (versionField != null && versionField.getFieldName().equals(entityField.getFieldName()))) {
return null;
}
// Generating new FieldItem element
FieldItem fieldItem = null;
if (referencedFieldName != null) {
fieldItem = new FieldItem(entityField.getFieldName().getSymbolName(), ctx.getEntityName(), referencedFieldName, entityName, suffixId);
} else {
fieldItem = new FieldItem(entityField.getFieldName().getSymbolName(), entityName, suffixId);
}
// Calculate fieldType
JavaType type = entityField.getFieldType();
ClassOrInterfaceTypeDetails typeDetails = getTypeLocationService().getTypeDetails(type);
// ROO-3810: Getting @NotNull annotation to include required attr
AnnotationMetadata notNullAnnotation = entityField.getAnnotation(Jsr303JavaType.NOT_NULL);
if (notNullAnnotation != null) {
fieldItem.addConfigurationElement("required", true);
} else {
fieldItem.addConfigurationElement("required", false);
}
// Check if is a referenced field
if (typeDetails != null && typeDetails.getAnnotation(RooJavaType.ROO_JPA_ENTITY) != null) {
// If is child part field of a composition relation, specify it if view context
if (entityMetadata.getCompositionRelationField() != null && entityMetadata.getCompositionRelationField().getFieldName().equals(entityField.getFieldName())) {
fieldItem.addConfigurationElement("isCompositionChildField", true);
} else {
fieldItem.addConfigurationElement("isCompositionChildField", false);
}
boolean shouldBeAdded = getReferenceField(fieldItem, typeDetails, ctx);
if (!shouldBeAdded) {
return null;
}
} else if (type.isBoolean()) {
// Check if is a boolean field
fieldItem.setType(FieldTypes.BOOLEAN.toString());
} else if (typeDetails != null && typeDetails.getPhysicalTypeCategory().equals(PhysicalTypeCategory.ENUMERATION)) {
// Saving enum and items to display. Same name as
// populateForm method
fieldItem.setType(FieldTypes.ENUM.toString());
fieldItem.addConfigurationElement("items", fieldItem.getFieldName());
} else if (type.getFullyQualifiedTypeName().equals(Date.class.getName()) || type.getFullyQualifiedTypeName().equals(Calendar.class.getName())) {
// Check if is a date field
fieldItem.setType(FieldTypes.DATE.toString());
// Getting datetime format to use
AnnotationMetadata dateTimeFormatAnnotation = entityField.getAnnotation(SpringJavaType.DATE_TIME_FORMAT);
String format = "d/m/Y";
if (dateTimeFormatAnnotation != null) {
AnnotationAttributeValue<String> styleAttribute = dateTimeFormatAnnotation.getAttribute("style");
if (styleAttribute != null) {
String annotationFormat = styleAttribute.getValue();
if (annotationFormat.equals("M-")) {
format = "d-M-Y";
} else {
format = annotationFormat;
}
}
}
fieldItem.addConfigurationElement("format", format);
} else if (type.getFullyQualifiedTypeName().equals("java.util.Set") || type.getFullyQualifiedTypeName().equals("java.util.List")) {
// getDetailsFieldViewItems method
return null;
} else if (type.isNumber()) {
// ROO-3810: Getting @Min and @Max annotations to add validations if
// necessary
AnnotationMetadata minAnnotation = entityField.getAnnotation(Jsr303JavaType.MIN);
if (minAnnotation != null) {
AnnotationAttributeValue<Object> min = minAnnotation.getAttribute("value");
if (min != null) {
fieldItem.addConfigurationElement("min", min.getValue().toString());
} else {
fieldItem.addConfigurationElement("min", "NULL");
}
} else {
fieldItem.addConfigurationElement("min", "NULL");
}
AnnotationMetadata maxAnnotation = entityField.getAnnotation(Jsr303JavaType.MAX);
if (maxAnnotation != null) {
AnnotationAttributeValue<Object> max = maxAnnotation.getAttribute("value");
if (max != null) {
fieldItem.addConfigurationElement("max", max.getValue().toString());
} else {
fieldItem.addConfigurationElement("max", "NULL");
}
} else {
fieldItem.addConfigurationElement("max", "NULL");
}
// ROO-3872: Support numeric input validation from client and server side
// Add digits constraints
AnnotationMetadata digitsAnnotation = entityField.getAnnotation(Jsr303JavaType.DIGITS);
if (digitsAnnotation != null) {
// Fraction digits
AnnotationAttributeValue<Object> digitsFraction = digitsAnnotation.getAttribute("fraction");
fieldItem.addConfigurationElement("digitsFraction", digitsFraction.getValue().toString());
// Integer digits
AnnotationAttributeValue<Object> digitsInteger = digitsAnnotation.getAttribute("integer");
fieldItem.addConfigurationElement("digitsInteger", digitsInteger.getValue().toString());
} else {
// Add default values for decimals
if (type.equals(JavaType.INT_OBJECT) || type.equals(JavaType.INT_PRIMITIVE) || type.equals(JdkJavaType.BIG_INTEGER)) {
fieldItem.addConfigurationElement("digitsFraction", "0");
} else {
fieldItem.addConfigurationElement("digitsFraction", "2");
}
fieldItem.addConfigurationElement("digitsInteger", "NULL");
}
fieldItem.setType(FieldTypes.NUMBER.toString());
} else {
// ROO-3810: Getting @Size annotation
AnnotationMetadata sizeAnnotation = entityField.getAnnotation(Jsr303JavaType.SIZE);
if (sizeAnnotation != null) {
AnnotationAttributeValue<Object> maxLength = sizeAnnotation.getAttribute("max");
if (maxLength != null) {
fieldItem.addConfigurationElement("maxLength", maxLength.getValue().toString());
} else {
fieldItem.addConfigurationElement("maxLength", "NULL");
}
} else {
fieldItem.addConfigurationElement("maxLength", "NULL");
}
fieldItem.setType(FieldTypes.TEXT.toString());
}
return fieldItem;
}
Aggregations