← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 11494: Impl method fror adding item content for dashboard in web api

 

------------------------------------------------------------
revno: 11494
committer: Lars Helge Øverland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2013-07-23 19:21:14 +0200
message:
  Impl method fror adding item content for dashboard in web api
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/IdentifiableObjectManager.java
  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/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-core/src/main/java/org/hisp/dhis/common/DefaultIdentifiableObjectManager.java
  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/resources/META-INF/dhis/beans.xml
  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/common/IdentifiableObjectManager.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/IdentifiableObjectManager.java	2013-05-12 21:50:10 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/IdentifiableObjectManager.java	2013-07-23 17:21:14 +0000
@@ -61,9 +61,9 @@
 
     <T extends IdentifiableObject> Collection<T> getLikeName( Class<T> clazz, String name );
 
-    <T extends IdentifiableObject> Collection<T> getBetween( Class<T> clazz, int first, int max );
+    <T extends IdentifiableObject> List<T> getBetween( Class<T> clazz, int first, int max );
 
-    <T extends IdentifiableObject> Collection<T> getBetweenByName( Class<T> clazz, String name, int first, int max );
+    <T extends IdentifiableObject> List<T> getBetweenByName( Class<T> clazz, String name, int first, int max );
 
     <T extends IdentifiableObject> Collection<T> getByLastUpdated( Class<T> clazz, Date lastUpdated );
 

=== 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-23 09:55:18 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/dashboard/Dashboard.java	2013-07-23 17:21:14 +0000
@@ -99,6 +99,26 @@
         return true;
     }
     
+    /**
+     * 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.
+     * 
+     * @param type the type of content to return.
+     * @return an item.
+     */
+    public DashboardItem getAvailableItemByType( String type )
+    {
+        for ( DashboardItem item : items )
+        {
+            if ( type.equals( item.getType() ) && item.getContentCount() < DashboardItem.MAX_CONTENT )
+            {
+                return item;
+            }
+        }
+        
+        return null;
+    }    
+    
     // -------------------------------------------------------------------------
     // Getters and setters
     // -------------------------------------------------------------------------

=== 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-23 10:10:48 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/dashboard/DashboardItem.java	2013-07-23 17:21:14 +0000
@@ -53,6 +53,15 @@
 public class DashboardItem
     extends BaseIdentifiableObject
 {
+    public static final int MAX_CONTENT = 9;
+    
+    public static final String TYPE_CHART = "chart";
+    public static final String TYPE_MAP = "map";
+    public static final String TYPE_USERS = "users";
+    public static final String TYPE_REPORT_TABLES = "reportTables";
+    public static final String TYPE_REPORTS = "reports";
+    public static final String TYPE_RESOURCES = "resources";
+    
     private Chart chart;
     
     private Map map;
@@ -87,32 +96,42 @@
     {
         if ( chart != null )
         {
-            return "chart";
+            return TYPE_CHART;
         }
         else if ( map != null )
         {
-            return "map";
+            return TYPE_MAP;
         }
         else if ( !users.isEmpty() )
         {
-            return "users";
+            return TYPE_USERS;
         }
         else if ( !reportTables.isEmpty() )
         {
-            return "reportTables";
+            return TYPE_REPORT_TABLES;
         }
         else if ( !reports.isEmpty() )
         {
-            return "reports";
+            return TYPE_REPORTS;
         }
         else if ( !resources.isEmpty() )
         {
-            return "resources";
+            return TYPE_RESOURCES;
         }
         
         return null;
     }
     
+    public int getContentCount()
+    {
+        int count = 0;
+        count += users.size();
+        count += reportTables.size();
+        count += reports.size();
+        count += resources.size();
+        return count;
+    }
+    
     // -------------------------------------------------------------------------
     // Getters and setters
     // -------------------------------------------------------------------------

=== 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	2013-07-23 09:55:18 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/dashboard/DashboardService.java	2013-07-23 17:21:14 +0000
@@ -41,6 +41,8 @@
 
     DashboardSearchResult search( String query );
     
+    void addItemContent( String dashboardUid, String type, String contentUid );
+    
     void mergeDashboard( Dashboard dashboard );
     
     void mergeDashboardItem( DashboardItem item );

=== 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 09:55:18 +0000
+++ dhis-2/dhis-api/src/test/java/org/hisp/dhis/dashboard/DashboardTest.java	2013-07-23 17:21:14 +0000
@@ -30,7 +30,11 @@
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
 
+import org.hisp.dhis.chart.Chart;
+import org.hisp.dhis.document.Document;
+import org.hisp.dhis.report.Report;
 import org.junit.Test;
 
 /**
@@ -95,4 +99,33 @@
         assertFalse( dashboard.moveItem( "A", 1 ) ); // Already at position
         assertFalse( dashboard.moveItem( "A", 2 ) ); // Pointless move        
     }
+
+    @Test
+    public void testGetAvailableItemByType()
+    {
+        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" );
+        
+        diA.setChart( new Chart( "A" ) );
+        diB.getReports().add( new Report( "A", null, null, null ) );
+        diB.getReports().add( new Report( "B", null, null, null ) );
+        diC.getResources().add( new Document( "A", null, false, null ) );
+        diC.getResources().add( new Document( "B", null, false, null ) );
+        diC.getResources().add( new Document( "C", null, false, null ) );
+        
+        dashboard.getItems().add( diA );
+        dashboard.getItems().add( diB );
+        dashboard.getItems().add( diC );
+        
+        assertEquals( diB, dashboard.getAvailableItemByType( DashboardItem.TYPE_REPORTS ) );
+        assertEquals( diC, dashboard.getAvailableItemByType( DashboardItem.TYPE_RESOURCES ) );
+        assertNull( dashboard.getAvailableItemByType( DashboardItem.TYPE_REPORT_TABLES ) );
+    }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/common/DefaultIdentifiableObjectManager.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/common/DefaultIdentifiableObjectManager.java	2013-05-12 21:50:10 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/common/DefaultIdentifiableObjectManager.java	2013-07-23 17:21:14 +0000
@@ -240,7 +240,7 @@
 
     @Override
     @SuppressWarnings("unchecked")
-    public <T extends IdentifiableObject> Collection<T> getBetween( Class<T> clazz, int first, int max )
+    public <T extends IdentifiableObject> List<T> getBetween( Class<T> clazz, int first, int max )
     {
         GenericIdentifiableObjectStore<IdentifiableObject> store = getIdentifiableObjectStore( clazz );
 
@@ -249,12 +249,12 @@
             return new ArrayList<T>();
         }
 
-        return (Collection<T>) store.getAllOrderedName( first, max );
+        return (List<T>) store.getAllOrderedName( first, max );
     }
 
     @Override
     @SuppressWarnings("unchecked")
-    public <T extends IdentifiableObject> Collection<T> getBetweenByName( Class<T> clazz, String name, int first, int max )
+    public <T extends IdentifiableObject> List<T> getBetweenByName( Class<T> clazz, String name, int first, int max )
     {
         GenericIdentifiableObjectStore<IdentifiableObject> store = getIdentifiableObjectStore( clazz );
 
@@ -263,7 +263,7 @@
             return new ArrayList<T>();
         }
 
-        return (Collection<T>) store.getAllLikeNameOrderedName( name, first, max );
+        return (List<T>) store.getAllLikeNameOrderedName( name, first, max );
     }
 
     @Override

=== 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	2013-07-23 09:55:18 +0000
+++ dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/dashboard/impl/DefaultDashboardService.java	2013-07-23 17:21:14 +0000
@@ -28,21 +28,28 @@
  */
 
 import static org.hisp.dhis.common.IdentifiableObjectUtils.getUids;
+import static org.hisp.dhis.dashboard.DashboardItem.TYPE_CHART;
+import static org.hisp.dhis.dashboard.DashboardItem.TYPE_MAP;
+import static org.hisp.dhis.dashboard.DashboardItem.TYPE_REPORTS;
+import static org.hisp.dhis.dashboard.DashboardItem.TYPE_REPORT_TABLES;
+import static org.hisp.dhis.dashboard.DashboardItem.TYPE_RESOURCES;
+import static org.hisp.dhis.dashboard.DashboardItem.TYPE_USERS;
 
 import java.util.List;
 
-import org.hisp.dhis.chart.ChartService;
+import org.hisp.dhis.chart.Chart;
+import org.hisp.dhis.common.IdentifiableObjectManager;
 import org.hisp.dhis.common.hibernate.HibernateIdentifiableObjectStore;
 import org.hisp.dhis.dashboard.Dashboard;
 import org.hisp.dhis.dashboard.DashboardItem;
 import org.hisp.dhis.dashboard.DashboardSearchResult;
 import org.hisp.dhis.dashboard.DashboardService;
-import org.hisp.dhis.document.DocumentService;
-import org.hisp.dhis.mapping.MappingService;
-import org.hisp.dhis.report.ReportService;
-import org.hisp.dhis.reporttable.ReportTableService;
+import org.hisp.dhis.document.Document;
+import org.hisp.dhis.mapping.Map;
+import org.hisp.dhis.report.Report;
+import org.hisp.dhis.reporttable.ReportTable;
 import org.hisp.dhis.user.User;
-import org.hisp.dhis.user.UserService;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.transaction.annotation.Transactional;
 
 /**
@@ -67,48 +74,9 @@
         this.dashboardStore = dashboardStore;
     }
 
-    private UserService userService;
-    
-    public void setUserService( UserService userService )
-    {
-        this.userService = userService;
-    }
-
-    private ChartService chartService;
-
-    public void setChartService( ChartService chartService )
-    {
-        this.chartService = chartService;
-    }
-
-    private MappingService mappingService;
-
-    public void setMappingService( MappingService mappingService )
-    {
-        this.mappingService = mappingService;
-    }
-
-    private ReportService reportService;
-
-    public void setReportService( ReportService reportService )
-    {
-        this.reportService = reportService;
-    }
-    
-    private ReportTableService reportTableService;
-
-    public void setReportTableService( ReportTableService reportTableService )
-    {
-        this.reportTableService = reportTableService;
-    }
-    
-    private DocumentService documentService;
-
-    public void setDocumentService( DocumentService documentService )
-    {
-        this.documentService = documentService;
-    }
-
+    @Autowired
+    private IdentifiableObjectManager objectManager;
+    
     // -------------------------------------------------------------------------
     // DashboardService implementation
     // -------------------------------------------------------------------------
@@ -118,15 +86,64 @@
     {
         DashboardSearchResult result = new DashboardSearchResult();
         
-        result.setUsers( userService.getAllUsersBetweenByName( query, 0, MAX_PER_OBJECT ) );
-        result.setCharts( chartService.getChartsBetweenByName( query, 0, MAX_PER_OBJECT ) );
-        result.setMaps( mappingService.getMapsBetweenLikeName( query, 0, MAX_PER_OBJECT ) );
-        result.setReportTables( reportTableService.getReportTablesBetweenByName( query, 0, MAX_PER_OBJECT ) );
-        result.setReports( reportService.getReportsBetweenByName( query, 0, MAX_PER_OBJECT ) );
-        result.setResources( documentService.getDocumentsBetweenByName( query, 0, MAX_PER_OBJECT ) );
+        result.setUsers( objectManager.getBetweenByName( User.class, query, 0, MAX_PER_OBJECT ) );
+        result.setCharts( objectManager.getBetweenByName( Chart.class, query, 0, MAX_PER_OBJECT ) );
+        result.setMaps( objectManager.getBetweenByName( Map.class, query, 0, MAX_PER_OBJECT ) );
+        result.setReportTables( objectManager.getBetweenByName( ReportTable.class, query, 0, MAX_PER_OBJECT ) );
+        result.setReports( objectManager.getBetweenByName( Report.class, query, 0, MAX_PER_OBJECT ) );
+        result.setResources( objectManager.getBetweenByName( Document.class, query, 0, MAX_PER_OBJECT ) );
         
         return result;
     }
+
+    @Override
+    public void addItemContent( String dashboardUid, String type, String contentUid )
+    {
+        Dashboard dashboard = getDashboard( dashboardUid );               
+        
+        if ( TYPE_CHART.equals( type ) )
+        {
+            DashboardItem item = new DashboardItem();
+            item.setChart( objectManager.get( Chart.class, contentUid ) );
+            dashboard.getItems().add( 0, item );
+        }
+        else if ( TYPE_MAP.equals( type ) )
+        {
+            DashboardItem item = new DashboardItem();
+            item.setMap( objectManager.get( Map.class, contentUid ) );
+            dashboard.getItems().add( 0, item );
+        }
+        else // Link item
+        {
+            DashboardItem availableItem = dashboard.getAvailableItemByType( type );
+            
+            DashboardItem item = availableItem == null ? new DashboardItem() : availableItem;
+            
+            if ( TYPE_USERS.equals( type ) )
+            {
+                item.getUsers().add( objectManager.get( User.class, contentUid ) );
+            }
+            else if ( TYPE_REPORT_TABLES.equals( type ) )
+            {
+                item.getReportTables().add( objectManager.get( ReportTable.class, contentUid ) );
+            }
+            else if ( TYPE_REPORTS.equals( type ) )
+            {
+                item.getReports().add( objectManager.get( Report.class, contentUid ) );
+            }
+            else if ( TYPE_RESOURCES.equals( type ) )
+            {
+                item.getResources().add( objectManager.get( Document.class, contentUid ) );
+            }
+            
+            if ( availableItem == null )
+            {
+                dashboard.getItems().add( 0, item );
+            }
+        }
+        
+        updateDashboard( dashboard );
+    }
     
     public void mergeDashboard( Dashboard dashboard )
     {
@@ -143,32 +160,32 @@
     {
         if ( item.getChart() != null )
         {
-            item.setChart( chartService.getChart( item.getChart().getUid() ) );
+            item.setChart( objectManager.get( Chart.class, item.getChart().getUid() ) );
         }
         
-        if ( item.getChart() != null )
+        if ( item.getMap() != null )
         {
-            item.setMap( mappingService.getMap( item.getMap().getUid() ) );
+            item.setMap( objectManager.get( Map.class, item.getMap().getUid() ) );
         }
         
         if ( item.getUsers() != null )
         {
-            item.setUsers( userService.getUsersByUid( getUids( item.getUsers() ) ) );
+            item.setUsers( objectManager.getByUid( User.class, getUids( item.getUsers() ) ) );
         }
         
         if ( item.getReportTables() != null )
         {
-            item.setReportTables( reportTableService.getReportTablesByUid( getUids( item.getReportTables() ) ) );
+            item.setReportTables( objectManager.getByUid( ReportTable.class, getUids( item.getReportTables() ) ) );
         }
         
         if ( item.getReports() != null )
         {
-            item.setReports( reportService.getReportsByUid( getUids( item.getReports() ) ) );
+            item.setReports( objectManager.getByUid( Report.class, getUids( item.getReports() ) ) );
         }
         
         if ( item.getResources() != null )
         {
-            item.setResources( documentService.getDocumentsByUid( getUids( item.getResources() ) ) );
+            item.setResources( objectManager.getByUid( Document.class, getUids( item.getResources() ) ) );
         }
     }
 

=== modified file 'dhis-2/dhis-services/dhis-service-reporting/src/main/resources/META-INF/dhis/beans.xml'
--- dhis-2/dhis-services/dhis-service-reporting/src/main/resources/META-INF/dhis/beans.xml	2013-07-22 21:08:05 +0000
+++ dhis-2/dhis-services/dhis-service-reporting/src/main/resources/META-INF/dhis/beans.xml	2013-07-23 17:21:14 +0000
@@ -140,12 +140,6 @@
 
   <bean id="org.hisp.dhis.dashboard.DashboardService" class="org.hisp.dhis.dashboard.impl.DefaultDashboardService">
     <property name="dashboardStore" ref="org.hisp.dhis.dashboard.DashboardStore" />
-    <property name="userService" ref="org.hisp.dhis.user.UserService" />
-    <property name="chartService" ref="org.hisp.dhis.chart.ChartService" />
-    <property name="mappingService" ref="org.hisp.dhis.mapping.MappingService" />
-    <property name="reportService" ref="org.hisp.dhis.report.ReportService" />
-    <property name="reportTableService" ref="org.hisp.dhis.reporttable.ReportTableService" />
-    <property name="documentService" ref="org.hisp.dhis.document.DocumentService" />
   </bean>
 
   <!-- Dashboard Content providers -->

=== 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-23 10:27:03 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/DashboardController.java	2013-07-23 17:21:14 +0000
@@ -57,14 +57,12 @@
     extends AbstractCrudController<Dashboard>
 {
     public static final String RESOURCE_PATH = "/dashboards";
-        
+    
     @Autowired
     private DashboardService dashboardService;
     
     @RequestMapping( value = "/q/{query}", method = RequestMethod.GET )
-    public String search( @PathVariable String query, 
-        Model model,
-        HttpServletResponse response ) throws Exception
+    public String search( @PathVariable String query, Model model, HttpServletResponse response ) throws Exception
     {
         DashboardSearchResult result = dashboardService.search( query );
         
@@ -85,7 +83,7 @@
         
         ContextUtils.createdResponse( response, "Dashboard created", RESOURCE_PATH + "/" + dashboard.getUid() );
     }
-
+    
     @Override
     @RequestMapping( value = "/{uid}", method = RequestMethod.PUT, consumes = "application/json" )
     @ResponseStatus( value = HttpStatus.NO_CONTENT )
@@ -134,6 +132,15 @@
         ContextUtils.createdResponse( response, "Dashboard item created", item.getUid() );
     }
     
+    @RequestMapping( value = "/{uid}/items/content", method = RequestMethod.POST, consumes = "application/json" )
+    public void postJsonItemContent( HttpServletResponse response, HttpServletRequest request, 
+        @PathVariable String uid, @RequestParam String type, @RequestParam( "uid" ) String contentUid ) throws Exception
+    {
+        dashboardService.addItemContent( uid, type, contentUid );
+        
+        ContextUtils.okResponse( response, "Dashboard item added" );
+    }
+    
     @RequestMapping( value = "/{dashboardUid}/items/{itemUid}/move", method = RequestMethod.PUT, consumes = "application/json" )
     public void moveItem( HttpServletResponse response, HttpServletRequest request,
         @PathVariable String dashboardUid, @PathVariable String itemUid, @RequestParam int position ) throws Exception