← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 14260: Trackendentityinstance search, xls and csv download

 

------------------------------------------------------------
revno: 14260
committer: Lars Helge Øverland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2014-03-17 15:55:54 +0100
message:
  Trackendentityinstance search, xls and csv download
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceQueryParams.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceService.java
  dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentity/DefaultTrackedEntityInstanceService.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/event/TrackedEntityInstanceController.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/trackedentity/TrackedEntityInstanceQueryParams.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceQueryParams.java	2014-03-17 14:00:38 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceQueryParams.java	2014-03-17 14:55:54 +0000
@@ -52,12 +52,12 @@
     
     private List<QueryItem> items = new ArrayList<QueryItem>();
 
+    private Set<OrganisationUnit> organisationUnits = new HashSet<OrganisationUnit>();
+    
     private Program program;
     
     private TrackedEntity trackedEntity;
     
-    private Set<OrganisationUnit> organisationUnits = new HashSet<OrganisationUnit>();
-    
     private String organisationUnitMode;
 
     private Integer page;
@@ -114,6 +114,16 @@
         this.items = items;
     }
 
+    public String getOrganisationUnitMode()
+    {
+        return organisationUnitMode;
+    }
+
+    public void setOrganisationUnitMode( String organisationUnitMode )
+    {
+        this.organisationUnitMode = organisationUnitMode;
+    }
+
     public Program getProgram()
     {
         return program;
@@ -144,16 +154,6 @@
         this.organisationUnits = organisationUnits;
     }
 
-    public String getOrganisationUnitMode()
-    {
-        return organisationUnitMode;
-    }
-
-    public void setOrganisationUnitMode( String organisationUnitMode )
-    {
-        this.organisationUnitMode = organisationUnitMode;
-    }
-
     public Integer getPage()
     {
         return page;

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceService.java	2014-03-17 13:36:52 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceService.java	2014-03-17 14:55:54 +0000
@@ -55,8 +55,8 @@
 
     Grid getTrackedEntityInstances( TrackedEntityInstanceQueryParams params );
     
-    TrackedEntityInstanceQueryParams getFromUrl( Set<String> items, String program, String trackedEntity, 
-        Set<String> ou, String ouMode, Integer page, Integer pageSize );
+    TrackedEntityInstanceQueryParams getFromUrl( Set<String> items, Set<String> ou, String ouMode, 
+        String program, String trackedEntity, Integer page, Integer pageSize );
     
     /**
      * Adds an {@link TrackedEntityInstance}

=== modified file 'dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentity/DefaultTrackedEntityInstanceService.java'
--- dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentity/DefaultTrackedEntityInstanceService.java	2014-03-17 14:07:43 +0000
+++ dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentity/DefaultTrackedEntityInstanceService.java	2014-03-17 14:55:54 +0000
@@ -144,8 +144,8 @@
         grid.addHeader( new GridHeader( TRACKED_ENTITY_INSTANCE_ID, "Instance" ) );
         grid.addHeader( new GridHeader( CREATED_ID, "Created" ) );
         grid.addHeader( new GridHeader( LAST_UPDATED_ID, "Last updated" ) );
+        grid.addHeader( new GridHeader( ORG_UNIT_ID, "Org unit" ) );
         grid.addHeader( new GridHeader( TRACKED_ENTITY_ID, "Tracked entity" ) );
-        grid.addHeader( new GridHeader( ORG_UNIT_ID, "Org unit" ) );
         
         for ( QueryItem item : params.getItems() )
         {
@@ -160,8 +160,8 @@
             grid.addValue( entity.get( TRACKED_ENTITY_INSTANCE_ID ) );
             grid.addValue( entity.get( CREATED_ID ) );
             grid.addValue( entity.get( LAST_UPDATED_ID ) );
+            grid.addValue( entity.get( ORG_UNIT_ID ) );
             grid.addValue( entity.get( TRACKED_ENTITY_ID ) );
-            grid.addValue( entity.get( ORG_UNIT_ID ) );
             
             for ( QueryItem item : params.getItems() )
             {
@@ -173,8 +173,8 @@
     }
     
     @Override
-    public TrackedEntityInstanceQueryParams getFromUrl( Set<String> items, String program, String trackedEntity, 
-        Set<String> ou, String ouMode, Integer page, Integer pageSize )
+    public TrackedEntityInstanceQueryParams getFromUrl( Set<String> items, Set<String> ou, String ouMode, 
+        String program, String trackedEntity, Integer page, Integer pageSize )
     {
         TrackedEntityInstanceQueryParams params = new TrackedEntityInstanceQueryParams();
 
@@ -184,6 +184,18 @@
             
             params.getItems().add( it );
         }
+
+        for ( String orgUnit : ou )
+        {
+            OrganisationUnit organisationUnit = organisationUnitService.getOrganisationUnit( orgUnit );
+            
+            if ( organisationUnit == null )
+            {
+                throw new IllegalQueryException( "Organisation unit does not exist: " + orgUnit );
+            }
+            
+            params.getOrganisationUnits().add( organisationUnit );
+        }
         
         Program pr = program != null ? programService.getProgram( program ) : null;
         
@@ -199,18 +211,6 @@
             throw new IllegalQueryException( "Tracked entity does not exist: " + program );
         }
         
-        for ( String orgUnit : ou )
-        {
-            OrganisationUnit organisationUnit = organisationUnitService.getOrganisationUnit( orgUnit );
-            
-            if ( organisationUnit == null )
-            {
-                throw new IllegalQueryException( "Organisation unit does not exist: " + orgUnit );
-            }
-            
-            params.getOrganisationUnits().add( organisationUnit );
-        }
-        
         params.setOrganisationUnitMode( ouMode );
         params.setPage( page );
         params.setPageSize( pageSize );

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/event/TrackedEntityInstanceController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/event/TrackedEntityInstanceController.java	2014-03-17 14:00:38 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/event/TrackedEntityInstanceController.java	2014-03-17 14:55:54 +0000
@@ -40,6 +40,7 @@
 import org.hisp.dhis.api.controller.WebOptions;
 import org.hisp.dhis.api.controller.exception.NotFoundException;
 import org.hisp.dhis.api.utils.ContextUtils;
+import org.hisp.dhis.api.utils.ContextUtils.CacheStrategy;
 import org.hisp.dhis.common.DimensionalObject;
 import org.hisp.dhis.common.Grid;
 import org.hisp.dhis.common.IdentifiableObjectManager;
@@ -50,6 +51,7 @@
 import org.hisp.dhis.dxf2.importsummary.ImportSummaries;
 import org.hisp.dhis.dxf2.importsummary.ImportSummary;
 import org.hisp.dhis.dxf2.utils.JacksonUtils;
+import org.hisp.dhis.system.grid.GridUtils;
 import org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.HttpStatus;
@@ -85,31 +87,76 @@
 
     @Autowired
     private SessionFactory sessionFactory;
+    
+    @Autowired
+    private ContextUtils contextUtils;
 
     // -------------------------------------------------------------------------
     // READ
     // -------------------------------------------------------------------------
     
-    @RequestMapping( method = RequestMethod.GET, produces = { "application/json", "application/javascript" } )
-    public String queryTrackedEntityInstances( // JSON, JSONP
+    @RequestMapping( method = RequestMethod.GET, produces = { ContextUtils.CONTENT_TYPE_JSON, ContextUtils.CONTENT_TYPE_JAVASCRIPT } )
+    public String queryTrackedEntityInstancesJson(
         @RequestParam Set<String> item,
+        @RequestParam String ou,
+        @RequestParam(required=false, defaultValue=DimensionalObject.OU_MODE_SELECTED) String ouMode,
         @RequestParam(required=false) String program,
         @RequestParam(required=false) String trackedEntity,
-        @RequestParam String ou,
-        @RequestParam(required=false, defaultValue=DimensionalObject.OU_MODE_SELECTED) String ouMode,
         @RequestParam(required=false) Integer page,
         @RequestParam(required=false) Integer pageSize,
         Model model,
         HttpServletResponse response ) throws Exception
     {
         Set<String> orgUnits = new HashSet<String>( ContextUtils.getQueryParamValues( ou ) );        
-        TrackedEntityInstanceQueryParams params = instanceService.getFromUrl( item, program, trackedEntity, orgUnits, ouMode, page, pageSize );
+        TrackedEntityInstanceQueryParams params = instanceService.getFromUrl( item, orgUnits, ouMode, program, trackedEntity, page, pageSize );
+        
+        contextUtils.configureResponse( response, ContextUtils.CONTENT_TYPE_JSON, CacheStrategy.NO_CACHE );
         Grid grid = instanceService.getTrackedEntityInstances( params );
         
         model.addAttribute( "model", grid );
         model.addAttribute( "viewClass", "detailed" );
         return "grid";
     }
+
+    @RequestMapping( method = RequestMethod.GET, produces = ContextUtils.CONTENT_TYPE_EXCEL )
+    public void queryTrackedEntityInstancesXls(
+        @RequestParam Set<String> item,
+        @RequestParam String ou,
+        @RequestParam(required=false, defaultValue=DimensionalObject.OU_MODE_SELECTED) String ouMode,
+        @RequestParam(required=false) String program,
+        @RequestParam(required=false) String trackedEntity,
+        @RequestParam(required=false) Integer page,
+        @RequestParam(required=false) Integer pageSize,
+        Model model,
+        HttpServletResponse response ) throws Exception
+    {
+        Set<String> orgUnits = new HashSet<String>( ContextUtils.getQueryParamValues( ou ) );        
+        TrackedEntityInstanceQueryParams params = instanceService.getFromUrl( item, orgUnits, ouMode, program, trackedEntity, page, pageSize );
+        
+        contextUtils.configureResponse( response, ContextUtils.CONTENT_TYPE_EXCEL, CacheStrategy.NO_CACHE );
+        Grid grid = instanceService.getTrackedEntityInstances( params );
+        GridUtils.toXls( grid, response.getOutputStream() );
+    }
+
+    @RequestMapping( method = RequestMethod.GET, produces = ContextUtils.CONTENT_TYPE_CSV )
+    public void queryTrackedEntityInstancesCsv(
+        @RequestParam Set<String> item,
+        @RequestParam String ou,
+        @RequestParam(required=false, defaultValue=DimensionalObject.OU_MODE_SELECTED) String ouMode,
+        @RequestParam(required=false) String program,
+        @RequestParam(required=false) String trackedEntity,
+        @RequestParam(required=false) Integer page,
+        @RequestParam(required=false) Integer pageSize,
+        Model model,
+        HttpServletResponse response ) throws Exception
+    {
+        Set<String> orgUnits = new HashSet<String>( ContextUtils.getQueryParamValues( ou ) );        
+        TrackedEntityInstanceQueryParams params = instanceService.getFromUrl( item, orgUnits, ouMode, program, trackedEntity, page, pageSize );
+        
+        contextUtils.configureResponse( response, ContextUtils.CONTENT_TYPE_CSV, CacheStrategy.NO_CACHE );
+        Grid grid = instanceService.getTrackedEntityInstances( params );
+        GridUtils.toCsv( grid, response.getOutputStream() );
+    }
     
     @RequestMapping( value = "/{id}", method = RequestMethod.GET )
     @PreAuthorize( "hasRole('ALL') or hasRole('F_ACCESS_PATIENT_ATTRIBUTES')" )