DefaultMapToDDMFormValuesConverterStrategy implements MapToDDMFormValuesConverterStrategy { protected List<DDMFormFieldValue> createDDMFormFieldValues( Map<String, Object> dataRecordValues, DDMFormField ddmFormField, Locale defaultLocale, Locale locale) { if ((dataRecordValues == null) || !dataRecordValues.containsKey(ddmFormField.getName())) { return ListUtil.fromArray( new DDMFormFieldValue() { { setName(ddmFormField.getName()); } }); } if (StringUtil.equals(ddmFormField.getType(), "fieldset")) { if (ListUtil.isEmpty(ddmFormField.getNestedDDMFormFields())) { return ListUtil.fromArray( new DDMFormFieldValue() { { setName(ddmFormField.getName()); } }); } Map<String, Object> fieldSetInstanceValues = (Map<String, Object>)dataRecordValues.get( ddmFormField.getName()); List<DDMFormFieldValue> ddmFormFieldValues = new ArrayList<>( fieldSetInstanceValues.size()); for (Map.Entry<String, Object> entry : fieldSetInstanceValues.entrySet()) { DDMFormFieldValue ddmFormFieldValue = new DDMFormFieldValue() { { setName(ddmFormField.getName()); setInstanceId(entry.getKey()); } }; for (DDMFormField nestedDDMFormField : ddmFormField.getNestedDDMFormFields()) { List<DDMFormFieldValue> nestedDDMFormFieldValues = createDDMFormFieldValues( (Map<String, Object>)fieldSetInstanceValues.get( ddmFormFieldValue.getInstanceId()), nestedDDMFormField, defaultLocale, locale); Stream<DDMFormFieldValue> stream = nestedDDMFormFieldValues.stream(); stream.forEach( ddmFormFieldValue::addNestedDDMFormFieldValue); } ddmFormFieldValues.add(ddmFormFieldValue); } return ddmFormFieldValues; } DDMFormFieldValue ddmFormFieldValue = new DDMFormFieldValue() { { setName(ddmFormField.getName()); setValue(ddmFormField.getPredefinedValue()); } }; if (ddmFormField.isRepeatable()) { List<Object> list = null; if (ddmFormField.isLocalizable()) { Object value = dataRecordValues.get(ddmFormField.getName()); if (!(value instanceof Map)) { throw new IllegalArgumentException( "Field value is not a map"); } Map<String, Object> localizedValues = (Map<String, Object>)value; list = (List<Object>)localizedValues.get( LanguageUtil.getLanguageId( (Locale)GetterUtil.getObject(locale, defaultLocale))); } else { list = (List<Object>)dataRecordValues.get( ddmFormField.getName()); } if (list == null) { return ListUtil.fromArray(ddmFormFieldValue); } List<DDMFormFieldValue> ddmFormFieldValues = new ArrayList<>( list.size()); for (Object object : list) { ddmFormFieldValue = new DDMFormFieldValue(); ddmFormFieldValue.setName(ddmFormField.getName()); LocalizedValue localizedValue = new LocalizedValue(); localizedValue.addString( (Locale)GetterUtil.getObject(locale, defaultLocale), String.valueOf(object)); ddmFormFieldValue.setValue(localizedValue); ddmFormFieldValues.add(ddmFormFieldValue); } return ddmFormFieldValues; } if (dataRecordValues.get(ddmFormField.getName()) != null) { ddmFormFieldValue.setValue( createValue( ddmFormField, locale, dataRecordValues.get(ddmFormField.getName()))); } return ListUtil.fromArray(ddmFormFieldValue); } private  DefaultMapToDDMFormValuesConverterStrategy(); static DefaultMapToDDMFormValuesConverterStrategy getInstance(); @Override void setDDMFormFieldValues(
		Map<String, Object> dataRecordValues, DDMForm ddmForm,
		DDMFormValues ddmFormValues, Locale locale);  }
@Test public void testCreateDDMFormFieldValueInvalidName() { DDMFormField ddmFormField = _createDDMFormField("field1", "text", true); List<DDMFormFieldValue> ddmFormFieldValues = _defaultMapToDDMFormValuesConverterStrategy. createDDMFormFieldValues( HashMapBuilder.<String, Object>put( "field2", HashMapBuilder.put( "en_US", "Value 2" ).put( "pt_BR", "Valor 2" ).build() ).build(), ddmFormField, null, null); DDMFormFieldValue ddmFormFieldValue = ddmFormFieldValues.get(0); Assert.assertEquals("field1", ddmFormFieldValue.getName()); Assert.assertNull(ddmFormFieldValue.getValue()); }
@Test public void testCreateDDMFormFieldValueNestedField() { DDMFormField ddmFormField = _createDDMFormField( "parent", "fieldset", true); ddmFormField.addNestedDDMFormField( _createDDMFormField("child", "text", true)); List<DDMFormFieldValue> ddmFormFieldValues = _defaultMapToDDMFormValuesConverterStrategy. createDDMFormFieldValues( HashMapBuilder.<String, Object>put( "parent", HashMapBuilder.<String, Object>put( "instanceId", HashMapBuilder.<String, Object>put( "child", HashMapBuilder.<String, Object>put( "en_US", "Child Value 1" ).put( "pt_BR", "Filho Valor 1" ).build() ).build() ).build() ).build(), ddmFormField, null, null); DDMFormFieldValue ddmFormFieldValue = ddmFormFieldValues.get(0); Assert.assertEquals("parent", ddmFormFieldValue.getName()); Map<String, List<DDMFormFieldValue>> nestedDDMFormFieldValuesMap = ddmFormFieldValue.getNestedDDMFormFieldValuesMap(); Assert.assertTrue(nestedDDMFormFieldValuesMap.containsKey("child")); List<DDMFormFieldValue> nestedDDMFormFieldValues = nestedDDMFormFieldValuesMap.get("child"); DDMFormFieldValue nestedDDMFormFieldValue = nestedDDMFormFieldValues.get(0); Value value = nestedDDMFormFieldValue.getValue(); Assert.assertTrue(value instanceof LocalizedValue); LocalizedValue localizedValue = (LocalizedValue)value; Assert.assertEquals( "Child Value 1", localizedValue.getString(LocaleUtil.ENGLISH)); Assert.assertEquals( "Filho Valor 1", localizedValue.getString(LocaleUtil.BRAZIL)); }
@Test public void testCreateDDMFormFieldValueNoLocale() { DDMFormField ddmFormField = _createDDMFormField("field1", "text", true); List<DDMFormFieldValue> ddmFormFieldValues = _defaultMapToDDMFormValuesConverterStrategy. createDDMFormFieldValues( HashMapBuilder.<String, Object>put( "field1", HashMapBuilder.put( "en_US", "Value 1" ).put( "pt_BR", "Valor 1" ).build() ).build(), ddmFormField, null, null); DDMFormFieldValue ddmFormFieldValue = ddmFormFieldValues.get(0); Assert.assertEquals("field1", ddmFormFieldValue.getName()); Value value = ddmFormFieldValue.getValue(); Assert.assertTrue(value instanceof LocalizedValue); LocalizedValue localizedValue = (LocalizedValue)value; Assert.assertEquals( "Value 1", localizedValue.getString(LocaleUtil.ENGLISH)); Assert.assertEquals( "Valor 1", localizedValue.getString(LocaleUtil.BRAZIL)); }