NumericDDMFormFieldTypeReportProcessor extends TextDDMFormFieldTypeReportProcessor { @Override public JSONObject process( DDMFormFieldValue ddmFormFieldValue, JSONObject fieldJSONObject, long formInstanceRecordId, String ddmFormInstanceReportEvent) throws Exception { JSONObject jsonObject = super.process( ddmFormFieldValue, fieldJSONObject, formInstanceRecordId, ddmFormInstanceReportEvent); BigDecimal valueBigDecimal = _getValueBigDecimal(ddmFormFieldValue); if (valueBigDecimal == null) { return jsonObject; } JSONObject summaryJSONObject = jsonObject.getJSONObject("summary"); if (summaryJSONObject == null) { summaryJSONObject = JSONFactoryUtil.createJSONObject(); } BigDecimal sumBigDecimal = new BigDecimal( summaryJSONObject.getString("sum", "0")); if (ddmFormInstanceReportEvent.equals( DDMFormInstanceReportConstants.EVENT_ADD_RECORD_VERSION)) { BigDecimal maxValueBigDecimal = null; if (summaryJSONObject.has("max")) { maxValueBigDecimal = new BigDecimal( summaryJSONObject.getString("max")); } if ((maxValueBigDecimal == null) || (valueBigDecimal.compareTo(maxValueBigDecimal) == 1)) { summaryJSONObject.put("max", valueBigDecimal.toString()); } BigDecimal minValueBigDecimal = null; if (summaryJSONObject.has("min")) { minValueBigDecimal = new BigDecimal( summaryJSONObject.getString("min")); } if ((minValueBigDecimal == null) || (valueBigDecimal.compareTo(minValueBigDecimal) == -1)) { summaryJSONObject.put("min", valueBigDecimal.toString()); } sumBigDecimal = sumBigDecimal.add(valueBigDecimal); } else if (ddmFormInstanceReportEvent.equals( DDMFormInstanceReportConstants. EVENT_DELETE_RECORD_VERSION)) { DDMFormInstanceRecord ddmFormInstanceRecord = ddmFormInstanceRecordLocalService.getFormInstanceRecord( formInstanceRecordId); DDMFormInstance ddmFormInstance = ddmFormInstanceRecord.getFormInstance(); List<DDMFormInstanceRecord> ddmFormInstanceRecords = ddmFormInstance.getFormInstanceRecords(); Supplier<Stream<DDMFormInstanceRecord>> streamSupplier = () -> { Stream<DDMFormInstanceRecord> stream = ddmFormInstanceRecords.stream(); return stream.filter( currentDDMFormInstanceRecord -> formInstanceRecordId != currentDDMFormInstanceRecord. getFormInstanceRecordId()); }; Stream<DDMFormInstanceRecord> stream = streamSupplier.get(); if (stream.count() == 0) { jsonObject.remove("summary"); return jsonObject; } Comparator<Number> comparator = (number1, number2) -> Double.compare( number1.doubleValue(), number2.doubleValue()); BigDecimal maxValueBigDecimal = _getValueBigDecimalsStream( ddmFormFieldValue.getName(), streamSupplier.get() ).max( comparator ).get(); BigDecimal minValueBigDecimal = _getValueBigDecimalsStream( ddmFormFieldValue.getName(), streamSupplier.get() ).min( comparator ).get(); summaryJSONObject.put( "max", maxValueBigDecimal.toString() ).put( "min", minValueBigDecimal.toString() ); sumBigDecimal = sumBigDecimal.subtract(valueBigDecimal); } BigDecimal averageBigDecimal = new BigDecimal("0.0"); if (jsonObject.getInt("totalEntries") > 0) { averageBigDecimal = sumBigDecimal.divide( new BigDecimal(jsonObject.getInt("totalEntries")), 10, RoundingMode.HALF_UP); } summaryJSONObject.put( "average", formatBigDecimal(averageBigDecimal) ).put( "sum", sumBigDecimal.toString() ); jsonObject.put("summary", summaryJSONObject); return jsonObject; }  @Override JSONObject process(
			DDMFormFieldValue ddmFormFieldValue, JSONObject fieldJSONObject,
			long formInstanceRecordId, String ddmFormInstanceReportEvent);  }
@Test public void testProcessDDMFormInstanceReportOnDeleteEvent() throws Exception { DDMFormFieldValue ddmFormFieldValue = _mockDDMFormFieldValue( "field1", "3"); long formInstanceRecordId = 3; DDMFormInstanceRecord ddmFormInstanceRecord = _mockDDMFormInstanceRecord(formInstanceRecordId); DDMFormInstance ddmFormInstance = mock(DDMFormInstance.class); when( ddmFormInstance.getFormInstanceId() ).thenReturn( 0L ); List<DDMFormInstanceRecord> ddmFormInstanceRecords = new ArrayList<>(); for (int i = _VALUES_MAX_LENGTH; i > 0; i--) { ddmFormInstanceRecords.add( _createDDMFormInstanceRecord(String.valueOf(i))); } when( ddmFormInstance.getFormInstanceRecords() ).thenReturn( ddmFormInstanceRecords ); when( ddmFormInstanceRecord.getFormInstance() ).thenReturn( ddmFormInstance ); when( _ddmFormInstanceRecordLocalService.searchFormInstanceRecords( Mockito.eq(0L), Mockito.eq(new String[] {"field1"}), Mockito.eq(WorkflowConstants.STATUS_APPROVED), Mockito.eq(0), Mockito.eq(_VALUES_MAX_LENGTH + 1), Mockito.any()) ).thenReturn( new BaseModelSearchResult<DDMFormInstanceRecord>( ddmFormInstanceRecords, ddmFormInstanceRecords.size()) ); JSONObject processedFieldJSONObject = _numericDDMFormFieldTypeReportProcessor.process( ddmFormFieldValue, JSONUtil.put( "summary", JSONUtil.put( "average", "3" ).put( "max", "5" ).put( "min", "1" ).put( "sum", "15" ) ).put( "totalEntries", 5 ).put( "type", DDMFormFieldType.NUMERIC ).put( "values", JSONUtil.putAll( JSONUtil.put( "formInstanceRecordId", 5 ).put( "value", "5" ), JSONUtil.put( "formInstanceRecordId", 4 ).put( "value", "4" ), JSONUtil.put( "formInstanceRecordId", 3 ).put( "value", "3" ), JSONUtil.put( "formInstanceRecordId", 2 ).put( "value", "2" ), JSONUtil.put( "formInstanceRecordId", 1 ).put( "value", "1" )) ), formInstanceRecordId, DDMFormInstanceReportConstants.EVENT_DELETE_RECORD_VERSION); JSONObject summaryJSONObject = processedFieldJSONObject.getJSONObject( "summary"); Assert.assertEquals("3", summaryJSONObject.getString("average")); Assert.assertEquals("5", summaryJSONObject.getString("max")); Assert.assertEquals("1", summaryJSONObject.getString("min")); Assert.assertEquals("12", summaryJSONObject.getString("sum")); }
@Test public void testProcessDDMFormInstanceReportWithEmptyData() throws Exception { DDMFormFieldValue ddmFormFieldValue = _mockDDMFormFieldValue( "field1", "1"); long formInstanceRecordId = 1; _mockDDMFormInstanceRecord(formInstanceRecordId); JSONObject processedFieldJSONObject = _numericDDMFormFieldTypeReportProcessor.process( ddmFormFieldValue, JSONUtil.put( "type", DDMFormFieldType.NUMERIC ).put( "values", JSONFactoryUtil.createJSONObject() ), formInstanceRecordId, DDMFormInstanceReportConstants.EVENT_ADD_RECORD_VERSION); JSONObject summaryJSONObject = processedFieldJSONObject.getJSONObject( "summary"); Assert.assertEquals("1", summaryJSONObject.getString("average")); Assert.assertEquals("1", summaryJSONObject.getString("max")); Assert.assertEquals("1", summaryJSONObject.getString("min")); Assert.assertEquals("1", summaryJSONObject.getString("sum")); }
@Test public void testProcessDDMFormInstanceReportWithExistingData() throws Exception { DDMFormFieldValue ddmFormFieldValue = _mockDDMFormFieldValue( "field1", "3"); long formInstanceRecordId = 0; _mockDDMFormInstanceRecord(formInstanceRecordId); JSONObject processedFieldJSONObject = _numericDDMFormFieldTypeReportProcessor.process( ddmFormFieldValue, JSONUtil.put( "summary", JSONUtil.put( "average", "1" ).put( "max", "1" ).put( "min", "1" ).put( "sum", "1" ) ).put( "totalEntries", 1 ).put( "type", DDMFormFieldType.NUMERIC ).put( "values", JSONUtil.put( JSONUtil.put( "formInstanceRecordId", 1 ).put( "value", "1" )) ), formInstanceRecordId, DDMFormInstanceReportConstants.EVENT_ADD_RECORD_VERSION); JSONObject summaryJSONObject = processedFieldJSONObject.getJSONObject( "summary"); Assert.assertEquals("2", summaryJSONObject.getString("average")); Assert.assertEquals("3", summaryJSONObject.getString("max")); Assert.assertEquals("1", summaryJSONObject.getString("min")); Assert.assertEquals("4", summaryJSONObject.getString("sum")); }
@Test public void testProcessDDMFormInstanceReportWithLongFieldValue() throws Exception { DDMFormFieldValue ddmFormFieldValue = _mockDDMFormFieldValue( "field1", "99999999999999999999999999999999999999999"); long formInstanceRecordId = 0; _mockDDMFormInstanceRecord(formInstanceRecordId); JSONObject processedFieldJSONObject = _numericDDMFormFieldTypeReportProcessor.process( ddmFormFieldValue, JSONUtil.put( "summary", JSONUtil.put( "average", "1" ).put( "max", "1" ).put( "min", "1" ).put( "sum", "1" ) ).put( "totalEntries", 1 ).put( "type", DDMFormFieldType.NUMERIC ).put( "values", JSONUtil.put( JSONUtil.put( "formInstanceRecordId", 1 ).put( "value", "1" )) ), formInstanceRecordId, DDMFormInstanceReportConstants.EVENT_ADD_RECORD_VERSION); JSONObject summaryJSONObject = processedFieldJSONObject.getJSONObject( "summary"); Assert.assertEquals( "50000000000000000000000000000000000000000", summaryJSONObject.getString("average")); Assert.assertEquals( "99999999999999999999999999999999999999999", summaryJSONObject.getString("max")); Assert.assertEquals("1", summaryJSONObject.getString("min")); Assert.assertEquals( "100000000000000000000000000000000000000000", summaryJSONObject.getString("sum")); }