=== modified file 'dhis-2/dhis-services/dhis-service-mapping/src/main/java/org/hisp/dhis/mapping/DefaultMappingService.java' --- dhis-2/dhis-services/dhis-service-mapping/src/main/java/org/hisp/dhis/mapping/DefaultMappingService.java 2011-01-04 09:54:15 +0000 +++ dhis-2/dhis-services/dhis-service-mapping/src/main/java/org/hisp/dhis/mapping/DefaultMappingService.java 2011-01-07 12:11:50 +0000 @@ -130,30 +130,7 @@ public Collection getIndicatorMapValues( int indicatorId, Date startDate, Date endDate, int parentOrganisationUnitId ) { - Collection values = new HashSet(); - - OrganisationUnit parent = organisationUnitService.getOrganisationUnit( parentOrganisationUnitId ); - Indicator indicator = indicatorService.getIndicator( indicatorId ); - - for ( OrganisationUnit organisationUnit : parent.getChildren() ) - { - if ( organisationUnit.hasCoordinates() ) - { - Double value = aggregationService.getAggregatedIndicatorValue( indicator, startDate, endDate, - organisationUnit ); - - value = value != null ? value : 0; // TODO improve - - AggregatedMapValue mapValue = new AggregatedMapValue(); - mapValue.setOrganisationUnitId( organisationUnit.getId() ); - mapValue.setOrganisationUnitName( organisationUnit.getName() ); - mapValue.setValue( MathUtils.getRounded( value, 2 ) ); - - values.add( mapValue ); - } - } - - return values; + return getIndicatorMapValuesInternal( indicatorId, startDate, endDate, parentOrganisationUnitId, null ); } public Collection getIndicatorMapValues( int indicatorId, int periodId, @@ -168,34 +145,7 @@ public Collection getIndicatorMapValues( int indicatorId, Date startDate, Date endDate, int parentOrganisationUnitId, int level ) { - Collection values = new HashSet(); - - Indicator indicator = indicatorService.getIndicator( indicatorId ); - - OrganisationUnit parent = organisationUnitService.getOrganisationUnit( parentOrganisationUnitId ); - - Collection organisationUnits = organisationUnitService.getOrganisationUnitsAtLevel( level, - parent ); - - for ( OrganisationUnit organisationUnit : organisationUnits ) - { - if ( organisationUnit.hasCoordinates() ) - { - Double value = aggregationService.getAggregatedIndicatorValue( indicator, startDate, endDate, - organisationUnit ); - - value = value != null ? value : 0; // TODO improve - - AggregatedMapValue mapValue = new AggregatedMapValue(); - mapValue.setOrganisationUnitId( organisationUnit.getId() ); - mapValue.setOrganisationUnitName( organisationUnit.getName() ); - mapValue.setValue( MathUtils.getRounded( value, 2 ) ); - - values.add( mapValue ); - } - } - - return values; + return getIndicatorMapValuesInternal( indicatorId, startDate, endDate, parentOrganisationUnitId, level ); } public Collection getIndicatorMapValuesByLevel( int indicatorId, int periodId, int level ) @@ -204,22 +154,44 @@ return getIndicatorMapValuesByLevel( indicatorId, period.getStartDate(), period.getEndDate(), level ); } - - public Collection getIndicatorMapValuesByLevel( int indicatorId, Date startDate, Date endDate, + + public Collection getIndicatorMapValuesByLevel( int indicatorId, Date startDate, Date endDate, int level ) { + return getIndicatorMapValuesInternal( indicatorId, startDate, endDate, null, level ); + } + + private Collection getIndicatorMapValuesInternal( Integer indicatorId, Date startDate, Date endDate, + Integer parentOrganisationUnitId, Integer level ) + { Collection values = new HashSet(); - Collection organisationUnits = organisationUnitService.getOrganisationUnitsAtLevel( level ); - Indicator indicator = indicatorService.getIndicator( indicatorId ); + + Collection organisationUnits = null; + + if ( parentOrganisationUnitId != null && level != null ) + { + organisationUnits = organisationUnitService.getOrganisationUnitsAtLevel( level, organisationUnitService.getOrganisationUnit( parentOrganisationUnitId ) ); + } + else if ( level != null ) + { + organisationUnits = organisationUnitService.getOrganisationUnitsAtLevel( level ); + } + else if ( parentOrganisationUnitId != null ) + { + organisationUnits = organisationUnitService.getOrganisationUnit( parentOrganisationUnitId ).getChildren(); + } + else + { + throw new IllegalArgumentException( "Parent organisation unit or level must be specified" ); + } for ( OrganisationUnit organisationUnit : organisationUnits ) { if ( organisationUnit.hasCoordinates() ) { - Double value = aggregationService.getAggregatedIndicatorValue( indicator, startDate, endDate, - organisationUnit ); + Double value = aggregationService.getAggregatedIndicatorValue( indicator, startDate, endDate, organisationUnit ); value = value != null ? value : 0; // TODO improve @@ -232,9 +204,9 @@ } } - return values; + return values; } - + // ------------------------------------------------------------------------- // DataMapValues // ------------------------------------------------------------------------- @@ -251,30 +223,7 @@ public Collection getDataElementMapValues( int dataElementId, Date startDate, Date endDate, int parentOrganisationUnitId ) { - Collection values = new HashSet(); - - OrganisationUnit parent = organisationUnitService.getOrganisationUnit( parentOrganisationUnitId ); - DataElement dataElement = dataElementService.getDataElement( dataElementId ); - - for ( OrganisationUnit organisationUnit : parent.getChildren() ) - { - if ( organisationUnit.hasCoordinates() ) - { - Double value = aggregationService.getAggregatedDataValue( dataElement, null, startDate, endDate, - organisationUnit ); - - value = value != null ? value : 0; // TODO improve - - AggregatedMapValue mapValue = new AggregatedMapValue(); - mapValue.setOrganisationUnitId( organisationUnit.getId() ); - mapValue.setOrganisationUnitName( organisationUnit.getName() ); - mapValue.setValue( MathUtils.getRounded( value, 2 ) ); - - values.add( mapValue ); - } - } - - return values; + return getDataElementMapValuesInternal( dataElementId, startDate, endDate, parentOrganisationUnitId, null ); } public Collection getDataElementMapValues( int dataElementId, int periodId, @@ -289,34 +238,7 @@ public Collection getDataElementMapValues( int dataElementId, Date startDate, Date endDate, int parentOrganisationUnitId, int level ) { - Collection values = new HashSet(); - - DataElement dataElement = dataElementService.getDataElement( dataElementId ); - - OrganisationUnit parent = organisationUnitService.getOrganisationUnit( parentOrganisationUnitId ); - - Collection organisationUnits = organisationUnitService.getOrganisationUnitsAtLevel( level, - parent ); - - for ( OrganisationUnit organisationUnit : organisationUnits ) - { - if ( organisationUnit.hasCoordinates() ) - { - Double value = aggregationService.getAggregatedDataValue( dataElement, null, startDate, endDate, - organisationUnit ); - - value = value != null ? value : 0; // TODO improve - - AggregatedMapValue mapValue = new AggregatedMapValue(); - mapValue.setOrganisationUnitId( organisationUnit.getId() ); - mapValue.setOrganisationUnitName( organisationUnit.getName() ); - mapValue.setValue( MathUtils.getRounded( value, 2 ) ); - - values.add( mapValue ); - } - } - - return values; + return getDataElementMapValuesInternal( dataElementId, startDate, endDate, parentOrganisationUnitId, level ); } public Collection getDataElementMapValuesByLevel( int dataElementId, int periodId, int level ) @@ -329,11 +251,34 @@ public Collection getDataElementMapValuesByLevel( int dataElementId, Date startDate, Date endDate, int level ) { + return getDataElementMapValuesInternal( dataElementId, startDate, endDate, null, level ); + } + + public Collection getDataElementMapValuesInternal( Integer dataElementId, Date startDate, Date endDate, + Integer parentOrganisationUnitId, Integer level ) + { Collection values = new HashSet(); - Collection organisationUnits = organisationUnitService.getOrganisationUnitsAtLevel( level ); - DataElement dataElement = dataElementService.getDataElement( dataElementId ); + + Collection organisationUnits = null; + + if ( parentOrganisationUnitId != null && level != null ) + { + organisationUnits = organisationUnitService.getOrganisationUnitsAtLevel( level, organisationUnitService.getOrganisationUnit( parentOrganisationUnitId ) ); + } + else if ( level != null ) + { + organisationUnits = organisationUnitService.getOrganisationUnitsAtLevel( level ); + } + else if ( parentOrganisationUnitId != null ) + { + organisationUnits = organisationUnitService.getOrganisationUnit( parentOrganisationUnitId ).getChildren(); + } + else + { + throw new IllegalArgumentException( "Parent organisation unit or level must be specified" ); + } for ( OrganisationUnit organisationUnit : organisationUnits ) { @@ -353,9 +298,9 @@ } } - return values; + return values; } - + // ------------------------------------------------------------------------- // MapLegend // -------------------------------------------------------------------------