=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/datasetreport/DataSetReportService.java' --- dhis-2/dhis-api/src/main/java/org/hisp/dhis/datasetreport/DataSetReportService.java 2011-05-30 19:32:17 +0000 +++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/datasetreport/DataSetReportService.java 2011-08-25 08:52:40 +0000 @@ -28,10 +28,8 @@ */ import java.util.List; -import java.util.Map; import org.hisp.dhis.common.Grid; -import org.hisp.dhis.dataentryform.DataEntryForm; import org.hisp.dhis.dataset.DataSet; import org.hisp.dhis.i18n.I18n; import org.hisp.dhis.i18n.I18nFormat; @@ -45,41 +43,6 @@ public interface DataSetReportService { /** - * Generates a map with aggregated data values or regular data values (depending - * on the selectedUnitOnly argument) mapped to a DataElemenet operand identifier. - * - * @param dataSet the DataSet. - * @param unit the OrganisationUnit. - * @param period the Period. - * @param selectedUnitOnly whether to include aggregated or regular data in the map. - * @param format the I18nFormat. - * @return a map. - */ - Map getAggregatedValueMap( DataSet dataSet, OrganisationUnit unit, Period period, boolean selectedUnitOnly, I18nFormat format ); - - /** - * Generates a map with aggregated indicator values mapped to an Indicator identifier. - * - * @param dataSet the DataSet. - * @param unit the OrganisationUnit. - * @param period the Period. - * @param format the I18nFormat. - * @return a map. - */ - Map getAggregatedIndicatorValueMap( DataSet dataSet, OrganisationUnit unit, Period period, I18nFormat format ); - - /** - * Puts in aggregated datavalues in the custom dataentry form and returns - * whole report text. - * - * @param dataEntryForm the data entry form. - * @param a map with aggregated data values mapped to data element operands. - * @return data entry form HTML code populated with aggregated data in the - * input fields. - */ - String prepareReportContent( DataEntryForm dataEntryForm, Map dataValues, Map indicatorValues ); - - /** * Generates html code for a custom data set report. * * @param dataSet the data set. === modified file 'dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/datasetreport/impl/DefaultDataSetReportService.java' --- dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/datasetreport/impl/DefaultDataSetReportService.java 2011-06-14 15:00:55 +0000 +++ dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/datasetreport/impl/DefaultDataSetReportService.java 2011-08-25 08:52:40 +0000 @@ -127,122 +127,6 @@ // DataSetReportService implementation // ------------------------------------------------------------------------- - public Map getAggregatedValueMap( DataSet dataSet, OrganisationUnit unit, Period period, boolean selectedUnitOnly, I18nFormat format ) - { - String aggregationStrategy = (String) systemSettingManager.getSystemSetting( KEY_AGGREGATION_STRATEGY, DEFAULT_AGGREGATION_STRATEGY ); - - Collection dataElements = new ArrayList( dataSet.getDataElements()); - - FilterUtils.filter( dataElements, new AggregatableDataElementFilter() ); - - Map map = new TreeMap(); - - for ( DataElement dataElement : dataElements ) - { - for ( DataElementCategoryOptionCombo categoryOptionCombo : dataElement.getCategoryCombo().getOptionCombos() ) - { - String value; - - if ( selectedUnitOnly ) - { - DataValue dataValue = dataValueService.getDataValue( unit, dataElement, period, categoryOptionCombo ); - value = ( dataValue != null ) ? dataValue.getValue() : null; - } - else - { - Double aggregatedValue = aggregationStrategy.equals( AGGREGATION_STRATEGY_REAL_TIME ) ? - aggregationService.getAggregatedDataValue( dataElement, categoryOptionCombo, period.getStartDate(), period.getEndDate(), unit ) : - aggregatedDataValueService.getAggregatedValue( dataElement, categoryOptionCombo, period, unit ); - - value = format.formatValue( aggregatedValue ); - } - - if ( value != null ) - { - map.put( dataElement.getId() + SEPARATOR + categoryOptionCombo.getId(), value ); - } - } - } - - return map; - } - - public Map getAggregatedIndicatorValueMap( DataSet dataSet, OrganisationUnit unit, Period period, I18nFormat format ) - { - String aggregationStrategy = (String) systemSettingManager.getSystemSetting( KEY_AGGREGATION_STRATEGY, DEFAULT_AGGREGATION_STRATEGY ); - - Map map = new TreeMap(); - - for ( Indicator indicator : dataSet.getIndicators() ) - { - Double aggregatedValue = aggregationStrategy.equals( AGGREGATION_STRATEGY_REAL_TIME ) ? - aggregationService.getAggregatedIndicatorValue( indicator, period.getStartDate(), period.getEndDate(), unit ) : - aggregatedDataValueService.getAggregatedValue( indicator, period, unit ); - - String value = format.formatValue( aggregatedValue ); - - if ( value != null ) - { - map.put( indicator.getId(), value ); - } - } - - return map; - } - - public String prepareReportContent( DataEntryForm dataEntryForm, Map dataValues, Map indicatorValues ) - { - StringBuffer buffer = new StringBuffer(); - - Matcher inputMatcher = INPUT_PATTERN.matcher( dataEntryForm.getHtmlCode() ); - - // --------------------------------------------------------------------- - // Iterate through all matching data element fields. - // --------------------------------------------------------------------- - - while ( inputMatcher.find() ) - { - // ----------------------------------------------------------------- - // Get input HTML code - // ----------------------------------------------------------------- - - String inputHtml = inputMatcher.group( 1 ); - - Matcher identifierMatcher = IDENTIFIER_PATTERN.matcher( inputHtml ); - Matcher indicatorMatcher = INDICATOR_PATTERN.matcher( inputHtml ); - - // ----------------------------------------------------------------- - // Find existing data or indicator value and replace input tag - // ----------------------------------------------------------------- - - if ( identifierMatcher.find() && identifierMatcher.groupCount() > 0 ) - { - Integer dataElementId = Integer.parseInt( identifierMatcher.group( 1 ) ); - Integer optionComboId = Integer.parseInt( identifierMatcher.group( 2 ) ); - - String dataValue = dataValues.get( dataElementId + SEPARATOR + optionComboId ); - - dataValue = dataValue != null ? dataValue : NULL_REPLACEMENT; - - inputMatcher.appendReplacement( buffer, dataValue ); - } - else if ( indicatorMatcher.find() && indicatorMatcher.groupCount() > 0 ) - { - Integer indicatorId = Integer.parseInt( indicatorMatcher.group( 1 ) ); - - String indicatorValue = indicatorValues.get( indicatorId ); - - indicatorValue = indicatorValue != null ? indicatorValue : NULL_REPLACEMENT; - - inputMatcher.appendReplacement( buffer, indicatorValue ); - } - } - - inputMatcher.appendTail( buffer ); - - return buffer.toString(); - } - public String getCustomDataSetReport( DataSet dataSet, OrganisationUnit unit, Period period, boolean selectedUnitOnly, I18nFormat format ) { Map aggregatedDataValueMap = getAggregatedValueMap( dataSet, unit, period, selectedUnitOnly, format ); @@ -434,4 +318,153 @@ return grid; } + + // ------------------------------------------------------------------------- + // Supportive methods + // ------------------------------------------------------------------------- + + /** + * Generates a map with aggregated data values or regular data values (depending + * on the selectedUnitOnly argument) mapped to a DataElemenet operand identifier. + * + * @param dataSet the DataSet. + * @param unit the OrganisationUnit. + * @param period the Period. + * @param selectedUnitOnly whether to include aggregated or regular data in the map. + * @param format the I18nFormat. + * @return a map. + */ + private Map getAggregatedValueMap( DataSet dataSet, OrganisationUnit unit, Period period, boolean selectedUnitOnly, I18nFormat format ) + { + String aggregationStrategy = (String) systemSettingManager.getSystemSetting( KEY_AGGREGATION_STRATEGY, DEFAULT_AGGREGATION_STRATEGY ); + + Collection dataElements = new ArrayList( dataSet.getDataElements()); + + FilterUtils.filter( dataElements, new AggregatableDataElementFilter() ); + + Map map = new TreeMap(); + + for ( DataElement dataElement : dataElements ) + { + for ( DataElementCategoryOptionCombo categoryOptionCombo : dataElement.getCategoryCombo().getOptionCombos() ) + { + String value; + + if ( selectedUnitOnly ) + { + DataValue dataValue = dataValueService.getDataValue( unit, dataElement, period, categoryOptionCombo ); + value = ( dataValue != null ) ? dataValue.getValue() : null; + } + else + { + Double aggregatedValue = aggregationStrategy.equals( AGGREGATION_STRATEGY_REAL_TIME ) ? + aggregationService.getAggregatedDataValue( dataElement, categoryOptionCombo, period.getStartDate(), period.getEndDate(), unit ) : + aggregatedDataValueService.getAggregatedValue( dataElement, categoryOptionCombo, period, unit ); + + value = format.formatValue( aggregatedValue ); + } + + if ( value != null ) + { + map.put( dataElement.getId() + SEPARATOR + categoryOptionCombo.getId(), value ); + } + } + } + + return map; + } + + /** + * Generates a map with aggregated indicator values mapped to an Indicator identifier. + * + * @param dataSet the DataSet. + * @param unit the OrganisationUnit. + * @param period the Period. + * @param format the I18nFormat. + * @return a map. + */ + private Map getAggregatedIndicatorValueMap( DataSet dataSet, OrganisationUnit unit, Period period, I18nFormat format ) + { + String aggregationStrategy = (String) systemSettingManager.getSystemSetting( KEY_AGGREGATION_STRATEGY, DEFAULT_AGGREGATION_STRATEGY ); + + Map map = new TreeMap(); + + for ( Indicator indicator : dataSet.getIndicators() ) + { + Double aggregatedValue = aggregationStrategy.equals( AGGREGATION_STRATEGY_REAL_TIME ) ? + aggregationService.getAggregatedIndicatorValue( indicator, period.getStartDate(), period.getEndDate(), unit ) : + aggregatedDataValueService.getAggregatedValue( indicator, period, unit ); + + String value = format.formatValue( aggregatedValue ); + + if ( value != null ) + { + map.put( indicator.getId(), value ); + } + } + + return map; + } + + /** + * Puts in aggregated datavalues in the custom dataentry form and returns + * whole report text. + * + * @param dataEntryForm the data entry form. + * @param a map with aggregated data values mapped to data element operands. + * @return data entry form HTML code populated with aggregated data in the + * input fields. + */ + private String prepareReportContent( DataEntryForm dataEntryForm, Map dataValues, Map indicatorValues ) + { + StringBuffer buffer = new StringBuffer(); + + Matcher inputMatcher = INPUT_PATTERN.matcher( dataEntryForm.getHtmlCode() ); + + // --------------------------------------------------------------------- + // Iterate through all matching data element fields. + // --------------------------------------------------------------------- + + while ( inputMatcher.find() ) + { + // ----------------------------------------------------------------- + // Get input HTML code + // ----------------------------------------------------------------- + + String inputHtml = inputMatcher.group( 1 ); + + Matcher identifierMatcher = IDENTIFIER_PATTERN.matcher( inputHtml ); + Matcher indicatorMatcher = INDICATOR_PATTERN.matcher( inputHtml ); + + // ----------------------------------------------------------------- + // Find existing data or indicator value and replace input tag + // ----------------------------------------------------------------- + + if ( identifierMatcher.find() && identifierMatcher.groupCount() > 0 ) + { + Integer dataElementId = Integer.parseInt( identifierMatcher.group( 1 ) ); + Integer optionComboId = Integer.parseInt( identifierMatcher.group( 2 ) ); + + String dataValue = dataValues.get( dataElementId + SEPARATOR + optionComboId ); + + dataValue = dataValue != null ? dataValue : NULL_REPLACEMENT; + + inputMatcher.appendReplacement( buffer, dataValue ); + } + else if ( indicatorMatcher.find() && indicatorMatcher.groupCount() > 0 ) + { + Integer indicatorId = Integer.parseInt( indicatorMatcher.group( 1 ) ); + + String indicatorValue = indicatorValues.get( indicatorId ); + + indicatorValue = indicatorValue != null ? indicatorValue : NULL_REPLACEMENT; + + inputMatcher.appendReplacement( buffer, indicatorValue ); + } + } + + inputMatcher.appendTail( buffer ); + + return buffer.toString(); + } } === modified file 'dhis-2/dhis-services/dhis-service-reporting/src/main/resources/META-INF/dhis/beans.xml' --- dhis-2/dhis-services/dhis-service-reporting/src/main/resources/META-INF/dhis/beans.xml 2011-07-21 03:29:35 +0000 +++ dhis-2/dhis-services/dhis-service-reporting/src/main/resources/META-INF/dhis/beans.xml 2011-08-25 08:52:40 +0000 @@ -337,13 +337,12 @@ - - + + - + + +