← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 12129: Move enrollment, complete program-instance and complete event to service layer.

 

------------------------------------------------------------
revno: 12129
committer: Tran Chau <tran.hispvietnam@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2013-09-18 13:54:48 +0700
message:
  Move enrollment, complete program-instance and complete event to service layer.
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramInstanceService.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramStageInstanceService.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramStageInstanceStore.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/java/org/hisp/dhis/program/hibernate/HibernateProgramStageInstanceStore.java
  dhis-2/dhis-services/dhis-service-patient/src/main/resources/META-INF/dhis/beans.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/ProgramEnrollmentAction.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/reminder/SetEventStatusAction.java
  dhis-2/dhis-web/dhis-web-caseentry/src/main/resources/META-INF/dhis/beans.xml


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramInstanceService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramInstanceService.java	2013-09-17 15:26:14 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramInstanceService.java	2013-09-18 06:54:48 +0000
@@ -110,4 +110,17 @@
     Collection<MessageConversation> sendMessageConversations( ProgramInstance programInstance,
         int sendWhenToC0mpletedEvent, I18nFormat format );
 
+    ProgramInstance enrollmentPatient( Patient patient, Program program, Date enrollmentDate, Date dateOfIncident,
+        OrganisationUnit orgunit, I18nFormat format );
+
+    boolean canAutoCompleteProgramInstanceStatus( ProgramInstance programInstance );
+
+    void completeProgramInstanceStatus( ProgramInstance programInstance, I18nFormat format );
+
+    /**
+     * Set status as skipped for overdue events; Remove scheduled events
+     * 
+     * **/
+
+    void cancelProgramInstanceStatus( ProgramInstance programInstance );
 }

=== 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-17 11:13:16 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramStageInstanceService.java	2013-09-18 06:54:48 +0000
@@ -73,7 +73,8 @@
 
     Collection<ProgramStageInstance> getProgramStageInstances( Collection<ProgramInstance> programInstances );
 
-    Collection<ProgramStageInstance> getProgramStageInstances( Collection<ProgramInstance> programInstances, boolean completed );
+    Collection<ProgramStageInstance> getProgramStageInstances( Collection<ProgramInstance> programInstances,
+        boolean completed );
 
     Collection<ProgramStageInstance> getProgramStageInstances( Date dueDate );
 
@@ -148,9 +149,8 @@
 
     Collection<OutboundSms> sendMessages( ProgramStageInstance programStageInstance, int status, I18nFormat format );
 
-    Collection<ProgramStageInstance> getProgramStageInstance( Patient patient );
-
     Collection<MessageConversation> sendMessageConversations( ProgramStageInstance programStageInstance, int status,
         I18nFormat format );
 
+    void completeProgramStageInstance( ProgramStageInstance programStageInstance, I18nFormat format );
 }

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramStageInstanceStore.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramStageInstanceStore.java	2013-09-17 11:13:16 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramStageInstanceStore.java	2013-09-18 06:54:48 +0000
@@ -71,12 +71,12 @@
 
     /**
      * Get all {@link ProgramStageInstance program stage instances} for unit.
-     *
-     * @param unit      - the unit to get instances for.
-     * @param after     - optional date the instance should be on or after.
-     * @param before    - optional date the instance should be on or before.
+     * 
+     * @param unit - the unit to get instances for.
+     * @param after - optional date the instance should be on or after.
+     * @param before - optional date the instance should be on or before.
      * @param completed - optional flag to only get completed (<code>true</code>
-     *                  ) or uncompleted (<code>false</code>) instances.
+     *        ) or uncompleted (<code>false</code>) instances.
      * @return
      */
     public List<ProgramStageInstance> get( OrganisationUnit unit, Date after, Date before, Boolean completed );
@@ -130,7 +130,4 @@
     Collection<Integer> getOrgunitIds( Date startDate, Date endDate );
 
     Grid getCompleteness( Collection<Integer> orgunitIds, Program program, String startDate, String endDate, I18n i18n );
-
-    Collection<ProgramStageInstance> get( Patient patient );
-
 }

=== 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-17 15:26:14 +0000
+++ dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/program/DefaultProgramInstanceService.java	2013-09-18 06:54:48 +0000
@@ -47,14 +47,17 @@
 import org.hisp.dhis.patientcomment.PatientComment;
 import org.hisp.dhis.patientdatavalue.PatientDataValue;
 import org.hisp.dhis.patientdatavalue.PatientDataValueService;
+import org.hisp.dhis.period.PeriodType;
 import org.hisp.dhis.sms.SmsSender;
 import org.hisp.dhis.sms.SmsServiceException;
 import org.hisp.dhis.sms.outbound.OutboundSms;
 import org.hisp.dhis.system.grid.ListGrid;
+import org.hisp.dhis.system.util.DateUtils;
 import org.hisp.dhis.user.CurrentUserService;
 import org.springframework.transaction.annotation.Transactional;
 
 import java.util.ArrayList;
+import java.util.Calendar;
 import java.util.Collection;
 import java.util.Date;
 import java.util.HashSet;
@@ -130,6 +133,13 @@
         this.messageService = messageService;
     }
 
+    private ProgramStageInstanceService programStageInstanceService;
+
+    public void setProgramStageInstanceService( ProgramStageInstanceService programStageInstanceService )
+    {
+        this.programStageInstanceService = programStageInstanceService;
+    }
+
     // -------------------------------------------------------------------------
     // Implementation methods
     // -------------------------------------------------------------------------
@@ -595,10 +605,223 @@
         return messageConversations;
     }
 
+    @Override
+    public ProgramInstance enrollmentPatient( Patient patient, Program program, Date enrollmentDate,
+        Date dateOfIncident, OrganisationUnit orgunit, I18nFormat format )
+    {
+        if ( enrollmentDate == null )
+        {
+            if ( program.getUseBirthDateAsIncidentDate() )
+            {
+                enrollmentDate = patient.getBirthDate();
+            }
+            else
+            {
+                enrollmentDate = new Date();
+            }
+        }
+
+        if ( dateOfIncident == null )
+        {
+            if ( program.getUseBirthDateAsIncidentDate() )
+            {
+                dateOfIncident = patient.getBirthDate();
+            }
+            else
+            {
+                dateOfIncident = enrollmentDate;
+            }
+        }
+
+        ProgramInstance programInstance = new ProgramInstance();
+        programInstance.setEnrollmentDate( enrollmentDate );
+        programInstance.setDateOfIncident( dateOfIncident );
+        programInstance.setProgram( program );
+        programInstance.setPatient( patient );
+        programInstance.setStatus( ProgramInstance.STATUS_ACTIVE );
+
+        addProgramInstance( programInstance );
+
+        // ---------------------------------------------------------------------
+        // Generate events of program-instance
+        // ---------------------------------------------------------------------
+
+        for ( ProgramStage programStage : program.getProgramStages() )
+        {
+            if ( programStage.getAutoGenerateEvent() )
+            {
+                ProgramStageInstance programStageInstance = generateEvent( programInstance, programStage,
+                    enrollmentDate, dateOfIncident, orgunit );
+                programStageInstanceService.addProgramStageInstance( programStageInstance );
+            }
+        }
+
+        // -----------------------------------------------------------------
+        // send messages after enrollment program
+        // -----------------------------------------------------------------
+
+        List<OutboundSms> outboundSms = programInstance.getOutboundSms();
+        if ( outboundSms == null )
+        {
+            outboundSms = new ArrayList<OutboundSms>();
+        }
+
+        outboundSms.addAll( 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( sendMessageConversations( programInstance,
+            PatientReminder.SEND_WHEN_TO_EMROLLEMENT, format ) );
+
+        updateProgramInstance( programInstance );
+
+        return programInstance;
+    }
+
+    @Override
+    public boolean canAutoCompleteProgramInstanceStatus( ProgramInstance programInstance )
+    {
+        Set<ProgramStageInstance> stageInstances = programInstance.getProgramStageInstances();
+
+        for ( ProgramStageInstance stageInstance : stageInstances )
+        {
+            if ( !stageInstance.isCompleted() || stageInstance.getProgramStage().getIrregular() )
+            {
+                return false;
+            }
+        }
+
+        return true;
+    }
+
+    @Override
+    public void completeProgramInstanceStatus( ProgramInstance programInstance, I18nFormat format )
+    {
+        // ---------------------------------------------------------------------
+        // Send sms-message when to completed the program
+        // ---------------------------------------------------------------------
+
+        List<OutboundSms> outboundSms = programInstance.getOutboundSms();
+        if ( outboundSms == null )
+        {
+            outboundSms = new ArrayList<OutboundSms>();
+        }
+
+        outboundSms.addAll( sendMessages( programInstance, PatientReminder.SEND_WHEN_TO_C0MPLETED_PROGRAM, format ) );
+
+        // -----------------------------------------------------------------
+        // Send DHIS message when to completed the program
+        // -----------------------------------------------------------------
+
+        List<MessageConversation> messageConversations = programInstance.getMessageConversations();
+        if ( messageConversations == null )
+        {
+            messageConversations = new ArrayList<MessageConversation>();
+        }
+
+        messageConversations.addAll( sendMessageConversations( programInstance,
+            PatientReminder.SEND_WHEN_TO_C0MPLETED_PROGRAM, format ) );
+
+        // -----------------------------------------------------------------
+        // Update program-instance
+        // -----------------------------------------------------------------
+
+        programInstance.setStatus( ProgramInstance.STATUS_COMPLETED );
+
+        programInstance.setEndDate( new Date() );
+
+        updateProgramInstance( programInstance );
+    }
+
+    public void cancelProgramInstanceStatus( ProgramInstance programInstance )
+    {
+        // ---------------------------------------------------------------------
+        // Set status of the program-instance
+        // ---------------------------------------------------------------------
+
+        Calendar today = Calendar.getInstance();
+        PeriodType.clearTimeOfDay( today );
+        Date currentDate = today.getTime();
+
+        programInstance.setEndDate( currentDate );
+        updateProgramInstance( programInstance );
+
+        // ---------------------------------------------------------------------
+        // Set statuses of the program-stage-instances
+        // ---------------------------------------------------------------------
+
+        for ( ProgramStageInstance programStageInstance : programInstance.getProgramStageInstances() )
+        {
+            if ( programStageInstance.getExecutionDate() == null )
+            {
+                // ---------------------------------------------------------------------
+                // Set status as skipped for overdue events
+                // ---------------------------------------------------------------------
+                if ( programStageInstance.getDueDate().before( currentDate ) )
+                {
+                    programStageInstance.setStatus( ProgramStageInstance.SKIPPED_STATUS );
+                    programStageInstanceService.updateProgramStageInstance( programStageInstance );
+                }
+
+                // ---------------------------------------------------------------------
+                // Remove scheduled events
+                // ---------------------------------------------------------------------
+                else
+                {
+                    programStageInstanceService.deleteProgramStageInstance( programStageInstance );
+                }
+            }
+        }
+    }
+
     // -------------------------------------------------------------------------
     // Supportive methods
     // -------------------------------------------------------------------------
 
+    private ProgramStageInstance generateEvent( ProgramInstance programInstance, ProgramStage programStage,
+        Date enrollmentDate, Date dateOfIncident, OrganisationUnit orgunit )
+    {
+        ProgramStageInstance programStageInstance = null;
+
+        Date currentDate = new Date();
+        Date dateCreatedEvent = null;
+        if ( programStage.getGeneratedByEnrollmentDate() )
+        {
+            dateCreatedEvent = enrollmentDate;
+        }
+        else
+        {
+            dateCreatedEvent = dateOfIncident;
+        }
+
+        Date dueDate = DateUtils.getDateAfterAddition( dateCreatedEvent, programStage.getMinDaysFromStart() );
+
+        if ( !programInstance.getProgram().getIgnoreOverdueEvents()
+            || !(programInstance.getProgram().getIgnoreOverdueEvents() && dueDate.before( currentDate )) )
+        {
+            programStageInstance = new ProgramStageInstance();
+            programStageInstance.setProgramInstance( programInstance );
+            programStageInstance.setProgramStage( programStage );
+            programStageInstance.setDueDate( dueDate );
+
+            if ( programInstance.getProgram().isSingleEvent() )
+            {
+                programStageInstance.setOrganisationUnit( orgunit );
+                programStageInstance.setExecutionDate( dueDate );
+            }
+        }
+
+        return programStageInstance;
+    }
+
     private void getProgramStageInstancesReport( Grid grid, ProgramInstance programInstance, I18nFormat format,
         I18n i18n )
     {

=== 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-17 11:13:16 +0000
+++ dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/program/DefaultProgramStageInstanceService.java	2013-09-18 06:54:48 +0000
@@ -45,6 +45,7 @@
 import org.hisp.dhis.patientdatavalue.PatientDataValueService;
 import org.hisp.dhis.patientreport.TabularReportColumn;
 import org.hisp.dhis.period.Period;
+import org.hisp.dhis.period.PeriodType;
 import org.hisp.dhis.sms.SmsSender;
 import org.hisp.dhis.sms.SmsServiceException;
 import org.hisp.dhis.sms.outbound.OutboundSms;
@@ -53,6 +54,7 @@
 import org.springframework.transaction.annotation.Transactional;
 
 import java.util.ArrayList;
+import java.util.Calendar;
 import java.util.Collection;
 import java.util.Date;
 import java.util.HashMap;
@@ -206,7 +208,8 @@
         return programStageInstanceStore.get( programInstances );
     }
 
-    public Collection<ProgramStageInstance> getProgramStageInstances( Collection<ProgramInstance> programInstances, boolean completed )
+    public Collection<ProgramStageInstance> getProgramStageInstances( Collection<ProgramInstance> programInstances,
+        boolean completed )
     {
         return programStageInstanceStore.get( programInstances, completed );
     }
@@ -639,9 +642,48 @@
         return messageConversations;
     }
 
-    public Collection<ProgramStageInstance> getProgramStageInstance( Patient patient )
+    @Override
+    public void completeProgramStageInstance( ProgramStageInstance programStageInstance, I18nFormat format )
     {
-        return programStageInstanceStore.get( patient );
+        programStageInstance.setCompleted( true );
+
+        Calendar today = Calendar.getInstance();
+        PeriodType.clearTimeOfDay( today );
+        Date date = today.getTime();
+
+        programStageInstance.setCompletedDate( date );
+        programStageInstance.setCompletedUser( currentUserService.getCurrentUsername() );
+
+        // ---------------------------------------------------------------------
+        // Send sms-message when to completed the event
+        // ---------------------------------------------------------------------
+
+        List<OutboundSms> outboundSms = programStageInstance.getOutboundSms();
+        if ( outboundSms == null )
+        {
+            outboundSms = new ArrayList<OutboundSms>();
+        }
+
+        outboundSms.addAll( sendMessages( programStageInstance, PatientReminder.SEND_WHEN_TO_C0MPLETED_EVENT, format ) );
+
+        // ---------------------------------------------------------------------
+        // Send DHIS message when to completed the event
+        // ---------------------------------------------------------------------
+
+        List<MessageConversation> messageConversations = programStageInstance.getMessageConversations();
+        if ( messageConversations == null )
+        {
+            messageConversations = new ArrayList<MessageConversation>();
+        }
+
+        messageConversations.addAll( sendMessageConversations( programStageInstance,
+            PatientReminder.SEND_WHEN_TO_C0MPLETED_EVENT, format ) );
+      
+        // ---------------------------------------------------------------------
+        // Update the event
+        // ---------------------------------------------------------------------
+
+        updateProgramStageInstance( programStageInstance );
     }
 
     // -------------------------------------------------------------------------

=== modified file 'dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/program/hibernate/HibernateProgramStageInstanceStore.java'
--- dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/program/hibernate/HibernateProgramStageInstanceStore.java	2013-09-17 11:13:16 +0000
+++ dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/program/hibernate/HibernateProgramStageInstanceStore.java	2013-09-18 06:54:48 +0000
@@ -793,13 +793,6 @@
         return grid;
     }
 
-    @SuppressWarnings("unchecked")
-    public Collection<ProgramStageInstance> get( Patient patient )
-    {
-        return getCriteria().createAlias( "patients", "patient" )
-            .add( Restrictions.eq( "patient.id", patient.getId() ) ).list();
-    }
-
     // -------------------------------------------------------------------------
     // Supportive methods
     // -------------------------------------------------------------------------

=== 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-09-17 06:08:16 +0000
+++ dhis-2/dhis-services/dhis-service-patient/src/main/resources/META-INF/dhis/beans.xml	2013-09-18 06:54:48 +0000
@@ -209,6 +209,8 @@
 		<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" />
+		<property name="programStageInstanceService"
+			ref="org.hisp.dhis.program.ProgramStageInstanceService" />
 	</bean>
 
 	<bean id="org.hisp.dhis.program.ProgramStageService" class="org.hisp.dhis.program.DefaultProgramStageService">

=== 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-18 03:49:35 +0000
+++ dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/caseentry/CompleteDataEntryAction.java	2013-09-18 06:54:48 +0000
@@ -28,23 +28,11 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import java.util.ArrayList;
-import java.util.Calendar;
-import java.util.Date;
-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.PatientReminder;
-import org.hisp.dhis.period.PeriodType;
 import org.hisp.dhis.program.Program;
-import org.hisp.dhis.program.ProgramInstance;
 import org.hisp.dhis.program.ProgramInstanceService;
 import org.hisp.dhis.program.ProgramStageInstance;
 import org.hisp.dhis.program.ProgramStageInstanceService;
-import org.hisp.dhis.sms.outbound.OutboundSms;
-import org.hisp.dhis.user.CurrentUserService;
 
 import com.opensymphony.xwork2.Action;
 
@@ -72,13 +60,6 @@
         this.programInstanceService = programInstanceService;
     }
 
-    private CurrentUserService currentUserService;
-
-    public void setCurrentUserService( CurrentUserService currentUserService )
-    {
-        this.currentUserService = currentUserService;
-    }
-
     private I18nFormat format;
 
     public void setFormat( I18nFormat format )
@@ -129,42 +110,7 @@
         ProgramStageInstance programStageInstance = programStageInstanceService
             .getProgramStageInstance( programStageInstanceId );
 
-        programStageInstance.setCompleted( true );
-
-        Calendar today = Calendar.getInstance();
-        PeriodType.clearTimeOfDay( today );
-        Date date = today.getTime();
-
-        programStageInstance.setCompletedDate( date );
-        programStageInstance.setCompletedUser( currentUserService.getCurrentUsername() );
-
-        // ---------------------------------------------------------------------
-        // Send sms-message when to completed the event
-        // ---------------------------------------------------------------------
-
-        List<OutboundSms> psiOutboundSms = programStageInstance.getOutboundSms();
-        if ( psiOutboundSms == null )
-        {
-            psiOutboundSms = new ArrayList<OutboundSms>();
-        }
-
-        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 );
+        programStageInstanceService.completeProgramStageInstance( programStageInstance, format );
 
         // ---------------------------------------------------------------------
         // Check Completed status for all of ProgramStageInstance of
@@ -174,48 +120,14 @@
         if ( !programStageInstance.getProgramInstance().getProgram().getType()
             .equals( Program.SINGLE_EVENT_WITHOUT_REGISTRATION ) )
         {
-            ProgramInstance programInstance = programStageInstance.getProgramInstance();
-
-            Set<ProgramStageInstance> stageInstances = programInstance.getProgramStageInstances();
-
-            for ( ProgramStageInstance stageInstance : stageInstances )
-            {
-                if ( !stageInstance.isCompleted() || stageInstance.getProgramStage().getIrregular() )
-                {
-                    return SUCCESS;
-                }
-            }
-
-            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 )
-            {
-                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 ) );
-
-            programInstanceService.updateProgramInstance( programInstance );
+            boolean canCompleted = programInstanceService.canAutoCompleteProgramInstanceStatus( programStageInstance
+                .getProgramInstance() );
+            if ( !canCompleted )
+            {
+                return SUCCESS;
+            }
+
+            programInstanceService.completeProgramInstanceStatus( programStageInstance.getProgramInstance(), format );
 
         }
 

=== modified file 'dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/patient/ProgramEnrollmentAction.java'
--- dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/patient/ProgramEnrollmentAction.java	2013-08-23 16:05:01 +0000
+++ dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/patient/ProgramEnrollmentAction.java	2013-09-18 06:54:48 +0000
@@ -49,7 +49,6 @@
 import org.hisp.dhis.program.ProgramInstance;
 import org.hisp.dhis.program.ProgramInstanceService;
 import org.hisp.dhis.program.ProgramStageInstance;
-import org.hisp.dhis.program.ProgramStageInstanceService;
 import org.hisp.dhis.program.comparator.ProgramStageInstanceVisitDateComparator;
 
 import com.opensymphony.xwork2.Action;
@@ -72,8 +71,6 @@
 
     private SelectedStateManager selectedStateManager;
 
-    private ProgramStageInstanceService programStageInstanceService;
-
     // -------------------------------------------------------------------------
     // Input/Output
     // -------------------------------------------------------------------------
@@ -98,8 +95,6 @@
 
     private ProgramInstance programInstance;
 
-    private Collection<ProgramStageInstance> linkProgramStageInstances = new HashSet<ProgramStageInstance>();
-
     // -------------------------------------------------------------------------
     // Getters/Setters
     // -------------------------------------------------------------------------
@@ -109,16 +104,6 @@
         this.selectedStateManager = selectedStateManager;
     }
 
-    public Collection<ProgramStageInstance> getLinkProgramStageInstances()
-    {
-        return linkProgramStageInstances;
-    }
-
-    public void setProgramStageInstanceService( ProgramStageInstanceService programStageInstanceService )
-    {
-        this.programStageInstanceService = programStageInstanceService;
-    }
-
     public Collection<PatientAttribute> getNoGroupAttributes()
     {
         return noGroupAttributes;
@@ -202,8 +187,6 @@
         programStageInstances = new ArrayList<ProgramStageInstance>( programInstance.getProgramStageInstances() );
 
         Collections.sort( programStageInstances, new ProgramStageInstanceVisitDateComparator() );
-        
-        linkProgramStageInstances = programStageInstanceService.getProgramStageInstance( programInstance.getPatient() );
 
         loadIdentifierTypes( programInstance );
 

=== 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-18 03:49:35 +0000
+++ dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/patient/SaveProgramEnrollmentAction.java	2013-09-18 06:54:48 +0000
@@ -30,24 +30,21 @@
 
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Collections;
 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.organisationunit.OrganisationUnit;
 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;
 import org.hisp.dhis.program.ProgramInstanceService;
 import org.hisp.dhis.program.ProgramService;
-import org.hisp.dhis.program.ProgramStage;
 import org.hisp.dhis.program.ProgramStageInstance;
-import org.hisp.dhis.program.ProgramStageInstanceService;
-import org.hisp.dhis.sms.outbound.OutboundSms;
-import org.hisp.dhis.system.util.DateUtils;
+import org.hisp.dhis.program.comparator.ProgramStageInstanceVisitDateComparator;
 
 import com.opensymphony.xwork2.Action;
 
@@ -83,13 +80,6 @@
         this.programInstanceService = programInstanceService;
     }
 
-    private ProgramStageInstanceService programStageInstanceService;
-
-    public void setProgramStageInstanceService( ProgramStageInstanceService programStageInstanceService )
-    {
-        this.programStageInstanceService = programStageInstanceService;
-    }
-
     private SelectedStateManager selectedStateManager;
 
     public void setSelectedStateManager( SelectedStateManager selectedStateManager )
@@ -162,143 +152,65 @@
     public String execute()
         throws Exception
     {
+
         Patient patient = patientService.getPatient( patientId );
 
         Program program = programService.getProgram( programId );
 
-        if ( enrollmentDate == null || enrollmentDate.isEmpty() )
-        {
-            if ( program.getUseBirthDateAsIncidentDate() )
-            {
-                enrollmentDate = format.formatDate( patient.getBirthDate() );
-            }
-        }
-
-        if ( dateOfIncident == null || dateOfIncident.isEmpty() )
-        {
-            if ( program.getUseBirthDateAsIncidentDate() )
-            {
-                dateOfIncident = format.formatDate( patient.getBirthDate() );
-            }
-            else
-            {
-                dateOfIncident = enrollmentDate;
-            }
-        }
+        OrganisationUnit orgunit = selectedStateManager.getSelectedOrganisationUnit();
+
+        Date enrollment = (enrollmentDate == null || enrollmentDate.isEmpty()) ? null : format
+            .parseDate( enrollmentDate );
+
+        Date incident = (dateOfIncident == null || dateOfIncident.isEmpty()) ? null : format.parseDate( dateOfIncident );
 
         Collection<ProgramInstance> programInstances = programInstanceService.getProgramInstances( patient, program,
             ProgramInstance.STATUS_ACTIVE );
 
+        ProgramInstance programInstance = null;
+
         if ( programInstances.iterator().hasNext() )
         {
             programInstance = programInstances.iterator().next();
         }
 
+        // ---------------------------------------------------------------------
+        // Generate program-instance and visits scheduled
+        // ---------------------------------------------------------------------
+
         if ( programInstance == null )
         {
-            programInstance = new ProgramInstance();
-            programInstance.setEnrollmentDate( format.parseDate( enrollmentDate ) );
-            programInstance.setDateOfIncident( format.parseDate( dateOfIncident ) );
-            programInstance.setProgram( program );
-            programInstance.setPatient( patient );
-            programInstance.setStatus( ProgramInstance.STATUS_ACTIVE );
-
-            programInstanceService.addProgramInstance( programInstance );
-
-            boolean isFirstStage = false;
-            Date currentDate = new Date();
-            for ( ProgramStage programStage : program.getProgramStages() )
-            {
-                if ( programStage.getAutoGenerateEvent() )
-                {
-                    Date dateCreatedEvent = null;
-                    if ( programStage.getGeneratedByEnrollmentDate() )
-                    {
-                        dateCreatedEvent = format.parseDate( enrollmentDate );
-                    }
-                    else
-                    {
-                        dateCreatedEvent = format.parseDate( dateOfIncident );
-
-                    }
-
-                    Date dueDate = DateUtils
-                        .getDateAfterAddition( dateCreatedEvent, programStage.getMinDaysFromStart() );
-
-                    if ( !program.getIgnoreOverdueEvents()
-                        || !(program.getIgnoreOverdueEvents() && dueDate.before( currentDate )) )
-                    {
-                        ProgramStageInstance programStageInstance = new ProgramStageInstance();
-                        programStageInstance.setProgramInstance( programInstance );
-                        programStageInstance.setProgramStage( programStage );
-                        programStageInstance.setDueDate( dueDate );
-
-                        if ( program.isSingleEvent() )
-                        {
-                            programStageInstance.setOrganisationUnit( selectedStateManager
-                                .getSelectedOrganisationUnit() );
-                            programStageInstance.setExecutionDate( dueDate );
-                        }
-                        programStageInstanceService.addProgramStageInstance( programStageInstance );
-
-                        if ( !isFirstStage )
-                        {
-                            activeProgramStageInstance = programStageInstance;
-                            isFirstStage = true;
-                        }
-                    }
-                }
-            }
-
-            // -----------------------------------------------------------------
-            // send messages after enrollment program
-            // -----------------------------------------------------------------
-
-            List<OutboundSms> outboundSms = programInstance.getOutboundSms();
-            if ( outboundSms == null )
-            {
-                outboundSms = new ArrayList<OutboundSms>();
-            }
-
-            outboundSms.addAll( programInstanceService.sendMessages( programInstance,
-                PatientReminder.SEND_WHEN_TO_EMROLLEMENT, format ) );
-
-            programInstanceService.updateProgramInstance( programInstance );
-            
-            // -----------------------------------------------------------------
-            // 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 );
+            programInstance = programInstanceService.enrollmentPatient( patient, program, enrollment, incident,
+                orgunit, format );
         }
+
+        // ---------------------------------------------------------------------
+        // Update enrollment-date and incident-date
+        // ---------------------------------------------------------------------
+
         else
         {
-            programInstance.setEnrollmentDate( format.parseDate( enrollmentDate ) );
-            programInstance.setDateOfIncident( format.parseDate( dateOfIncident ) );
+            programInstance.setEnrollmentDate( enrollment );
+            programInstance.setDateOfIncident( incident );
 
             programInstanceService.updateProgramInstance( programInstance );
-
-            for ( ProgramStageInstance programStageInstance : programInstance.getProgramStageInstances() )
+        }
+
+        // ---------------------------------------------------------------------
+        // Get the active event of program-instance
+        // ---------------------------------------------------------------------
+
+        List<ProgramStageInstance> programStageInstances = new ArrayList<ProgramStageInstance>(
+            programInstance.getProgramStageInstances() );
+        Collections.sort( programStageInstances, new ProgramStageInstanceVisitDateComparator() );
+
+        for ( ProgramStageInstance programStageInstance : programStageInstances )
+        {
+            if ( !programStageInstance.isCompleted()
+                && programStageInstance.getStatus() != ProgramStageInstance.SKIPPED_STATUS )
             {
-                if ( !programStageInstance.isCompleted()
-                    || (programStageInstance.getStatus() != null && programStageInstance.getStatus() != ProgramStageInstance.SKIPPED_STATUS) )
-                {
-                    Date dueDate = DateUtils.getDateAfterAddition( format.parseDate( dateOfIncident ),
-                        programStageInstance.getProgramStage().getMinDaysFromStart() );
-
-                    programStageInstance.setDueDate( dueDate );
-
-                    programStageInstanceService.updateProgramStageInstance( programStageInstance );
-                }
+                activeProgramStageInstance = programStageInstance;
+                break;
             }
         }
 

=== 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-09-18 03:49:35 +0000
+++ dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/patient/SetProgramInstanceStatusAction.java	2013-09-18 06:54:48 +0000
@@ -29,21 +29,12 @@
  */
 
 import java.util.ArrayList;
-import java.util.Calendar;
 import java.util.Collection;
-import java.util.Date;
-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.period.PeriodType;
 import org.hisp.dhis.program.Program;
 import org.hisp.dhis.program.ProgramInstance;
 import org.hisp.dhis.program.ProgramInstanceService;
-import org.hisp.dhis.program.ProgramStageInstance;
-import org.hisp.dhis.program.ProgramStageInstanceService;
-import org.hisp.dhis.sms.outbound.OutboundSms;
 
 import com.opensymphony.xwork2.Action;
 
@@ -65,13 +56,6 @@
         this.programInstanceService = programInstanceService;
     }
 
-    private ProgramStageInstanceService programStageInstanceService;
-
-    public void setProgramStageInstanceService( ProgramStageInstanceService programStageInstanceService )
-    {
-        this.programStageInstanceService = programStageInstanceService;
-    }
-
     private I18nFormat format;
 
     public void setFormat( I18nFormat format )
@@ -120,61 +104,14 @@
 
         if ( status == ProgramInstance.STATUS_COMPLETED )
         {
-            // ---------------------------------------------------------------------
-            // 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() );
+            programInstanceService.completeProgramInstanceStatus( programInstance, format );
         }
 
         else if ( status == ProgramInstance.STATUS_CANCELLED )
         {
-            Calendar today = Calendar.getInstance();
-            PeriodType.clearTimeOfDay( today );
-            Date currentDate = today.getTime();
-
-            programInstance.setEndDate( currentDate );
-
-            for ( ProgramStageInstance programStageInstance : programInstance.getProgramStageInstances() )
-            {
-                if ( programStageInstance.getExecutionDate() == null )
-                {
-                    // Set status as skipped for overdue events
-                    if ( programStageInstance.getDueDate().before( currentDate ) )
-                    {
-                        programStageInstance.setStatus( ProgramStageInstance.SKIPPED_STATUS );
-                        programStageInstanceService.updateProgramStageInstance( programStageInstance );
-                    }
-                    // Delete scheduled events
-                    else
-                    {
-                        programStageInstanceService.deleteProgramStageInstance( programStageInstance );
-                    }
-                }
-            }
+            programInstanceService.cancelProgramInstanceStatus( programInstance );
         }
+        // For Active status
         else
         {
             programInstance.setEndDate( null );

=== 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-09-10 04:51:49 +0000
+++ dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/reminder/SetEventStatusAction.java	2013-09-18 06:54:48 +0000
@@ -28,15 +28,9 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import java.util.ArrayList;
-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;
-import org.hisp.dhis.sms.outbound.OutboundSms;
 
 import com.opensymphony.xwork2.Action;
 
@@ -98,56 +92,31 @@
         switch ( status.intValue() )
         {
         case ProgramStageInstance.COMPLETED_STATUS:
-            programStageInstance.setCompleted( true );
-            programStageInstance.setStatus( null );
-
-            // -----------------------------------------------------------------
-            // Send sms when to completed program
-            // -----------------------------------------------------------------
-
-            List<OutboundSms> psiOutboundSms = programStageInstance.getOutboundSms();
-            if ( psiOutboundSms == null )
-            {
-                psiOutboundSms = new ArrayList<OutboundSms>();
-            }
-            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.completeProgramStageInstance( programStageInstance, format );
             break;
         case ProgramStageInstance.VISITED_STATUS:
             programStageInstance.setCompleted( false );
             programStageInstance.setStatus( null );
+            programStageInstanceService.updateProgramStageInstance( programStageInstance );
             break;
         case ProgramStageInstance.LATE_VISIT_STATUS:
             programStageInstance.setCompleted( false );
             programStageInstance.setStatus( null );
+            programStageInstanceService.updateProgramStageInstance( programStageInstance );
             break;
         case ProgramStageInstance.FUTURE_VISIT_STATUS:
             programStageInstance.setCompleted( false );
             programStageInstance.setStatus( null );
+            programStageInstanceService.updateProgramStageInstance( programStageInstance );
             break;
         case ProgramStageInstance.SKIPPED_STATUS:
             programStageInstance.setStatus( status );
+            programStageInstanceService.updateProgramStageInstance( programStageInstance );
             break;
         default:
             break;
         }
 
-        programStageInstanceService.updateProgramStageInstance( programStageInstance );
-
         return SUCCESS;
     }
 }

=== 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-18 03:49:35 +0000
+++ dhis-2/dhis-web/dhis-web-caseentry/src/main/resources/META-INF/dhis/beans.xml	2013-09-18 06:54:48 +0000
@@ -119,7 +119,6 @@
 		<property name="programStageInstanceService"
 			ref="org.hisp.dhis.program.ProgramStageInstanceService" />
 		<property name="programInstanceService" ref="org.hisp.dhis.program.ProgramInstanceService" />
-		<property name="currentUserService" ref="org.hisp.dhis.user.CurrentUserService" />
 	</bean>
 
 	<bean
@@ -641,8 +640,6 @@
 			ref="org.hisp.dhis.patientattributevalue.PatientAttributeValueService" />
 		<property name="selectedStateManager"
 			ref="org.hisp.dhis.caseentry.state.SelectedStateManager" />
-		<property name="programStageInstanceService"
-			ref="org.hisp.dhis.program.ProgramStageInstanceService" />
 	</bean>
 
 	<bean
@@ -652,8 +649,6 @@
 		<property name="patientService" ref="org.hisp.dhis.patient.PatientService" />
 		<property name="programService" ref="org.hisp.dhis.program.ProgramService" />
 		<property name="programInstanceService" ref="org.hisp.dhis.program.ProgramInstanceService" />
-		<property name="programStageInstanceService"
-			ref="org.hisp.dhis.program.ProgramStageInstanceService" />
 		<property name="selectedStateManager"
 			ref="org.hisp.dhis.caseentry.state.SelectedStateManager" />
 	</bean>
@@ -700,8 +695,6 @@
 		class="org.hisp.dhis.caseentry.action.patient.SetProgramInstanceStatusAction"
 		scope="prototype">
 		<property name="programInstanceService" ref="org.hisp.dhis.program.ProgramInstanceService" />
-		<property name="programStageInstanceService"
-			ref="org.hisp.dhis.program.ProgramStageInstanceService" />
 	</bean>
 
 	<bean id="org.hisp.dhis.caseentry.action.patient.GetProgramAction"