dhis2-devs team mailing list archive
-
dhis2-devs team
-
Mailing list archive
-
Message #23741
[Branch ~dhis2-devs-core/dhis2/trunk] Rev 11556: Add a functionality to send sms after a person enrolls into a program.
------------------------------------------------------------
revno: 11556
committer: Tran Chau <tran.hispvietnam@xxxxxxxxx>
branch nick: dhis2
timestamp: Fri 2013-08-02 10:34:24 +0700
message:
Add a functionality to send sms after a person enrolls into a program.
modified:
dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/patient/SaveProgramEnrollmentAction.java
dhis-2/dhis-web/dhis-web-caseentry/src/main/resources/META-INF/dhis/beans.xml
dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/webapp/dhis-web-maintenance-patient/javascript/program.js
dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/webapp/dhis-web-maintenance-patient/updateProgramForm.vm
--
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/patient/SaveProgramEnrollmentAction.java'
--- dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/patient/SaveProgramEnrollmentAction.java 2013-07-16 04:31:27 +0000
+++ dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/patient/SaveProgramEnrollmentAction.java 2013-08-02 03:34:24 +0000
@@ -26,12 +26,17 @@
*/
package org.hisp.dhis.caseentry.action.patient;
+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.caseentry.state.SelectedStateManager;
import org.hisp.dhis.i18n.I18nFormat;
import org.hisp.dhis.patient.Patient;
+import org.hisp.dhis.patient.PatientReminder;
import org.hisp.dhis.patient.PatientService;
import org.hisp.dhis.program.Program;
import org.hisp.dhis.program.ProgramInstance;
@@ -40,7 +45,12 @@
import org.hisp.dhis.program.ProgramStage;
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;
@@ -97,6 +107,20 @@
this.format = format;
}
+ private CurrentUserService currentUserService;
+
+ public void setCurrentUserService( CurrentUserService currentUserService )
+ {
+ this.currentUserService = currentUserService;
+ }
+
+ private OutboundSmsService outboundSmsService;
+
+ public void setOutboundSmsService( OutboundSmsService outboundSmsService )
+ {
+ this.outboundSmsService = outboundSmsService;
+ }
+
// -------------------------------------------------------------------------
// Input/Output
// -------------------------------------------------------------------------
@@ -166,7 +190,7 @@
enrollmentDate = format.formatDate( patient.getBirthDate() );
}
}
-
+
if ( dateOfIncident == null || dateOfIncident.isEmpty() )
{
if ( program.getUseBirthDateAsIncidentDate() )
@@ -178,8 +202,6 @@
dateOfIncident = enrollmentDate;
}
}
-
-
Collection<ProgramInstance> programInstances = programInstanceService.getProgramInstances( patient, program,
ProgramInstance.STATUS_ACTIVE );
@@ -242,6 +264,9 @@
}
}
}
+
+ // send messages after enrollment program
+ sendMessage(programInstance);
}
else
{
@@ -267,4 +292,130 @@
return SUCCESS;
}
+
+ // -------------------------------------------------------------------------
+ // Supportive methods
+ // -------------------------------------------------------------------------
+
+ private void sendMessage( ProgramInstance programInstance )
+ {
+ Patient patient = programInstance.getPatient();
+
+ if ( patient != null )
+ {
+ Collection<PatientReminder> reminders = programInstance.getProgram().getPatientReminders();
+ for ( PatientReminder rm : reminders )
+ {
+ if ( rm != null && rm.getWhenToSend() != null
+ && rm.getWhenToSend() == PatientReminder.SEND_WHEN_TO_EMROLLEMENT )
+ {
+ sendProgramMessage( rm, programInstance, patient );
+ }
+ }
+ }
+ }
+
+ private void sendProgramMessage( PatientReminder reminder, ProgramInstance programInstance, Patient patient )
+ {
+ Set<String> phoneNumbers = getPhonenumbers( reminder, patient );
+
+ if ( phoneNumbers.size() > 0 )
+ {
+ String msg = getStringMsgFromTemplateMsg( reminder, programInstance, patient );
+
+ try
+ {
+ OutboundSms outboundSms = new OutboundSms();
+ outboundSms.setMessage( msg );
+ outboundSms.setRecipients( phoneNumbers );
+ outboundSms.setSender( currentUserService.getCurrentUsername() );
+ outboundSmsService.sendMessage( outboundSms, null );
+
+ List<OutboundSms> outboundSmsList = programInstance.getOutboundSms();
+ if ( outboundSmsList == null )
+ {
+ outboundSmsList = new ArrayList<OutboundSms>();
+ }
+ outboundSmsList.add( outboundSms );
+ programInstance.setOutboundSms( outboundSmsList );
+ }
+ catch ( SmsServiceException e )
+ {
+ e.printStackTrace();
+ }
+ }
+ }
+
+ 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;
+ case PatientReminder.SEND_TO_USER_GROUP:
+ for ( User user : reminder.getUserGroup().getMembers() )
+ {
+ if ( user.getPhoneNumber() != null && !user.getPhoneNumber().isEmpty() )
+ {
+ phoneNumbers.add( user.getPhoneNumber() );
+ }
+ }
+ break;
+ default:
+ if ( patient.getPhoneNumber() != null && !patient.getPhoneNumber().isEmpty() )
+ {
+ phoneNumbers.add( patient.getPhoneNumber() );
+ }
+ break;
+ }
+ return phoneNumbers;
+ }
+
+ public String getStringMsgFromTemplateMsg( PatientReminder reminder, ProgramInstance programInstance,
+ Patient patient )
+ {
+ String msg = reminder.getTemplateMessage();
+
+ String patientName = patient.getFirstName();
+ String organisationunitName = patient.getOrganisationUnit().getName();
+ String programName = programInstance.getProgram().getName();
+ String daysSinceEnrollementDate = DateUtils.daysBetween( new Date(), programInstance.getEnrollmentDate() ) + "";
+ String daysSinceIncidentDate = DateUtils.daysBetween( new Date(), programInstance.getDateOfIncident() ) + "";
+ String incidentDate = format.formatDate( programInstance.getDateOfIncident() );
+ String erollmentDate = format.formatDate( programInstance.getEnrollmentDate() );
+
+ msg = msg.replace( PatientReminder.TEMPLATE_MESSSAGE_PATIENT_NAME, patientName );
+ msg = msg.replace( PatientReminder.TEMPLATE_MESSSAGE_PROGRAM_NAME, programName );
+ msg = msg.replace( PatientReminder.TEMPLATE_MESSSAGE_ORGUNIT_NAME, organisationunitName );
+ msg = msg.replace( PatientReminder.TEMPLATE_MESSSAGE_INCIDENT_DATE, incidentDate );
+ msg = msg.replace( PatientReminder.TEMPLATE_MESSSAGE_ENROLLMENT_DATE, erollmentDate );
+ msg = msg.replace( PatientReminder.TEMPLATE_MESSSAGE_DAYS_SINCE_ENROLLMENT_DATE, daysSinceEnrollementDate );
+ msg = msg.replace( PatientReminder.TEMPLATE_MESSSAGE_DAYS_SINCE_INCIDENT_DATE, daysSinceIncidentDate );
+
+ return msg;
+ }
+
}
=== 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-08-01 03:22:27 +0000
+++ dhis-2/dhis-web/dhis-web-caseentry/src/main/resources/META-INF/dhis/beans.xml 2013-08-02 03:34:24 +0000
@@ -652,6 +652,8 @@
ref="org.hisp.dhis.program.ProgramStageInstanceService" />
<property name="selectedStateManager"
ref="org.hisp.dhis.caseentry.state.SelectedStateManager" />
+ <property name="currentUserService" ref="org.hisp.dhis.user.CurrentUserService" />
+ <property name="outboundSmsService" ref="org.hisp.dhis.sms.outbound.OutboundSmsService" />
</bean>
<bean id="org.hisp.dhis.caseentry.action.patient.SaveDueDateAction"
=== modified file 'dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/webapp/dhis-web-maintenance-patient/javascript/program.js'
--- dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/webapp/dhis-web-maintenance-patient/javascript/program.js 2013-08-01 03:48:34 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/webapp/dhis-web-maintenance-patient/javascript/program.js 2013-08-02 03:34:24 +0000
@@ -275,6 +275,7 @@
+ '<select id="whenToSend' + rowId + '" name="whenToSend' + rowId + '" class="whenToSend" onchange="whenToSendOnChange(' + rowId + ')" >'
+ '<option value="">' + i18n_scheduled + '</option>'
+ '<option value="3">' + i18n_complete_program + '</option>'
+ + '<option value="1">' + i18n_program_enrollment + '</option>'
+ '</select>'
+ '</td>'
+ '</tr>'
@@ -358,12 +359,12 @@
function whenToSendOnChange(index)
{
var whenToSend = getFieldValue('whenToSend' + index );
- if(whenToSend==3){
+ if(whenToSend==""){
+ enable('dateToCompare' + index );
+ enable('daysAllowedSendMessage' + index );
+ }
+ else{
disable('dateToCompare' + index );
disable('daysAllowedSendMessage' + index );
}
- else{
- enable('dateToCompare' + index );
- enable('daysAllowedSendMessage' + index );
- }
}
\ No newline at end of file
=== modified file 'dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/webapp/dhis-web-maintenance-patient/updateProgramForm.vm'
--- dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/webapp/dhis-web-maintenance-patient/updateProgramForm.vm 2013-08-01 03:48:34 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/webapp/dhis-web-maintenance-patient/updateProgramForm.vm 2013-08-02 03:34:24 +0000
@@ -324,6 +324,7 @@
<select type="text" id="whenToSend$index" name="whenToSend$index" class="whenToSend" onchange='whenToSendOnChange($index)'>
<option value="" >$i18n.getString("scheduled")</option>
<option value="3" #if($!reminder.whenToSend==3) selected #end >$i18n.getString("complete_program")</option>
+ <option value="1" #if($!reminder.whenToSend==1) selected #end >$i18n.getString("program_enrollment")</option>
</select>
</td>
</tr>
@@ -340,7 +341,7 @@
<td><label>$i18n.getString( "days_before_after_comparison_date" )</label></td>
<td><input type="text" id="daysAllowedSendMessage$index" name="daysAllowedSendMessage$index"
class="daysAllowedSendMessage {validate:{required:true,number:true}}" value="$!reminder.daysAllowedSendMessage"
- #if($!reminder.whenToSend==3) disabled #end /></td>
+ #if($!reminder.whenToSend==1 || $!reminder.whenToSend==3 ) disabled #end /></td>
</tr>
<tr name="tr$index">
<td><label>$i18n.getString( "recipients" )</label></td>