← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 12000: Add functionalities to allow sending template message as DHIS message.

 

------------------------------------------------------------
revno: 12000
committer: Tran Chau <tran.hispvietnam@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2013-09-10 11:51:49 +0700
message:
  Add functionalities to allow sending template message as DHIS message.
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/patient/PatientReminder.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/patient/PatientReminderService.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramInstance.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramInstanceService.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramStageInstance.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramStageInstanceService.java
  dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patient/DefaultPatientReminderService.java
  dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patient/startup/TableAlteror.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/main/java/org/hisp/dhis/program/DefaultProgramStageInstanceService.java
  dhis-2/dhis-services/dhis-service-patient/src/main/resources/META-INF/dhis/beans.xml
  dhis-2/dhis-services/dhis-service-patient/src/main/resources/org/hisp/dhis/patient/hibernate/PatientReminder.hbm.xml
  dhis-2/dhis-services/dhis-service-patient/src/main/resources/org/hisp/dhis/program/hibernate/ProgramInstance.hbm.xml
  dhis-2/dhis-services/dhis-service-patient/src/main/resources/org/hisp/dhis/program/hibernate/ProgramStageInstance.hbm.xml
  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/patient/SaveProgramEnrollmentAction.java
  dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/patient/SetProgramInstanceStatusAction.java
  dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/patient/UpdatePatientAction.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
  dhis-2/dhis-web/dhis-web-caseentry/src/main/resources/org/hisp/dhis/caseentry/i18n_module.properties
  dhis-2/dhis-web/dhis-web-caseentry/src/main/webapp/dhis-web-caseentry/eventMessage.vm
  dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/java/org/hisp/dhis/patient/action/program/AddProgramAction.java
  dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/java/org/hisp/dhis/patient/action/program/UpdateProgramAction.java
  dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/java/org/hisp/dhis/patient/action/programstage/AddProgramStageAction.java
  dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/java/org/hisp/dhis/patient/action/programstage/UpdateProgramStageAction.java
  dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/resources/org/hisp/dhis/patient/i18n_module.properties
  dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/webapp/dhis-web-maintenance-patient/addProgramForm.vm
  dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/webapp/dhis-web-maintenance-patient/addProgramStageForm.vm
  dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/webapp/dhis-web-maintenance-patient/javascript/addProgramStageForm.js
  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/programStage.js
  dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/webapp/dhis-web-maintenance-patient/javascript/updateProgramStageForm.js
  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/updateProgramStageForm.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-api/src/main/java/org/hisp/dhis/patient/PatientReminder.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/patient/PatientReminder.java	2013-08-23 15:56:19 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/patient/PatientReminder.java	2013-09-10 04:51:49 +0000
@@ -66,6 +66,10 @@
     public static final int SEND_WHEN_TO_C0MPLETED_EVENT = 2;
     public static final int SEND_WHEN_TO_C0MPLETED_PROGRAM = 3;
 
+    public static final int MESSAGE_TYPE_DIRECT_SMS = 1;
+    public static final int MESSAGE_TYPE_DHIS_MESSAGE = 2;
+    public static final int MESSAGE_TYPE_BOTH = 3;
+    
     private int id;
 
     private String name;
@@ -80,6 +84,8 @@
 
     private Integer whenToSend;
 
+    private Integer messageType;
+    
     private UserGroup userGroup;
 
     // -------------------------------------------------------------------------
@@ -182,4 +188,14 @@
     {
         this.userGroup = userGroup;
     }
+
+    public Integer getMessageType()
+    {
+        return messageType;
+    }
+
+    public void setMessageType( Integer messageType )
+    {
+        this.messageType = messageType;
+    }
 }

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/patient/PatientReminderService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/patient/PatientReminderService.java	2013-08-23 15:56:19 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/patient/PatientReminderService.java	2013-09-10 04:51:49 +0000
@@ -33,6 +33,7 @@
 import org.hisp.dhis.i18n.I18nFormat;
 import org.hisp.dhis.program.ProgramInstance;
 import org.hisp.dhis.program.ProgramStageInstance;
+import org.hisp.dhis.user.User;
 
 /**
  * @author Chau Thu Tran
@@ -43,7 +44,10 @@
 {
     String getMessageFromTemplate( PatientReminder patientReminder, ProgramInstance programInstance, I18nFormat format );
 
-    String getMessageFromTemplate( PatientReminder patientReminder, ProgramStageInstance programStageInstance, I18nFormat format );
-    
+    String getMessageFromTemplate( PatientReminder patientReminder, ProgramStageInstance programStageInstance,
+        I18nFormat format );
+
     Set<String> getPhonenumbers( PatientReminder patientReminder, Patient patient );
+
+    Set<User> getUsers( PatientReminder patientReminder, Patient patient );
 }

=== 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-09-03 15:04:30 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramInstance.java	2013-09-10 04:51:49 +0000
@@ -37,6 +37,7 @@
 import org.hisp.dhis.common.DxfNamespaces;
 import org.hisp.dhis.common.view.DetailedView;
 import org.hisp.dhis.common.view.ExportView;
+import org.hisp.dhis.message.MessageConversation;
 import org.hisp.dhis.patient.Patient;
 import org.hisp.dhis.patientcomment.PatientComment;
 import org.hisp.dhis.sms.outbound.OutboundSms;
@@ -52,10 +53,13 @@
  * @version $Id$
  */
 @JacksonXmlRootElement( localName = "programInstance", namespace = DxfNamespaces.DXF_2_0 )
-public class ProgramInstance extends BaseIdentifiableObject
+public class ProgramInstance
+    extends BaseIdentifiableObject
 {
     public static int STATUS_ACTIVE = 0;
+
     public static int STATUS_COMPLETED = 1;
+
     public static int STATUS_CANCELLED = 2;
 
     /**
@@ -81,6 +85,8 @@
 
     private List<OutboundSms> outboundSms;
 
+    private List<MessageConversation> messageConversations;
+
     private Boolean followup;
 
     private PatientComment patientComment;
@@ -119,10 +125,10 @@
         final int prime = 31;
         int result = 1;
 
-        result = prime * result + ( ( dateOfIncident == null) ? 0 : dateOfIncident.hashCode() );
-        result = prime * result + ( ( enrollmentDate == null) ? 0 : enrollmentDate.hashCode() );
-        result = prime * result + ( ( patient == null) ? 0 : patient.hashCode() );
-        result = prime * result + ( ( program == null) ? 0 : program.hashCode() );
+        result = prime * result + ((dateOfIncident == null) ? 0 : dateOfIncident.hashCode());
+        result = prime * result + ((enrollmentDate == null) ? 0 : enrollmentDate.hashCode());
+        result = prime * result + ((patient == null) ? 0 : patient.hashCode());
+        result = prime * result + ((program == null) ? 0 : program.hashCode());
 
         return result;
     }
@@ -373,6 +379,16 @@
         this.patientComment = patientComment;
     }
 
+    public List<MessageConversation> getMessageConversations()
+    {
+        return messageConversations;
+    }
+
+    public void setMessageConversations( List<MessageConversation> messageConversations )
+    {
+        this.messageConversations = messageConversations;
+    }
+
     // -------------------------------------------------------------------------
     // Convenience method
     // -------------------------------------------------------------------------

=== 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	2013-08-23 15:56:19 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramInstanceService.java	2013-09-10 04:51:49 +0000
@@ -35,6 +35,7 @@
 import org.hisp.dhis.common.Grid;
 import org.hisp.dhis.i18n.I18n;
 import org.hisp.dhis.i18n.I18nFormat;
+import org.hisp.dhis.message.MessageConversation;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.patient.Patient;
 import org.hisp.dhis.sms.outbound.OutboundSms;
@@ -104,4 +105,7 @@
 
     Collection<OutboundSms> sendMessages( ProgramInstance programInstance, int status, I18nFormat format );
 
+    Collection<MessageConversation> sendMessageConversations( ProgramInstance programInstance,
+        int sendWhenToC0mpletedEvent, I18nFormat format );
+
 }

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramStageInstance.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramStageInstance.java	2013-09-06 08:24:36 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramStageInstance.java	2013-09-10 04:51:49 +0000
@@ -29,6 +29,7 @@
  */
 
 import org.hisp.dhis.common.BaseIdentifiableObject;
+import org.hisp.dhis.message.MessageConversation;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.patient.Patient;
 import org.hisp.dhis.patientcomment.PatientComment;
@@ -74,6 +75,8 @@
 
     private List<OutboundSms> outboundSms;
 
+    private List<MessageConversation> messageConversations;
+
     private PatientComment patientComment;
 
     private Integer status;
@@ -306,6 +309,16 @@
         this.patients = patients;
     }
 
+    public List<MessageConversation> getMessageConversations()
+    {
+        return messageConversations;
+    }
+
+    public void setMessageConversations( List<MessageConversation> messageConversations )
+    {
+        this.messageConversations = messageConversations;
+    }
+
     public Integer getEventStatus()
     {
         if ( this.status != null )

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramStageInstanceService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramStageInstanceService.java	2013-09-02 10:37:36 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramStageInstanceService.java	2013-09-10 04:51:49 +0000
@@ -36,6 +36,7 @@
 import org.hisp.dhis.common.Grid;
 import org.hisp.dhis.i18n.I18n;
 import org.hisp.dhis.i18n.I18nFormat;
+import org.hisp.dhis.message.MessageConversation;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.patient.Patient;
 import org.hisp.dhis.patientreport.TabularReportColumn;
@@ -64,9 +65,11 @@
 
     Collection<ProgramStageInstance> getProgramStageInstances( ProgramStage programStage );
 
-    Collection<ProgramStageInstance> getProgramStageInstances( ProgramStage programStage, OrganisationUnit organisationUnit);
+    Collection<ProgramStageInstance> getProgramStageInstances( ProgramStage programStage,
+        OrganisationUnit organisationUnit );
 
-    Collection<ProgramStageInstance> getProgramStageInstances( ProgramStage programStage, OrganisationUnit organisationUnit, Date start, Date end);
+    Collection<ProgramStageInstance> getProgramStageInstances( ProgramStage programStage,
+        OrganisationUnit organisationUnit, Date start, Date end );
 
     Collection<ProgramStageInstance> getProgramStageInstances( Collection<ProgramInstance> programInstances );
 
@@ -145,4 +148,7 @@
 
     Collection<ProgramStageInstance> getProgramStageInstance( Patient patient );
 
+    Collection<MessageConversation> sendMessageConversations( ProgramStageInstance programStageInstance, int status,
+        I18nFormat format );
+
 }

=== modified file 'dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patient/DefaultPatientReminderService.java'
--- dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patient/DefaultPatientReminderService.java	2013-09-04 10:01:16 +0000
+++ dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patient/DefaultPatientReminderService.java	2013-09-10 04:51:49 +0000
@@ -163,4 +163,31 @@
         }
         return phoneNumbers;
     }
+
+    public Set<User> getUsers( PatientReminder patientReminder, Patient patient )
+    {
+        Set<User> users = new HashSet<User>();
+
+        switch ( patientReminder.getSendTo() )
+        {
+        case PatientReminder.SEND_TO_ALL_USERS_IN_ORGUGNIT_REGISTERED:
+            users.addAll( patient.getOrganisationUnit().getUsers() );
+            break;
+        case PatientReminder.SEND_TO_HEALTH_WORKER:
+            if ( patient.getHealthWorker() != null && patient.getHealthWorker().getPhoneNumber() != null )
+            {
+                users.add( patient.getHealthWorker() );
+            }
+            break;
+        case PatientReminder.SEND_TO_USER_GROUP:
+            if ( patientReminder.getUserGroup().getMembers().size() > 0 )
+            {
+                users.addAll( patientReminder.getUserGroup().getMembers() );
+            }
+            break;
+        default:
+            break;
+        }
+        return users;
+    }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patient/startup/TableAlteror.java'
--- dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patient/startup/TableAlteror.java	2013-09-08 09:55:20 +0000
+++ dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patient/startup/TableAlteror.java	2013-09-10 04:51:49 +0000
@@ -233,6 +233,8 @@
         executeSql( "ALTER TABLE program DROP COLUMN remindCompleted" );
         executeSql( "ALTER TABLE program DROP COLUMN displayProvidedOtherFacility" );
         executeSql( "UPDATE programstage_dataelements SET displayAsRadioButton=false WHERE displayAsRadioButton is null" );
+        executeSql( "UPDATE patientreminder SET messageType=1 WHERE messageType is null" );
+        
     }
 
     // -------------------------------------------------------------------------

=== 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-09 14:47:21 +0000
+++ dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/program/DefaultProgramInstanceService.java	2013-09-10 04:51:49 +0000
@@ -41,6 +41,8 @@
 import org.hisp.dhis.dataelement.DataElement;
 import org.hisp.dhis.i18n.I18n;
 import org.hisp.dhis.i18n.I18nFormat;
+import org.hisp.dhis.message.MessageConversation;
+import org.hisp.dhis.message.MessageService;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.patient.Patient;
 import org.hisp.dhis.patient.PatientAttribute;
@@ -121,6 +123,13 @@
         this.patientReminderService = patientReminderService;
     }
 
+    private MessageService messageService;
+
+    public void setMessageService( MessageService messageService )
+    {
+        this.messageService = messageService;
+    }
+
     // -------------------------------------------------------------------------
     // Implementation methods
     // -------------------------------------------------------------------------
@@ -477,6 +486,18 @@
                 + format.formatDateTime( messasge.getDate() ) );
             grid.addValue( messasge.getMessage() );
         }
+        
+        // Get message conversations of the program-instance
+
+        List<MessageConversation> conversations = programInstance.getMessageConversations();
+
+        for ( MessageConversation conversation : conversations )
+        {
+            grid.addRow();
+            grid.addValue( i18n.getString( "message" ) + " " + i18n.getString( "on" ) + " "
+                + format.formatDateTime( conversation.getLastUpdated()) );
+            grid.addValue( conversation.getMessages().get( 0 ) );
+        }
 
         // Program-instance attributes
 
@@ -528,7 +549,10 @@
         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 )
@@ -541,6 +565,30 @@
         return outboundSmsList;
     }
 
+    @Override
+    public Collection<MessageConversation> sendMessageConversations( ProgramInstance programInstance, int status,
+        I18nFormat format )
+    {
+        Collection<MessageConversation> messageConversations = new HashSet<MessageConversation>();
+
+        Collection<PatientReminder> reminders = programInstance.getProgram().getPatientReminders();
+        for ( PatientReminder rm : reminders )
+        {
+            if ( rm != null
+                && rm.getWhenToSend() != null
+                && rm.getWhenToSend() == status
+                && (rm.getMessageType() == PatientReminder.MESSAGE_TYPE_DHIS_MESSAGE || rm.getMessageType() == PatientReminder.MESSAGE_TYPE_BOTH) )
+            {
+                int id = messageService.sendMessage( programInstance.getProgram().getDisplayName(),
+                    patientReminderService.getMessageFromTemplate( rm, programInstance, format ), null,
+                    patientReminderService.getUsers( rm, programInstance.getPatient() ), null, false, true );
+                messageConversations.add( messageService.getMessageConversation( id ) );
+            }
+        }
+
+        return messageConversations;
+    }
+
     // -------------------------------------------------------------------------
     // Supportive methods
     // -------------------------------------------------------------------------
@@ -586,7 +634,7 @@
                     + format.formatDateTime( comment.getCreatedDate() ) );
                 grid.addValue( comment.getCommentText() );
             }
-            
+
             // SMS messages
 
             List<OutboundSms> messasges = programStageInstance.getOutboundSms();

=== modified file 'dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/program/DefaultProgramStageInstanceService.java'
--- dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/program/DefaultProgramStageInstanceService.java	2013-09-02 10:37:36 +0000
+++ dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/program/DefaultProgramStageInstanceService.java	2013-09-10 04:51:49 +0000
@@ -33,6 +33,8 @@
 import org.hisp.dhis.dataelement.DataElement;
 import org.hisp.dhis.i18n.I18n;
 import org.hisp.dhis.i18n.I18nFormat;
+import org.hisp.dhis.message.MessageConversation;
+import org.hisp.dhis.message.MessageService;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.organisationunit.OrganisationUnitLevel;
 import org.hisp.dhis.organisationunit.OrganisationUnitService;
@@ -120,6 +122,13 @@
         this.patientReminderService = patientReminderService;
     }
 
+    private MessageService messageService;
+
+    public void setMessageService( MessageService messageService )
+    {
+        this.messageService = messageService;
+    }
+
     // -------------------------------------------------------------------------
     // Implementation methods
     // -------------------------------------------------------------------------
@@ -161,13 +170,15 @@
     }
 
     @Override
-    public Collection<ProgramStageInstance> getProgramStageInstances( ProgramStage programStage, OrganisationUnit organisationUnit )
+    public Collection<ProgramStageInstance> getProgramStageInstances( ProgramStage programStage,
+        OrganisationUnit organisationUnit )
     {
         return programStageInstanceStore.get( programStage, organisationUnit );
     }
 
     @Override
-    public Collection<ProgramStageInstance> getProgramStageInstances( ProgramStage programStage, OrganisationUnit organisationUnit, Date start, Date end )
+    public Collection<ProgramStageInstance> getProgramStageInstances( ProgramStage programStage,
+        OrganisationUnit organisationUnit, Date start, Date end )
     {
         return programStageInstanceStore.get( programStage, organisationUnit, start, end, 0, Integer.MAX_VALUE );
     }
@@ -582,7 +593,10 @@
         Collection<PatientReminder> reminders = programStageInstance.getProgramStage().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 = sendEventMessage( rm, programStageInstance, patient, format );
                 if ( outboundSms != null )
@@ -595,6 +609,31 @@
         return outboundSmsList;
     }
 
+    @Override
+    public Collection<MessageConversation> sendMessageConversations( ProgramStageInstance programStageInstance,
+        int status, I18nFormat format )
+    {
+        Collection<MessageConversation> messageConversations = new HashSet<MessageConversation>();
+
+        Collection<PatientReminder> reminders = programStageInstance.getProgramStage().getPatientReminders();
+        for ( PatientReminder rm : reminders )
+        {
+            if ( rm != null
+                && rm.getWhenToSend() != null
+                && rm.getWhenToSend() == status
+                && (rm.getMessageType() == PatientReminder.MESSAGE_TYPE_DHIS_MESSAGE || rm.getMessageType() == PatientReminder.MESSAGE_TYPE_BOTH) )
+            {
+                int id = messageService.sendMessage( programStageInstance.getProgramStage().getDisplayName(),
+                    patientReminderService.getMessageFromTemplate( rm, programStageInstance, format ), null,
+                    patientReminderService.getUsers( rm, programStageInstance.getProgramInstance().getPatient() ),
+                    null, false, true );
+                messageConversations.add( messageService.getMessageConversation( id ) );
+            }
+        }
+
+        return messageConversations;
+    }
+
     public Collection<ProgramStageInstance> getProgramStageInstance( Patient patient )
     {
         return programStageInstanceStore.get( patient );

=== modified file 'dhis-2/dhis-services/dhis-service-patient/src/main/resources/META-INF/dhis/beans.xml'
--- dhis-2/dhis-services/dhis-service-patient/src/main/resources/META-INF/dhis/beans.xml	2013-08-22 12:09:00 +0000
+++ dhis-2/dhis-services/dhis-service-patient/src/main/resources/META-INF/dhis/beans.xml	2013-09-10 04:51:49 +0000
@@ -194,6 +194,7 @@
 		<property name="smsSender" ref="org.hisp.dhis.sms.SmsSender" />
 		<property name="currentUserService" ref="org.hisp.dhis.user.CurrentUserService" />
 		<property name="patientReminderService" ref="org.hisp.dhis.patient.PatientReminderService" />
+		<property name="messageService" ref="org.hisp.dhis.message.MessageService" />
 	</bean>
 
 	<bean id="org.hisp.dhis.program.ProgramInstanceService" class="org.hisp.dhis.program.DefaultProgramInstanceService">
@@ -206,6 +207,7 @@
 		<property name="smsSender" ref="org.hisp.dhis.sms.SmsSender" />
 		<property name="currentUserService" ref="org.hisp.dhis.user.CurrentUserService" />
 		<property name="patientReminderService" ref="org.hisp.dhis.patient.PatientReminderService" />
+		<property name="messageService" ref="org.hisp.dhis.message.MessageService" />
 	</bean>
 
 	<bean id="org.hisp.dhis.program.ProgramStageService" class="org.hisp.dhis.program.DefaultProgramStageService">
@@ -416,7 +418,8 @@
 		class="org.hisp.dhis.patient.scheduling.SendScheduledMessageTask">
 		<property name="programStageInstanceService"
 			ref="org.hisp.dhis.program.ProgramStageInstanceService" />
-		<property name="outboundSmsService" ref="org.hisp.dhis.sms.outbound.OutboundSmsService" />
+		<property name="outboundSmsService"
+			ref="org.hisp.dhis.sms.outbound.OutboundSmsService" />
 		<property name="smsSender" ref="org.hisp.dhis.sms.SmsSender" />
 		<property name="programInstanceService" ref="org.hisp.dhis.program.ProgramInstanceService" />
 		<property name="notifier" ref="notifier" />
@@ -500,11 +503,13 @@
 			<list>
 				<list>
 					<ref local="org.hisp.dhis.patient.PatientDeletionHandler" />
-					<ref local="org.hisp.dhis.patientattributevalue.PatientAttributeValueDeletionHandler" />
+					<ref
+						local="org.hisp.dhis.patientattributevalue.PatientAttributeValueDeletionHandler" />
 					<ref local="org.hisp.dhis.patient.PatientIdentifierDeletionHandler" />
 					<ref local="org.hisp.dhis.relationship.RelationshipDeletionHandler" />
 					<ref local="org.hisp.dhis.patient.PatientAuditDeletionHandler" />
-					<ref local="org.hisp.dhis.patientdatavalue.PatientDataValueDeletionHandler" />
+					<ref
+						local="org.hisp.dhis.patientdatavalue.PatientDataValueDeletionHandler" />
 					<ref local="org.hisp.dhis.program.ProgramDeletionHandler" />
 					<ref local="org.hisp.dhis.program.ProgramStageSectionDeletionHandler" />
 					<ref local="org.hisp.dhis.program.ProgramInstanceDeletionHandler" />

=== modified file 'dhis-2/dhis-services/dhis-service-patient/src/main/resources/org/hisp/dhis/patient/hibernate/PatientReminder.hbm.xml'
--- dhis-2/dhis-services/dhis-service-patient/src/main/resources/org/hisp/dhis/patient/hibernate/PatientReminder.hbm.xml	2013-07-19 06:04:09 +0000
+++ dhis-2/dhis-services/dhis-service-patient/src/main/resources/org/hisp/dhis/patient/hibernate/PatientReminder.hbm.xml	2013-09-10 04:51:49 +0000
@@ -22,6 +22,8 @@
  	
  	<property name="whenToSend" />
  	
+ 	<property name="messageType" />
+ 	
  	<many-to-one name="userGroup" class="org.hisp.dhis.user.UserGroup" column="usergroupid" foreign-key="fk_patientreminder_usergroup"/>
   </class>
 </hibernate-mapping>

=== modified file 'dhis-2/dhis-services/dhis-service-patient/src/main/resources/org/hisp/dhis/program/hibernate/ProgramInstance.hbm.xml'
--- dhis-2/dhis-services/dhis-service-patient/src/main/resources/org/hisp/dhis/program/hibernate/ProgramInstance.hbm.xml	2013-08-29 11:59:39 +0000
+++ dhis-2/dhis-services/dhis-service-patient/src/main/resources/org/hisp/dhis/program/hibernate/ProgramInstance.hbm.xml	2013-09-10 04:51:49 +0000
@@ -10,11 +10,11 @@
 			<generator class="native" />
 		</id>
 
-    <property name="uid" column="uid" unique="true" length="11" />
-
-    <property name="created" type="timestamp"/>
-
-    <property name="lastUpdated" type="timestamp"/>
+		<property name="uid" column="uid" unique="true" length="11" />
+
+		<property name="created" type="timestamp" />
+
+		<property name="lastUpdated" type="timestamp" />
 
 		<property name="dateOfIncident" column="dateofincident"
 			not-null="true" />
@@ -43,8 +43,13 @@
 			<many-to-many class="org.hisp.dhis.sms.outbound.OutboundSms"
 				column="outboundsmsid" />
 		</list>
-
-		<property name="followup" />
+		
+		<list name="messageConversations" table="programinstance_messageconversation">
+			<key column="programinstanceid" />
+			<list-index column="sort_order" base="1" />
+			<many-to-many class="org.hisp.dhis.message.MessageConversation"
+				column="messageconversationid" />
+		</list>
 
 		<many-to-one name="patientComment" cascade="all"
 			class="org.hisp.dhis.patientcomment.PatientComment" column="patientcommentid"

=== modified file 'dhis-2/dhis-services/dhis-service-patient/src/main/resources/org/hisp/dhis/program/hibernate/ProgramStageInstance.hbm.xml'
--- dhis-2/dhis-services/dhis-service-patient/src/main/resources/org/hisp/dhis/program/hibernate/ProgramStageInstance.hbm.xml	2013-09-06 07:54:55 +0000
+++ dhis-2/dhis-services/dhis-service-patient/src/main/resources/org/hisp/dhis/program/hibernate/ProgramStageInstance.hbm.xml	2013-09-10 04:51:49 +0000
@@ -40,11 +40,14 @@
 			<many-to-many class="org.hisp.dhis.sms.outbound.OutboundSms"
 				column="outboundsmsid" />
 		</list>
-
-		<many-to-one name="patientComment" cascade="all"
-			class="org.hisp.dhis.patientcomment.PatientComment" column="patientcommentid"
-			foreign-key="fk_programstageinstance_patientcommentid" />
-
+		
+		<list name="messageConversations" table="programstageinstance_messageconversation">
+			<key column="programstageinstanceid" />
+			<list-index column="sort_order" base="1" />
+			<many-to-many class="org.hisp.dhis.message.MessageConversation"
+				column="messageconversationid" />
+		</list>
+		
 		<property name="status" column="status" />
 
 		<property name="coordinates" type="text" />

=== 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-09-04 03:23:44 +0000
+++ dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/caseentry/CompleteDataEntryAction.java	2013-09-10 04:51:49 +0000
@@ -35,6 +35,7 @@
 import java.util.Set;
 
 import org.hisp.dhis.i18n.I18nFormat;
+import org.hisp.dhis.message.MessageConversation;
 import org.hisp.dhis.patient.Patient;
 import org.hisp.dhis.patient.PatientReminder;
 import org.hisp.dhis.patient.PatientService;
@@ -146,7 +147,9 @@
         programStageInstance.setCompletedDate( date );
         programStageInstance.setCompletedUser( currentUserService.getCurrentUsername() );
 
-        // Send message when to completed the event
+        // ---------------------------------------------------------------------
+        // Send sms-message when to completed the event
+        // ---------------------------------------------------------------------
 
         List<OutboundSms> psiOutboundSms = programStageInstance.getOutboundSms();
         if ( psiOutboundSms == null )
@@ -157,8 +160,21 @@
         psiOutboundSms.addAll( programStageInstanceService.sendMessages( programStageInstance,
             PatientReminder.SEND_WHEN_TO_C0MPLETED_EVENT, format ) );
 
+        // ---------------------------------------------------------------------
+        // Send DHIS message when to completed the event
+        // ---------------------------------------------------------------------
+
+        List<MessageConversation> psiMessageConversations = programStageInstance.getMessageConversations();
+        if ( psiMessageConversations == null )
+        {
+            psiMessageConversations = new ArrayList<MessageConversation>();
+        }
+
+        psiMessageConversations.addAll( programStageInstanceService.sendMessageConversations( programStageInstance,
+            PatientReminder.SEND_WHEN_TO_C0MPLETED_EVENT, format ) );
+
         programStageInstanceService.updateProgramStageInstance( programStageInstance );
-        
+
         // ---------------------------------------------------------------------
         // Check Completed status for all of ProgramStageInstance of
         // ProgramInstance
@@ -181,6 +197,11 @@
 
             programInstance.setStatus( ProgramInstance.STATUS_COMPLETED );
             programInstance.setEndDate( new Date() );
+
+            // ---------------------------------------------------------------------
+            // Send sms-message when to completed the program
+            // ---------------------------------------------------------------------
+
             List<OutboundSms> piOutboundSms = programInstance.getOutboundSms();
             if ( piOutboundSms == null )
             {
@@ -190,6 +211,19 @@
             piOutboundSms.addAll( programInstanceService.sendMessages( programInstance,
                 PatientReminder.SEND_WHEN_TO_C0MPLETED_PROGRAM, format ) );
 
+            // ---------------------------------------------------------------------
+            // Send DHIS message when to completed the program
+            // ---------------------------------------------------------------------
+
+            List<MessageConversation> piMessageConversations = programInstance.getMessageConversations();
+            if ( piMessageConversations == null )
+            {
+                piMessageConversations = new ArrayList<MessageConversation>();
+            }
+
+            piMessageConversations.addAll( programInstanceService.sendMessageConversations( programInstance,
+                PatientReminder.SEND_WHEN_TO_C0MPLETED_PROGRAM, format ) );
+
             programInstanceService.updateProgramInstance( programInstance );
 
             Program program = programInstance.getProgram();

=== 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-09-02 21:20:47 +0000
+++ dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/patient/SaveProgramEnrollmentAction.java	2013-09-10 04:51:49 +0000
@@ -32,8 +32,10 @@
 import java.util.Collection;
 import java.util.Date;
 import java.util.List;
+
 import org.hisp.dhis.caseentry.state.SelectedStateManager;
 import org.hisp.dhis.i18n.I18nFormat;
+import org.hisp.dhis.message.MessageConversation;
 import org.hisp.dhis.patient.Patient;
 import org.hisp.dhis.patient.PatientReminder;
 import org.hisp.dhis.patient.PatientService;
@@ -225,7 +227,7 @@
 
                     Date dueDate = DateUtils
                         .getDateAfterAddition( dateCreatedEvent, programStage.getMinDaysFromStart() );
-                    
+
                     if ( !program.getIgnoreOverdueEvents()
                         || !(program.getIgnoreOverdueEvents() && dueDate.before( currentDate )) )
                     {
@@ -251,7 +253,10 @@
                 }
             }
 
+            // -----------------------------------------------------------------
             // send messages after enrollment program
+            // -----------------------------------------------------------------
+
             List<OutboundSms> outboundSms = programInstance.getOutboundSms();
             if ( outboundSms == null )
             {
@@ -260,6 +265,20 @@
 
             outboundSms.addAll( programInstanceService.sendMessages( programInstance,
                 PatientReminder.SEND_WHEN_TO_EMROLLEMENT, format ) );
+
+            // -----------------------------------------------------------------
+            // Send DHIS message when to completed the program
+            // -----------------------------------------------------------------
+
+            List<MessageConversation> piMessageConversations = programInstance.getMessageConversations();
+            if ( piMessageConversations == null )
+            {
+                piMessageConversations = new ArrayList<MessageConversation>();
+            }
+
+            piMessageConversations.addAll( programInstanceService.sendMessageConversations( programInstance,
+                PatientReminder.SEND_WHEN_TO_EMROLLEMENT, format ) );
+
             programInstanceService.updateProgramInstance( programInstance );
         }
         else
@@ -272,7 +291,7 @@
             for ( ProgramStageInstance programStageInstance : programInstance.getProgramStageInstances() )
             {
                 if ( !programStageInstance.isCompleted()
-                    || programStageInstance.getStatus() != ProgramStageInstance.SKIPPED_STATUS )
+                    || (programStageInstance.getStatus() != null && programStageInstance.getStatus() != ProgramStageInstance.SKIPPED_STATUS) )
                 {
                     Date dueDate = DateUtils.getDateAfterAddition( format.parseDate( dateOfIncident ),
                         programStageInstance.getProgramStage().getMinDaysFromStart() );

=== modified file 'dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/patient/SetProgramInstanceStatusAction.java'
--- dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/patient/SetProgramInstanceStatusAction.java	2013-08-23 16:05:01 +0000
+++ dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/patient/SetProgramInstanceStatusAction.java	2013-09-10 04:51:49 +0000
@@ -32,11 +32,10 @@
 import java.util.Calendar;
 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.message.MessageConversation;
 import org.hisp.dhis.patient.Patient;
 import org.hisp.dhis.patient.PatientReminder;
 import org.hisp.dhis.patient.PatientService;
@@ -46,12 +45,7 @@
 import org.hisp.dhis.program.ProgramInstanceService;
 import org.hisp.dhis.program.ProgramStageInstance;
 import org.hisp.dhis.program.ProgramStageInstanceService;
-import org.hisp.dhis.sms.SmsSender;
-import org.hisp.dhis.sms.SmsServiceException;
 import org.hisp.dhis.sms.outbound.OutboundSms;
-import org.hisp.dhis.system.util.DateUtils;
-import org.hisp.dhis.user.CurrentUserService;
-import org.hisp.dhis.user.User;
 
 import com.opensymphony.xwork2.Action;
 
@@ -87,20 +81,6 @@
         this.programStageInstanceService = programStageInstanceService;
     }
 
-    private CurrentUserService currentUserService;
-
-    public void setCurrentUserService( CurrentUserService currentUserService )
-    {
-        this.currentUserService = currentUserService;
-    }
-
-    private SmsSender smsSender;
-    
-    public void setSmsSender( SmsSender smsSender )
-    {
-        this.smsSender = smsSender;
-    }
-
     private I18nFormat format;
 
     public void setFormat( I18nFormat format )
@@ -153,7 +133,31 @@
 
         if ( status == ProgramInstance.STATUS_COMPLETED )
         {
-            sendSMSToCompletedProgram( programInstance );
+            // ---------------------------------------------------------------------
+            // Send sms-message when to completed the program
+            // ---------------------------------------------------------------------
+
+            List<OutboundSms> piOutboundSms = programInstance.getOutboundSms();
+            if ( piOutboundSms == null )
+            {
+                piOutboundSms = new ArrayList<OutboundSms>();
+            }
+
+            piOutboundSms.addAll( programInstanceService.sendMessages( programInstance,
+                PatientReminder.SEND_WHEN_TO_C0MPLETED_PROGRAM, format ) );
+
+            // -----------------------------------------------------------------
+            // Send DHIS message when to completed the program
+            // -----------------------------------------------------------------
+
+            List<MessageConversation> piMessageConversations = programInstance.getMessageConversations();
+            if ( piMessageConversations == null )
+            {
+                piMessageConversations = new ArrayList<MessageConversation>();
+            }
+
+            piMessageConversations.addAll( programInstanceService.sendMessageConversations( programInstance,
+                PatientReminder.SEND_WHEN_TO_C0MPLETED_PROGRAM, format ) );
 
             programInstance.setEndDate( new Date() );
             if ( !program.getOnlyEnrollOnce() )
@@ -204,115 +208,4 @@
         return SUCCESS;
     }
 
-    private void sendSMSToCompletedProgram( ProgramInstance programInstance )
-    {
-        Patient patient = programInstance.getPatient();
-
-        if ( patient != null )
-        {
-            Collection<PatientReminder> reminders = programInstance.getProgram().getPatientReminders();
-            for ( PatientReminder rm : reminders )
-            {
-                if ( rm.getWhenToSend() == PatientReminder.SEND_WHEN_TO_C0MPLETED_PROGRAM )
-                {
-                    sendProgramMessage( rm, programInstance, patient );
-                }
-            }
-        }
-    }
-
-    private void sendProgramMessage( PatientReminder reminder, ProgramInstance programInstance, 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() )
-            {
-                String[] _phoneNumbers = patient.getPhoneNumber().split( ";" );
-                for ( String phoneNumber : _phoneNumbers )
-                {
-                    phoneNumbers.add( phoneNumber );
-                }
-            }
-            break;
-        }
-
-        if ( phoneNumbers.size() > 0 )
-        {
-            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 );
-
-            try
-            {
-                OutboundSms outboundSms = new OutboundSms();
-                outboundSms.setMessage( msg );
-                outboundSms.setRecipients( phoneNumbers );
-                outboundSms.setSender( currentUserService.getCurrentUsername() );
-                smsSender.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();
-            }
-        }
-    }
-
 }

=== modified file 'dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/patient/UpdatePatientAction.java'
--- dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/patient/UpdatePatientAction.java	2013-08-23 16:05:01 +0000
+++ dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/patient/UpdatePatientAction.java	2013-09-10 04:51:49 +0000
@@ -212,6 +212,10 @@
         {
             patient.setHealthWorker( userService.getUser( healthWorker ) );
         }
+        else
+        {
+            patient.setHealthWorker( null );
+        }
 
         if ( birthDate != null || age != null )
         {
@@ -379,9 +383,9 @@
         this.userService = userService;
     }
 
-    public void setHealthWorkerId( Integer healthWorkerId )
+    public void setHealthWorker( Integer healthWorker )
     {
-        this.healthWorker = healthWorkerId;
+        this.healthWorker = healthWorker;
     }
 
     public void setPatientIdentifierTypeService( PatientIdentifierTypeService patientIdentifierTypeService )

=== 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	2013-08-23 16:05:01 +0000
+++ dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/reminder/SetEventStatusAction.java	2013-09-10 04:51:49 +0000
@@ -32,6 +32,7 @@
 import java.util.List;
 
 import org.hisp.dhis.i18n.I18nFormat;
+import org.hisp.dhis.message.MessageConversation;
 import org.hisp.dhis.patient.PatientReminder;
 import org.hisp.dhis.program.ProgramStageInstance;
 import org.hisp.dhis.program.ProgramStageInstanceService;
@@ -100,7 +101,10 @@
             programStageInstance.setCompleted( true );
             programStageInstance.setStatus( null );
 
-            // Send message when to completed the event
+            // -----------------------------------------------------------------
+            // Send sms when to completed program
+            // -----------------------------------------------------------------
+
             List<OutboundSms> psiOutboundSms = programStageInstance.getOutboundSms();
             if ( psiOutboundSms == null )
             {
@@ -108,6 +112,20 @@
             }
             psiOutboundSms.addAll( programStageInstanceService.sendMessages( programStageInstance,
                 PatientReminder.SEND_WHEN_TO_C0MPLETED_EVENT, format ) );
+
+            // -----------------------------------------------------------------
+            // Send DHIS message when to completed the event
+            // -----------------------------------------------------------------
+
+            List<MessageConversation> psiMessageConversations = programStageInstance.getMessageConversations();
+            if ( psiMessageConversations == null )
+            {
+                psiMessageConversations = new ArrayList<MessageConversation>();
+            }
+
+            psiMessageConversations.addAll( programStageInstanceService.sendMessageConversations( programStageInstance,
+                PatientReminder.SEND_WHEN_TO_C0MPLETED_EVENT, format ) );
+
             break;
         case ProgramStageInstance.VISITED_STATUS:
             programStageInstance.setCompleted( false );

=== 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-09-07 21:25:35 +0000
+++ dhis-2/dhis-web/dhis-web-caseentry/src/main/resources/META-INF/dhis/beans.xml	2013-09-10 04:51:49 +0000
@@ -705,8 +705,6 @@
 		<property name="programInstanceService" ref="org.hisp.dhis.program.ProgramInstanceService" />
 		<property name="programStageInstanceService"
 			ref="org.hisp.dhis.program.ProgramStageInstanceService" />
-		<property name="currentUserService" ref="org.hisp.dhis.user.CurrentUserService" />
-		<property name="smsSender" ref="org.hisp.dhis.sms.SmsSender" />
 	</bean>
 
 	<bean id="org.hisp.dhis.caseentry.action.patient.GetProgramAction"

=== modified file 'dhis-2/dhis-web/dhis-web-caseentry/src/main/resources/org/hisp/dhis/caseentry/i18n_module.properties'
--- dhis-2/dhis-web/dhis-web-caseentry/src/main/resources/org/hisp/dhis/caseentry/i18n_module.properties	2013-09-07 21:25:35 +0000
+++ dhis-2/dhis-web/dhis-web-caseentry/src/main/resources/org/hisp/dhis/caseentry/i18n_module.properties	2013-09-10 04:51:49 +0000
@@ -681,4 +681,5 @@
 non_value = No value
 update_comment = Update comment
 update_success = Update successfully
-send_message = Send message
\ No newline at end of file
+send_message = Send message
+sms = SMS
\ No newline at end of file

=== modified file 'dhis-2/dhis-web/dhis-web-caseentry/src/main/webapp/dhis-web-caseentry/eventMessage.vm'
--- dhis-2/dhis-web/dhis-web-caseentry/src/main/webapp/dhis-web-caseentry/eventMessage.vm	2013-09-05 19:20:05 +0000
+++ dhis-2/dhis-web/dhis-web-caseentry/src/main/webapp/dhis-web-caseentry/eventMessage.vm	2013-09-10 04:51:49 +0000
@@ -37,7 +37,7 @@
 				<td>$format.formatDateTime($!sms.date)</td>
 				<td>$i18n.getString('all')</td>
 				<td>$sms.sender</td>
-				<td>$i18n.getString('message')</td>
+				<td>$i18n.getString('sms')</td>
 				<td #if( $sms.status=='ERROR') style="color:red" #end >$sms.status</td>
 				<td>$sms.message</td>
 				<td>
@@ -45,6 +45,20 @@
 				</td>
 			</tr>
 			#set( $mark = !$mark )
+		#end 
+		#foreach( $messageConversation in $programInstance.messageConversations )
+			<tr id="tr${sms.id}" #alternate( $mark )>
+				<td>$format.formatDateTime($!messageConversation.lastMessage)</td>
+				<td>$i18n.getString('all')</td>
+				<td>$messageConversation.lastSender.username</td>
+				<td>$i18n.getString('message')</td>
+				<td></td>
+				<td>$messageConversation.messages.get(0)</td>
+				<td>
+					<a href="javascript:removeMessage( $programInstances.id, '', $sms.id )" title="$i18n.getString( 'remove' )"><img src="../images/delete.png" alt="$i18n.getString( 'remove' )"></a>
+				</td>
+			</tr>
+			#set( $mark = !$mark )
 		#end
 		#foreach($programStageInstance in $programStageInstances)
 			#foreach( $comment in $programStageInstance.patientComments )
@@ -78,6 +92,20 @@
 				</tr>
 				#set( $mark = !$mark )
 			#end
+			#foreach( $messageConversation in $programStageInstance.messageConversations )
+			<tr id="tr${sms.id}" #alternate( $mark )>
+				<td>$format.formatDateTime($!messageConversation.lastMessage)</td>
+				<td>$programStageInstance.programStage.displayName</td>
+				<td>$messageConversation.lastSender.username</td>
+				<td>$i18n.getString('message')</td>
+				<td></td>
+				<td>$messageConversation.messages.get(0)</td>
+				<td>
+					<a href="javascript:removeMessage( $programInstances.id, '', $sms.id )" title="$i18n.getString( 'remove' )"><img src="../images/delete.png" alt="$i18n.getString( 'remove' )"></a>
+				</td>
+			</tr>
+			#set( $mark = !$mark )
+		#end
 		#end
 	</tbody>
 </table>

=== modified file 'dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/java/org/hisp/dhis/patient/action/program/AddProgramAction.java'
--- dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/java/org/hisp/dhis/patient/action/program/AddProgramAction.java	2013-09-05 17:59:57 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/java/org/hisp/dhis/patient/action/program/AddProgramAction.java	2013-09-10 04:51:49 +0000
@@ -254,6 +254,13 @@
         this.whenToSend = whenToSend;
     }
 
+    private List<Integer> messageType = new ArrayList<Integer>();
+
+    public void setMessageType( List<Integer> messageType )
+    {
+        this.messageType = messageType;
+    }
+
     private Boolean selectEnrollmentDatesInFuture;
 
     public void setSelectEnrollmentDatesInFuture( Boolean selectEnrollmentDatesInFuture )
@@ -348,7 +355,7 @@
             RelationshipType relationshipType = relationshipTypeService.getRelationshipType( relationshipTypeId );
             program.setRelationshipType( relationshipType );
         }
-        program.setRelationshipFromA( relationshipFromA );;
+        program.setRelationshipFromA( relationshipFromA );
         program.setRelationshipText( relationshipText );
 
         List<PatientIdentifierType> identifierTypes = new ArrayList<PatientIdentifierType>();
@@ -390,6 +397,7 @@
             reminder.setDateToCompare( datesToCompare.get( i ) );
             reminder.setSendTo( sendTo.get( i ) );
             reminder.setWhenToSend( whenToSend.get( i ) );
+            reminder.setMessageType( messageType.get( i ) );
             if ( sendTo.get( i ) == PatientReminder.SEND_TO_USER_GROUP )
             {
                 UserGroup selectedUserGroup = userGroupService.getUserGroup( userGroup.get( i ) );

=== modified file 'dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/java/org/hisp/dhis/patient/action/program/UpdateProgramAction.java'
--- dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/java/org/hisp/dhis/patient/action/program/UpdateProgramAction.java	2013-09-05 17:59:57 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/java/org/hisp/dhis/patient/action/program/UpdateProgramAction.java	2013-09-10 04:51:49 +0000
@@ -257,6 +257,13 @@
         this.whenToSend = whenToSend;
     }
 
+    private List<Integer> messageType = new ArrayList<Integer>();
+
+    public void setMessageType( List<Integer> messageType )
+    {
+        this.messageType = messageType;
+    }
+
     private Boolean useBirthDateAsIncidentDate;
 
     public void setUseBirthDateAsIncidentDate( Boolean useBirthDateAsIncidentDate )
@@ -415,6 +422,7 @@
             reminder.setDateToCompare( datesToCompare.get( i ) );
             reminder.setSendTo( sendTo.get( i ) );
             reminder.setWhenToSend( whenToSend.get( i ) );
+            reminder.setMessageType( messageType.get( i ) );
 
             if ( reminder.getSendTo() == PatientReminder.SEND_TO_USER_GROUP )
             {

=== modified file 'dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/java/org/hisp/dhis/patient/action/programstage/AddProgramStageAction.java'
--- dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/java/org/hisp/dhis/patient/action/programstage/AddProgramStageAction.java	2013-09-06 15:53:07 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/java/org/hisp/dhis/patient/action/programstage/AddProgramStageAction.java	2013-09-10 04:51:49 +0000
@@ -208,6 +208,13 @@
         this.whenToSend = whenToSend;
     }
 
+    private List<Integer> messageType = new ArrayList<Integer>();
+
+    public void setMessageType( List<Integer> messageType )
+    {
+        this.messageType = messageType;
+    }
+
     private Boolean autoGenerateEvent;
 
     public void setAutoGenerateEvent( Boolean autoGenerateEvent )
@@ -329,6 +336,7 @@
             reminder.setDateToCompare( PatientReminder.DUE_DATE_TO_COMPARE );
             reminder.setSendTo( sendTo.get( i ) );
             reminder.setWhenToSend( whenToSend.get( i ) );
+            reminder.setMessageType( messageType.get( i ) );
             if ( sendTo.get( i ) == PatientReminder.SEND_TO_USER_GROUP )
             {
                 UserGroup selectedUserGroup = userGroupService.getUserGroup( userGroup.get( i ) );

=== modified file 'dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/java/org/hisp/dhis/patient/action/programstage/UpdateProgramStageAction.java'
--- dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/java/org/hisp/dhis/patient/action/programstage/UpdateProgramStageAction.java	2013-09-09 13:38:36 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/java/org/hisp/dhis/patient/action/programstage/UpdateProgramStageAction.java	2013-09-10 04:51:49 +0000
@@ -236,6 +236,13 @@
         this.whenToSend = whenToSend;
     }
 
+    private List<Integer> messageType = new ArrayList<Integer>();
+
+    public void setMessageType( List<Integer> messageType )
+    {
+        this.messageType = messageType;
+    }
+
     private List<Integer> userGroup = new ArrayList<Integer>();
 
     public void setUserGroup( List<Integer> userGroup )
@@ -295,8 +302,7 @@
         generatedByEnrollmentDate = (generatedByEnrollmentDate == null) ? false : generatedByEnrollmentDate;
         blockEntryForm = (blockEntryForm == null) ? false : blockEntryForm;
         remindCompleted = (remindCompleted == null) ? false : remindCompleted;
-        captureCoordinates = (captureCoordinates == null) ? false : captureCoordinates;
-        
+
         ProgramStage programStage = programStageService.getProgramStage( id );
 
         programStage.setName( name );
@@ -328,6 +334,7 @@
             reminder.setDateToCompare( PatientReminder.DUE_DATE_TO_COMPARE );
             reminder.setSendTo( sendTo.get( i ) );
             reminder.setWhenToSend( whenToSend.get( i ) );
+            reminder.setMessageType( messageType.get( i ) );
             if ( reminder.getSendTo() == PatientReminder.SEND_TO_USER_GROUP )
             {
                 UserGroup selectedUserGroup = userGroupService.getUserGroup( userGroup.get( i ) );
@@ -363,7 +370,7 @@
                     this.compulsories.get( i ), i );
                 programStageDataElement.setAllowProvidedElsewhere( allowed );
                 programStageDataElement.setDisplayInReports( displayInReport );
-                programStageDataElement.setAllowDateInFuture( allowDate ); 
+                programStageDataElement.setAllowDateInFuture( allowDate );
                 programStageDataElement.setDisplayAsRadioButton( displayRadioButton );
                 programStageDataElementService.addProgramStageDataElement( programStageDataElement );
             }

=== modified file 'dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/resources/org/hisp/dhis/patient/i18n_module.properties'
--- dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/resources/org/hisp/dhis/patient/i18n_module.properties	2013-09-09 18:00:50 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/resources/org/hisp/dhis/patient/i18n_module.properties	2013-09-10 04:51:49 +0000
@@ -466,4 +466,8 @@
 characters_without_params = Characters without params
 scheduled_date  = scheduled date 
 send_message = Send message
-message_is_sent = Message is sent
\ No newline at end of file
+message_is_sent = Message is sent
+recipients = Recipients
+message_type = Message type
+both = Both
+direct_sms = Direct SMS
\ 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/addProgramForm.vm'
--- dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/webapp/dhis-web-maintenance-patient/addProgramForm.vm	2013-09-09 16:11:17 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/webapp/dhis-web-maintenance-patient/addProgramForm.vm	2013-09-10 04:51:49 +0000
@@ -26,6 +26,9 @@
 		
 		var whenToSend = jQuery( "#whenToSend" );
 		whenToSend.empty();
+		
+		var messageType = jQuery( "#messageType" );
+		messageType.empty();
 
 		var userGroup = jQuery( "#userGroup" );
 		userGroup.empty();		
@@ -51,6 +54,10 @@
 			whenToSend.append( "<option value='" + item.value + "' selected='true'>" + item.value + "</option>" );
 		});
 		
+		jQuery(".messageType").each( function( i, item ){ 
+			messageType.append( "<option value='" + item.value + "' selected='true'>" + item.value + "</option>" );
+		});
+		
 		jQuery(".userGroup").each( function( i, item ){ 
 			userGroup.append( "<option value='" + item.value + "' selected='true'>" + item.value + "</option>" );
 		});
@@ -85,6 +92,7 @@
 	<select id='datesToCompare' name="datesToCompare" multiple="multiple" class="hidden"></select>
 	<select id='sendTo' name="sendTo" multiple="multiple" class="hidden"></select>
 	<select id='whenToSend' name="whenToSend" multiple="multiple" class="hidden"></select>
+	<select id='messageType' name="messageType" multiple="multiple" class="hidden"></select>
 	<select id='userGroup' name="userGroup" multiple="multiple" class="hidden"></select>
 	
 <table>	 
@@ -339,4 +347,8 @@
 	var i18n_before = '$encoder.jsEscape( $i18n.getString( "before" ) , "'")';
 	var i18n_after = '$encoder.jsEscape( $i18n.getString( "after" ) , "'")';
 	var i18n_scheduled_date = '$encoder.jsEscape( $i18n.getString( "scheduled_date" ) , "'")';
+	var i18n_message_type = '$encoder.jsEscape( $i18n.getString( "message_type" ) , "'")';
+	var i18n_direct_sms = '$encoder.jsEscape( $i18n.getString( "direct_sms" ) , "'")';
+	var i18n_message = '$encoder.jsEscape( $i18n.getString( "message" ) , "'")';
+	var i18n_both = '$encoder.jsEscape( $i18n.getString( "both" ) , "'")';
 </script>

=== modified file 'dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/webapp/dhis-web-maintenance-patient/addProgramStageForm.vm'
--- dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/webapp/dhis-web-maintenance-patient/addProgramStageForm.vm	2013-09-09 16:11:17 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/webapp/dhis-web-maintenance-patient/addProgramStageForm.vm	2013-09-10 04:51:49 +0000
@@ -18,6 +18,7 @@
 	<select id='templateMessages' name="templateMessages" multiple="multiple" class="hidden"></select>
 	<select id='sendTo' name="sendTo" multiple="multiple" class="hidden"></select>
 	<select id='whenToSend' name="whenToSend" multiple="multiple" class="hidden"></select>
+	<select id='messageType' name="messageType" multiple="multiple" class="hidden"></select>
 	<select id='userGroup' name="userGroup" multiple="multiple" class="hidden"></select>
 <table>
 	<thead>
@@ -223,6 +224,10 @@
 	var i18n_before = '$encoder.jsEscape( $i18n.getString( "before" ) , "'")';
 	var i18n_after = '$encoder.jsEscape( $i18n.getString( "after" ) , "'")';
 	var i18n_scheduled_date = '$encoder.jsEscape( $i18n.getString( "scheduled_date" ) , "'")';
+	var i18n_message_type = '$encoder.jsEscape( $i18n.getString( "message_type" ) , "'")';
+	var i18n_direct_sms = '$encoder.jsEscape( $i18n.getString( "direct_sms" ) , "'")';
+	var i18n_message = '$encoder.jsEscape( $i18n.getString( "message" ) , "'")';
+	var i18n_both = '$encoder.jsEscape( $i18n.getString( "both" ) , "'")';
 	
 	var programStageList = new Array();
 	#foreach($programStage in $program.programStages)

=== modified file 'dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/webapp/dhis-web-maintenance-patient/javascript/addProgramStageForm.js'
--- dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/webapp/dhis-web-maintenance-patient/javascript/addProgramStageForm.js	2013-09-09 16:11:17 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/webapp/dhis-web-maintenance-patient/javascript/addProgramStageForm.js	2013-09-10 04:51:49 +0000
@@ -37,6 +37,9 @@
 		var userGroup = jQuery( "#userGroup" );
 		userGroup.empty();	
 		
+		var messageType = jQuery( "#messageType" );
+		messageType.empty();
+
 		jQuery("#selectedList").find("tr").each( function( i, item ){ 
 			
 			selectedDataElementsValidator.append( "<option value='" + item.id + "' selected='true'>" + item.id + "</option>" );
@@ -74,6 +77,9 @@
 		jQuery(".whenToSend").each( function( i, item ){ 
 			whenToSend.append( "<option value='" + item.value + "' selected='true'>" + item.value + "</option>" );
 		});
+		jQuery(".messageType").each( function( i, item ){ 
+			messageType.append( "<option value='" + item.value + "' selected='true'>" + item.value + "</option>" );
+		});
 		jQuery(".userGroup").each( function( i, item ){ 
 			userGroup.append( "<option value='" + item.value + "' selected='true'>" + item.value + "</option>" );
 		});
@@ -100,12 +106,3 @@
 		
 	checkValueIsExist( "name", "validateProgramStage.action",{id: getFieldValue('programId')});	
 });
-
-function onchangeUserGroup( id )
-{
-	var value = document.getElementById( 'sendTo'+id ).value;
-	hideById( 'tr'+id );
-	if ( value == 5) {
-		showById( 'tr'+id );
-	}
-};
\ 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/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-09-07 12:09:05 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/webapp/dhis-web-maintenance-patient/javascript/program.js	2013-09-10 04:51:49 +0000
@@ -297,7 +297,7 @@
 				+   '</td>'
 				+ '</tr>'
 				+ '<tr name="tr' + rowId + '">'
-				+ 	'<td><label>' + i18n_days_before_after_comparison_date + '</label></td>'
+				+ 	'<td><label>' + i18n_send_message + '</label></td>'
 				+ 	'<td>'
 				+		'<input type="text" onchange="setRealDays(' + rowId + ')" style="width:100px;" realvalue="" id="daysAllowedSendMessage' + rowId + '" name="daysAllowedSendMessage' + rowId + '" class="daysAllowedSendMessage {validate:{required:true,number:true}}"/> '
 				+ 		i18n_days
@@ -327,6 +327,16 @@
 				+	'</td>'
 				+ '/<tr>'
 				+ '<tr name="tr' + rowId + '">'
+				+ '	<td><label>' + i18n_message_type + '</label></td>'
+				+ '	<td>'
+				+ '		<select type="text" id="messageType' + rowId + '" name="messageType' + rowId + '" class="messageType {validate:{required:true,number:true}}" >'
+				+ '			<option value="1">' + i18n_direct_sms + '</option>'
+				+ '			<option value="2">' + i18n_message + '</option>'
+				+ '			<option value="3">' + i18n_both + '</option>'
+				+ '		</select>'
+				+ '	</td>'
+				+ '</tr>'
+				+ '<tr name="tr' + rowId + '">'
 				+	'<td>' + i18n_params + '</td>'
 				+	'<td>'
 				+		'<select multiple size="4" id="params' + rowId +'" name="params" ondblclick="insertParams(this.value, ' + rowId + ');">'
@@ -342,7 +352,7 @@
 				+ '</tr>'
 				+ '<tr name="tr' + rowId + '">'
 				+	'<td><label>' + i18n_message + '</label></td>'
-				+	'<td><textarea onkeypress="getMessageLength( ' + rowId + ');" id="templateMessage' + rowId + '" name="templateMessage' + rowId + '" style="width:320px" class="templateMessage {validate:{required:true}}"></textarea></td>'
+				+	'<td><textarea onkeyup="getMessageLength( ' + rowId + ');" id="templateMessage' + rowId + '" name="templateMessage' + rowId + '" style="width:320px" class="templateMessage {validate:{required:true}}"></textarea></td>'
 				+ '</tr>'
 				+ '<tr>'
 				+	'<td></td>'
@@ -424,3 +434,20 @@
 	daysAllowedSendMessage.attr("realvalue", time * eval(daysAllowedSendMessage).val());
 	var aasdf= 0;
 }
+
+function onchangeUserGroup( id )
+{
+	var value = document.getElementById( 'sendTo' + id ).value;
+	hideById( 'tr'+id );
+	
+	if( value=="1" || value=="3" ){
+		setFieldValue('messageType' + id , '1');
+		disable('messageType' + id );
+	}
+	else{
+		if ( value == "5") {
+			showById( 'tr' + id );
+		}
+		enable ('messageType' + id );
+	}
+}

=== modified file 'dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/webapp/dhis-web-maintenance-patient/javascript/programStage.js'
--- dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/webapp/dhis-web-maintenance-patient/javascript/programStage.js	2013-09-09 16:11:17 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/webapp/dhis-web-maintenance-patient/javascript/programStage.js	2013-09-10 04:51:49 +0000
@@ -351,6 +351,16 @@
 				+	'</td>'
 				+ '/<tr>'
 				+ '<tr name="tr' + rowId + '">'
+				+ '	<td><label>' + i18n_message_type + '</label></td>'
+				+ '	<td>'
+				+ '		<select type="text" id="messageType' + rowId + '" name="messageType' + rowId + '" class="messageType {validate:{required:true,number:true}}" >'
+				+ '			<option value="1">' + i18n_direct_sms + '</option>'
+				+ '			<option value="2">' + i18n_message + '</option>'
+				+ '			<option value="3">' + i18n_both + '</option>'
+				+ '		</select>'
+				+ '	</td>'
+				+ '</tr>'
+				+ '<tr name="tr' + rowId + '">'
 				+	'<td>' + i18n_params + '</td>'
 				+	'<td>'
 				+		'<select multiple size="4" id="params' + rowId +'" name="params" ondblclick="insertParams(this.value, ' + rowId + ');">'
@@ -407,13 +417,20 @@
 
 function onchangeUserGroup( id )
 {
-	var value = document.getElementById( 'sendTo'+id ).value;
+	var value = document.getElementById( 'sendTo' + id ).value;
 	hideById( 'tr'+id );
-	if ( value == 5) {
-		showById( 'tr'+id );
-	}
-};
-
+	
+	if( value=="1" || value=="3" ){
+		setFieldValue('messageType' + id , '1');
+		disable('messageType' + id );
+	}
+	else{
+		if ( value == "5") {
+			showById( 'tr' + id );
+		}
+		enable ('messageType' + id );
+	}
+}
 
 function getMessageLength(rowId)
 {

=== modified file 'dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/webapp/dhis-web-maintenance-patient/javascript/updateProgramStageForm.js'
--- dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/webapp/dhis-web-maintenance-patient/javascript/updateProgramStageForm.js	2013-09-09 16:11:17 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/webapp/dhis-web-maintenance-patient/javascript/updateProgramStageForm.js	2013-09-10 04:51:49 +0000
@@ -38,6 +38,9 @@
 		var userGroup = jQuery( "#userGroup" );
 		userGroup.empty();
 		
+		var messageType = jQuery( "#messageType" );
+		messageType.empty();
+
 		jQuery("#selectedList").find("tr").each( function( i, item ){ 
 			
 			selectedDataElementsValidator.append( "<option value='" + item.id + "' selected='true'>" + item.id + "</option>" );
@@ -75,6 +78,9 @@
 		jQuery(".whenToSend").each( function( i, item ){ 
 			whenToSend.append( "<option value='" + item.value + "' selected='true'>" + item.value + "</option>" );
 		});
+		jQuery(".messageType").each( function( i, item ){ 
+			messageType.append( "<option value='" + item.value + "' selected='true'>" + item.value + "</option>" );
+		});
 		jQuery(".userGroup").each( function( i, item ){ 
 			userGroup.append( "<option value='" + item.value + "' selected='true'>" + item.value + "</option>" );
 		});

=== 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-09-09 16:11:17 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/webapp/dhis-web-maintenance-patient/updateProgramForm.vm	2013-09-10 04:51:49 +0000
@@ -29,6 +29,9 @@
 				var whenToSend = jQuery( "#whenToSend" );
 				whenToSend.empty();
 				
+				var messageType = jQuery( "#messageType" );
+				messageType.empty();
+				
 				var userGroup = jQuery( "#userGroup" );
 				userGroup.empty();
 				
@@ -53,6 +56,10 @@
 					whenToSend.append( "<option value='" + item.value + "' selected='true'>" + item.value + "</option>" );
 				});
 				
+				jQuery(".messageType").each( function( i, item ){ 
+					messageType.append( "<option value='" + item.value + "' selected='true'>" + item.value + "</option>" );
+				});
+				
 				jQuery(".userGroup").each( function( i, item ){ 
 					userGroup.append( "<option value='" + item.value + "' selected='true'>" + item.value + "</option>" );
 				});
@@ -63,9 +70,7 @@
 				personDisplayNames.empty();
 				if(getFieldValue('type')!=3){
 					jQuery("#selectedList").find("tr").each( function( i, item ){ 
-						
 						selectedPropertyIds.append( "<option value='" + item.id + "' selected='true'>" + item.id + "</option>" );
-						
 						var displayed = jQuery( item ).find( "input[name='displayed']:first");
 						var checked = displayed.attr('checked') ? true : false;
 						personDisplayNames.append( "<option value='" + checked + "' selected='true'>" + checked + "</option>" );
@@ -74,15 +79,7 @@
 			});
 	checkValueIsExist( "name", "validateProgram.action",{id:getFieldValue('id')} );
 });
-	function onchangeUserGroup( id )
-	{
-		var value = document.getElementById( 'sendTo'+id ).value;
-		hideById( 'tr'+id );
-		if ( value == 5) {
-			showById( 'tr'+id );
-		}
-	};
-	
+
 </script>
 
 <h3>$i18n.getString( "edit_program" )</h3>
@@ -93,6 +90,7 @@
   <select id='datesToCompare' name="datesToCompare" multiple="multiple" class="hidden"></select>
   <select id='sendTo' name="sendTo" multiple="multiple" class="hidden"></select>
   <select id='whenToSend' name="whenToSend" multiple="multiple" class="hidden"></select>
+	<select id='messageType' name="messageType" multiple="multiple" class="hidden"></select>
   <select id='userGroup' name="userGroup" multiple="multiple" class="hidden"></select>
 
   <input type="hidden" id="id" name="id" value="$program.id">
@@ -335,7 +333,7 @@
 					</td>
 				</tr>
 				<tr name="tr$index">
-					<td><label>$i18n.getString( "send_when_to" )</label></td>
+					<td><label>$i18n.getString( "send_base_on" )</label></td>
 					<td>
 						<select type="text" id="whenToSend$index" name="whenToSend$index" class="whenToSend" onchange='whenToSendOnChange($index)'>
 							<option value="" >$i18n.getString("days_scheduled")</option>
@@ -380,8 +378,8 @@
 					<td>
 						<select type="text" id="sendTo$index" name="sendTo$index" class="sendTo {validate:{required:true,number:true}}" onchange="onchangeUserGroup( $index )">
 							<option value="1" #if($!reminder.sendTo==1) selected #end >$i18n.getString("patient_sms_only")</option>
-							<option value="2" #if($!reminder.sendTo==2) selected #end >$i18n.getString("orgunit_phone_number_sms_only")</option>
-							<option value="3" #if($!reminder.sendTo==3) selected #end >$i18n.getString("health_worker_assigned_to_person")</option>
+							<option value="3" #if($!reminder.sendTo==3) selected #end >$i18n.getString("orgunit_phone_number_sms_only")</option>
+							<option value="2" #if($!reminder.sendTo==2) selected #end >$i18n.getString("health_worker_assigned_to_person")</option>
 							<option value="4" #if($!reminder.sendTo==4) selected #end >$i18n.getString("all_users_in_orgunit_registered")</option>
 							<option value="5" #if($!reminder.sendTo==5) selected #end >$i18n.getString("user_group")</option>
 						</select>
@@ -398,6 +396,16 @@
 					</td>
 				</tr>
 				<tr name="tr$index">
+					<td><label>$i18n.getString( "message_type" )</label></td>
+					<td>
+						<select type="text" id="messageType$index" name="messageType$index" #if($!reminder.sendTo==1 || $!reminder.sendTo==3) disabled #end class="messageType {validate:{required:true,number:true}}" >
+							<option value="1" #if($!reminder.messageType==1) selected #end >$i18n.getString("direct_sms")</option>
+							<option value="2" #if($!reminder.messageType==2) selected #end >$i18n.getString("message")</option>
+							<option value="3" #if($!reminder.messageType==3) selected #end >$i18n.getString("both")</option>
+						</select>
+					</td>
+				</tr>
+				<tr name="tr$index">
 					<td>$i18n.getString( "params" )</td>
 					<td>
 						<select multiple size='4' ondblclick="insertParams(this.value, $index);">
@@ -413,7 +421,7 @@
 				</tr>
 				<tr name="tr$index">
 					<td><label>$i18n.getString( "message" )</label></td>
-					<td><textarea onkeypress="getMessageLength($index)" id="templateMessage$index" name="templateMessage$index" style="width:320px" class="templateMessage {validate:{required:true, rangelength:[3,160]}}">$reminder.templateMessage</textarea></td>
+					<td><textarea onkeyup="getMessageLength($index)" id="templateMessage$index" name="templateMessage$index" style="width:320px" class="templateMessage {validate:{required:true}}">$reminder.templateMessage</textarea></td>
 				</tr>
 				<tr>
 					<td></td>
@@ -472,5 +480,10 @@
 	var i18n_before = '$encoder.jsEscape( $i18n.getString( "before" ) , "'")';
 	var i18n_after = '$encoder.jsEscape( $i18n.getString( "after" ) , "'")';
 	var i18n_scheduled_date = '$encoder.jsEscape( $i18n.getString( "scheduled_date" ) , "'")';
+	var i18n_message_type = '$encoder.jsEscape( $i18n.getString( "message_type" ) , "'")';
+	var i18n_direct_sms = '$encoder.jsEscape( $i18n.getString( "direct_sms" ) , "'")';
+	var i18n_message = '$encoder.jsEscape( $i18n.getString( "message" ) , "'")';
+	var i18n_both = '$encoder.jsEscape( $i18n.getString( "both" ) , "'")';
+	
 	programOnChange();
 </script>

=== modified file 'dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/webapp/dhis-web-maintenance-patient/updateProgramStageForm.vm'
--- dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/webapp/dhis-web-maintenance-patient/updateProgramStageForm.vm	2013-09-09 16:11:17 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/webapp/dhis-web-maintenance-patient/updateProgramStageForm.vm	2013-09-10 04:51:49 +0000
@@ -18,6 +18,7 @@
 	<select id='templateMessages' name="templateMessages" multiple="multiple" class="hidden"></select>
 	<select id='sendTo' name="sendTo" multiple="multiple" class="hidden"></select>
 	<select id='whenToSend' name="whenToSend" multiple="multiple" class="hidden"></select>
+	<select id='messageType' name="messageType" multiple="multiple" class="hidden"></select>
 	<select id='userGroup' name="userGroup" multiple="multiple" class="hidden"></select>
 	
 	<table id="detailsList">
@@ -248,8 +249,8 @@
 					<td>
 						<select type="text" id="sendTo$index" name="sendTo$index" class="sendTo {validate:{required:true,number:true}}" onchange="onchangeUserGroup( $index )">
 							<option value="1" #if($!reminder.sendTo==1) selected #end >$i18n.getString("patient_sms_only")</option>
-							<option value="2" #if($!reminder.sendTo==2) selected #end >$i18n.getString("orgunit_phone_number_sms_only")</option>
-							<option value="3" #if($!reminder.sendTo==3) selected #end >$i18n.getString("health_worker_assigned_to_person")</option>
+							<option value="3" #if($!reminder.sendTo==3) selected #end >$i18n.getString("orgunit_phone_number_sms_only")</option>
+							<option value="2" #if($!reminder.sendTo==2) selected #end >$i18n.getString("health_worker_assigned_to_person")</option>
 							<option value="4" #if($!reminder.sendTo==4) selected #end >$i18n.getString("all_users_at_orgunit")</option>
 							<option value="5" #if($!reminder.sendTo==5) selected #end >$i18n.getString("user_group")</option>
 						</select>
@@ -266,6 +267,16 @@
 					</td>
 				</tr>
 				<tr name="tr$index">
+					<td><label>$i18n.getString( "message_type" )</label></td>
+					<td>
+						<select type="text" id="messageType$index" name="messageType$index" #if($!reminder.sendTo==1 || $!reminder.sendTo==3) disabled #end  class="messageType {validate:{required:true,number:true}}" >
+							<option value="1" #if($!reminder.messageType==1) selected #end >$i18n.getString("direct_sms")</option>
+							<option value="2" #if($!reminder.messageType==2) selected #end >$i18n.getString("message")</option>
+							<option value="3" #if($!reminder.messageType==3) selected #end >$i18n.getString("both")</option>
+						</select>
+					</td>
+				</tr>
+				<tr name="tr$index">
 					<td>$i18n.getString( "params" )</td>
 					<td>
 						<select multiple size='4' ondblclick="insertParams(this.value, $index);">
@@ -280,7 +291,7 @@
 				</tr>
 				<tr name="tr$index">
 					<td><label>$i18n.getString( "message" )</label></td>
-					<td><textarea id="templateMessage$index" onkeypress="getMessageLength($index)" name="templateMessage$index" style="width:320px" class="templateMessage {validate:{required:true}}">$reminder.templateMessage</textarea></td>
+					<td><textarea id="templateMessage$index" onkeyup="getMessageLength($index)" name="templateMessage$index" style="width:320px" class="templateMessage {validate:{required:true}}">$reminder.templateMessage</textarea></td>
 				</tr>
 				<tr>
 					<td></td>
@@ -336,4 +347,8 @@
 	var i18n_before = '$encoder.jsEscape( $i18n.getString( "before" ) , "'")';
 	var i18n_after = '$encoder.jsEscape( $i18n.getString( "after" ) , "'")';
 	var i18n_scheduled_date = '$encoder.jsEscape( $i18n.getString( "scheduled_date" ) , "'")';
+	var i18n_message_type = '$encoder.jsEscape( $i18n.getString( "message_type" ) , "'")';
+	var i18n_direct_sms = '$encoder.jsEscape( $i18n.getString( "direct_sms" ) , "'")';
+	var i18n_message = '$encoder.jsEscape( $i18n.getString( "message" ) , "'")';
+	var i18n_both = '$encoder.jsEscape( $i18n.getString( "both" ) , "'")';
 </script>
\ No newline at end of file