GridDDMFormFieldValueRenderer implements DDMFormFieldValueRenderer { @Override public String render(DDMFormFieldValue ddmFormFieldValue, Locale locale) { JSONObject valuesJSONObject = gridDDMFormFieldValueAccessor.getValue( ddmFormFieldValue, locale); if (valuesJSONObject.length() == 0) { return StringPool.BLANK; } DDMFormFieldOptions rows = getDDMFormFieldOptions( ddmFormFieldValue, "rows"); DDMFormFieldOptions columns = getDDMFormFieldOptions( ddmFormFieldValue, "columns"); StringBundler sb = new StringBundler(valuesJSONObject.length() * 5); Set<String> rowOptions = rows.getOptionsValues(); for (String rowOption : rowOptions) { if (valuesJSONObject.has(rowOption)) { String columnOption = valuesJSONObject.getString(rowOption); LocalizedValue rowLabel = rows.getOptionLabels(rowOption); LocalizedValue columnLabel = columns.getOptionLabels( columnOption); sb.append(getLabel(rowLabel, rowOption, locale)); sb.append(StringPool.COLON); sb.append(StringPool.SPACE); sb.append(getLabel(columnLabel, columnOption, locale)); sb.append(StringPool.COMMA_AND_SPACE); } } if (sb.index() > 0) { sb.setIndex(sb.index() - 1); } return sb.toString(); }  @Override String render(DDMFormFieldValue ddmFormFieldValue, Locale locale);  }
@Test public void testRender() throws Exception { DDMForm ddmForm = DDMFormTestUtil.createDDMForm(); DDMFormField ddmFormField = DDMFormTestUtil.createDDMFormField( "Grid", "Grid", "grid", "string", false, false, false); DDMFormFieldOptions ddmFormFieldRows = new DDMFormFieldOptions(); ddmFormFieldRows.addOptionLabel( "rowValue 1", LocaleUtil.US, "rowLabel 1"); ddmFormFieldRows.addOptionLabel( "rowValue 2", LocaleUtil.US, "rowLabel 2"); ddmFormField.setProperty("rows", ddmFormFieldRows); DDMFormFieldOptions ddmFormFieldColumns = new DDMFormFieldOptions(); ddmFormFieldColumns.addOptionLabel( "columnValue 1", LocaleUtil.US, "columnLabel 1"); ddmFormFieldColumns.addOptionLabel( "columnValue 2", LocaleUtil.US, "columnLabel 2"); ddmFormField.setProperty("columns", ddmFormFieldColumns); ddmForm.addDDMFormField(ddmFormField); DDMFormValues ddmFormValues = DDMFormValuesTestUtil.createDDMFormValues( ddmForm); DDMFormFieldValue ddmFormFieldValue = DDMFormValuesTestUtil.createDDMFormFieldValue( "Grid", new UnlocalizedValue("{\"rowValue 1\":\"columnValue 1\"}")); ddmFormValues.addDDMFormFieldValue(ddmFormFieldValue); GridDDMFormFieldValueRenderer gridDDMFormFieldValueRenderer = createGridDDMFormFieldValueRenderer(); Assert.assertEquals( "rowLabel 1: columnLabel 1", gridDDMFormFieldValueRenderer.render( ddmFormFieldValue, LocaleUtil.US)); }
@Test public void testRenderWithTwoRows() throws Exception { DDMForm ddmForm = DDMFormTestUtil.createDDMForm(); DDMFormField ddmFormField = DDMFormTestUtil.createDDMFormField( "Grid", "Grid", "grid", "string", false, false, false); DDMFormFieldOptions ddmFormFieldRows = new DDMFormFieldOptions(); ddmFormFieldRows.addOptionLabel( "rowValue 1", LocaleUtil.US, "rowLabel 1"); ddmFormFieldRows.addOptionLabel( "rowValue 2", LocaleUtil.US, "rowLabel 2"); ddmFormField.setProperty("rows", ddmFormFieldRows); DDMFormFieldOptions ddmFormFieldColumns = new DDMFormFieldOptions(); ddmFormFieldColumns.addOptionLabel( "columnValue 1", LocaleUtil.US, "columnLabel 1"); ddmFormFieldColumns.addOptionLabel( "columnValue 2", LocaleUtil.US, "columnLabel 2"); ddmFormField.setProperty("columns", ddmFormFieldColumns); ddmForm.addDDMFormField(ddmFormField); DDMFormValues ddmFormValues = DDMFormValuesTestUtil.createDDMFormValues( ddmForm); String value = "{\"rowValue 2\":\"columnValue 2\", \"rowValue 1\":\"" + "columnValue 1\"}"; DDMFormFieldValue ddmFormFieldValue = DDMFormValuesTestUtil.createDDMFormFieldValue( "Grid", new UnlocalizedValue(value)); ddmFormValues.addDDMFormFieldValue(ddmFormFieldValue); GridDDMFormFieldValueRenderer gridDDMFormFieldValueRenderer = createGridDDMFormFieldValueRenderer(); Assert.assertEquals( "rowLabel 1: columnLabel 1, rowLabel 2: columnLabel 2", gridDDMFormFieldValueRenderer.render( ddmFormFieldValue, LocaleUtil.US)); }