← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 13510: Simplified add/remove org units to groups

 

------------------------------------------------------------
revno: 13510
committer: Lars Helge Øverland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2013-12-31 09:17:28 +0100
message:
  Simplified add/remove org units to groups
modified:
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/organisationunit/OrganisationUnitGroupController.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/OrganisationUnitGroupController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/organisationunit/OrganisationUnitGroupController.java	2013-12-31 04:23:11 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/organisationunit/OrganisationUnitGroupController.java	2013-12-31 08:17:28 +0000
@@ -29,7 +29,6 @@
  */
 
 import java.util.Map;
-import java.util.Set;
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
@@ -44,6 +43,7 @@
 import org.hisp.dhis.organisationunit.OrganisationUnitGroupService;
 import org.hisp.dhis.organisationunit.OrganisationUnitService;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.HttpStatus;
 import org.springframework.security.access.prepost.PreAuthorize;
 import org.springframework.stereotype.Controller;
 import org.springframework.ui.Model;
@@ -52,6 +52,7 @@
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.ResponseStatus;
 
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
@@ -109,71 +110,35 @@
 
     @RequestMapping( value = "/{uid}/members/{orgUnitUid}", method = RequestMethod.POST )
     @PreAuthorize( "hasRole('ALL') or hasRole('F_ORGANISATIONUNIT_ADD')" )
+    @ResponseStatus( value = HttpStatus.NO_CONTENT )
     public void addMember( HttpServletResponse response, HttpServletRequest request, @PathVariable( "uid" )
     String uid, @PathVariable( "orgUnitUid" )
     String orgUnitUid )
         throws Exception
     {
-        Boolean existsInList = false;
-
-        // Add the relationship and commit the change.
-        OrganisationUnitGroup organisationUnitGroup = getEntity( uid );
-
-        Set<OrganisationUnit> orgUnitList = organisationUnitGroup.getMembers();
-
-        if ( orgUnitList != null && orgUnitList.size() > 0 )
-        {
-            for ( OrganisationUnit orgUnit_temp : orgUnitList )
-            {
-                if ( orgUnit_temp.getUid().equals( orgUnitUid ) )
-                {
-                    existsInList = true;
-                    break;
-                }
-            }
-        }
-
-        if ( !existsInList )
-        {
-            organisationUnitGroup.addOrganisationUnit( organisationUnitService.getOrganisationUnit( orgUnitUid ) );
-
-            organisationUnitGroupService.updateOrganisationUnitGroup( organisationUnitGroup );
-        }
-
+        OrganisationUnitGroup group = organisationUnitGroupService.getOrganisationUnitGroup( uid );
+        OrganisationUnit unit = organisationUnitService.getOrganisationUnit( orgUnitUid );
+        
+        if ( group.addOrganisationUnit( unit ) )
+        {
+            organisationUnitGroupService.updateOrganisationUnitGroup( group );
+        }
     }
 
     @RequestMapping( value = "/{uid}/members/{orgUnitUid}", method = RequestMethod.DELETE )
     @PreAuthorize( "hasRole('ALL') or hasRole('F_ORGANISATIONUNIT_ADD')" )
-    public void deleteMember( HttpServletResponse response, HttpServletRequest request, @PathVariable( "uid" )
+    @ResponseStatus( value = HttpStatus.NO_CONTENT )
+    public void removeMember( HttpServletResponse response, HttpServletRequest request, @PathVariable( "uid" )
     String uid, @PathVariable( "orgUnitUid" )
     String orgUnitUid )
         throws Exception
     {
-        OrganisationUnit orgUnit = null;
-
-        OrganisationUnitGroup organisationUnitGroup = getEntity( uid );
-
-        Set<OrganisationUnit> orgUnitList = organisationUnitGroup.getMembers();
-
-        // Remove the relationship and commit the change.
-        if ( orgUnitList != null && orgUnitList.size() > 0 )
-        {
-            for ( OrganisationUnit orgUnit_temp : orgUnitList )
-            {
-                if ( orgUnit_temp.getUid().equals( orgUnitUid ) )
-                {
-                    orgUnit = orgUnit_temp;
-                    break;
-                }
-            }
-        }
-
-        if ( orgUnit != null )
-        {
-            organisationUnitGroup.removeOrganisationUnit( orgUnit );
-
-            organisationUnitGroupService.updateOrganisationUnitGroup( organisationUnitGroup );
+        OrganisationUnitGroup group = organisationUnitGroupService.getOrganisationUnitGroup( uid );
+        OrganisationUnit unit = organisationUnitService.getOrganisationUnit( orgUnitUid );
+        
+        if ( group.removeOrganisationUnit( unit ) )
+        {
+            organisationUnitGroupService.updateOrganisationUnitGroup( group );
         }
     }
-
 }