← Back to team overview

dhis2-devs team mailing list archive

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

 

------------------------------------------------------------
revno: 12639
committer: Lars Helge Øverland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2013-10-14 16:00:47 +0200
message:
  PatientStore. Re-implemented method using query instead of one-by-one fetching. Made sure that bi-directional associations are updated when enrolling patient into program.
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramInstance.java
  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/program/DefaultProgramInstanceService.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-api/src/main/java/org/hisp/dhis/program/ProgramInstance.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramInstance.java	2013-10-14 12:41:35 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramInstance.java	2013-10-14 14:00:47 +0000
@@ -111,6 +111,22 @@
     // Logic
     // -------------------------------------------------------------------------
 
+    /**
+     * Updated the bi-directional associations between this program instance and
+     * the given patient and program.
+     * 
+     * @param patient the patient to enroll.
+     * @param program the program to enroll the patient to.
+     */
+    public void enrollPatient( Patient patient, Program program )
+    {
+        setPatient( patient );
+        patient.getProgramInstances().add( this );
+        
+        setProgram( program );
+        program.getProgramInstances().add( this );
+    }
+    
     public ProgramStageInstance getProgramStageInstanceByStage( int stage )
     {
         int count = 1;

=== 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 12:24:56 +0000
+++ dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patient/hibernate/HibernatePatientStore.java	2013-10-14 14:00:47 +0000
@@ -177,38 +177,21 @@
     }
 
     @Override
-    //TODO this method must be changed - cannot retrieve one by one
+    @SuppressWarnings("unchecked")
     public Collection<Patient> getByOrgUnitProgram( OrganisationUnit organisationUnit, 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 p.organisationunitid=" + organisationUnit.getId() + " and 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 pt.organisationUnit = :organisationUnit " +
+            "and pi.program = :program";
+        
+        Query query = getQuery( hql );
+        query.setEntity( "organisationUnit", organisationUnit );
+        query.setEntity( "program", program );
+        
+        return query.list();
     }
 
     @Override

=== modified file 'dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/program/DefaultProgramInstanceService.java'
--- dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/program/DefaultProgramInstanceService.java	2013-09-30 19:54:38 +0000
+++ dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/program/DefaultProgramInstanceService.java	2013-10-14 14:00:47 +0000
@@ -67,7 +67,6 @@
 
 /**
  * @author Abyot Asalefew
- * @version $Id$
  */
 @Transactional
 public class DefaultProgramInstanceService
@@ -575,14 +574,14 @@
         Collection<OutboundSms> outboundSmsList = new HashSet<OutboundSms>();
 
         Collection<PatientReminder> reminders = programInstance.getProgram().getPatientReminders();
+        
         for ( PatientReminder rm : reminders )
         {
-            if ( rm != null
-                && rm.getWhenToSend() != null
-                && rm.getWhenToSend() == status
+            if ( rm != null && rm.getWhenToSend() != null && rm.getWhenToSend() == status
                 && (rm.getMessageType() == PatientReminder.MESSAGE_TYPE_DIRECT_SMS || rm.getMessageType() == PatientReminder.MESSAGE_TYPE_BOTH) )
             {
                 OutboundSms outboundSms = sendProgramMessage( rm, programInstance, patient, format );
+                
                 if ( outboundSms != null )
                 {
                     outboundSmsList.add( outboundSms );
@@ -623,39 +622,29 @@
     {
         if ( enrollmentDate == null )
         {
-            if ( program.getUseBirthDateAsIncidentDate() )
-            {
-                enrollmentDate = patient.getBirthDate();
-            }
-            else
-            {
-                enrollmentDate = new Date();
-            }
+            enrollmentDate = program.getUseBirthDateAsIncidentDate() ? patient.getBirthDate() : new Date();
         }
 
         if ( dateOfIncident == null )
         {
-            if ( program.getUseBirthDateAsIncidentDate() )
-            {
-                dateOfIncident = patient.getBirthDate();
-            }
-            else
-            {
-                dateOfIncident = enrollmentDate;
-            }
+            dateOfIncident = program.getUseBirthDateAsIncidentDate() ? patient.getBirthDate() : enrollmentDate;
         }
 
+        // ---------------------------------------------------------------------
+        // Add program instance
+        // ---------------------------------------------------------------------
+
         ProgramInstance programInstance = new ProgramInstance();
+        
+        programInstance.enrollPatient( patient, program );
         programInstance.setEnrollmentDate( enrollmentDate );
         programInstance.setDateOfIncident( dateOfIncident );
-        programInstance.setProgram( program );
-        programInstance.setPatient( patient );
         programInstance.setStatus( ProgramInstance.STATUS_ACTIVE );
 
         addProgramInstance( programInstance );
 
         // ---------------------------------------------------------------------
-        // Generate events of program-instance
+        // Generate events for program instance
         // ---------------------------------------------------------------------
 
         for ( ProgramStage programStage : program.getProgramStages() )
@@ -673,10 +662,11 @@
         }
 
         // -----------------------------------------------------------------
-        // send messages after enrollment program
+        // Send messages after enrolling in program
         // -----------------------------------------------------------------
 
         List<OutboundSms> outboundSms = programInstance.getOutboundSms();
+        
         if ( outboundSms == null )
         {
             outboundSms = new ArrayList<OutboundSms>();
@@ -685,17 +675,17 @@
         outboundSms.addAll( sendMessages( programInstance, PatientReminder.SEND_WHEN_TO_EMROLLEMENT, format ) );
 
         // -----------------------------------------------------------------
-        // Send DHIS message when to completed the program
+        // Send message when to completed the program
         // -----------------------------------------------------------------
 
-        List<MessageConversation> piMessageConversations = programInstance.getMessageConversations();
-        if ( piMessageConversations == null )
+        List<MessageConversation> messages = programInstance.getMessageConversations();
+        
+        if ( messages == null )
         {
-            piMessageConversations = new ArrayList<MessageConversation>();
+            messages = new ArrayList<MessageConversation>();
         }
 
-        piMessageConversations.addAll( sendMessageConversations( programInstance,
-            PatientReminder.SEND_WHEN_TO_EMROLLEMENT, format ) );
+        messages.addAll( sendMessageConversations( programInstance, PatientReminder.SEND_WHEN_TO_EMROLLEMENT, format ) );
 
         updateProgramInstance( programInstance );
 

=== 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 12:24:56 +0000
+++ dhis-2/dhis-services/dhis-service-patient/src/test/java/org/hisp/dhis/patient/PatientStoreTest.java	2013-10-14 14:00:47 +0000
@@ -34,35 +34,52 @@
 import static org.junit.Assert.assertTrue;
 
 import java.util.Collection;
+import java.util.Date;
+import java.util.HashSet;
 
 import org.hisp.dhis.DhisSpringTest;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.organisationunit.OrganisationUnitService;
+import org.hisp.dhis.program.Program;
+import org.hisp.dhis.program.ProgramInstanceService;
+import org.hisp.dhis.program.ProgramService;
+import org.hisp.dhis.program.ProgramStage;
 import org.junit.Test;
+import org.springframework.beans.factory.annotation.Autowired;
 
 /**
  * @author Lars Helge Overland
- * @version $Id$
  */
 public class PatientStoreTest
     extends DhisSpringTest
 {
+    @Autowired
     private PatientStore patientStore;
+
+    @Autowired
+    private ProgramService programService;
+    
+    @Autowired
+    private ProgramInstanceService programInstanceService;
+    
+    @Autowired
     private OrganisationUnitService organisationUnitService;
     
     private Patient patientA;
     private Patient patientB;
     private Patient patientC;
     private Patient patientD;
-    
+        
+    private Program programA;
+    private Program programB;
+        
     private OrganisationUnit organisationUnit;
+    
+    private Date date = new Date();
 
     @Override
     public void setUpTest()
     {
-        patientStore = (PatientStore) getBean( PatientStore.ID );
-        organisationUnitService = (OrganisationUnitService) getBean ( OrganisationUnitService.ID );
-        
         organisationUnit = createOrganisationUnit( 'A' );
         organisationUnitService.addOrganisationUnit( organisationUnit );
         
@@ -70,6 +87,9 @@
         patientB = createPatient( 'B', organisationUnit );
         patientC = createPatient( 'A', null );
         patientD = createPatient( 'B', organisationUnit );
+        
+        programA = createProgram( 'A', new HashSet<ProgramStage>(), organisationUnit );
+        programB = createProgram( 'B', new HashSet<ProgramStage>(), organisationUnit );
     }
     
     @Test
@@ -140,4 +160,32 @@
         assertTrue( patients.contains( patientB ) );
         assertTrue( patients.contains( patientD ) );
     }
+    
+    @Test
+    public void testGetByOrgUnitProgram()
+    {
+        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.getByOrgUnitProgram( organisationUnit, programA, 0, 100 );
+        
+        assertEquals( 2, patients.size() );
+        assertTrue( patients.contains( patientA ) );
+        assertTrue( patients.contains( patientB ) );
+
+        patients = patientStore.getByOrgUnitProgram( organisationUnit, programB, 0, 100 );
+        
+        assertEquals( 1, patients.size() );
+        assertTrue( patients.contains( patientD ) );
+    }
 }