← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 9293: FRED-API: basic filtering using ?fields=field1, field2 etc. Does not support sub-filtering for pro...

 

------------------------------------------------------------
revno: 9293
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2012-12-13 12:18:43 +0300
message:
  FRED-API: basic filtering using ?fields=field1,field2 etc. Does not support sub-filtering for properties.
modified:
  dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/controller/FacilityController.java
  dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/utils/ToFacilityConverter.java


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/controller/FacilityController.java'
--- dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/controller/FacilityController.java	2012-12-12 07:57:09 +0000
+++ dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/controller/FacilityController.java	2012-12-13 09:18:43 +0000
@@ -52,7 +52,13 @@
 import org.springframework.stereotype.Controller;
 import org.springframework.ui.Model;
 import org.springframework.web.bind.WebDataBinder;
-import org.springframework.web.bind.annotation.*;
+import org.springframework.web.bind.annotation.ExceptionHandler;
+import org.springframework.web.bind.annotation.InitBinder;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.RequestParam;
 
 import javax.validation.ConstraintViolation;
 import javax.validation.Validator;
@@ -61,7 +67,12 @@
 import java.io.IOException;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Date;
+import java.util.List;
+import java.util.Set;
 
 import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;
 
@@ -129,9 +140,77 @@
     // GET HTML
     //--------------------------------------------------------------------------
 
+    private Facility filterFacility( Facility facility, boolean allProperties, String fields )
+    {
+        // if allProperties=false is added, filter away the properties block, and don't care about fields
+        if ( !allProperties )
+        {
+            facility.setProperties( null );
+
+            return facility;
+        }
+
+        if ( fields == null )
+        {
+            return facility;
+        }
+
+        List<String> strings = Arrays.asList( fields.split( "," ) );
+
+        // simple field filtering
+        if ( !strings.contains( "id" ) )
+        {
+            facility.setId( null );
+        }
+
+        if ( !strings.contains( "name" ) )
+        {
+            facility.setName( null );
+        }
+
+        if ( !strings.contains( "active" ) )
+        {
+            facility.setActive( null );
+        }
+
+        if ( !strings.contains( "createdAt" ) )
+        {
+            facility.setCreatedAt( null );
+        }
+
+        if ( !strings.contains( "updatedAt" ) )
+        {
+            facility.setUpdatedAt( null );
+        }
+
+        if ( !strings.contains( "coordinates" ) )
+        {
+            facility.setCoordinates( null );
+        }
+
+        if ( !strings.contains( "url" ) )
+        {
+            facility.setUrl( null );
+        }
+
+        if ( !strings.contains( "identifiers" ) )
+        {
+            facility.setIdentifiers( null );
+        }
+
+        if ( !strings.contains( "properties" ) )
+        {
+            facility.setProperties( null );
+        }
+
+        return facility;
+    }
+
     @RequestMapping( value = "", method = RequestMethod.GET )
     public String readFacilities( Model model, @RequestParam( required = false ) Boolean active,
-        @RequestParam( value = "updatedSince", required = false ) Date lastUpdated )
+        @RequestParam( value = "updatedSince", required = false ) Date lastUpdated,
+        @RequestParam( value = "allProperties", required = false, defaultValue = "true" ) Boolean allProperties,
+        @RequestParam( value = "fields", required = false ) String fields )
     {
         Facilities facilities = new Facilities();
         List<OrganisationUnit> allOrganisationUnits;
@@ -158,6 +237,7 @@
         for ( OrganisationUnit organisationUnit : allOrganisationUnits )
         {
             Facility facility = conversionService.convert( organisationUnit, Facility.class );
+            filterFacility( facility, allProperties, fields );
 
             facilities.getFacilities().add( facility );
         }
@@ -174,11 +254,14 @@
     }
 
     @RequestMapping( value = "/{id}", method = RequestMethod.GET )
-    public String readFacility( Model model, @PathVariable String id )
+    public String readFacility( Model model, @PathVariable String id,
+        @RequestParam( value = "allProperties", required = false, defaultValue = "true" ) Boolean allProperties,
+        @RequestParam( value = "fields", required = false ) String fields )
     {
         OrganisationUnit organisationUnit = organisationUnitService.getOrganisationUnit( id );
 
         Facility facility = conversionService.convert( organisationUnit, Facility.class );
+        filterFacility( facility, allProperties, fields );
 
         setAccessRights( model );
 

=== modified file 'dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/utils/ToFacilityConverter.java'
--- dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/utils/ToFacilityConverter.java	2012-12-09 16:55:20 +0000
+++ dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/utils/ToFacilityConverter.java	2012-12-13 09:18:43 +0000
@@ -29,14 +29,21 @@
 
 import org.hisp.dhis.dataset.DataSet;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
+import org.hisp.dhis.organisationunit.OrganisationUnitLevel;
+import org.hisp.dhis.organisationunit.OrganisationUnitService;
+import org.hisp.dhis.organisationunit.comparator.OrganisationUnitLevelComparator;
 import org.hisp.dhis.web.webapi.v1.controller.FacilityController;
 import org.hisp.dhis.web.webapi.v1.domain.Facility;
 import org.hisp.dhis.web.webapi.v1.domain.Identifier;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.core.convert.converter.Converter;
 import org.springframework.stereotype.Component;
 
 import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
 import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;
 
@@ -46,6 +53,9 @@
 @Component
 public class ToFacilityConverter implements Converter<OrganisationUnit, Facility>
 {
+    @Autowired
+    private OrganisationUnitService organisationUnitService;
+
     @Override
     public Facility convert( OrganisationUnit organisationUnit )
     {
@@ -99,6 +109,25 @@
             facility.getProperties().put( "dataSets", dataSets );
         }
 
+        facility.getProperties().put( "level", organisationUnit.getOrganisationUnitLevel() );
+
+        List<OrganisationUnitLevel> organisationUnitLevels = organisationUnitService.getOrganisationUnitLevels();
+        Collections.sort( organisationUnitLevels, new OrganisationUnitLevelComparator() );
+
+        // TODO this probably belongs in "meta": {}
+        List<Map<String, Object>> hierarchy = new ArrayList<Map<String, Object>>();
+        facility.getProperties().put( "hierarchy", hierarchy );
+
+        for ( OrganisationUnitLevel organisationUnitLevel : organisationUnitLevels )
+        {
+            Map<String, Object> level = new HashMap<String, Object>();
+
+            level.put( "name", organisationUnitLevel.getName() );
+            level.put( "level", organisationUnitLevel.getLevel() );
+
+            hierarchy.add( level );
+        }
+
         return facility;
     }
 }