← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 12830: add new parameter to /api/organisationUnits and /api/organisationUnits/{uid} called maxLevel=X, g...

 

------------------------------------------------------------
revno: 12830
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2013-10-30 11:44:00 +0100
message:
  add new parameter to /api/organisationUnits and /api/organisationUnits/{uid} called maxLevel=X, gives X number of levels starting from root or selected orgUnit
modified:
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/organisationunit/OrganisationUnitController.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/src/main/java/org/hisp/dhis/api/controller/organisationunit/OrganisationUnitController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/organisationunit/OrganisationUnitController.java	2013-10-11 13:01:38 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/organisationunit/OrganisationUnitController.java	2013-10-30 10:44:00 +0000
@@ -83,11 +83,28 @@
 
         Integer level = null;
 
+        Integer maxLevel = null;
+
         if ( options.getOptions().containsKey( "level" ) )
         {
             level = Integer.parseInt( options.getOptions().get( "level" ) );
         }
 
+        if ( options.getOptions().containsKey( "maxLevel" ) )
+        {
+            maxLevel = Integer.parseInt( options.getOptions().get( "maxLevel" ) );
+
+            if ( organisationUnitService.getOrganisationUnitLevelByLevel( maxLevel ) == null )
+            {
+                maxLevel = null;
+            }
+
+            if ( level == null )
+            {
+                level = 1;
+            }
+        }
+
         if ( "true".equals( options.getOptions().get( "userOnly" ) ) )
         {
             entityList = new ArrayList<OrganisationUnit>( currentUserService.getCurrentUser().getOrganisationUnits() );
@@ -101,9 +118,23 @@
                 Collections.sort( entityList, OrganisationUnitByLevelComparator.INSTANCE );
             }
         }
-        else if ( level != null )
+        else if ( maxLevel != null || level != null )
         {
-            entityList = new ArrayList<OrganisationUnit>( organisationUnitService.getOrganisationUnitsAtLevel( level ) );
+            entityList = new ArrayList<OrganisationUnit>();
+
+            if ( maxLevel == null )
+            {
+                entityList.addAll( organisationUnitService.getOrganisationUnitsAtLevel( level ) );
+            }
+            else
+            {
+                entityList.addAll( organisationUnitService.getOrganisationUnitsAtLevel( level ) );
+
+                while ( !level.equals( maxLevel ) )
+                {
+                    entityList.addAll( organisationUnitService.getOrganisationUnitsAtLevel( ++level ) );
+                }
+            }
         }
         else if ( levelSorted )
         {
@@ -128,8 +159,8 @@
     }
 
     @Override
-    @RequestMapping(value = "/{uid}", method = RequestMethod.GET)
-    public String getObject( @PathVariable("uid") String uid, @RequestParam Map<String, String> parameters,
+    @RequestMapping( value = "/{uid}", method = RequestMethod.GET )
+    public String getObject( @PathVariable( "uid" ) String uid, @RequestParam Map<String, String> parameters,
         Model model, HttpServletRequest request, HttpServletResponse response ) throws Exception
     {
         WebOptions options = new WebOptions( parameters );
@@ -140,49 +171,45 @@
             throw new NotFoundException( uid );
         }
 
-        if ( options.getOptions().containsKey( "level" ) )
+        Integer maxLevel = null;
+
+        if ( options.getOptions().containsKey( "maxLevel" ) )
         {
-            int level = -1;
-
             try
             {
-                level = Integer.parseInt( options.getOptions().get( "level" ) );
-            }
-            catch ( Exception e )
-            {
-                level = entity.getOrganisationUnitLevel();
-            }
-
-            if ( level < 1 || level > organisationUnitService.getNumberOfOrganisationalLevels() )
-            {
-                level = entity.getOrganisationUnitLevel();
-            }
-
-            if ( level == entity.getOrganisationUnitLevel() )
-            {
-                model.addAttribute( "model", entity );
-            }
-            else if ( level < entity.getOrganisationUnitLevel() )
-            {
-                while ( level < entity.getOrganisationUnitLevel() )
-                {
-                    entity = entity.getParent();
-                }
-
-                model.addAttribute( "model", entity );
-            }
-            else
-            {
-                List<OrganisationUnit> entities = new ArrayList<OrganisationUnit>(
-                    organisationUnitService.getOrganisationUnitsAtLevel( level, entity ) );
-
-                MetaData metaData = new MetaData();
-                metaData.setOrganisationUnits( entities );
-
-                model.addAttribute( "model", metaData );
-            }
-        }
-        if ( options.getOptions().containsKey( "includeDescendants" ) && Boolean.parseBoolean( options.getOptions().get( "includeDescendants" ) ) )
+                maxLevel = Integer.parseInt( options.getOptions().get( "maxLevel" ) );
+            }
+            catch ( NumberFormatException ignored )
+            {
+            }
+
+            if ( maxLevel != null && (organisationUnitService.getOrganisationUnitLevelByLevel( maxLevel ) == null
+                || maxLevel > organisationUnitService.getNumberOfOrganisationalLevels()) )
+            {
+                maxLevel = organisationUnitService.getNumberOfOrganisationalLevels();
+            }
+        }
+
+        if ( maxLevel != null )
+        {
+            List<OrganisationUnit> entities = new ArrayList<OrganisationUnit>();
+            entities.add( entity );
+
+            int level = entity.getOrganisationUnitLevel();
+
+            while ( maxLevel > level )
+            {
+                entities.addAll( organisationUnitService.getOrganisationUnitsAtLevel( ++level, entity ) );
+            }
+
+            MetaData metaData = new MetaData();
+            metaData.setOrganisationUnits( entities );
+
+            model.addAttribute( "model", metaData );
+
+            return StringUtils.uncapitalize( getEntitySimpleName() );
+        }
+        else if ( options.getOptions().containsKey( "includeDescendants" ) && Boolean.parseBoolean( options.getOptions().get( "includeDescendants" ) ) )
         {
             List<OrganisationUnit> entities = new ArrayList<OrganisationUnit>(
                 organisationUnitService.getOrganisationUnitsWithChildren( uid ) );
@@ -192,7 +219,7 @@
 
             model.addAttribute( "model", metaData );
         }
-        if ( options.getOptions().containsKey( "includeChildren" ) && Boolean.parseBoolean( options.getOptions().get( "includeChildren" ) ) )
+        else if ( options.getOptions().containsKey( "includeChildren" ) && Boolean.parseBoolean( options.getOptions().get( "includeChildren" ) ) )
         {
             List<OrganisationUnit> entities = new ArrayList<OrganisationUnit>();
             entities.add( entity );