Search in sources :

Example 1 with TypeValidation

use of org.motechproject.mds.domain.TypeValidation in project motech by motech.

the class EntityServiceImpl method createFieldForDraft.

private void createFieldForDraft(EntityDraft draft, DraftData draftData) {
    String typeClass = draftData.getValue(DraftData.TYPE_CLASS).toString();
    String displayName = draftData.getValue(DraftData.DISPLAY_NAME).toString();
    String name = draftData.getValue(DraftData.NAME).toString();
    Type type = ("textArea".equalsIgnoreCase(typeClass)) ? allTypes.retrieveByClassName("java.lang.String") : allTypes.retrieveByClassName(typeClass);
    if (type == null) {
        throw new NoSuchTypeException(typeClass);
    }
    Set<Lookup> fieldLookups = new HashSet<>();
    Field field = new Field(draft, name, displayName, fieldLookups);
    field.setType(type);
    if (type.hasSettings()) {
        for (TypeSetting setting : type.getSettings()) {
            field.addSetting(new FieldSetting(field, setting));
        }
    }
    if (type.hasValidation()) {
        for (TypeValidation validation : type.getValidations()) {
            field.addValidation(new FieldValidation(field, validation));
        }
    }
    if (TypeDto.BLOB.getTypeClass().equals(typeClass)) {
        field.setUIDisplayable(false);
    } else {
        field.setUIDisplayable(true);
        field.setUIDisplayPosition((long) draft.getFields().size());
    }
    if ("textArea".equalsIgnoreCase(typeClass)) {
        setSettingForTextArea(field);
    }
    FieldHelper.addMetadataForRelationship(typeClass, field);
    FieldHelper.addOrUpdateMetadataForCombobox(field);
    draft.addField(field);
    allEntityDrafts.update(draft);
}
Also used : FieldSetting(org.motechproject.mds.domain.FieldSetting) Field(org.motechproject.mds.domain.Field) Type(org.motechproject.mds.domain.Type) TypeValidation(org.motechproject.mds.domain.TypeValidation) NoSuchTypeException(org.motechproject.mds.exception.type.NoSuchTypeException) TypeSetting(org.motechproject.mds.domain.TypeSetting) Lookup(org.motechproject.mds.domain.Lookup) FieldValidation(org.motechproject.mds.domain.FieldValidation) HashSet(java.util.HashSet)

Example 2 with TypeValidation

use of org.motechproject.mds.domain.TypeValidation in project motech by motech.

the class AllTypesContextIT method assertType.

private void assertType(String displayName, String description, String className, List<String> settings, List<String> validations) {
    Type type = allTypes.retrieveByClassName(className);
    assertNotNull("Not found type with class name: " + className, type);
    assertNotNull("Type should have id", type.getId());
    assertEquals(displayName, type.getDisplayName());
    assertEquals(description, type.getDescription());
    assertEquals(className, type.getTypeClassName());
    assertEquals("Field " + displayName + " settings: ", null != settings, type.hasSettings());
    assertEquals("Field " + displayName + " validations: ", null != validations, type.hasValidation());
    if (null != settings) {
        List<TypeSetting> typeSettings = type.getSettings();
        List<String> strings = new ArrayList<>();
        for (TypeSetting typeSetting : typeSettings) {
            strings.add(typeSetting.getName());
        }
        Collections.sort(settings);
        Collections.sort(strings);
        assertEquals(settings, strings);
    }
    if (null != validations) {
        List<TypeValidation> typeValidations = type.getValidations();
        List<String> strings = new ArrayList<>();
        for (TypeValidation typeValidation : typeValidations) {
            strings.add(typeValidation.getDisplayName());
        }
        Collections.sort(validations);
        Collections.sort(strings);
        assertEquals(validations, strings);
    }
}
Also used : Type(org.motechproject.mds.domain.Type) TypeValidation(org.motechproject.mds.domain.TypeValidation) TypeSetting(org.motechproject.mds.domain.TypeSetting) ArrayList(java.util.ArrayList)

Example 3 with TypeValidation

use of org.motechproject.mds.domain.TypeValidation in project motech by motech.

the class TypeServiceImpl method findValidations.

@Override
@Transactional
public List<TypeValidation> findValidations(TypeDto type, Class<? extends Annotation> aClass) {
    Type typeSource = allTypes.retrieveByClassName(type.getTypeClass());
    List<TypeValidation> list = null == typeSource ? new ArrayList<>() : typeSource.getValidations();
    List<TypeValidation> validations = new ArrayList<>();
    for (TypeValidation validation : list) {
        if (validation.getAnnotations().contains(aClass)) {
            validations.add(validation);
        }
    }
    return validations;
}
Also used : Type(org.motechproject.mds.domain.Type) TypeValidation(org.motechproject.mds.domain.TypeValidation) ArrayList(java.util.ArrayList) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with TypeValidation

use of org.motechproject.mds.domain.TypeValidation 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)

Aggregations

Type (org.motechproject.mds.domain.Type)4 TypeValidation (org.motechproject.mds.domain.TypeValidation)4 TypeSetting (org.motechproject.mds.domain.TypeSetting)3 ArrayList (java.util.ArrayList)2 Field (org.motechproject.mds.domain.Field)2 FieldSetting (org.motechproject.mds.domain.FieldSetting)2 FieldValidation (org.motechproject.mds.domain.FieldValidation)2 HashSet (java.util.HashSet)1 FieldMetadata (org.motechproject.mds.domain.FieldMetadata)1 Lookup (org.motechproject.mds.domain.Lookup)1 FieldBasicDto (org.motechproject.mds.dto.FieldBasicDto)1 FieldValidationDto (org.motechproject.mds.dto.FieldValidationDto)1 MetadataDto (org.motechproject.mds.dto.MetadataDto)1 SettingDto (org.motechproject.mds.dto.SettingDto)1 ValidationCriterionDto (org.motechproject.mds.dto.ValidationCriterionDto)1 NoSuchTypeException (org.motechproject.mds.exception.type.NoSuchTypeException)1 Transactional (org.springframework.transaction.annotation.Transactional)1