← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 18102: Dashboard. Deleteing dashboard item from database when removed from dashboard

 

------------------------------------------------------------
revno: 18102
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2015-01-22 12:23:36 +0100
message:
  Dashboard. Deleteing dashboard item from database when removed from dashboard
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/dashboard/Dashboard.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/dashboard/DashboardService.java
  dhis-2/dhis-api/src/test/java/org/hisp/dhis/dashboard/DashboardTest.java
  dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/dashboard/impl/DefaultDashboardService.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/DashboardController.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/dashboard/Dashboard.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/dashboard/Dashboard.java	2015-01-17 07:41:26 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/dashboard/Dashboard.java	2015-01-22 11:23:36 +0000
@@ -28,22 +28,22 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import java.util.ArrayList;
+import java.util.List;
+
+import org.hisp.dhis.common.BaseIdentifiableObject;
+import org.hisp.dhis.common.DxfNamespaces;
+import org.hisp.dhis.common.IdentifiableObject;
+import org.hisp.dhis.common.annotation.Scanned;
+import org.hisp.dhis.common.view.DetailedView;
+import org.hisp.dhis.common.view.ExportView;
+
 import com.fasterxml.jackson.annotation.JsonProperty;
 import com.fasterxml.jackson.annotation.JsonView;
 import com.fasterxml.jackson.databind.annotation.JsonSerialize;
 import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
 import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
 import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
-import org.hisp.dhis.common.BaseIdentifiableObject;
-import org.hisp.dhis.common.DxfNamespaces;
-import org.hisp.dhis.common.IdentifiableObject;
-import org.hisp.dhis.common.annotation.Scanned;
-import org.hisp.dhis.common.view.DetailedView;
-import org.hisp.dhis.common.view.ExportView;
-
-import java.util.ArrayList;
-import java.util.Iterator;
-import java.util.List;
 
 /**
  * @author Lars Helge Overland
@@ -110,27 +110,6 @@
     }
 
     /**
-     * Removes the item with the given identifier from this dashboard.
-     *
-     * @param uid the item uid.
-     */
-    public boolean removeItem( String uid )
-    {
-        Iterator<DashboardItem> iter = items.iterator();
-
-        while ( iter.hasNext() )
-        {
-            if ( uid.equals( iter.next().getUid() ) )
-            {
-                iter.remove();
-                return true;
-            }
-        }
-
-        return false;
-    }
-
-    /**
      * Returns the item with the given uid, or null if no item with the given
      * uid is present for this dashboard.
      *
@@ -163,6 +142,14 @@
 
         return null;
     }
+    
+    /**
+     * Indicates whether this dashboard has at least one item.
+     */
+    public boolean hasItems()
+    {
+        return items != null && !items.isEmpty();
+    }
 
     @JsonProperty
     @JacksonXmlProperty( namespace = DxfNamespaces.DXF_2_0 )

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/dashboard/DashboardService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/dashboard/DashboardService.java	2015-01-21 09:30:45 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/dashboard/DashboardService.java	2015-01-22 11:23:36 +0000
@@ -43,6 +43,10 @@
 {
     final String ID = DashboardService.class.getName();
 
+    // -------------------------------------------------------------------------
+    // Dashboard
+    // -------------------------------------------------------------------------
+
     DashboardSearchResult search( String query );
 
     DashboardSearchResult search( String query, Set<String> maxTypes );
@@ -63,10 +67,16 @@
 
     Dashboard getDashboard( String uid );
 
+    // -------------------------------------------------------------------------
+    // DashboardItem
+    // -------------------------------------------------------------------------
+
     void updateDashboardItem( DashboardItem item );
     
     DashboardItem getDashboardItem( String uid );
     
+    void deleteDashboardItem( DashboardItem item );
+    
     int countMapDashboardItems( Map map );
 
     int countChartDashboardItems( Chart chart );

=== modified file 'dhis-2/dhis-api/src/test/java/org/hisp/dhis/dashboard/DashboardTest.java'
--- dhis-2/dhis-api/src/test/java/org/hisp/dhis/dashboard/DashboardTest.java	2015-01-17 07:41:26 +0000
+++ dhis-2/dhis-api/src/test/java/org/hisp/dhis/dashboard/DashboardTest.java	2015-01-22 11:23:36 +0000
@@ -131,30 +131,6 @@
     }
     
     @Test
-    public void testRemoveItem()
-    {
-        Dashboard dashboard = new Dashboard();
-        
-        DashboardItem diA = new DashboardItem();
-        DashboardItem diB = new DashboardItem();
-        DashboardItem diC = new DashboardItem();
-        
-        diA.setUid( "A" );
-        diB.setUid( "B" );
-        diC.setUid( "C" );
-
-        dashboard.getItems().add( diA );
-        dashboard.getItems().add( diB );
-        dashboard.getItems().add( diC );
-        
-        assertEquals( 3, dashboard.getItems().size() );
-        assertTrue( dashboard.removeItem( "B" ) );
-        assertEquals( 2, dashboard.getItems().size() );
-        assertFalse( dashboard.removeItem( "X" ) );
-        assertEquals( 2, dashboard.getItems().size() );
-    }
-    
-    @Test
     public void testGetItem()
     {
         Dashboard dashboard = new Dashboard();

=== modified file 'dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/dashboard/impl/DefaultDashboardService.java'
--- dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/dashboard/impl/DefaultDashboardService.java	2015-01-21 09:30:45 +0000
+++ dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/dashboard/impl/DefaultDashboardService.java	2015-01-22 11:23:36 +0000
@@ -304,6 +304,12 @@
     {
         return dashboardItemStore.getByUid( uid );
     }
+
+    @Override
+    public void deleteDashboardItem( DashboardItem item )
+    {
+        dashboardItemStore.delete( item );
+    }
     
     @Override
     public int countMapDashboardItems( Map map )

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/DashboardController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/DashboardController.java	2015-01-21 09:30:45 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/DashboardController.java	2015-01-22 11:23:36 +0000
@@ -201,8 +201,17 @@
             return;
         }
 
-        if ( dashboard.removeItem( itemUid ) )
-        {
+        DashboardItem item = dashboardService.getDashboardItem( itemUid );
+
+        if ( item == null )
+        {
+            ContextUtils.notFoundResponse( response, "Dashboard item does not exist: " + itemUid );
+            return;
+        }
+
+        if ( dashboard.hasItems() && dashboard.getItems().remove( item ) )
+        {
+            dashboardService.deleteDashboardItem( item );
             dashboardService.updateDashboard( dashboard );
 
             ContextUtils.okResponse( response, "Dashboard item removed" );
@@ -231,9 +240,9 @@
 
         if ( item.removeItemContent( contentUid ) )
         {
-            if ( item.getContentCount() == 0 )
+            if ( item.getContentCount() == 0 && dashboard.getItems().remove( item ) )
             {
-                dashboard.removeItem( item.getUid() ); // Remove if empty
+                dashboardService.deleteDashboardItem( item ); // Delete if empty                
             }
 
             dashboardService.updateDashboard( dashboard );