=== 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-04-01 11:19:55 +0000 +++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/datasetreport/DataSetReportService.java 2011-05-10 10:59:59 +0000 @@ -47,13 +47,25 @@ * 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 containing the DataElement to include in the map. + * @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 @@ -64,7 +76,7 @@ * @return data entry form HTML code populated with aggregated data in the * input fields. */ - String prepareReportContent( String dataEntryFormCode, Map dataValues ); + String prepareReportContent( String dataEntryFormCode, Map dataValues, Map indicatorValues ); /** * Generates a Grid representing a data set report with all data elements === 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-05-10 09:48:46 +0000 +++ dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/datasetreport/impl/DefaultDataSetReportService.java 2011-05-10 10:59:59 +0000 @@ -42,6 +42,7 @@ import java.util.Set; import java.util.TreeMap; import java.util.regex.Matcher; +import java.util.regex.Pattern; import org.hisp.dhis.aggregation.AggregatedDataValueService; import org.hisp.dhis.aggregation.AggregationService; @@ -62,6 +63,7 @@ import org.hisp.dhis.datavalue.DataValueService; import org.hisp.dhis.i18n.I18n; import org.hisp.dhis.i18n.I18nFormat; +import org.hisp.dhis.indicator.Indicator; import org.hisp.dhis.options.SystemSettingManager; import org.hisp.dhis.organisationunit.OrganisationUnit; import org.hisp.dhis.period.Period; @@ -77,6 +79,8 @@ public class DefaultDataSetReportService implements DataSetReportService { + final Pattern INDICATOR_PATTERN = Pattern.compile( "indicatorid=\"(.*?)\"" ); + private static final String NULL_REPLACEMENT = ""; private static final String SEPARATOR = ":"; private static final String DEFAULT_HEADER = "Value"; @@ -133,13 +137,11 @@ FilterUtils.filter( dataElements, new AggregatableDataElementFilter() ); - Map map = new TreeMap(); + Map map = new TreeMap(); for ( DataElement dataElement : dataElements ) { - DataElementCategoryCombo categoryCombo = dataElement.getCategoryCombo(); - - for ( DataElementCategoryOptionCombo categoryOptionCombo : categoryCombo.getOptionCombos() ) + for ( DataElementCategoryOptionCombo categoryOptionCombo : dataElement.getCategoryCombo().getOptionCombos() ) { String value; @@ -167,53 +169,78 @@ return map; } - public String prepareReportContent( String dataEntryFormCode, Map dataValues ) + 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( String dataEntryFormCode, Map dataValues, Map indicatorValues ) { StringBuffer buffer = new StringBuffer(); - Matcher matDataElement = INPUT_PATTERN.matcher( dataEntryFormCode ); + Matcher inputMatcher = INPUT_PATTERN.matcher( dataEntryFormCode ); // --------------------------------------------------------------------- // Iterate through all matching data element fields. // --------------------------------------------------------------------- - while ( matDataElement.find() ) + while ( inputMatcher.find() ) { // ----------------------------------------------------------------- // Get input HTML code // ----------------------------------------------------------------- - String dataElementCode = matDataElement.group( 1 ); - - Matcher matDataElementId = IDENTIFIER_PATTERN.matcher( dataElementCode ); - - if ( matDataElementId.find() && matDataElementId.groupCount() > 0 ) - { - int dataElementId = Integer.parseInt( matDataElementId.group( 1 ) ); - int optionComboId = Integer.parseInt( matDataElementId.group( 2 ) ); - - // -------------------------------------------------------------- - // Find existing value of data element in data set and replace - // -------------------------------------------------------------- - - String dataElementValue = dataValues.get( dataElementId + SEPARATOR + optionComboId ); - - if ( dataElementValue == null ) - { - dataElementValue = NULL_REPLACEMENT; - } - - dataElementCode = dataElementValue; - - matDataElement.appendReplacement( buffer, dataElementCode ); + 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 ); } } - // --------------------------------------------------------------------- - // Add remaining text - // --------------------------------------------------------------------- - - matDataElement.appendTail( buffer ); + inputMatcher.appendTail( buffer ); return buffer.toString(); } === modified file 'dhis-2/dhis-web/dhis-web-reporting/src/main/java/org/hisp/dhis/reporting/dataset/action/GenerateCustomDataSetReportAction.java' --- dhis-2/dhis-web/dhis-web-reporting/src/main/java/org/hisp/dhis/reporting/dataset/action/GenerateCustomDataSetReportAction.java 2011-03-20 13:57:42 +0000 +++ dhis-2/dhis-web/dhis-web-reporting/src/main/java/org/hisp/dhis/reporting/dataset/action/GenerateCustomDataSetReportAction.java 2011-05-10 10:59:59 +0000 @@ -134,10 +134,12 @@ Map aggregatedDataValueMap = dataSetReportService.getAggregatedValueMap( selectedDataSet, selectedOrgunit, selectedPeriod, selectedUnitOnly, format ); + Map aggregatedIndicatorMap = dataSetReportService.getAggregatedIndicatorValueMap( selectedDataSet, selectedOrgunit, selectedPeriod, format ); + DataEntryForm dataEntryForm = selectedDataSet.getDataEntryForm(); customDataEntryFormCode = dataSetReportService.prepareReportContent( dataEntryForm.getHtmlCode(), - aggregatedDataValueMap ); + aggregatedDataValueMap, aggregatedIndicatorMap ); reportingUnit = selectedOrgunit.getName();