← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 21830: Data approval level renumber after delete to prevent level number gaps.

 

------------------------------------------------------------
revno: 21830
committer: jimgrace@xxxxxxxxx
branch nick: dhis2
timestamp: Thu 2016-01-28 12:55:37 -0500
message:
  Data approval level renumber after delete to prevent level number gaps.
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataapproval/DataApprovalLevelService.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataapproval/DefaultDataApprovalLevelService.java
  dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/dataapproval/DataApprovalLevelServiceTest.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/DataApprovalLevelController.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/dataapproval/DataApprovalLevelService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataapproval/DataApprovalLevelService.java	2016-01-04 02:27:49 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataapproval/DataApprovalLevelService.java	2016-01-28 17:55:37 +0000
@@ -223,6 +223,12 @@
     void deleteDataApprovalLevel( DataApprovalLevel dataApprovalLevel );
 
     /**
+     * Reorders the remaining approval levels, if necessary, after deleting
+     * an approval level. Should follow the deleting of an approval level.
+     */
+    void postDeleteDataApprovalLevel();
+
+    /**
      * Gets the approval level at which this user may make approval actions
      * (if the user is authorized for any) on this organisation unit.
      *

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataapproval/DefaultDataApprovalLevelService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataapproval/DefaultDataApprovalLevelService.java	2016-01-04 02:27:49 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataapproval/DefaultDataApprovalLevelService.java	2016-01-28 17:55:37 +0000
@@ -443,19 +443,19 @@
     @Override
     public void deleteDataApprovalLevel( DataApprovalLevel dataApprovalLevel )
     {
+        dataApprovalLevelStore.delete( dataApprovalLevel );
+
+        postDeleteDataApprovalLevel();
+    }
+
+    @Override
+    public void postDeleteDataApprovalLevel()
+    {
         List<DataApprovalLevel> dataApprovalLevels = getAllDataApprovalLevels();
 
-        int index = dataApprovalLevel.getLevel() - 1;
-        
-        if ( index >= 0 && index < dataApprovalLevels.size() )
+        for ( int i = 0; i < dataApprovalLevels.size(); i++ )
         {
-            dataApprovalLevelStore.delete( dataApprovalLevel );
-
-            dataApprovalLevels.remove( index );
-
-            // Move up from here to end, to avoid duplicate level in database.
-
-            for ( int i = index; i < dataApprovalLevels.size(); i++ )
+            if ( dataApprovalLevels.get( i ).getLevel() != i + 1 )
             {
                 update( dataApprovalLevels.get( i ), i );
             }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/dataapproval/DataApprovalLevelServiceTest.java'
--- dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/dataapproval/DataApprovalLevelServiceTest.java	2016-01-04 02:27:49 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/dataapproval/DataApprovalLevelServiceTest.java	2016-01-28 17:55:37 +0000
@@ -242,6 +242,12 @@
         assertNull( dataApprovalLevelService.getDataApprovalLevel( id2 ) );
         assertNotNull( dataApprovalLevelService.getDataApprovalLevel( id3 ) );
         assertNotNull( dataApprovalLevelService.getDataApprovalLevel( id4 ) );
+
+        List<DataApprovalLevel> levels = dataApprovalLevelService.getAllDataApprovalLevels();
+        assertEquals( 3, levels.size() );
+        assertEquals( 1, levels.get( 0 ).getLevel() );
+        assertEquals( 2, levels.get( 1 ).getLevel() );
+        assertEquals( 3, levels.get( 2 ).getLevel() );
     }
 
     @Test

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/DataApprovalLevelController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/DataApprovalLevelController.java	2016-01-04 02:27:49 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/DataApprovalLevelController.java	2016-01-28 17:55:37 +0000
@@ -48,4 +48,10 @@
     {
         dataApprovalLevelService.prepareAddDataApproval( entity );
     }
+
+    @Override
+    protected void postDeleteEntity()
+    {
+        dataApprovalLevelService.postDeleteDataApprovalLevel();
+    }
 }