GridDDMFormFieldTypeReportProcessor implements DDMFormFieldTypeReportProcessor { @Override public JSONObject process( DDMFormFieldValue ddmFormFieldValue, JSONObject fieldJSONObject, long formInstanceRecordId, String ddmFormInstanceReportEvent) throws Exception { JSONObject valuesJSONObject = fieldJSONObject.getJSONObject("values"); Value value = ddmFormFieldValue.getValue(); JSONObject valueJSONObject = JSONFactoryUtil.createJSONObject( value.getString(value.getDefaultLocale())); Iterator<String> iterator = valueJSONObject.keys(); while (iterator.hasNext()) { String rowName = iterator.next(); JSONObject rowJSONObject = valuesJSONObject.getJSONObject(rowName); if (rowJSONObject == null) { rowJSONObject = JSONFactoryUtil.createJSONObject(); } String columnName = valueJSONObject.getString(rowName); int count = rowJSONObject.getInt(columnName); if (ddmFormInstanceReportEvent.equals( DDMFormInstanceReportConstants.EVENT_ADD_RECORD_VERSION)) { count++; } else if (ddmFormInstanceReportEvent.equals( DDMFormInstanceReportConstants. EVENT_DELETE_RECORD_VERSION)) { count--; } rowJSONObject.put(columnName, count); valuesJSONObject.put(rowName, rowJSONObject); } DDMFormInstanceRecord ddmFormInstanceRecord = ddmFormInstanceRecordLocalService.getDDMFormInstanceRecord( formInstanceRecordId); DDMFormInstance ddmFormInstance = ddmFormInstanceRecord.getFormInstance(); DDMStructure ddmStructure = ddmFormInstance.getStructure(); DDMFormField ddmFormField = ddmStructure.getDDMFormField( ddmFormFieldValue.getName()); int totalEntries = fieldJSONObject.getInt("totalEntries"); if (valueJSONObject.length() != 0) { if (ddmFormInstanceReportEvent.equals( DDMFormInstanceReportConstants.EVENT_ADD_RECORD_VERSION)) { totalEntries++; } else if (ddmFormInstanceReportEvent.equals( DDMFormInstanceReportConstants. EVENT_DELETE_RECORD_VERSION)) { totalEntries--; } } fieldJSONObject.put( "structure", JSONUtil.put( "columns", _getOptionValuesJSONArray(ddmFormField, "columns") ).put( "rows", _getOptionValuesJSONArray(ddmFormField, "rows") ) ).put( "totalEntries", totalEntries ); return fieldJSONObject; }  @Override JSONObject process(
			DDMFormFieldValue ddmFormFieldValue, JSONObject fieldJSONObject,
			long formInstanceRecordId, String ddmFormInstanceReportEvent);  }
@Test public void testProcessDDMFormInstanceReportOnDeleteEvent() throws Exception { DDMFormFieldValue ddmFormFieldValue = _mockDDMFormFieldValue( "field1", JSONUtil.put("option1", "option2")); JSONObject processedFieldJSONObject = _gridDDMFormFieldTypeReportProcessor.process( ddmFormFieldValue, JSONUtil.put( "type", DDMFormFieldType.GRID ).put( "values", JSONFactoryUtil.createJSONObject( "{option1 : {option1 : 1, option2 : 1}}") ), _FORM_INSTANCE_RECORD_ID, DDMFormInstanceReportConstants.EVENT_DELETE_RECORD_VERSION); JSONObject valuesJSONObject = processedFieldJSONObject.getJSONObject( "values"); JSONObject rowJSONObject = valuesJSONObject.getJSONObject("option1"); Assert.assertEquals(1, rowJSONObject.getLong("option1")); Assert.assertEquals(0, rowJSONObject.getLong("option2")); JSONObject structureJSONObject = processedFieldJSONObject.getJSONObject( "structure"); _assertStructure(structureJSONObject); }
@Test public void testProcessDDMFormInstanceReportWithEmptyData() throws Exception { DDMFormFieldValue ddmFormFieldValue = _mockDDMFormFieldValue( "field1", JSONUtil.put("option1", "option2")); JSONObject processedFieldJSONObject = _gridDDMFormFieldTypeReportProcessor.process( ddmFormFieldValue, JSONUtil.put( "type", DDMFormFieldType.GRID ).put( "values", JSONFactoryUtil.createJSONObject() ), _FORM_INSTANCE_RECORD_ID, DDMFormInstanceReportConstants.EVENT_ADD_RECORD_VERSION); Assert.assertEquals( DDMFormFieldType.GRID, processedFieldJSONObject.getString("type")); JSONObject valuesJSONObject = processedFieldJSONObject.getJSONObject( "values"); JSONObject rowJSONObject = valuesJSONObject.getJSONObject("option1"); Assert.assertEquals(1, rowJSONObject.getLong("option2")); JSONObject structureJSONObject = processedFieldJSONObject.getJSONObject( "structure"); _assertStructure(structureJSONObject); }
@Test public void testProcessDDMFormInstanceReportWithEmptyField() throws Exception { DDMFormFieldValue ddmFormFieldValue = _mockDDMFormFieldValue( "field1", JSONFactoryUtil.createJSONObject()); JSONObject processedFieldJSONObject = _gridDDMFormFieldTypeReportProcessor.process( ddmFormFieldValue, JSONUtil.put( "type", DDMFormFieldType.GRID ).put( "values", JSONFactoryUtil.createJSONObject() ), _FORM_INSTANCE_RECORD_ID, DDMFormInstanceReportConstants.EVENT_ADD_RECORD_VERSION); Assert.assertEquals( DDMFormFieldType.GRID, processedFieldJSONObject.getString("type")); JSONObject valuesJSONObject = processedFieldJSONObject.getJSONObject( "values"); Assert.assertFalse(valuesJSONObject.has("")); JSONObject structureJSONObject = processedFieldJSONObject.getJSONObject( "structure"); _assertStructure(structureJSONObject); }
@Test public void testProcessDDMFormInstanceReportWithExistingData() throws Exception { DDMFormFieldValue ddmFormFieldValue = _mockDDMFormFieldValue( "field1", JSONUtil.put("option1", "option2")); JSONObject processedFieldJSONObject = _gridDDMFormFieldTypeReportProcessor.process( ddmFormFieldValue, JSONUtil.put( "type", DDMFormFieldType.GRID ).put( "values", JSONFactoryUtil.createJSONObject( "{option1 : {option1 : 1, option2 : 1}}") ), _FORM_INSTANCE_RECORD_ID, DDMFormInstanceReportConstants.EVENT_ADD_RECORD_VERSION); JSONObject valuesJSONObject = processedFieldJSONObject.getJSONObject( "values"); JSONObject rowJSONObject = valuesJSONObject.getJSONObject("option1"); Assert.assertEquals(1, rowJSONObject.getLong("option1")); Assert.assertEquals(2, rowJSONObject.getLong("option2")); JSONObject structureJSONObject = processedFieldJSONObject.getJSONObject( "structure"); _assertStructure(structureJSONObject); }