Search in sources :

Example 16 with SettingDto

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

the class FieldRecord method extendOptionsIfNecessary.

private void extendOptionsIfNecessary() {
    // don't add null or empty string, only for list types
    if (!canExtendOptions()) {
        return;
    }
    // find the correct option
    SettingDto listValuesOption = getSettingByName(COMBOBOX_VALUES);
    // add the value
    if (listValuesOption != null) {
        if (listValuesOption.getValue() instanceof List) {
            // copy current values to avoid running into unmodifiable lists
            List listValues = new ArrayList((List) listValuesOption.getValue());
            // for lists, we add all not included
            if (List.class.isAssignableFrom(getValue().getClass())) {
                for (Object objectFromValueList : (List) getValue()) {
                    if (!listValues.contains(objectFromValueList.toString())) {
                        listValues.add(objectFromValueList);
                    }
                }
            // a case for single select comboboxes, just add the value
            } else {
                if (!listValues.contains(getValue().toString())) {
                    listValues.add(getValue());
                }
            }
            listValuesOption.setValue(listValues);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) SettingDto(org.motechproject.mds.dto.SettingDto) List(java.util.List) ArrayList(java.util.ArrayList)

Example 17 with SettingDto

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

the class FieldRecordTest method shouldExtendOptionsAndHandleDefListValuesForMultiSelect.

@Test
public void shouldExtendOptionsAndHandleDefListValuesForMultiSelect() {
    FieldDto fieldDto = FieldTestHelper.fieldDto(1L, "name", List.class.getName(), "disp", "[one, two]");
    fieldDto.setSettings(asList(new SettingDto(COMBOBOX_VALUES, asList("one", "two", "three"), null, null), new SettingDto(ALLOW_MULTIPLE_SELECTIONS, true, null, null)));
    FieldRecord fieldRecord = new FieldRecord(fieldDto);
    assertEquals(asList("one", "two"), fieldRecord.getValue());
    fieldRecord.setValue(asList("defVal", "secondVal", "two"));
    assertEquals(asList("one", "two", "three", "defVal", "secondVal"), fieldRecord.getSettingByName(COMBOBOX_VALUES).getValue());
    fieldRecord = new FieldRecord(fieldDto);
    fieldRecord.setValue("testSingleObject");
    assertEquals(asList("one", "two", "three", "testSingleObject"), fieldRecord.getSettingByName(COMBOBOX_VALUES).getValue());
    fieldRecord.setValue("[one, two]");
    assertEquals(asList("one", "two"), fieldRecord.getValue());
    assertEquals(asList("one", "two", "three", "testSingleObject"), fieldRecord.getSettingByName(COMBOBOX_VALUES).getValue());
    fieldRecord.setValue("[one, two]");
    assertEquals(asList("one", "two"), fieldRecord.getValue());
    // should return a string for single selections
    fieldRecord.setValue("defVal");
    assertEquals(asList("defVal"), fieldRecord.getValue());
    fieldRecord.setValue("[defVal]");
    assertEquals(asList("defVal"), fieldRecord.getValue());
    // test with enum
    fieldRecord.setValue(TestEnum.ONE);
    assertEquals(asList("ONE"), fieldRecord.getValue());
    fieldRecord.setValue(asList(TestEnum.ONE, TestEnum.THREE));
    assertEquals(asList("ONE", "THREE"), fieldRecord.getValue());
    fieldRecord.setValue(null);
    assertNull(fieldRecord.getValue());
}
Also used : List(java.util.List) Arrays.asList(java.util.Arrays.asList) SettingDto(org.motechproject.mds.dto.SettingDto) FieldRecord(org.motechproject.mds.web.domain.FieldRecord) FieldDto(org.motechproject.mds.dto.FieldDto) Test(org.junit.Test)

Example 18 with SettingDto

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

the class FieldProcessor method createComboboxSettings.

private List<SettingDto> createComboboxSettings(AccessibleObject ac, Class<?> classType) {
    boolean allowMultipleSelections = Collection.class.isAssignableFrom(classType);
    boolean allowUserSupplied = false;
    Map<String, String> values = new HashMap<>();
    final EnumDisplayName annotation = ac.getAnnotation(EnumDisplayName.class);
    String nameDisplayField = "";
    if (annotation != null) {
        nameDisplayField = annotation.enumField();
    }
    if (Collection.class.isAssignableFrom(classType)) {
        Class<?> genericType = MemberUtil.getGenericType(ac);
        if (genericType.isEnum()) {
            Object[] enumConstants = genericType.getEnumConstants();
            populateEnumDisplayValues(enumConstants, values, nameDisplayField, annotation);
        } else {
            allowUserSupplied = true;
        }
    } else {
        Object[] enumConstants = classType.getEnumConstants();
        populateEnumDisplayValues(enumConstants, values, nameDisplayField, annotation);
    }
    List<SettingDto> list = new ArrayList<>();
    list.add(new SettingDto(Constants.Settings.ALLOW_MULTIPLE_SELECTIONS, allowMultipleSelections));
    list.add(new SettingDto(Constants.Settings.ALLOW_USER_SUPPLIED, allowUserSupplied));
    list.add(new SettingDto(Constants.Settings.COMBOBOX_VALUES, values));
    return list;
}
Also used : HashMap(java.util.HashMap) EnumDisplayName(org.motechproject.mds.annotations.EnumDisplayName) ArrayList(java.util.ArrayList) AccessibleObject(java.lang.reflect.AccessibleObject) SettingDto(org.motechproject.mds.dto.SettingDto)

Example 19 with SettingDto

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

the class FieldProcessor method createStringSettings.

private List<SettingDto> createStringSettings(AccessibleObject ac, boolean isTextArea) {
    List<SettingDto> list = new ArrayList<>();
    // get length from jdo @Column annotation
    Column columnAnnotation = ReflectionsUtil.getAnnotation(ac, Column.class);
    // try getting the annotation from the private field if this is a getter/setter
    if (columnAnnotation == null && ac instanceof Method) {
        String fieldName = MemberUtil.getFieldName(ac);
        Method method = (Method) ac;
        Class<?> entityClass = method.getDeclaringClass();
        java.lang.reflect.Field referencedField = FieldUtils.getDeclaredField(entityClass, fieldName, true);
        if (referencedField != null) {
            columnAnnotation = ReflectionsUtil.getAnnotation(referencedField, Column.class);
        }
    }
    if (columnAnnotation != null) {
        int length = columnAnnotation.length();
        if (length >= 0) {
            list.add(new SettingDto(Constants.Settings.STRING_MAX_LENGTH, length));
        }
    }
    if (isTextArea) {
        list.add(new SettingDto(Constants.Settings.STRING_TEXT_AREA, true));
    }
    return list;
}
Also used : Column(javax.jdo.annotations.Column) ArrayList(java.util.ArrayList) SettingDto(org.motechproject.mds.dto.SettingDto) Method(java.lang.reflect.Method)

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