← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 1890: Bug fix in program stage sorting

 

------------------------------------------------------------
revno: 1890
committer: Abyot Asalefew Gizaw <abyota@xxxxxxxxx>
branch nick: cbhis-mobile
timestamp: Thu 2010-08-05 12:48:44 +0200
message:
  Bug fix in program stage sorting
  New API for activityplan service
added:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/activityplan/
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/activityplan/Activity.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/activityplan/ActivityPlanService.java
  dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/activityplan/
  dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/activityplan/DefaultActivityPlanService.java
modified:
  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/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/visitplan/VisitPlanAction.java
  dhis-2/dhis-web/dhis-web-caseentry/src/main/resources/META-INF/dhis/beans.xml
  dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/java/org/hisp/dhis/patient/action/programstage/SaveProgramStageSortOrderAction.java
  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/sortProgramStageForm.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
=== added directory 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/activityplan'
=== added file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/activityplan/Activity.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/activityplan/Activity.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/activityplan/Activity.java	2010-08-05 10:48:44 +0000
@@ -0,0 +1,98 @@
+/**
+ * 
+ */
+package org.hisp.dhis.activityplan;
+
+import java.util.Date;
+
+import org.hisp.dhis.organisationunit.OrganisationUnit;
+import org.hisp.dhis.patient.Patient;
+import org.hisp.dhis.program.ProgramStageInstance;
+
+/**
+ * @author abyotag_adm
+ * 
+ */
+public class Activity
+{
+    private OrganisationUnit provider;
+
+    private Patient beneficiary;
+
+    private ProgramStageInstance task;
+
+    private Date dueDate;
+
+    // -------------------------------------------------------------------------
+    // Constructors
+    // -------------------------------------------------------------------------
+
+    public Activity()
+    {
+    }
+
+    /**
+     * @return the provider
+     */
+    public OrganisationUnit getProvider()
+    {
+        return provider;
+    }
+
+    /**
+     * @param provider the provider to set
+     */
+    public void setProvider( OrganisationUnit provider )
+    {
+        this.provider = provider;
+    }
+
+    /**
+     * @return the beneficiary
+     */
+    public Patient getBeneficiary()
+    {
+        return beneficiary;
+    }
+
+    /**
+     * @param beneficiary the beneficiary to set
+     */
+    public void setBeneficiary( Patient beneficiary )
+    {
+        this.beneficiary = beneficiary;
+    }
+
+    /**
+     * @return the task
+     */
+    public ProgramStageInstance getTask()
+    {
+        return task;
+    }
+
+    /**
+     * @param task the task to set
+     */
+    public void setTask( ProgramStageInstance task )
+    {
+        this.task = task;
+    }
+    
+    /**
+     * @param dueDate the dueDate to set
+     */
+    public void setDueDate( Date dueDate )
+    {
+        this.dueDate = dueDate;
+    }
+
+    /**
+     * @return the dueDate
+     */
+    public Date getDueDate()
+    {
+        return dueDate;
+    }
+
+}

=== added file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/activityplan/ActivityPlanService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/activityplan/ActivityPlanService.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/activityplan/ActivityPlanService.java	2010-08-05 10:48:44 +0000
@@ -0,0 +1,31 @@
+/**
+ * 
+ */
+package org.hisp.dhis.activityplan;
+
+import java.util.Collection;
+import java.util.Date;
+
+import org.hisp.dhis.organisationunit.OrganisationUnit;
+import org.hisp.dhis.patient.Patient;
+import org.hisp.dhis.program.ProgramStageInstance;
+
+/**
+ * @author abyotag_adm
+ *
+ */
+public interface ActivityPlanService
+{
+    String ID = ActivityPlanService.class.getName();
+    
+    Collection<Activity> getActivitiesByProvider( OrganisationUnit organisationUnit );
+    
+    Collection<Activity> getActivitiesByBeneficiary( Patient beneficiary );
+    
+    Collection<Activity> getActivitiesByTask( ProgramStageInstance task );
+    
+    Collection<Activity> getActivitiesByDueDate( Date dueDate );
+    
+    Collection<Activity> getActivitiesWithInDate( Date startDate, Date endDate );    
+
+}

=== 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	2009-11-12 16:47:23 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramStageInstanceService.java	2010-08-05 10:48:44 +0000
@@ -27,6 +27,7 @@
 package org.hisp.dhis.program;
 
 import java.util.Collection;
+import java.util.Date;
 import java.util.Map;
 
 /**
@@ -49,6 +50,16 @@
     
     Collection<ProgramStageInstance> getProgramStageInstances( ProgramStage programStage );
     
+    Collection<ProgramStageInstance> getProgramStageInstances( Collection<ProgramInstance> programInstances );
+    
+    Collection<ProgramStageInstance> getProgramStageInstances( Date dueDate );
+    
+    Collection<ProgramStageInstance> getProgramStageInstances( Date dueDate, Boolean completed );
+    
+    Collection<ProgramStageInstance> getProgramStageInstances( Date startDate, Date endDate );
+    
+    Collection<ProgramStageInstance> getProgramStageInstances( Date startDate, Date endDate, Boolean completed );
+    
     Collection<ProgramStageInstance> getAllProgramStageInstances();     
     
     Map<Integer, String> colorProgramStageInstances( Collection<ProgramStageInstance> programStageInstances );

=== 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	2009-11-20 07:45:12 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramStageInstanceStore.java	2010-08-05 10:48:44 +0000
@@ -27,6 +27,7 @@
 package org.hisp.dhis.program;
 
 import java.util.Collection;
+import java.util.Date;
 
 import org.hisp.dhis.common.GenericStore;
 
@@ -37,10 +38,20 @@
 public interface ProgramStageInstanceStore
     extends GenericStore<ProgramStageInstance>
 {
-    String ID = ProgramStageInstanceStore.class.getName();   
-    
-    ProgramStageInstance get( ProgramInstance programInstance, ProgramStage programStage );   
-    
-    Collection<ProgramStageInstance> get( ProgramStage programStage );   
-    
+    String ID = ProgramStageInstanceStore.class.getName();
+
+    ProgramStageInstance get( ProgramInstance programInstance, ProgramStage programStage );
+
+    Collection<ProgramStageInstance> get( ProgramStage programStage );
+
+    Collection<ProgramStageInstance> getProgramStageInstances( Date dueDate );
+    
+    Collection<ProgramStageInstance> getProgramStageInstances( Date dueDate, Boolean completed );
+
+    Collection<ProgramStageInstance> getProgramStageInstances( Date startDate, Date endDate );
+    
+    Collection<ProgramStageInstance> getProgramStageInstances( Date startDate, Date endDate, Boolean completed );
+
+    Collection<ProgramStageInstance> getProgramStageInstances( Collection<ProgramInstance> programInstances );
+
 }

=== added directory 'dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/activityplan'
=== added file 'dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/activityplan/DefaultActivityPlanService.java'
--- dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/activityplan/DefaultActivityPlanService.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/activityplan/DefaultActivityPlanService.java	2010-08-05 10:48:44 +0000
@@ -0,0 +1,271 @@
+/**
+ * 
+ */
+package org.hisp.dhis.activityplan;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.hisp.dhis.organisationunit.OrganisationUnit;
+import org.hisp.dhis.patient.Patient;
+import org.hisp.dhis.patientdatavalue.PatientDataValue;
+import org.hisp.dhis.patientdatavalue.PatientDataValueService;
+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;
+
+/**
+ * @author abyotag_adm
+ * 
+ */
+public class DefaultActivityPlanService
+    implements ActivityPlanService
+{
+    // -------------------------------------------------------------------------
+    // Dependencies
+    // -------------------------------------------------------------------------
+
+    private ProgramService programService;
+
+    public void setProgramService( ProgramService programService )
+    {
+        this.programService = programService;
+    }
+
+    private ProgramInstanceService programInstanceService;
+
+    public void setProgramInstanceService( ProgramInstanceService programInstanceService )
+    {
+        this.programInstanceService = programInstanceService;
+    }
+
+    private PatientDataValueService patientDataValueService;
+
+    public void setPatientDataValueService( PatientDataValueService patientDataValueService )
+    {
+        this.patientDataValueService = patientDataValueService;
+    }
+
+    private ProgramStageInstanceService programStageInstanceService;
+
+    public void setProgramStageInstanceService( ProgramStageInstanceService programStageInstanceService )
+    {
+        this.programStageInstanceService = programStageInstanceService;
+    }
+
+    // -------------------------------------------------------------------------
+    // ActivityPlan
+    // -------------------------------------------------------------------------
+
+    @Override
+    public Collection<Activity> getActivitiesByBeneficiary( Patient beneficiary )
+    {
+
+        // ---------------------------------------------------------------------
+        // Get any active program for the beneficiary ( completed = false )
+        // ---------------------------------------------------------------------
+
+        Collection<ProgramInstance> programInstances = programInstanceService.getProgramInstances( beneficiary, false );
+
+        // ---------------------------------------------------------------------
+        // Get next activities for the active programInstances
+        // ---------------------------------------------------------------------
+
+        return getActivties( programInstances );
+
+    }
+
+    @Override
+    public Collection<Activity> getActivitiesByProvider( OrganisationUnit organisationUnit )
+    {
+        Collection<Activity> activities = new ArrayList<Activity>();
+
+        Collection<Program> programs = programService.getPrograms( organisationUnit );
+
+        if ( programs.size() > 0 )
+        {
+            // -----------------------------------------------------------------
+            // For all the programs a facility is servicing get active programs,
+            // those with active instances (completed = false)
+            // -----------------------------------------------------------------
+
+            Collection<ProgramInstance> programInstances = programInstanceService.getProgramInstances( programs, false );
+
+            // -----------------------------------------------------------------
+            // Get next activities for the active programInstances
+            // -----------------------------------------------------------------
+
+            activities = getActivties( programInstances );
+        }
+
+        return activities;
+    }
+
+    @Override
+    public Collection<Activity> getActivitiesByTask( ProgramStageInstance task )
+    {
+        // ---------------------------------------------------------------------
+        // Get the parent program for the given program stage
+        // ---------------------------------------------------------------------
+
+        Program program = task.getProgramInstance().getProgram();
+
+        // ---------------------------------------------------------------------
+        // Pick only those active instances for the identified program
+        // ---------------------------------------------------------------------
+
+        Collection<ProgramInstance> programInstances = programInstanceService.getProgramInstances( program, false );
+
+        // ---------------------------------------------------------------------
+        // Get next activities for the active programInstances
+        // ---------------------------------------------------------------------
+
+        return getActivties( programInstances );
+    }
+
+    @Override
+    public Collection<Activity> getActivitiesByDueDate( Date dueDate )
+    {
+        // ---------------------------------------------------------------------
+        // Get all active stageInstances within the given due date
+        // ---------------------------------------------------------------------
+
+        Collection<ProgramStageInstance> programStageInstances = programStageInstanceService.getProgramStageInstances(
+            dueDate, false );
+
+        Collection<Activity> activities = new ArrayList<Activity>();
+
+        for ( ProgramStageInstance programStageInstance : programStageInstances )
+        {
+
+            Activity activity = new Activity();
+            activity.setBeneficiary( programStageInstance.getProgramInstance().getPatient() );
+            activity.setTask( programStageInstance );
+            activity.setDueDate( programStageInstance.getDueDate() );
+            activities.add( activity );
+        }
+
+        return activities;
+    }
+
+    @Override
+    public Collection<Activity> getActivitiesWithInDate( Date startDate, Date endDate )
+    {
+        // ---------------------------------------------------------------------
+        // Get all active stageInstances within the given time frame
+        // ---------------------------------------------------------------------
+
+        Collection<ProgramStageInstance> programStageInstances = programStageInstanceService.getProgramStageInstances(
+            startDate, endDate, false );
+
+        Collection<Activity> activities = new ArrayList<Activity>();
+
+        for ( ProgramStageInstance programStageInstance : programStageInstances )
+        {
+
+            Activity activity = new Activity();
+            activity.setBeneficiary( programStageInstance.getProgramInstance().getPatient() );
+            activity.setTask( programStageInstance );
+            activity.setDueDate( programStageInstance.getDueDate() );
+            activities.add( activity );
+        }
+
+        return activities;
+
+    }
+
+    // -------------------------------------------------------------------------
+    // Supportive methods
+    // -------------------------------------------------------------------------
+
+    Collection<Activity> getActivties( Collection<ProgramInstance> programInstances )
+    {
+        Collection<Activity> activities = new ArrayList<Activity>();
+
+        // ---------------------------------------------------------------------
+        // Get all stageInstances for the give programInstances
+        // ---------------------------------------------------------------------
+
+        Collection<ProgramStageInstance> programStageInstances = programStageInstanceService
+            .getProgramStageInstances( programInstances );
+
+        Map<String, ProgramStageInstance> mappedStageInstance = new HashMap<String, ProgramStageInstance>();
+
+        for ( ProgramStageInstance programStageInstance : programStageInstances )
+        {
+            mappedStageInstance.put( programStageInstance.getProgramInstance().getId() + "_"
+                + programStageInstance.getProgramStage().getId(), programStageInstance );
+
+        }
+
+        // -----------------------------------------------------------------
+        // Initially assume to have a first visit for all programInstances
+        // -----------------------------------------------------------------
+
+        Map<Integer, Integer> visitsByProgramInstances = new HashMap<Integer, Integer>();
+
+        for ( ProgramInstance programInstance : programInstances )
+        {
+            programStageInstances.addAll( programInstance.getProgramStageInstances() );
+
+            visitsByProgramInstances.put( programInstance.getId(), 0 );
+        }
+
+        // ---------------------------------------------------------------------
+        // For each of these active instances, see at which stage they are
+        // actually (may not necessarily be at the first stage)
+        // ---------------------------------------------------------------------
+
+        Collection<PatientDataValue> patientDataValues = patientDataValueService
+            .getPatientDataValues( programStageInstances );
+
+        for ( PatientDataValue patientDataValue : patientDataValues )
+        {
+            if ( visitsByProgramInstances.get( patientDataValue.getProgramStageInstance().getProgramInstance().getId() ) < patientDataValue
+                .getProgramStageInstance().getProgramStage().getStageInProgram() )
+            {
+                visitsByProgramInstances.put( patientDataValue.getProgramStageInstance().getProgramInstance().getId(),
+                    patientDataValue.getProgramStageInstance().getProgramStage().getStageInProgram() );
+            }
+        }
+
+        // ---------------------------------------------------------------------
+        // For each of these active instances, based on the current stage
+        // determine the next stage
+        // ---------------------------------------------------------------------
+
+        for ( ProgramInstance programInstance : programInstances )
+        {
+
+            Program program = programInstance.getProgram();
+
+            ProgramStage nextStage = program.getProgramStageByStage( visitsByProgramInstances.get( programInstance
+                .getId() ) + 1 );
+
+            if ( nextStage != null )
+            {
+
+                ProgramStageInstance nextStageInstance = mappedStageInstance.get( programInstance.getId() + "_"
+                    + nextStage.getId() );
+
+                Activity activity = new Activity();
+                activity.setBeneficiary( programInstance.getPatient() );
+                activity.setTask( nextStageInstance );
+                activity.setDueDate( nextStageInstance.getDueDate() );
+
+                activities.add( activity );
+            }
+        }
+
+        return activities;
+
+    }
+
+}

=== 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	2010-06-20 08:20:32 +0000
+++ dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/program/DefaultProgramStageInstanceService.java	2010-08-05 10:48:44 +0000
@@ -103,9 +103,9 @@
             {
                 colorMap.put( programStageInstance.getId(), ProgramStageInstance.COLOR_GREEN );
             }
-            else if( programStageInstance.getExecutionDate() != null )
+            else if ( programStageInstance.getExecutionDate() != null )
             {
-            	colorMap.put( programStageInstance.getId(), ProgramStageInstance.COLOR_LIGHTRED );
+                colorMap.put( programStageInstance.getId(), ProgramStageInstance.COLOR_LIGHTRED );
             }
             else
             {
@@ -132,4 +132,31 @@
 
         return colorMap;
     }
+
+    public Collection<ProgramStageInstance> getProgramStageInstances( Collection<ProgramInstance> programInstances )
+    {
+
+        return programStageInstanceStore.getProgramStageInstances( programInstances );
+
+    }
+
+    public Collection<ProgramStageInstance> getProgramStageInstances( Date dueDate )
+    {
+        return programStageInstanceStore.getProgramStageInstances( dueDate );
+    }    
+    
+    public Collection<ProgramStageInstance> getProgramStageInstances( Date dueDate, Boolean completed )
+    {
+        return programStageInstanceStore.getProgramStageInstances( dueDate, completed );
+    }
+    
+    public Collection<ProgramStageInstance> getProgramStageInstances( Date startDate, Date endDate )
+    {
+        return programStageInstanceStore.getProgramStageInstances( startDate, endDate );
+    }
+    
+    public Collection<ProgramStageInstance> getProgramStageInstances( Date startDate, Date endDate, Boolean completed )
+    {
+        return programStageInstanceStore.getProgramStageInstances( startDate, endDate, completed );
+    }
 }

=== 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	2009-11-20 07:45:12 +0000
+++ dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/program/hibernate/HibernateProgramStageInstanceStore.java	2010-08-05 10:48:44 +0000
@@ -27,6 +27,7 @@
 package org.hisp.dhis.program.hibernate;
 
 import java.util.Collection;
+import java.util.Date;
 
 import org.hibernate.criterion.Restrictions;
 import org.hisp.dhis.hibernate.HibernateGenericStore;
@@ -43,16 +44,52 @@
 public class HibernateProgramStageInstanceStore
     extends HibernateGenericStore<ProgramStageInstance>
     implements ProgramStageInstanceStore
-{    
+{
+
     public ProgramStageInstance get( ProgramInstance programInstance, ProgramStage programStage )
     {
         return (ProgramStageInstance) getCriteria( Restrictions.eq( "programInstance", programInstance ),
             Restrictions.eq( "programStage", programStage ) ).uniqueResult();
     }
-    
+
     @SuppressWarnings( "unchecked" )
     public Collection<ProgramStageInstance> get( ProgramStage programStage )
     {
         return getCriteria( Restrictions.eq( "programStage", programStage ) ).list();
     }
+
+    @SuppressWarnings( "unchecked" )
+    public Collection<ProgramStageInstance> getProgramStageInstances( Collection<ProgramInstance> programInstances )
+    {
+
+        return getCriteria( Restrictions.in( "programInstance", programInstances ) ).list();
+
+    }
+
+    @SuppressWarnings( "unchecked" )
+    public Collection<ProgramStageInstance> getProgramStageInstances( Date dueDate )
+    {
+        return getCriteria( Restrictions.eq( "dueDate", dueDate ) ).list();
+    }
+
+    @SuppressWarnings( "unchecked" )
+    public Collection<ProgramStageInstance> getProgramStageInstances( Date dueDate, Boolean completed )
+    {
+        return getCriteria( Restrictions.eq( "dueDate", dueDate ), Restrictions.eq( "completed", completed ) ).list();
+    }
+
+    @SuppressWarnings( "unchecked" )
+    public Collection<ProgramStageInstance> getProgramStageInstances( Date startDate, Date endDate )
+    {
+
+        return (getCriteria( Restrictions.ge( "startDate", startDate ), Restrictions.le( "endDate", endDate ) )).list();
+    }
+
+    @SuppressWarnings( "unchecked" )
+    public Collection<ProgramStageInstance> getProgramStageInstances( Date startDate, Date endDate, Boolean completed )
+    {
+
+        return (getCriteria( Restrictions.ge( "startDate", startDate ), Restrictions.le( "endDate", endDate ),
+            Restrictions.eq( "completed", completed ) )).list();
+    }
 }

=== 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	2010-05-27 08:42:06 +0000
+++ dhis-2/dhis-services/dhis-service-patient/src/main/resources/META-INF/dhis/beans.xml	2010-08-05 10:48:44 +0000
@@ -103,6 +103,18 @@
 
 	<!-- Service definitions -->
 	
+	<bean id="org.hisp.dhis.activityplan.ActivityPlanService"
+		class="org.hisp.dhis.activityplan.DefaultActivityPlanService">
+		<property name="patientDataValueService"
+			ref="org.hisp.dhis.patientdatavalue.PatientDataValueService" />
+		<property name="programStageInstanceService"
+			ref="org.hisp.dhis.program.ProgramStageInstanceService" />
+		<property name="programInstanceService"
+			ref="org.hisp.dhis.program.ProgramInstanceService" />
+		<property name="programService"
+			ref="org.hisp.dhis.program.ProgramService" />
+	</bean>
+	
 	<bean id="org.hisp.dhis.caseaggregation.CaseAggregationMappingService"
     	class="org.hisp.dhis.caseaggregation.DefaultCaseAggregationMappingService">
         <property name="caseAggregationMappingStore"

=== modified file 'dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/visitplan/VisitPlanAction.java'
--- dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/visitplan/VisitPlanAction.java	2009-11-24 16:26:10 +0000
+++ dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/visitplan/VisitPlanAction.java	2010-08-05 10:48:44 +0000
@@ -29,6 +29,7 @@
 
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.HashSet;
 import java.util.Map;
 import java.util.HashMap;
 import java.util.Set;
@@ -41,12 +42,9 @@
 import org.hisp.dhis.patient.PatientService;
 import org.hisp.dhis.patientattributevalue.PatientAttributeValue;
 import org.hisp.dhis.patientattributevalue.PatientAttributeValueService;
-import org.hisp.dhis.program.ProgramInstance;
-import org.hisp.dhis.program.ProgramInstanceService;
-import org.hisp.dhis.program.Program;
 import org.hisp.dhis.program.ProgramStageInstance;
-import org.hisp.dhis.program.ProgramService;
-import org.hisp.dhis.program.nextvisit.NextVisitGenerator;
+import org.hisp.dhis.activityplan.Activity;
+import org.hisp.dhis.activityplan.ActivityPlanService;
 
 import com.opensymphony.xwork2.Action;
 
@@ -73,21 +71,7 @@
     public void setPatientService( PatientService patientService )
     {
         this.patientService = patientService;
-    }
-
-    private ProgramService programService;
-
-    public void setProgramService( ProgramService programService )
-    {
-        this.programService = programService;
-    }
-
-    private ProgramInstanceService programInstanceService;
-
-    public void setProgramInstanceService( ProgramInstanceService programInstanceService )
-    {
-        this.programInstanceService = programInstanceService;
-    }
+    }    
 
     private PatientAttributeValueService patientAttributeValueService;
 
@@ -102,12 +86,12 @@
     {
         this.patientAttributeService = patientAttributeService;
     }
-
-    private NextVisitGenerator nextVisitGenerator;
-
-    public void setNextVisitGenerator( NextVisitGenerator nextVisitGenerator )
+    
+    private ActivityPlanService activityPlanService;
+
+    public void setActivityPlanService( ActivityPlanService activityPlanService )
     {
-        this.nextVisitGenerator = nextVisitGenerator;
+        this.activityPlanService = activityPlanService;
     }
 
     // -------------------------------------------------------------------------
@@ -168,6 +152,13 @@
         return sortedPatients;
     }
 
+    private Collection<Activity> activities = new ArrayList<Activity>();
+
+    public Collection<Activity> getActivities()
+    {
+        return activities;
+    }
+
     // -------------------------------------------------------------------------
     // Action implementation
     // -------------------------------------------------------------------------
@@ -188,64 +179,57 @@
 
         organisationUnit = selectionManager.getSelectedOrganisationUnit();
 
-        // ---------------------------------------------------------------------
-        // Get all the programs the facility is providing
-        // ---------------------------------------------------------------------
-
-        Collection<Program> programs = programService.getPrograms( organisationUnit );
-        
-
-        if ( programs.size() > 0 )
-        {
-
-            // -----------------------------------------------------------------
-            // For all the programs a facility is servicing get the active
-            // instances completed = false
-            // -----------------------------------------------------------------
-
-            Collection<ProgramInstance> programInstances = programInstanceService.getProgramInstances( programs, false );
-
-            // -----------------------------------------------------------------
-            // For all the active program instances determine the next visits
-            // and group these visits based on the patient to be visited
-            // -----------------------------------------------------------------
-
-            visitsByPatients = nextVisitGenerator.getNextVisits( programInstances );
-
-            if ( !visitsByPatients.keySet().isEmpty() )
-            {
-                Collection<Patient> patientsToBeVisted = visitsByPatients.keySet();
-
-                // -------------------------------------------------------------
-                // Get all the attributes of the patients to be visited (in case
-                // users want to make sorting based on attributes
-                // -------------------------------------------------------------
-
-                attributeValueMap = patientAttributeValueService
-                    .getPatientAttributeValueMapForPatients( patientsToBeVisted );
-
-                // -------------------------------------------------------------
-                // Sort patients to be visited based on the chosen attribute
-                // -------------------------------------------------------------
-
-                if ( attributes.size() > 0 )
-                {
-                    sortingAttribute = attributes.iterator().next();
-                }
-
-                if ( sortingAttributeId != null )
-                {
-                    sortingAttribute = patientAttributeService.getPatientAttribute( sortingAttributeId );
-                }
-
-                if ( sortingAttribute != null )
-                {
-                    sortedPatients = patientService.sortPatientsByAttribute( patientsToBeVisted, sortingAttribute );
-                }
-                else
-                {
-                    sortedPatients = patientsToBeVisted;
-                }
+        activities = activityPlanService.getActivitiesByProvider( organisationUnit );
+
+        for ( Activity activity : activities )
+        {
+            if ( visitsByPatients.containsKey( activity.getBeneficiary() ) )
+            {
+                visitsByPatients.get( activity.getBeneficiary() ).add( activity.getTask() );
+            }
+            else
+            {
+                Set<ProgramStageInstance> programStageInstancess = new HashSet<ProgramStageInstance>();
+
+                programStageInstancess.add( activity.getTask() );
+
+                visitsByPatients.put( activity.getBeneficiary(), programStageInstancess );
+            }
+        }
+
+        if ( !visitsByPatients.keySet().isEmpty() )
+        {
+            Collection<Patient> patientsToBeVisted = visitsByPatients.keySet();
+
+            // -------------------------------------------------------------
+            // Get all the attributes of the patients to be visited (in case
+            // users want to make sorting based on attributes
+            // -------------------------------------------------------------
+
+            attributeValueMap = patientAttributeValueService
+                .getPatientAttributeValueMapForPatients( patientsToBeVisted );
+
+            // -------------------------------------------------------------
+            // Sort patients to be visited based on the chosen attribute
+            // -------------------------------------------------------------
+
+            if ( attributes.size() > 0 )
+            {
+                sortingAttribute = attributes.iterator().next();
+            }
+
+            if ( sortingAttributeId != null )
+            {
+                sortingAttribute = patientAttributeService.getPatientAttribute( sortingAttributeId );
+            }
+
+            if ( sortingAttribute != null )
+            {
+                sortedPatients = patientService.sortPatientsByAttribute( patientsToBeVisted, sortingAttribute );
+            }
+            else
+            {
+                sortedPatients = patientsToBeVisted;
             }
         }
 

=== 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	2010-07-06 09:34:47 +0000
+++ dhis-2/dhis-web/dhis-web-caseentry/src/main/resources/META-INF/dhis/beans.xml	2010-08-05 10:48:44 +0000
@@ -240,17 +240,13 @@
     <property name="selectionManager"
       ref="org.hisp.dhis.ouwt.manager.OrganisationUnitSelectionManager"/>
     <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="nextVisitGenerator"
-      ref="org.hisp.dhis.program.nextvisit.NextVisitGenerator"/>
+      ref="org.hisp.dhis.patient.PatientService"/>  
     <property name="patientAttributeValueService"
       ref="org.hisp.dhis.patientattributevalue.PatientAttributeValueService"/>
     <property name="patientAttributeService"
-      ref="org.hisp.dhis.patient.PatientAttributeService"/>          
+      ref="org.hisp.dhis.patient.PatientAttributeService"/>    
+	<property name="activityPlanService"
+      ref="org.hisp.dhis.activityplan.ActivityPlanService"/>        
   </bean>
   
   <!-- Report --> 

=== modified file 'dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/java/org/hisp/dhis/patient/action/programstage/SaveProgramStageSortOrderAction.java'
--- dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/java/org/hisp/dhis/patient/action/programstage/SaveProgramStageSortOrderAction.java	2009-10-22 07:37:47 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/java/org/hisp/dhis/patient/action/programstage/SaveProgramStageSortOrderAction.java	2010-08-05 10:48:44 +0000
@@ -58,11 +58,11 @@
     // Input
     // -------------------------------------------------------------------------
 
-    private List<String> programStageList;
+    private List<String> sortedList = new ArrayList<String>();
 
-    public void setprogramStageList( List<String> programStageList )
+    public void setSortedList( List<String> sortedList )
     {
-        this.programStageList = programStageList;
+        this.sortedList = sortedList;
     }
     
     private Integer id;
@@ -85,10 +85,11 @@
 
         int stageInProgram = 1;
         
-        List<ProgramStage> programStages = new ArrayList<ProgramStage>(programStageList.size());       
+        List<ProgramStage> programStages = new ArrayList<ProgramStage>( sortedList.size() );        
 
-        for ( String programStageId : programStageList )
+        for ( String programStageId : sortedList )
         {
+            
             ProgramStage programStage = programStageService.getProgramStage( Integer.parseInt( programStageId ) );
             
             programStages.add( programStage );

=== 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	2010-02-22 09:19:15 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/webapp/dhis-web-maintenance-patient/javascript/programStage.js	2010-08-05 10:48:44 +0000
@@ -43,6 +43,11 @@
 	}
 }
 
+function submitSortOderForm()
+{
+    selectAllById( 'sortedList' );
+    document.getElementById( 'sortProgramStageForm' ).submit();
+}
 
 //-----------------------------------------------------------------------------
 // Move members

=== modified file 'dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/webapp/dhis-web-maintenance-patient/sortProgramStageForm.vm'
--- dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/webapp/dhis-web-maintenance-patient/sortProgramStageForm.vm	2009-10-22 07:37:47 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/webapp/dhis-web-maintenance-patient/sortProgramStageForm.vm	2010-08-05 10:48:44 +0000
@@ -14,7 +14,7 @@
     </tr>        
     <tr>
         <td>
-            <select size="15" id="programStageList" name="programStageList" multiple="multiple" style="min-width:20em;height:20em">
+            <select size="15" id="sortedList" name="sortedList" multiple="multiple" style="min-width:20em;height:20em">
                 #foreach ( $programStage in $programStages )
                     <option value="$programStage.id">$encoder.htmlEncode( $programStage.name )</option>
                 #end                
@@ -22,8 +22,8 @@
         </td>
         
         <td valign="top" align="center">             
-          <a href="#" onclick="moveUp('programStageList')"><img src="../images/move_up.png" border="0" alt=""></a><br><br>
-          <a href="#" onclick="moveDown('programStageList')"><img src="../images/move_down.png" border="0" alt=""></a>        
+          <a href="#" onclick="moveUp('sortedList')"><img src="../images/move_up.png" border="0" alt=""></a><br><br>
+          <a href="#" onclick="moveDown('sortedList')"><img src="../images/move_down.png" border="0" alt=""></a>        
         </td>
           
     <tr>
@@ -36,13 +36,4 @@
 
 </form> 	
 
-<span id="message"></span>
-
-<script type="text/javascript">
-    function submitSortOderForm()
-    {
-        selectAllById( "programStageList" );
-        document.getElementById( "sortProgramStageForm" ).submit();
-    }
-</script>
-
+<span id="message"></span>
\ No newline at end of file