← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 20291: OrganisationUnitController, simplification using OrganisationUnitQueryParams

 

------------------------------------------------------------
revno: 20291
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2015-09-22 13:29:52 +0200
message:
  OrganisationUnitController, simplification using OrganisationUnitQueryParams
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitQueryParams.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/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-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitQueryParams.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitQueryParams.java	2015-09-17 13:42:30 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitQueryParams.java	2015-09-22 11:29:52 +0000
@@ -115,6 +115,14 @@
         return CODE_SEP_LENGTH * level;
     }
     
+    public void setLevel( Integer level )
+    {
+        if ( level != null )
+        {
+            levels.add( level );
+        }
+    }
+        
     @Override
     public String toString()
     {

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/organisationunit/OrganisationUnitController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/organisationunit/OrganisationUnitController.java	2015-09-22 11:13:07 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/organisationunit/OrganisationUnitController.java	2015-09-22 11:29:52 +0000
@@ -28,19 +28,28 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import com.fasterxml.jackson.core.JsonFactory;
-import com.fasterxml.jackson.core.JsonGenerator;
-import com.google.common.collect.Lists;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collections;
+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.common.Pager;
 import org.hisp.dhis.dxf2.common.TranslateParams;
 import org.hisp.dhis.organisationunit.FeatureType;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
+import org.hisp.dhis.organisationunit.OrganisationUnitQueryParams;
 import org.hisp.dhis.organisationunit.OrganisationUnitService;
 import org.hisp.dhis.organisationunit.comparator.OrganisationUnitByLevelComparator;
 import org.hisp.dhis.query.Order;
 import org.hisp.dhis.query.Query;
 import org.hisp.dhis.schema.descriptors.OrganisationUnitSchemaDescriptor;
 import org.hisp.dhis.user.User;
+import org.hisp.dhis.util.ObjectUtils;
 import org.hisp.dhis.version.VersionService;
 import org.hisp.dhis.webapi.controller.AbstractCrudController;
 import org.hisp.dhis.webapi.webdomain.WebMetaData;
@@ -53,14 +62,9 @@
 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.List;
-import java.util.Map;
-import java.util.Set;
+import com.fasterxml.jackson.core.JsonFactory;
+import com.fasterxml.jackson.core.JsonGenerator;
+import com.google.common.collect.Lists;
 
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
@@ -89,19 +93,6 @@
         Integer maxLevel = options.getInt( "maxLevel" );
         boolean levelSorted = options.isTrue( "levelSorted" );
 
-        if ( maxLevel != null )
-        {
-            if ( organisationUnitService.getOrganisationUnitLevelByLevel( maxLevel ) == null )
-            {
-                maxLevel = null;
-            }
-
-            if ( level == null )
-            {
-                level = 1;
-            }
-        }
-
         if ( options.isTrue( "userOnly" ) )
         {
             entityList = new ArrayList<>( currentUserService.getCurrentUser().getOrganisationUnits() );
@@ -123,23 +114,13 @@
                 entityList = new ArrayList<>( organisationUnitService.getOrganisationUnitsAtLevel( 1 ) );
             }
         }
-        else if ( maxLevel != null || level != null )
+        else if ( ObjectUtils.firstNonNull( level, maxLevel ) != null )
         {
-            entityList = new ArrayList<>();
-
-            if ( maxLevel == null )
-            {
-                entityList.addAll( organisationUnitService.getOrganisationUnitsAtLevel( level ) );
-            }
-            else
-            {
-                entityList.addAll( organisationUnitService.getOrganisationUnitsAtLevel( level ) );
-
-                while ( !level.equals( maxLevel ) )
-                {
-                    entityList.addAll( organisationUnitService.getOrganisationUnitsAtLevel( ++level ) );
-                }
-            }
+            OrganisationUnitQueryParams params = new OrganisationUnitQueryParams();
+            params.setLevel( level );
+            params.setMaxLevels( maxLevel );
+                        
+            entityList = organisationUnitService.getOrganisationUnitsByQuery( params );
         }
         else if ( levelSorted )
         {