← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 12977: allow filtering on properties.level / properties.parent in the FRED API

 

------------------------------------------------------------
revno: 12977
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2013-11-19 17:59:04 +0100
message:
  allow filtering on properties.level / properties.parent in the FRED API
modified:
  dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/controller/FacilityController.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	2013-11-19 16:48:11 +0000
+++ dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/controller/FacilityController.java	2013-11-19 16:59:04 +0000
@@ -256,6 +256,8 @@
         @RequestParam( value = "active", required = false ) List<Boolean> activeList,
         @RequestParam( value = "name", required = false ) List<String> nameList,
         @RequestParam( value = "uuid", required = false ) List<String> uuidList,
+        @RequestParam( value = "properties.parent", required = false ) List<String> parentList,
+        @RequestParam( value = "properties.level", required = false ) List<String> levelList,
         HttpServletRequest request, HttpServletResponse response ) throws IOException
     {
         Facilities facilities = new Facilities();
@@ -280,6 +282,8 @@
         filterByActiveList( activeList, allOrganisationUnits );
         filterByNameList( nameList, allOrganisationUnits );
         filterByUuidList( uuidList, allOrganisationUnits );
+        filterByPropertiesParent( parentList, allOrganisationUnits );
+        filterByPropertiesLevel( levelList, allOrganisationUnits );
 
         filterByLimit( offset, limitValue, allOrganisationUnits );
 
@@ -329,6 +333,76 @@
         allOrganisationUnits.addAll( organisationUnits );
     }
 
+    private void filterByPropertiesLevel( List<String> levelList, List<OrganisationUnit> allOrganisationUnits )
+    {
+        if ( levelList == null || levelList.isEmpty() )
+        {
+            return;
+        }
+
+        Iterator<OrganisationUnit> organisationUnitIterator = allOrganisationUnits.iterator();
+
+        while ( organisationUnitIterator.hasNext() )
+        {
+            OrganisationUnit organisationUnit = organisationUnitIterator.next();
+
+            boolean shouldRemove = true;
+
+            for ( String level : levelList )
+            {
+                try
+                {
+                    int l = Integer.parseInt( level );
+
+                    if ( organisationUnit.getOrganisationUnitLevel() == l )
+                    {
+                        shouldRemove = false;
+                        break;
+                    }
+                }
+                catch ( NumberFormatException ignored )
+                {
+                }
+            }
+
+            if ( shouldRemove )
+            {
+                organisationUnitIterator.remove();
+            }
+        }
+    }
+
+    private void filterByPropertiesParent( List<String> parentList, List<OrganisationUnit> allOrganisationUnits )
+    {
+        if ( parentList == null || parentList.isEmpty() )
+        {
+            return;
+        }
+
+        Iterator<OrganisationUnit> organisationUnitIterator = allOrganisationUnits.iterator();
+
+        while ( organisationUnitIterator.hasNext() )
+        {
+            OrganisationUnit organisationUnit = organisationUnitIterator.next();
+
+            boolean shouldRemove = true;
+
+            for ( String parent : parentList )
+            {
+                if ( organisationUnit.getParent() != null && organisationUnit.getParent().getUid().equals( parent ) )
+                {
+                    shouldRemove = false;
+                    break;
+                }
+            }
+
+            if ( shouldRemove )
+            {
+                organisationUnitIterator.remove();
+            }
+        }
+    }
+
     private void filterByUuidList( List<String> uuidList, List<OrganisationUnit> allOrganisationUnits )
     {
         if ( uuidList == null || uuidList.isEmpty() )