← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 17133: support status parameter in ProgramInstanceController

 

------------------------------------------------------------
revno: 17133
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2014-10-16 12:49:47 +0700
message:
  support status parameter in ProgramInstanceController
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramInstanceService.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramInstanceStore.java
  dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/program/DefaultProgramInstanceService.java
  dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/program/hibernate/HibernateProgramInstanceStore.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/event/ProgramInstanceController.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/program/ProgramInstanceService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramInstanceService.java	2014-05-23 15:28:46 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramInstanceService.java	2014-10-16 05:49:47 +0000
@@ -247,6 +247,21 @@
         Collection<Integer> orgunitIds, Date startDate, Date endDate );
 
     /**
+     * Retrieve program instances with a certain status on a program and an
+     * orgunit ids list for a period
+     *
+     * @param status     of program-instance, include STATUS_ACTIVE,
+     *                   STATUS_COMPLETED and STATUS_CANCELLED
+     * @param program    ProgramInstance
+     * @param orgunitIds A list of orgunit ids
+     * @param startDate  The start date for retrieving on enrollment-date
+     * @param endDate    The end date for retrieving on enrollment-date
+     * @return ProgramInstance list
+     */
+    Collection<ProgramInstance> getProgramInstancesByStatus( Integer status, Program program,
+        Collection<Integer> orgunitIds, Date startDate, Date endDate, Integer min, Integer max );
+
+    /**
      * Get the number of program instances of a program which have a certain
      * status and an orgunit ids list for a period
      *

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramInstanceStore.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramInstanceStore.java	2014-05-23 15:28:46 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramInstanceStore.java	2014-10-16 05:49:47 +0000
@@ -210,6 +210,21 @@
         Date startDate, Date endDate );
 
     /**
+     * Retrieve program instances with a certain status on a program and an
+     * orgunit ids list for a period
+     *
+     * @param status     of program-instance, include STATUS_ACTIVE,
+     *                   STATUS_COMPLETED and STATUS_CANCELLED
+     * @param program    ProgramInstance
+     * @param orgunitIds A list of orgunit ids
+     * @param startDate  The start date for retrieving on enrollment-date
+     * @param endDate    The end date for retrieving on enrollment-date
+     * @return ProgramInstance list
+     */
+    Collection<ProgramInstance> getByStatus( Integer status, Program program, Collection<Integer> orgunitIds,
+        Date startDate, Date endDate, Integer min, Integer max );
+
+    /**
      * Rerieve schedule list of patiens registered
      *
      * @return A SchedulingProgramObject list

=== modified file 'dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/program/DefaultProgramInstanceService.java'
--- dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/program/DefaultProgramInstanceService.java	2014-08-15 07:40:20 +0000
+++ dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/program/DefaultProgramInstanceService.java	2014-10-16 05:49:47 +0000
@@ -158,16 +158,19 @@
     // Implementation methods
     // -------------------------------------------------------------------------
 
+    @Override
     public int addProgramInstance( ProgramInstance programInstance )
     {
         return programInstanceStore.save( programInstance );
     }
 
+    @Override
     public void deleteProgramInstance( ProgramInstance programInstance )
     {
         programInstanceStore.delete( programInstance );
     }
 
+    @Override
     public ProgramInstance getProgramInstance( int id )
     {
         return programInstanceStore.get( id );
@@ -179,81 +182,90 @@
         return programInstanceStore.getByUid( id );
     }
 
+    @Override
     public void updateProgramInstance( ProgramInstance programInstance )
     {
         programInstanceStore.update( programInstance );
     }
 
+    @Override
     public Collection<ProgramInstance> getProgramInstances( Program program )
     {
         return programInstanceStore.get( program );
     }
 
+    @Override
     public Collection<ProgramInstance> getProgramInstances( Collection<Program> programs )
     {
         return programInstanceStore.get( programs );
     }
 
+    @Override
     public Collection<ProgramInstance> getProgramInstances( Collection<Program> programs,
         OrganisationUnit organisationUnit )
     {
         return programInstanceStore.get( programs, organisationUnit );
     }
 
+    @Override
     public Collection<ProgramInstance> getProgramInstances( Collection<Program> programs,
         OrganisationUnit organisationUnit, int status )
     {
         return programInstanceStore.get( programs, organisationUnit, status );
     }
 
+    @Override
     public Collection<ProgramInstance> getProgramInstances( Collection<Program> programs, Integer status )
     {
         return programInstanceStore.get( programs, status );
     }
 
+    @Override
     public Collection<ProgramInstance> getProgramInstances( Program program, Integer status )
     {
         return programInstanceStore.get( program, status );
     }
 
+    @Override
     public Collection<ProgramInstance> getProgramInstances( TrackedEntityInstance entityInstance, Integer status )
     {
         return programInstanceStore.get( entityInstance, status );
     }
 
+    @Override
     public Collection<ProgramInstance> getProgramInstances( TrackedEntityInstance entityInstance, Program program )
     {
         return programInstanceStore.get( entityInstance, program );
     }
 
+    @Override
     public Collection<ProgramInstance> getProgramInstances( TrackedEntityInstance entityInstance, Program program,
         Integer status )
     {
         return programInstanceStore.get( entityInstance, program, status );
     }
 
+    @Override
     public Collection<ProgramInstance> getProgramInstances( Program program, OrganisationUnit organisationUnit,
         Integer min, Integer max )
     {
         return programInstanceStore.get( program, organisationUnit, min, max );
     }
 
+    @Override
     public Collection<ProgramInstance> getProgramInstances( Program program, Collection<Integer> orgunitIds,
         Date startDate, Date endDate, Integer min, Integer max )
     {
         return programInstanceStore.get( program, orgunitIds, startDate, endDate, min, max );
     }
 
-    public int countProgramInstances( Program program, OrganisationUnit organisationUnit )
-    {
-        return programInstanceStore.count( program, organisationUnit );
-    }
-
+    @Override
     public int countProgramInstances( Program program, Collection<Integer> orgunitIds, Date startDate, Date endDate )
     {
         return programInstanceStore.count( program, orgunitIds, startDate, endDate );
     }
 
+    @Override
     public List<Grid> getProgramInstanceReport( TrackedEntityInstance instance, I18n i18n )
     {
 
@@ -321,6 +333,7 @@
         return grids;
     }
 
+    @Override
     public Grid getProgramInstanceReport( ProgramInstance programInstance, I18n i18n )
     {
         I18nFormat format = i18nManager.getI18nFormat();
@@ -418,18 +431,28 @@
         return grid;
     }
 
+    @Override
     public int countProgramInstancesByStatus( Integer status, Program program, Collection<Integer> orgunitIds,
         Date startDate, Date endDate )
     {
         return programInstanceStore.countByStatus( status, program, orgunitIds, startDate, endDate );
     }
 
+    @Override
     public Collection<ProgramInstance> getProgramInstancesByStatus( Integer status, Program program,
         Collection<Integer> orgunitIds, Date startDate, Date endDate )
     {
         return programInstanceStore.getByStatus( status, program, orgunitIds, startDate, endDate );
     }
 
+    @Override
+    public Collection<ProgramInstance> getProgramInstancesByStatus( Integer status, Program program,
+        Collection<Integer> orgunitIds, Date startDate, Date endDate, Integer min, Integer max )
+    {
+        return programInstanceStore.getByStatus( status, program, orgunitIds, startDate, endDate, min, max );
+    }
+
+    @Override
     public Collection<SchedulingProgramObject> getScheduleMesssages()
     {
         Collection<SchedulingProgramObject> result = programInstanceStore

=== modified file 'dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/program/hibernate/HibernateProgramInstanceStore.java'
--- dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/program/hibernate/HibernateProgramInstanceStore.java	2014-10-15 14:21:42 +0000
+++ dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/program/hibernate/HibernateProgramInstanceStore.java	2014-10-16 05:49:47 +0000
@@ -28,12 +28,6 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Date;
-import java.util.HashSet;
-import java.util.List;
-
 import org.hibernate.Criteria;
 import org.hibernate.criterion.Order;
 import org.hibernate.criterion.Projections;
@@ -51,6 +45,12 @@
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.jdbc.support.rowset.SqlRowSet;
 
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Date;
+import java.util.HashSet;
+import java.util.List;
+
 /**
  * @author Abyot Asalefew
  */
@@ -60,18 +60,18 @@
 {
     @Autowired
     private TrackedEntityInstanceReminderService reminderService;
-    
+
     // -------------------------------------------------------------------------
     // Implemented methods
     // -------------------------------------------------------------------------
 
-    @SuppressWarnings( "unchecked" )
+    @SuppressWarnings("unchecked")
     public Collection<ProgramInstance> get( Program program )
     {
         return getCriteria( Restrictions.eq( "program", program ) ).list();
     }
 
-    @SuppressWarnings( "unchecked" )
+    @SuppressWarnings("unchecked")
     public Collection<ProgramInstance> get( Collection<Program> programs )
     {
         if ( programs == null || programs.isEmpty() )
@@ -83,7 +83,7 @@
     }
 
     @Override
-    @SuppressWarnings( "unchecked" )
+    @SuppressWarnings("unchecked")
     public Collection<ProgramInstance> get( Collection<Program> programs, OrganisationUnit organisationUnit )
     {
         if ( programs == null || programs.isEmpty() )
@@ -91,14 +91,14 @@
             return new ArrayList<>();
         }
 
-        return getCriteria( 
+        return getCriteria(
             Restrictions.in( "program", programs ) ).
             createAlias( "entityInstance", "entityInstance" ).
             add( Restrictions.eq( "entityInstance.organisationUnit", organisationUnit ) ).list();
     }
 
     @Override
-    @SuppressWarnings( "unchecked" )
+    @SuppressWarnings("unchecked")
     public Collection<ProgramInstance> get( Collection<Program> programs, OrganisationUnit organisationUnit, int status )
     {
         if ( programs == null || programs.isEmpty() )
@@ -106,20 +106,20 @@
             return new ArrayList<>();
         }
 
-        return getCriteria( 
-            Restrictions.eq( "status", status ), 
+        return getCriteria(
+            Restrictions.eq( "status", status ),
             Restrictions.in( "program", programs ) ).
             createAlias( "entityInstance", "entityInstance" ).
             add( Restrictions.eq( "entityInstance.organisationUnit", organisationUnit ) ).list();
     }
 
-    @SuppressWarnings( "unchecked" )
+    @SuppressWarnings("unchecked")
     public Collection<ProgramInstance> get( Program program, Integer status )
     {
         return getCriteria( Restrictions.eq( "program", program ), Restrictions.eq( "status", status ) ).list();
     }
 
-    @SuppressWarnings( "unchecked" )
+    @SuppressWarnings("unchecked")
     public Collection<ProgramInstance> get( Collection<Program> programs, Integer status )
     {
         if ( programs == null || programs.isEmpty() )
@@ -130,29 +130,29 @@
         return getCriteria( Restrictions.in( "program", programs ), Restrictions.eq( "status", status ) ).list();
     }
 
-    @SuppressWarnings( "unchecked" )
+    @SuppressWarnings("unchecked")
     public Collection<ProgramInstance> get( TrackedEntityInstance entityInstance, Integer status )
     {
         return getCriteria( Restrictions.eq( "entityInstance", entityInstance ), Restrictions.eq( "status", status ) ).list();
     }
 
-    @SuppressWarnings( "unchecked" )
+    @SuppressWarnings("unchecked")
     public Collection<ProgramInstance> get( TrackedEntityInstance entityInstance, Program program )
     {
         return getCriteria( Restrictions.eq( "entityInstance", entityInstance ), Restrictions.eq( "program", program ) ).list();
     }
 
-    @SuppressWarnings( "unchecked" )
+    @SuppressWarnings("unchecked")
     public Collection<ProgramInstance> get( TrackedEntityInstance entityInstance, Program program, Integer status )
     {
         return getCriteria( Restrictions.eq( "entityInstance", entityInstance ), Restrictions.eq( "program", program ),
             Restrictions.eq( "status", status ) ).list();
     }
 
-    @SuppressWarnings( "unchecked" )
+    @SuppressWarnings("unchecked")
     public Collection<ProgramInstance> get( Program program, OrganisationUnit organisationUnit, Integer min, Integer max )
     {
-        Criteria criteria = getCriteria( 
+        Criteria criteria = getCriteria(
             Restrictions.eq( "program", program ), Restrictions.isNull( "endDate" ) ).
             add( Restrictions.eq( "entityInstance.organisationUnit", organisationUnit ) ).
             createAlias( "entityInstance", "entityInstance" ).
@@ -162,16 +162,16 @@
         {
             criteria.setFirstResult( min );
         }
-        
+
         if ( max != null )
         {
             criteria.setMaxResults( max );
         }
-        
+
         return criteria.list();
     }
 
-    @SuppressWarnings( "unchecked" )
+    @SuppressWarnings("unchecked")
     public Collection<ProgramInstance> get( Program program, Collection<Integer> orgunitIds, Date startDate,
         Date endDate, Integer min, Integer max )
     {
@@ -184,31 +184,31 @@
         {
             criteria.setFirstResult( min );
         }
-        
+
         if ( max != null )
         {
             criteria.setMaxResults( max );
         }
-        
+
         return criteria.list();
     }
 
     public int count( Program program, OrganisationUnit organisationUnit )
     {
-        Number rs = (Number) getCriteria( 
+        Number rs = (Number) getCriteria(
             Restrictions.eq( "program", program ), Restrictions.isNull( "endDate" ) ).
             createAlias( "entityInstance", "entityInstance" ).
             add( Restrictions.eq( "entityInstance.organisationUnit", organisationUnit ) ).
             setProjection( Projections.rowCount() ).uniqueResult();
-        
+
         return rs != null ? rs.intValue() : 0;
     }
 
     public int count( Program program, Collection<Integer> orgunitIds, Date startDate, Date endDate )
     {
-        Number rs = (Number) getCriteria( 
+        Number rs = (Number) getCriteria(
             Restrictions.eq( "program", program ),
-            Restrictions.ge( "enrollmentDate", startDate ), 
+            Restrictions.ge( "enrollmentDate", startDate ),
             Restrictions.le( "enrollmentDate", endDate ) ).
             createAlias( "entityInstance", "entityInstance" ).
             createAlias( "entityInstance.organisationUnit", "organisationUnit" ).
@@ -220,7 +220,7 @@
 
     public int countByStatus( Integer status, Program program, Collection<Integer> orgunitIds, Date startDate, Date endDate )
     {
-        Number rs = (Number) getCriteria( 
+        Number rs = (Number) getCriteria(
             Restrictions.eq( "program", program ),
             Restrictions.between( "enrollmentDate", startDate, endDate ) ).
             createAlias( "entityInstance", "entityInstance" ).
@@ -236,7 +236,7 @@
     public Collection<ProgramInstance> getByStatus( Integer status, Program program, Collection<Integer> orgunitIds,
         Date startDate, Date endDate )
     {
-        return getCriteria( 
+        return getCriteria(
             Restrictions.eq( "program", program ),
             Restrictions.between( "enrollmentDate", startDate, endDate ) ).
             createAlias( "entityInstance", "entityInstance" ).
@@ -244,7 +244,32 @@
             add( Restrictions.in( "organisationUnit.id", orgunitIds ) ).
             add( Restrictions.eq( "status", status ) ).list();
     }
-    
+
+    @SuppressWarnings("unchecked")
+    public Collection<ProgramInstance> getByStatus( Integer status, Program program, Collection<Integer> orgunitIds,
+        Date startDate, Date endDate, Integer min, Integer max )
+    {
+        Criteria criteria = getCriteria(
+            Restrictions.eq( "program", program ),
+            Restrictions.between( "enrollmentDate", startDate, endDate ) ).
+            createAlias( "entityInstance", "entityInstance" ).
+            createAlias( "entityInstance.organisationUnit", "organisationUnit" ).
+            add( Restrictions.in( "organisationUnit.id", orgunitIds ) ).
+            add( Restrictions.eq( "status", status ) );
+
+        if ( min != null )
+        {
+            criteria.setFirstResult( min );
+        }
+
+        if ( max != null )
+        {
+            criteria.setMaxResults( max );
+        }
+
+        return criteria.list();
+    }
+
     //TODO from here this class must be rewritten
 
     public Collection<SchedulingProgramObject> getSendMesssageEvents( String dateToCompare )
@@ -260,7 +285,7 @@
         SqlRowSet rs = jdbcTemplate.queryForRowSet( sql );
 
         Collection<SchedulingProgramObject> schedulingProgramObjects = new HashSet<>();
-        
+
         if ( rs.getRow() > 0 )
         {
             while ( rs.next() )
@@ -311,7 +336,7 @@
                 schedulingProgramObjects.add( schedulingProgramObject );
             }
         }
-        
+
         return schedulingProgramObjects;
     }
 

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/event/ProgramInstanceController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/event/ProgramInstanceController.java	2014-10-16 05:27:09 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/event/ProgramInstanceController.java	2014-10-16 05:49:47 +0000
@@ -83,6 +83,7 @@
         @RequestParam( value = "orgUnit" ) List<String> orgUnits,
         @RequestParam @DateTimeFormat( pattern = "yyyy-MM-dd" ) Date startDate,
         @RequestParam @DateTimeFormat( pattern = "yyyy-MM-dd" ) Date endDate,
+        @RequestParam( required = false ) Integer status,
         @RequestParam Map<String, String> parameters, HttpServletResponse response
     )
     {
@@ -115,18 +116,22 @@
 
         if ( options.hasPaging() )
         {
-            int count = programInstanceService.countProgramInstances( program, identifiers, startDate, endDate );
+            int count = status == null ?
+                programInstanceService.countProgramInstances( program, identifiers, startDate, endDate ) :
+                programInstanceService.countProgramInstancesByStatus( status, program, identifiers, startDate, endDate );
 
             Pager pager = new Pager( options.getPage(), count, options.getPageSize() );
             metaData.setPager( pager );
 
-            programInstances = new ArrayList<>( programInstanceService.getProgramInstances( program, identifiers,
-                startDate, endDate, pager.getOffset(), pager.getPageSize() ) );
+            programInstances = new ArrayList<>( status == null ?
+                programInstanceService.getProgramInstances( program, identifiers, startDate, endDate, pager.getOffset(), pager.getPageSize() ) :
+                programInstanceService.getProgramInstancesByStatus( status, program, identifiers, startDate, endDate, pager.getOffset(), pager.getPageSize() ) );
         }
         else
         {
-            programInstances = new ArrayList<>( programInstanceService.getProgramInstances( program, identifiers,
-                startDate, endDate, 0, Integer.MAX_VALUE ) );
+            programInstances = new ArrayList<>( status == null ?
+                programInstanceService.getProgramInstances( program, identifiers, startDate, endDate, 0, Integer.MAX_VALUE ) :
+                programInstanceService.getProgramInstancesByStatus( status, program, identifiers, startDate, endDate ) );
         }
 
         RootNode rootNode = new RootNode( "metadata" );