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);
}
}
}
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());
}
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;
}
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;
}
Aggregations