dhis2-devs team mailing list archive
-
dhis2-devs team
-
Mailing list archive
-
Message #23609
[Branch ~dhis2-devs-core/dhis2/trunk] Rev 11500: Impl method for removing dashboard items in web api
------------------------------------------------------------
revno: 11500
committer: Lars Helge Øverland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2013-07-24 13:48:13 +0200
message:
Impl method for removing dashboard items in web api
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/DashboardItem.java
dhis-2/dhis-api/src/test/java/org/hisp/dhis/dashboard/DashboardTest.java
dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/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 2013-07-24 11:10:40 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/dashboard/Dashboard.java 2013-07-24 11:48:13 +0000
@@ -28,6 +28,7 @@
*/
import java.util.ArrayList;
+import java.util.Iterator;
import java.util.List;
import org.hisp.dhis.common.BaseIdentifiableObject;
@@ -100,6 +101,27 @@
}
/**
+ * 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 an item from this dashboard of the given type which number of
* content is less than max. Returns null if no item matches the criteria.
*
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/dashboard/DashboardItem.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/dashboard/DashboardItem.java 2013-07-24 11:10:40 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/dashboard/DashboardItem.java 2013-07-24 11:48:13 +0000
@@ -80,6 +80,7 @@
public DashboardItem()
{
+ super.setAutoFields();
}
public DashboardItem( String uid )
=== 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 2013-07-23 17:21:14 +0000
+++ dhis-2/dhis-api/src/test/java/org/hisp/dhis/dashboard/DashboardTest.java 2013-07-24 11:48:13 +0000
@@ -128,4 +128,28 @@
assertEquals( diC, dashboard.getAvailableItemByType( DashboardItem.TYPE_RESOURCES ) );
assertNull( dashboard.getAvailableItemByType( DashboardItem.TYPE_REPORT_TABLES ) );
}
+
+ @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() );
+ }
}
=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/DashboardController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/DashboardController.java 2013-07-24 11:10:40 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/DashboardController.java 2013-07-24 11:48:13 +0000
@@ -104,11 +104,18 @@
dashboardService.updateDashboard( dashboard );
}
+ @Override
@RequestMapping( value = "/{uid}", method = RequestMethod.DELETE )
@ResponseStatus( value = HttpStatus.NO_CONTENT )
public void deleteObject( HttpServletResponse response, HttpServletRequest request, @PathVariable( "uid" ) String uid ) throws Exception
{
Dashboard dashboard = dashboardService.getDashboard( uid );
+
+ if ( dashboard == null )
+ {
+ ContextUtils.notFoundResponse( response, "Dashboard does not exist: " + uid );
+ return;
+ }
dashboardService.deleteDashboard( dashboard );
@@ -120,6 +127,12 @@
InputStream input, @PathVariable String uid ) throws Exception
{
Dashboard dashboard = dashboardService.getDashboard( uid );
+
+ if ( dashboard == null )
+ {
+ ContextUtils.notFoundResponse( response, "Dashboard does not exist: " + uid );
+ return;
+ }
DashboardItem item = JacksonUtils.fromJson( input, DashboardItem.class );
@@ -146,6 +159,12 @@
@PathVariable String dashboardUid, @PathVariable String itemUid, @RequestParam int position ) throws Exception
{
Dashboard dashboard = dashboardService.getDashboard( dashboardUid );
+
+ if ( dashboard == null )
+ {
+ ContextUtils.notFoundResponse( response, "Dashboard does not exist: " + dashboardUid );
+ return;
+ }
if ( dashboard.moveItem( itemUid, position ) )
{
@@ -154,4 +173,24 @@
ContextUtils.okResponse( response, "Dashboard item moved" );
}
}
+
+ @RequestMapping( value = "/{dashboardUid}/items/{itemUid}", method = RequestMethod.DELETE )
+ public void deleteItem( HttpServletResponse response, HttpServletRequest request,
+ @PathVariable String dashboardUid, @PathVariable String itemUid )
+ {
+ Dashboard dashboard = dashboardService.getDashboard( dashboardUid );
+
+ if ( dashboard == null )
+ {
+ ContextUtils.notFoundResponse( response, "Dashboard does not exist: " + dashboardUid );
+ return;
+ }
+
+ if ( dashboard.removeItem( itemUid ) )
+ {
+ dashboardService.updateDashboard( dashboard );
+
+ ContextUtils.okResponse( response, "Dashboard item removed" );
+ }
+ }
}