← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 12640: PatientStore. Re-implemented method using query instead of one-by-one fetching.

 

------------------------------------------------------------
revno: 12640
committer: Lars Helge Øverland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2013-10-14 16:03:52 +0200
message:
  PatientStore. Re-implemented method using query instead of one-by-one fetching.
modified:
  dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patient/hibernate/HibernatePatientStore.java
  dhis-2/dhis-services/dhis-service-patient/src/test/java/org/hisp/dhis/patient/PatientStoreTest.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-services/dhis-service-patient/src/main/java/org/hisp/dhis/patient/hibernate/HibernatePatientStore.java'
--- dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patient/hibernate/HibernatePatientStore.java	2013-10-14 14:00:47 +0000
+++ dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patient/hibernate/HibernatePatientStore.java	2013-10-14 14:03:52 +0000
@@ -185,46 +185,32 @@
             "select pt from Patient pt " +
             "inner join pt.programInstances pi " +
             "where pt.organisationUnit = :organisationUnit " +
-            "and pi.program = :program";
+            "and pi.program = :program " +
+            "and pi.status = :status";
         
         Query query = getQuery( hql );
         query.setEntity( "organisationUnit", organisationUnit );
         query.setEntity( "program", program );
+        query.setInteger( "status", ProgramInstance.STATUS_ACTIVE );
         
         return query.list();
     }
 
     @Override
-    //TODO this method must be changed - cannot retrieve one by one
+    @SuppressWarnings("unchecked")
     public Collection<Patient> getByProgram( Program program, Integer min, Integer max )
     {
-        List<Patient> patients = new ArrayList<Patient>();
-
-        String sql = "select p.patientid from patient p join programinstance pi on p.patientid=pi.patientid "
-            + "where pi.programid=" + program.getId() + " and pi.status=" + ProgramInstance.STATUS_ACTIVE;
-
-        if ( min != null && max != null )
-        {
-            sql += statementBuilder.limitRecord( min, max );
-        }
-
-        try
-        {
-            patients = jdbcTemplate.query( sql, new RowMapper<Patient>()
-            {
-                public Patient mapRow( ResultSet rs, int rowNum )
-                    throws SQLException
-                {
-                    return get( rs.getInt( 1 ) );
-                }
-            } );
-        }
-        catch ( Exception ex )
-        {
-            ex.printStackTrace();
-        }
-
-        return patients;
+        String hql = 
+            "select pt from Patient pt " +
+            "inner join pt.programInstances pi " +
+            "where pi.program = :program " +
+            "and pi.status = :status";
+        
+        Query query = getQuery( hql );
+        query.setEntity( "program", program );
+        query.setInteger( "status", ProgramInstance.STATUS_ACTIVE );
+        
+        return query.list();
     }
 
     @Override

=== modified file 'dhis-2/dhis-services/dhis-service-patient/src/test/java/org/hisp/dhis/patient/PatientStoreTest.java'
--- dhis-2/dhis-services/dhis-service-patient/src/test/java/org/hisp/dhis/patient/PatientStoreTest.java	2013-10-14 14:00:47 +0000
+++ dhis-2/dhis-services/dhis-service-patient/src/test/java/org/hisp/dhis/patient/PatientStoreTest.java	2013-10-14 14:03:52 +0000
@@ -188,4 +188,33 @@
         assertEquals( 1, patients.size() );
         assertTrue( patients.contains( patientD ) );
     }
+
+    @Test
+    public void testGetByProgram()
+    {
+        programService.saveProgram( programA );
+        programService.saveProgram( programB );
+        
+        patientStore.save( patientA );
+        patientStore.save( patientB );
+        patientStore.save( patientC );
+        patientStore.save( patientD );
+        
+        programInstanceService.enrollPatient( patientA, programA, date, date, organisationUnit, null );
+        programInstanceService.enrollPatient( patientB, programA, date, date, organisationUnit, null );
+        programInstanceService.enrollPatient( patientC, programA, date, date, organisationUnit, null );
+        programInstanceService.enrollPatient( patientD, programB, date, date, organisationUnit, null );
+        
+        Collection<Patient> patients = patientStore.getByProgram( programA, 0, 100 );
+        
+        assertEquals( 3, patients.size() );
+        assertTrue( patients.contains( patientA ) );
+        assertTrue( patients.contains( patientB ) );
+        assertTrue( patients.contains( patientC ) );
+
+        patients = patientStore.getByOrgUnitProgram( organisationUnit, programB, 0, 100 );
+        
+        assertEquals( 1, patients.size() );
+        assertTrue( patients.contains( patientD ) );
+    }
 }