=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/mapping/GeoFeatureController.java' --- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/mapping/GeoFeatureController.java 2015-08-04 11:09:17 +0000 +++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/mapping/GeoFeatureController.java 2015-08-05 14:30:34 +0000 @@ -28,6 +28,18 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + import org.hisp.dhis.analytics.AggregationType; import org.hisp.dhis.analytics.AnalyticsService; import org.hisp.dhis.analytics.DataQueryParams; @@ -52,17 +64,7 @@ import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; +import com.google.common.collect.ImmutableMap; /** * @author Lars Helge Overland @@ -73,16 +75,11 @@ { public static final String RESOURCE_PATH = "/geoFeatures"; - private static final Map FEATURE_TYPE_MAP = new HashMap() - { - { - put( OrganisationUnit.FEATURETYPE_POINT, GeoFeature.TYPE_POINT ); - put( OrganisationUnit.FEATURETYPE_MULTIPOLYGON, GeoFeature.TYPE_POLYGON ); - put( OrganisationUnit.FEATURETYPE_POLYGON, GeoFeature.TYPE_POLYGON ); - put( null, 0 ); - } - }; - + private static final Map FEATURE_TYPE_MAP = ImmutableMap.builder(). + put( OrganisationUnit.FEATURETYPE_POINT, GeoFeature.TYPE_POINT ). + put( OrganisationUnit.FEATURETYPE_MULTIPOLYGON, GeoFeature.TYPE_POLYGON ). + put( OrganisationUnit.FEATURETYPE_POLYGON, GeoFeature.TYPE_POLYGON ).build(); + @Autowired private AnalyticsService analyticsService; @@ -95,10 +92,15 @@ @Autowired private CurrentUserService currentUserService; + // ------------------------------------------------------------------------- + // Resources + // ------------------------------------------------------------------------- + @RequestMapping( method = RequestMethod.GET, produces = { ContextUtils.CONTENT_TYPE_JSON, ContextUtils.CONTENT_TYPE_HTML } ) public void getGeoFeaturesJson( @RequestParam String ou, @RequestParam( required = false ) DisplayProperty displayProperty, + @RequestParam( required = false ) String userOrgUnit, @RequestParam( defaultValue = "false", value = "includeGroupSets" ) boolean rpIncludeGroupSets, @RequestParam Map parameters, HttpServletRequest request, HttpServletResponse response ) throws IOException @@ -106,8 +108,12 @@ WebOptions options = new WebOptions( parameters ); boolean includeGroupSets = "detailed".equals( options.getViewClass() ) || rpIncludeGroupSets; - List features = getGeoFeatures( ou, displayProperty, request, response, includeGroupSets ); - if ( features == null ) return; + List features = getGeoFeatures( ou, displayProperty, userOrgUnit, request, response, includeGroupSets ); + + if ( features == null ) + { + return; + } response.setContentType( MediaType.APPLICATION_JSON_VALUE ); renderService.toJson( response.getOutputStream(), features ); @@ -117,6 +123,7 @@ public void getGeoFeaturesJsonP( @RequestParam String ou, @RequestParam( required = false ) DisplayProperty displayProperty, + @RequestParam( required = false ) String userOrgUnit, @RequestParam( defaultValue = "callback" ) String callback, @RequestParam( defaultValue = "false", value = "includeGroupSets" ) boolean rpIncludeGroupSets, @RequestParam Map parameters, @@ -125,20 +132,41 @@ WebOptions options = new WebOptions( parameters ); boolean includeGroupSets = "detailed".equals( options.getViewClass() ) || rpIncludeGroupSets; - List features = getGeoFeatures( ou, displayProperty, request, response, includeGroupSets ); - if ( features == null ) return; + List features = getGeoFeatures( ou, displayProperty, userOrgUnit, request, response, includeGroupSets ); + + if ( features == null ) + { + return; + } response.setContentType( "application/javascript" ); renderService.toJsonP( response.getOutputStream(), features, callback ); } - private List getGeoFeatures( String ou, DisplayProperty displayProperty, HttpServletRequest request, HttpServletResponse response, boolean includeGroupSets ) + // ------------------------------------------------------------------------- + // Supportive methods + // ------------------------------------------------------------------------- + + /** + * Returns list of geo features. Returns null if not modified based on the + * request. + * + * @param ou the organisation unit parameter. + * @param displayProperty the display property. + * @param userOrgUnit the user organisation unit parameter. + * @param request the HTTP request. + * @param response the HTTP response. + * @param includeGroupSets whether to include organisation unit group sets. + * @return a list of geo features or null. + */ + private List getGeoFeatures( String ou, DisplayProperty displayProperty, + String userOrgUnit, HttpServletRequest request, HttpServletResponse response, boolean includeGroupSets ) { Set set = new HashSet<>(); set.add( ou ); DataQueryParams params = analyticsService.getFromUrl( set, null, AggregationType.SUM, null, - false, false, false, false, false, false, false, displayProperty, null, null, null, null, null, null ); + false, false, false, false, false, false, false, displayProperty, null, null, userOrgUnit, null, null, null ); DimensionalObject dim = params.getDimension( DimensionalObject.ORGUNIT_DIM_ID ); @@ -159,34 +187,34 @@ Set roots = currentUserService.getCurrentUser().getDataViewOrganisationUnitsWithFallback(); - for ( OrganisationUnit organisationUnit : organisationUnits ) + for ( OrganisationUnit unit : organisationUnits ) { GeoFeature feature = new GeoFeature(); - feature.setId( organisationUnit.getUid() ); - feature.setCode( organisationUnit.getCode() ); - feature.setHcd( organisationUnit.hasChildrenWithCoordinates() ); - feature.setHcu( organisationUnit.hasCoordinatesUp() ); - feature.setLe( organisationUnit.getLevel() ); - feature.setPg( organisationUnit.getParentGraph( roots ) ); - feature.setPi( organisationUnit.getParent() != null ? organisationUnit.getParent().getUid() : null ); - feature.setPn( organisationUnit.getParent() != null ? organisationUnit.getParent().getDisplayName() : null ); - feature.setTy( FEATURE_TYPE_MAP.get( organisationUnit.getFeatureType() ) ); - feature.setCo( organisationUnit.getCoordinates() ); + feature.setId( unit.getUid() ); + feature.setCode( unit.getCode() ); + feature.setHcd( unit.hasChildrenWithCoordinates() ); + feature.setHcu( unit.hasCoordinatesUp() ); + feature.setLe( unit.getLevel() ); + feature.setPg( unit.getParentGraph( roots ) ); + feature.setPi( unit.getParent() != null ? unit.getParent().getUid() : null ); + feature.setPn( unit.getParent() != null ? unit.getParent().getDisplayName() : null ); + feature.setTy( unit.getFeatureType() != null ? FEATURE_TYPE_MAP.get( unit.getFeatureType() ) : 0 ); + feature.setCo( unit.getCoordinates() ); if ( DisplayProperty.SHORTNAME.equals( params.getDisplayProperty() ) ) { - feature.setNa( organisationUnit.getDisplayShortName() ); + feature.setNa( unit.getDisplayShortName() ); } else { - feature.setNa( organisationUnit.getDisplayName() ); + feature.setNa( unit.getDisplayName() ); } if ( includeGroupSets ) { for ( OrganisationUnitGroupSet groupSet : groupSets ) { - OrganisationUnitGroup group = organisationUnit.getGroupInGroupSet( groupSet ); + OrganisationUnitGroup group = unit.getGroupInGroupSet( groupSet ); if ( group != null ) { @@ -199,6 +227,7 @@ } Collections.sort( features, GeoFeatureTypeComparator.INSTANCE ); + return features; }