Search in sources :

Example 1 with FieldValidationDto

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

the class Field method toDto.

public FieldDto toDto() {
    FieldBasicDto basic = new FieldBasicDto(displayName, name, required, unique, parseDefaultValue(), tooltip, placeholder);
    TypeDto typeDto = null;
    List<MetadataDto> metaDto = new ArrayList<>();
    for (FieldMetadata meta : metadata) {
        metaDto.add(meta.toDto());
    }
    FieldValidationDto validationDto = null;
    if (CollectionUtils.isNotEmpty(validations)) {
        validationDto = new FieldValidationDto();
        for (FieldValidation validation : validations) {
            validationDto.addCriterion(validation.toDto());
        }
    }
    List<SettingDto> settingsDto = new ArrayList<>();
    for (FieldSetting setting : settings) {
        settingsDto.add(setting.toDto());
    }
    List<LookupDto> lookupDtos = new ArrayList<>();
    for (Lookup lookup : getLookups()) {
        lookupDtos.add(lookup.toDto());
    }
    if (typeDto == null && type != null) {
        typeDto = type.toDto();
    }
    return new FieldDto(id, entity == null ? null : entity.getId(), typeDto, basic, readOnly, nonEditable, nonDisplayable, uiFilterable, uiChanged, metaDto, validationDto, settingsDto, lookupDtos);
}
Also used : LookupDto(org.motechproject.mds.dto.LookupDto) ArrayList(java.util.ArrayList) MetadataDto(org.motechproject.mds.dto.MetadataDto) FieldBasicDto(org.motechproject.mds.dto.FieldBasicDto) FieldValidationDto(org.motechproject.mds.dto.FieldValidationDto) TypeDto(org.motechproject.mds.dto.TypeDto) SettingDto(org.motechproject.mds.dto.SettingDto) FieldDto(org.motechproject.mds.dto.FieldDto)

Example 2 with FieldValidationDto

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

the class FieldHelperTest method fieldDto.

private FieldDto fieldDto() {
    TypeDto type = new TypeDto("typeDispName", "typeDesc", "typeDefaultName", "typeClass");
    FieldBasicDto basic = new FieldBasicDto("fieldDispName", "fieldName", true, false, "defVal", "tooltip", "placeholder");
    MetadataDto metadata = new MetadataDto("key", "val");
    ValidationCriterionDto criterion = new ValidationCriterionDto("criterionDispName", type);
    FieldValidationDto validation = new FieldValidationDto(criterion);
    return new FieldDto(1L, 100L, type, basic, false, new ArrayList<>(asList(metadata)), validation, null, null);
}
Also used : FieldBasicDto(org.motechproject.mds.dto.FieldBasicDto) ValidationCriterionDto(org.motechproject.mds.dto.ValidationCriterionDto) FieldValidationDto(org.motechproject.mds.dto.FieldValidationDto) TypeDto(org.motechproject.mds.dto.TypeDto) MetadataDto(org.motechproject.mds.dto.MetadataDto) FieldDto(org.motechproject.mds.dto.FieldDto)

Example 3 with FieldValidationDto

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

the class EntityServiceImpl method addField.

private void addField(Entity entity, FieldDto fieldDto) {
    FieldBasicDto basic = fieldDto.getBasic();
    String typeClass = fieldDto.getType().getTypeClass();
    Type type = allTypes.retrieveByClassName(typeClass);
    Field field = new Field(entity, basic.getName(), basic.getDisplayName(), basic.isRequired(), basic.isUnique(), fieldDto.isReadOnly(), fieldDto.isNonEditable(), fieldDto.isNonDisplayable(), (String) basic.getDefaultValue(), basic.getTooltip(), basic.getPlaceholder(), null);
    field.setType(type);
    if (type.hasSettings()) {
        for (TypeSetting setting : type.getSettings()) {
            SettingDto settingDto = fieldDto.getSetting(setting.getName());
            FieldSetting fieldSetting = new FieldSetting(field, setting);
            if (null != settingDto) {
                fieldSetting.setValue(settingDto.getValueAsString());
            }
            field.addSetting(fieldSetting);
        }
    }
    if (type.hasValidation()) {
        for (TypeValidation validation : type.getValidations()) {
            FieldValidation fieldValidation = new FieldValidation(field, validation);
            FieldValidationDto validationDto = fieldDto.getValidation();
            if (null != validationDto) {
                ValidationCriterionDto criterion = validationDto.getCriterion(validation.getDisplayName());
                if (null != criterion) {
                    fieldValidation.setValue(criterion.valueAsString());
                    fieldValidation.setEnabled(criterion.isEnabled());
                }
            }
            field.addValidation(fieldValidation);
        }
    }
    for (MetadataDto metadata : fieldDto.getMetadata()) {
        field.addMetadata(new FieldMetadata(metadata));
    }
    entity.addField(field);
}
Also used : FieldMetadata(org.motechproject.mds.domain.FieldMetadata) MetadataDto(org.motechproject.mds.dto.MetadataDto) FieldSetting(org.motechproject.mds.domain.FieldSetting) Field(org.motechproject.mds.domain.Field) Type(org.motechproject.mds.domain.Type) TypeValidation(org.motechproject.mds.domain.TypeValidation) FieldBasicDto(org.motechproject.mds.dto.FieldBasicDto) ValidationCriterionDto(org.motechproject.mds.dto.ValidationCriterionDto) TypeSetting(org.motechproject.mds.domain.TypeSetting) FieldValidationDto(org.motechproject.mds.dto.FieldValidationDto) SettingDto(org.motechproject.mds.dto.SettingDto) FieldValidation(org.motechproject.mds.domain.FieldValidation)

Example 4 with FieldValidationDto

use of org.motechproject.mds.dto.FieldValidationDto 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

FieldValidationDto (org.motechproject.mds.dto.FieldValidationDto)4 FieldBasicDto (org.motechproject.mds.dto.FieldBasicDto)3 MetadataDto (org.motechproject.mds.dto.MetadataDto)3 TypeDto (org.motechproject.mds.dto.TypeDto)3 ValidationCriterionDto (org.motechproject.mds.dto.ValidationCriterionDto)3 FieldDto (org.motechproject.mds.dto.FieldDto)2 SettingDto (org.motechproject.mds.dto.SettingDto)2 Annotation (java.lang.annotation.Annotation)1 AccessibleObject (java.lang.reflect.AccessibleObject)1 ArrayList (java.util.ArrayList)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 LookupDto (org.motechproject.mds.dto.LookupDto)1