Search in sources :

Example 6 with ValidationCriterionDto

use of org.motechproject.mds.dto.ValidationCriterionDto in project motech by motech.

the class FieldProcessor method createValidation.

private FieldValidationDto createValidation(AccessibleObject ac, TypeDto type) {
    FieldValidationDto validationDto = null;
    for (Annotation annotation : ac.getAnnotations()) {
        List<TypeValidationDto> validations = getSchemaHolder().findValidations(type.getTypeClass(), annotation.annotationType());
        for (TypeValidationDto validation : validations) {
            String displayName = validation.getDisplayName();
            TypeDto valueType = getSchemaHolder().getType(validation.getValueType());
            if (null == valueType) {
                throw new IllegalStateException("The valueType is not set in: " + validation);
            }
            String valueAsString = getValidationValue(displayName, annotation);
            if (InSet.class.isAssignableFrom(annotation.annotationType()) || NotInSet.class.isAssignableFrom(annotation.annotationType())) {
                valueAsString = valueAsString.replaceAll("(\\{|\\})", "");
            }
            Object value = TypeHelper.parse(valueAsString, valueType.getClassObjectForType());
            ValidationCriterionDto dto = new ValidationCriterionDto();
            dto.setDisplayName(displayName);
            dto.setType(valueType);
            dto.setEnabled(true);
            dto.setValue(value);
            if (null == validationDto) {
                validationDto = new FieldValidationDto();
            }
            validationDto.addCriterion(dto);
        }
    }
    return validationDto;
}
Also used : NotInSet(org.motechproject.mds.annotations.NotInSet) TypeValidationDto(org.motechproject.mds.dto.TypeValidationDto) ValidationCriterionDto(org.motechproject.mds.dto.ValidationCriterionDto) FieldValidationDto(org.motechproject.mds.dto.FieldValidationDto) TypeDto(org.motechproject.mds.dto.TypeDto) InSet(org.motechproject.mds.annotations.InSet) NotInSet(org.motechproject.mds.annotations.NotInSet) AccessibleObject(java.lang.reflect.AccessibleObject) Annotation(java.lang.annotation.Annotation)

Aggregations

ValidationCriterionDto (org.motechproject.mds.dto.ValidationCriterionDto)6 FieldValidationDto (org.motechproject.mds.dto.FieldValidationDto)3 FieldBasicDto (org.motechproject.mds.dto.FieldBasicDto)2 MetadataDto (org.motechproject.mds.dto.MetadataDto)2 TypeDto (org.motechproject.mds.dto.TypeDto)2 Annotation (java.lang.annotation.Annotation)1 AccessibleObject (java.lang.reflect.AccessibleObject)1 IdentityType (javax.jdo.annotations.IdentityType)1 InSet (org.motechproject.mds.annotations.InSet)1 NotInSet (org.motechproject.mds.annotations.NotInSet)1 Field (org.motechproject.mds.domain.Field)1 FieldMetadata (org.motechproject.mds.domain.FieldMetadata)1 FieldSetting (org.motechproject.mds.domain.FieldSetting)1 FieldValidation (org.motechproject.mds.domain.FieldValidation)1 Type (org.motechproject.mds.domain.Type)1 TypeSetting (org.motechproject.mds.domain.TypeSetting)1 TypeValidation (org.motechproject.mds.domain.TypeValidation)1 FieldDto (org.motechproject.mds.dto.FieldDto)1 SettingDto (org.motechproject.mds.dto.SettingDto)1 TypeValidationDto (org.motechproject.mds.dto.TypeValidationDto)1