← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 9242: updated OrgUnitByLevelComparator to not rely on pre-populated level field. Added 'levelSorted' pa...

 

------------------------------------------------------------
revno: 9242
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Sun 2012-12-09 17:31:03 +0300
message:
  updated OrgUnitByLevelComparator to not rely on pre-populated level field. Added 'levelSorted' parameter to OrgUnitController, set to true if you want orgUnits sorted by level.
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/comparator/OrganisationUnitByLevelComparator.java
  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-api/src/main/java/org/hisp/dhis/organisationunit/comparator/OrganisationUnitByLevelComparator.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/comparator/OrganisationUnitByLevelComparator.java	2012-06-28 09:21:53 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/comparator/OrganisationUnitByLevelComparator.java	2012-12-09 14:31:03 +0000
@@ -40,6 +40,6 @@
     @Override
     public int compare( OrganisationUnit o1, OrganisationUnit o2 )
     {
-        return o1.getLevel() - o2.getLevel();
+        return Integer.compare( o1.getOrganisationUnitLevel(), o2.getOrganisationUnitLevel() );
     }
 }

=== 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	2012-12-06 12:27:30 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/organisationunit/OrganisationUnitController.java	2012-12-09 14:31:03 +0000
@@ -28,12 +28,15 @@
  */
 
 import org.hisp.dhis.api.controller.AbstractCrudController;
+import org.hisp.dhis.api.controller.WebMetaData;
 import org.hisp.dhis.api.controller.WebOptions;
 import org.hisp.dhis.api.utils.ContextUtils;
 import org.hisp.dhis.api.utils.WebUtils;
+import org.hisp.dhis.common.Pager;
 import org.hisp.dhis.dxf2.metadata.MetaData;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.organisationunit.OrganisationUnitService;
+import org.hisp.dhis.organisationunit.comparator.OrganisationUnitByLevelComparator;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Controller;
 import org.springframework.ui.Model;
@@ -45,15 +48,13 @@
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
  */
 @Controller
-@RequestMapping(value = OrganisationUnitController.RESOURCE_PATH)
+@RequestMapping( value = OrganisationUnitController.RESOURCE_PATH )
 public class OrganisationUnitController
     extends AbstractCrudController<OrganisationUnit>
 {
@@ -63,6 +64,39 @@
     private OrganisationUnitService organisationUnitService;
 
     @Override
+    protected List<OrganisationUnit> getEntityList( WebMetaData metaData, WebOptions options )
+    {
+        List<OrganisationUnit> entityList;
+
+        Date lastUpdated = options.getLastUpdated();
+
+        if ( options.getOptions().containsKey( "levelSorted" ) && Boolean.parseBoolean( options.getOptions().get( "levelSorted" ) ) )
+        {
+            entityList = new ArrayList<OrganisationUnit>( manager.getAll( getEntityClass() ) );
+            Collections.sort( entityList, new OrganisationUnitByLevelComparator() );
+        }
+        else if ( lastUpdated != null )
+        {
+            entityList = new ArrayList<OrganisationUnit>( manager.getByLastUpdatedSorted( getEntityClass(), lastUpdated ) );
+        }
+        else if ( options.hasPaging() )
+        {
+            int count = manager.getCount( getEntityClass() );
+
+            Pager pager = new Pager( options.getPage(), count, options.getPageSize() );
+            metaData.setPager( pager );
+
+            entityList = new ArrayList<OrganisationUnit>( manager.getBetween( getEntityClass(), pager.getOffset(), pager.getPageSize() ) );
+        }
+        else
+        {
+            entityList = new ArrayList<OrganisationUnit>( manager.getAllSorted( getEntityClass() ) );
+        }
+
+        return entityList;
+    }
+
+    @Override
     @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