Search in sources :

Example 11 with SettingDto

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

the class FieldSetting method toDto.

public SettingDto toDto() {
    List<SettingOptions> options = new ArrayList<>();
    if (details.getTypeSettingOptions() != null) {
        for (TypeSettingOption option : details.getTypeSettingOptions()) {
            options.add(SettingOptions.valueOf(option.getName()));
        }
    }
    Type valueType = details.getValueType();
    SettingDto dto = new SettingDto();
    dto.setType(valueType.toDto());
    dto.setValue(valueType.parse(value));
    dto.setName(details.getName());
    dto.setOptions(options);
    return dto;
}
Also used : IdentityType(javax.jdo.annotations.IdentityType) SettingOptions(org.motechproject.mds.dto.SettingOptions) ArrayList(java.util.ArrayList) SettingDto(org.motechproject.mds.dto.SettingDto)

Example 12 with SettingDto

use of org.motechproject.mds.dto.SettingDto 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 13 with SettingDto

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

the class EntityBuilderTest method shouldBuildAnEntityWithFields.

@Test
public void shouldBuildAnEntityWithFields() throws Exception {
    FieldDto enumListField = fieldDto("enumList", List.class);
    enumListField.addMetadata(new MetadataDto(Constants.MetadataKeys.ENUM_CLASS_NAME, FieldEnum.class.getName()));
    enumListField.getType().setDisplayName("mds.field.combobox");
    enumListField.addSetting(new SettingDto(Constants.Settings.ALLOW_MULTIPLE_SELECTIONS, "true"));
    fields.addAll(asList(fieldDto("count", Integer.class), fieldDto("time", Time.class), fieldDto("str", String.class), fieldDto("dec", Double.class), fieldDto("bool", Boolean.class), fieldDto("date", Date.class), fieldDto("dt", DateTime.class), fieldDto("ld", LocalDate.class), fieldDto("locale", Locale.class), enumListField, fieldDto("CapitalizedName", String.class), fieldDto("jd", java.time.LocalDate.class), fieldDto("jdt", LocalDateTime.class), fieldDto("blob", Byte[].class.getName())));
    Class<?> clazz = buildClass();
    assertNotNull(clazz);
    assertField(clazz, "count", Integer.class);
    assertField(clazz, "time", Time.class);
    assertField(clazz, "str", String.class);
    assertField(clazz, "dec", Double.class);
    assertField(clazz, "bool", Boolean.class);
    assertField(clazz, "date", Date.class);
    assertField(clazz, "dt", DateTime.class);
    assertField(clazz, "ld", LocalDate.class);
    assertField(clazz, "locale", Locale.class);
    assertField(clazz, "jd", java.time.LocalDate.class);
    assertField(clazz, "jdt", LocalDateTime.class);
    assertField(clazz, "blob", Byte[].class);
    // should use uncapitalized version
    assertField(clazz, "capitalizedName", String.class);
}
Also used : SettingDto(org.motechproject.mds.dto.SettingDto) MetadataDto(org.motechproject.mds.dto.MetadataDto) FieldDto(org.motechproject.mds.dto.FieldDto) Test(org.junit.Test)

Example 14 with SettingDto

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

the class EntityBuilderTest method shouldBuildEnumListFieldProperly.

@Test
public void shouldBuildEnumListFieldProperly() throws Exception {
    FieldDto enumListField = fieldDto("enumList", Collection.class);
    enumListField.addMetadata(new MetadataDto(Constants.MetadataKeys.ENUM_CLASS_NAME, FieldEnum.class.getName()));
    enumListField.addSetting(new SettingDto(Constants.Settings.ALLOW_MULTIPLE_SELECTIONS, "true"));
    fields.add(enumListField);
    Class<?> clazz = buildClass();
    assertNotNull(clazz);
    assertField(clazz, "enumList", List.class);
    // verify that getters and setters have correct generic signatures
    Method setter = clazz.getDeclaredMethod("setEnumList", List.class);
    assertNotNull(setter);
    ParameterizedType pType = (ParameterizedType) setter.getGenericParameterTypes()[0];
    assertEquals(FieldEnum.class, pType.getActualTypeArguments()[0]);
    Method getter = clazz.getDeclaredMethod("getEnumList");
    assertNotNull(getter);
    pType = (ParameterizedType) getter.getGenericReturnType();
    assertEquals(FieldEnum.class, pType.getActualTypeArguments()[0]);
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) SettingDto(org.motechproject.mds.dto.SettingDto) Method(java.lang.reflect.Method) MetadataDto(org.motechproject.mds.dto.MetadataDto) FieldDto(org.motechproject.mds.dto.FieldDto) Test(org.junit.Test)

Example 15 with SettingDto

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

the class FieldProcessorTest method shouldReadMaxLengthForStringField.

@Test
public void shouldReadMaxLengthForStringField() {
    Method getLength400 = getAccessibleMethod(Sample.class, "getLength400", new Class[0]);
    processor.process(getLength400);
    Collection<FieldDto> fields = processor.getElements();
    assertEquals(1, fields.size());
    FieldDto field = fields.iterator().next();
    assertEquals("length400", field.getBasic().getName());
    SettingDto lengthSetting = field.getSetting(Constants.Settings.STRING_MAX_LENGTH);
    assertNotNull(lengthSetting);
    assertEquals(400, lengthSetting.getValue());
}
Also used : SettingDto(org.motechproject.mds.dto.SettingDto) Method(java.lang.reflect.Method) MethodUtils.getAccessibleMethod(org.apache.commons.lang.reflect.MethodUtils.getAccessibleMethod) FieldDto(org.motechproject.mds.dto.FieldDto) Test(org.junit.Test)

Aggregations

SettingDto (org.motechproject.mds.dto.SettingDto)19 FieldDto (org.motechproject.mds.dto.FieldDto)11 ArrayList (java.util.ArrayList)8 MetadataDto (org.motechproject.mds.dto.MetadataDto)6 Test (org.junit.Test)5 FieldBasicDto (org.motechproject.mds.dto.FieldBasicDto)4 Method (java.lang.reflect.Method)3 List (java.util.List)3 LookupFieldDto (org.motechproject.mds.dto.LookupFieldDto)3 Arrays.asList (java.util.Arrays.asList)2 HashMap (java.util.HashMap)2 EntityDto (org.motechproject.mds.dto.EntityDto)2 FieldValidationDto (org.motechproject.mds.dto.FieldValidationDto)2 LookupDto (org.motechproject.mds.dto.LookupDto)2 SchemaHolder (org.motechproject.mds.dto.SchemaHolder)2 FieldRecord (org.motechproject.mds.web.domain.FieldRecord)2 AccessibleObject (java.lang.reflect.AccessibleObject)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Collection (java.util.Collection)1 LinkedList (java.util.LinkedList)1