← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 11479: Allow to send many template messages after completing a programstage.

 

------------------------------------------------------------
revno: 11479
committer: Tran Chau <tran.hispvietnam@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2013-07-23 09:52:29 +0700
message:
  Allow to send many template messages after completing a programstage.
modified:
  dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/caseentry/CompleteDataEntryAction.java
  dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/reminder/SetEventStatusAction.java
  dhis-2/dhis-web/dhis-web-caseentry/src/main/resources/META-INF/dhis/beans.xml


--
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-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/caseentry/CompleteDataEntryAction.java'
--- dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/caseentry/CompleteDataEntryAction.java	2013-07-22 09:24:53 +0000
+++ dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/caseentry/CompleteDataEntryAction.java	2013-07-23 02:52:29 +0000
@@ -225,7 +225,6 @@
                 if ( rm != null && rm.getWhenToSend() == PatientReminder.SEND_WHEN_TO_C0MPLETED_EVENT )
                 {
                     sendEventMessage( rm, programStageInstance, patient );
-                    break;
                 }
             }
         }
@@ -280,7 +279,6 @@
                 if ( rm != null && rm.getWhenToSend() == PatientReminder.SEND_WHEN_TO_C0MPLETED_PROGRAM )
                 {
                     sendProgramMessage( rm, programInstance, patient );
-                    break;
                 }
             }
         }

=== modified file 'dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/reminder/SetEventStatusAction.java'
--- dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/reminder/SetEventStatusAction.java	2012-09-20 06:42:42 +0000
+++ dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/reminder/SetEventStatusAction.java	2013-07-23 02:52:29 +0000
@@ -27,8 +27,24 @@
 
 package org.hisp.dhis.caseentry.action.reminder;
 
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Date;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import org.hisp.dhis.i18n.I18nFormat;
+import org.hisp.dhis.patient.Patient;
+import org.hisp.dhis.patient.PatientReminder;
 import org.hisp.dhis.program.ProgramStageInstance;
 import org.hisp.dhis.program.ProgramStageInstanceService;
+import org.hisp.dhis.sms.SmsServiceException;
+import org.hisp.dhis.sms.outbound.OutboundSms;
+import org.hisp.dhis.sms.outbound.OutboundSmsService;
+import org.hisp.dhis.system.util.DateUtils;
+import org.hisp.dhis.user.CurrentUserService;
+import org.hisp.dhis.user.User;
 
 import com.opensymphony.xwork2.Action;
 
@@ -51,6 +67,27 @@
         this.programStageInstanceService = programStageInstanceService;
     }
 
+    private CurrentUserService currentUserService;
+
+    public void setCurrentUserService( CurrentUserService currentUserService )
+    {
+        this.currentUserService = currentUserService;
+    }
+
+    private OutboundSmsService outboundSmsService;
+
+    public void setOutboundSmsService( OutboundSmsService outboundSmsService )
+    {
+        this.outboundSmsService = outboundSmsService;
+    }
+
+    private I18nFormat format;
+
+    public void setFormat( I18nFormat format )
+    {
+        this.format = format;
+    }
+
     // -------------------------------------------------------------------------
     // Input
     // -------------------------------------------------------------------------
@@ -85,6 +122,7 @@
         case ProgramStageInstance.COMPLETED_STATUS:
             programStageInstance.setCompleted( true );
             programStageInstance.setStatus( null );
+            sendSMSToCompletedEvent(programStageInstance);
             break;
         case ProgramStageInstance.VISITED_STATUS:
             programStageInstance.setCompleted( false );
@@ -110,4 +148,117 @@
         return SUCCESS;
     }
 
+    // -------------------------------------------------------------------------
+    // Supportive methods
+    // -------------------------------------------------------------------------
+
+    private void sendSMSToCompletedEvent( ProgramStageInstance programStageInstance )
+    {
+        Patient patient = programStageInstance.getProgramInstance().getPatient();
+
+        if ( patient != null )
+        {
+            Collection<PatientReminder> reminders = programStageInstance.getProgramStage().getPatientReminders();
+            for ( PatientReminder rm : reminders )
+            {
+                if ( rm != null && rm.getWhenToSend() == PatientReminder.SEND_WHEN_TO_C0MPLETED_EVENT )
+                {
+                    sendEventMessage( rm, programStageInstance, patient );
+                }
+            }
+        }
+    }
+
+    private void sendEventMessage( PatientReminder reminder, ProgramStageInstance programStageInstance, Patient patient )
+    {
+        Set<String> phoneNumbers = getPhonenumbers( reminder, patient );
+
+        if ( phoneNumbers.size() > 0 )
+        {
+            String msg = getStringMsgFromTemplateMsg( reminder, programStageInstance, patient );
+            try
+            {
+                OutboundSms outboundSms = new OutboundSms();
+                outboundSms.setMessage( msg );
+                outboundSms.setRecipients( phoneNumbers );
+                outboundSms.setSender( currentUserService.getCurrentUsername() );
+                outboundSmsService.sendMessage( outboundSms, null );
+
+                List<OutboundSms> outboundSmsList = programStageInstance.getOutboundSms();
+                if ( outboundSmsList == null )
+                {
+                    outboundSmsList = new ArrayList<OutboundSms>();
+                }
+                outboundSmsList.add( outboundSms );
+                programStageInstance.setOutboundSms( outboundSmsList );
+            }
+            catch ( SmsServiceException e )
+            {
+                e.printStackTrace();
+            }
+        }
+
+    }
+
+    public String getStringMsgFromTemplateMsg( PatientReminder reminder, ProgramStageInstance programStageInstance,
+        Patient patient )
+    {
+        String msg = reminder.getTemplateMessage();
+
+        String patientName = patient.getFirstName();
+        String organisationunitName = patient.getOrganisationUnit().getName();
+        String programName = programStageInstance.getProgramInstance().getProgram().getName();
+        String programStageName = programStageInstance.getProgramStage().getName();
+        String daysSinceDueDate = DateUtils.daysBetween( new Date(), programStageInstance.getDueDate() ) + "";
+        String dueDate = format.formatDate( programStageInstance.getDueDate() );
+
+        msg = msg.replace( PatientReminder.TEMPLATE_MESSSAGE_PATIENT_NAME, patientName );
+        msg = msg.replace( PatientReminder.TEMPLATE_MESSSAGE_PROGRAM_NAME, programName );
+        msg = msg.replace( PatientReminder.TEMPLATE_MESSSAGE_PROGAM_STAGE_NAME, programStageName );
+        msg = msg.replace( PatientReminder.TEMPLATE_MESSSAGE_DUE_DATE, dueDate );
+        msg = msg.replace( PatientReminder.TEMPLATE_MESSSAGE_ORGUNIT_NAME, organisationunitName );
+        msg = msg.replace( PatientReminder.TEMPLATE_MESSSAGE_DAYS_SINCE_DUE_DATE, daysSinceDueDate );
+
+        return msg;
+    }
+
+    private Set<String> getPhonenumbers( PatientReminder reminder, Patient patient )
+    {
+        Set<String> phoneNumbers = new HashSet<String>();
+
+        switch ( reminder.getSendTo() )
+        {
+        case PatientReminder.SEND_TO_ALL_USERS_IN_ORGUGNIT_REGISTERED:
+            Collection<User> users = patient.getOrganisationUnit().getUsers();
+            for ( User user : users )
+            {
+                if ( user.getPhoneNumber() != null && !user.getPhoneNumber().isEmpty() )
+                {
+                    phoneNumbers.add( user.getPhoneNumber() );
+                }
+            }
+            break;
+        case PatientReminder.SEND_TO_HEALTH_WORKER:
+            if ( patient.getHealthWorker() != null && patient.getHealthWorker().getPhoneNumber() != null )
+            {
+                phoneNumbers.add( patient.getHealthWorker().getPhoneNumber() );
+            }
+            break;
+        case PatientReminder.SEND_TO_ORGUGNIT_REGISTERED:
+            if ( patient.getOrganisationUnit().getPhoneNumber() != null
+                && !patient.getOrganisationUnit().getPhoneNumber().isEmpty() )
+            {
+                phoneNumbers.add( patient.getOrganisationUnit().getPhoneNumber() );
+            }
+            break;
+        default:
+            if ( patient.getPhoneNumber() != null && !patient.getPhoneNumber().isEmpty() )
+            {
+                phoneNumbers.add( patient.getPhoneNumber() );
+            }
+            break;
+        }
+        return phoneNumbers;
+    }
+
 }

=== modified file 'dhis-2/dhis-web/dhis-web-caseentry/src/main/resources/META-INF/dhis/beans.xml'
--- dhis-2/dhis-web/dhis-web-caseentry/src/main/resources/META-INF/dhis/beans.xml	2013-07-19 06:04:09 +0000
+++ dhis-2/dhis-web/dhis-web-caseentry/src/main/resources/META-INF/dhis/beans.xml	2013-07-23 02:52:29 +0000
@@ -1095,6 +1095,8 @@
 		scope="prototype">
 		<property name="programStageInstanceService"
 			ref="org.hisp.dhis.program.ProgramStageInstanceService" />
+		<property name="currentUserService" ref="org.hisp.dhis.user.CurrentUserService" />
+		<property name="outboundSmsService" ref="org.hisp.dhis.sms.outbound.OutboundSmsService" />
 	</bean>