← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 2714: A better and much faster (but still not perfect) definition and implementation of activity plan.

 

------------------------------------------------------------
revno: 2714
committer: Jo Størset <storset@xxxxxxxxx>
branch nick: trunk
timestamp: Fri 2011-01-21 23:58:26 +0530
message:
  A better and much faster (but still not perfect) definition and implementation of activity plan. 
  Haven't changed the web visit plan for now, though I'm pretty sure it is wrong, as well.
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-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/service/ActivityReportingServiceImpl.java
  dhis-2/dhis-web/dhis-web-api/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/ProgramStageInstanceService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramStageInstanceService.java	2010-08-05 10:48:44 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramStageInstanceService.java	2011-01-21 18:28:26 +0000
@@ -28,8 +28,11 @@
 
 import java.util.Collection;
 import java.util.Date;
+import java.util.List;
 import java.util.Map;
 
+import org.hisp.dhis.organisationunit.OrganisationUnit;
+
 /**
  * @author Abyot Asalefew
  * @version $Id$
@@ -63,4 +66,14 @@
     Collection<ProgramStageInstance> getAllProgramStageInstances();     
     
     Map<Integer, String> colorProgramStageInstances( Collection<ProgramStageInstance> programStageInstances );
+
+    /** Get all {@link ProgramStageInstance program stage instances} for unit, optionally filtering by date or completed.
+     * @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. 
+     * @return
+     */
+    public List<ProgramStageInstance> get(OrganisationUnit unit, Date after, Date before, Boolean completed);
+
 }

=== 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	2010-08-05 10:48:44 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramStageInstanceStore.java	2011-01-21 18:28:26 +0000
@@ -28,8 +28,10 @@
 
 import java.util.Collection;
 import java.util.Date;
+import java.util.List;
 
 import org.hisp.dhis.common.GenericStore;
+import org.hisp.dhis.organisationunit.OrganisationUnit;
 
 /**
  * @author Abyot Asalefew
@@ -54,4 +56,13 @@
 
     Collection<ProgramStageInstance> getProgramStageInstances( Collection<ProgramInstance> programInstances );
 
+    /** 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 completed - optional flag to only get completed (<code>true</code>) or uncompleted (<code>false</code>) instances. 
+     * @return
+     */
+    public List<ProgramStageInstance> get(OrganisationUnit unit, Date after, Date before, Boolean completed);
+
 }

=== 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-08-05 10:48:44 +0000
+++ dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/program/DefaultProgramStageInstanceService.java	2011-01-21 18:28:26 +0000
@@ -30,8 +30,10 @@
 import java.util.Collection;
 import java.util.Date;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 
+import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.springframework.transaction.annotation.Transactional;
 
 /**
@@ -159,4 +161,8 @@
     {
         return programStageInstanceStore.getProgramStageInstances( startDate, endDate, completed );
     }
+
+    public List<ProgramStageInstance> get(OrganisationUnit unit, Date after, Date before, Boolean completed) {
+        return programStageInstanceStore.get( unit, after, before, 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	2010-08-10 10:35:30 +0000
+++ dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/program/hibernate/HibernateProgramStageInstanceStore.java	2011-01-21 18:28:26 +0000
@@ -28,15 +28,17 @@
 
 import java.util.Collection;
 import java.util.Date;
+import java.util.List;
 
+import org.hibernate.Query;
 import org.hibernate.criterion.Restrictions;
 import org.hisp.dhis.hibernate.HibernateGenericStore;
+import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.program.ProgramInstance;
+import org.hisp.dhis.program.ProgramStage;
 import org.hisp.dhis.program.ProgramStageInstance;
 import org.hisp.dhis.program.ProgramStageInstanceStore;
 
-import org.hisp.dhis.program.ProgramStage;
-
 /**
  * @author Abyot Asalefew
  * @version $Id$
@@ -93,4 +95,29 @@
             Restrictions.eq( "completed", completed ) )).list();
     }
 
+    
+    @SuppressWarnings( "unchecked" )
+    public List<ProgramStageInstance> get(OrganisationUnit unit, Date after, Date before, Boolean completed) {
+
+        String hql = "from ProgramStageInstance where programInstance.patient.organisationUnit = :unit";
+        
+        if (after != null)
+            hql += " and dueDate >= :after";
+        if (before != null) 
+            hql += " and dueDate <= :before";
+        if (completed != null)
+            hql += " and completed = :completed";
+        
+        Query q = getQuery( hql ).setEntity( "unit", unit );
+
+        if (after != null)
+            q.setDate( "after", after );
+        if (before != null)
+            q.setDate( "before", before );
+        if (completed != null)
+            q.setBoolean( "completed", completed );
+
+        return q.list();
+    }
+    
 }

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/service/ActivityReportingServiceImpl.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/service/ActivityReportingServiceImpl.java	2011-01-20 10:25:24 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/service/ActivityReportingServiceImpl.java	2011-01-21 18:28:26 +0000
@@ -38,8 +38,6 @@
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
-import org.hisp.dhis.activityplan.Activity;
-import org.hisp.dhis.activityplan.ActivityPlanService;
 import org.hisp.dhis.dataelement.DataElement;
 import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
@@ -54,6 +52,7 @@
 import org.hisp.dhis.patientdatavalue.PatientDataValue;
 import org.hisp.dhis.program.ProgramStageDataElement;
 import org.hisp.dhis.program.ProgramStageInstance;
+import org.hisp.dhis.web.api.model.Activity;
 import org.hisp.dhis.web.api.model.ActivityPlan;
 import org.hisp.dhis.web.api.model.ActivityValue;
 import org.hisp.dhis.web.api.model.Beneficiary;
@@ -80,8 +79,6 @@
     // Dependencies
     // -------------------------------------------------------------------------
 
-    private ActivityPlanService activityPlanService;
-
     private org.hisp.dhis.program.ProgramStageInstanceService programStageInstanceService;
 
     private PatientAttributeValueService patientAttValueService;
@@ -100,49 +97,27 @@
 
     public ActivityPlan getCurrentActivityPlan( OrganisationUnit unit, String localeString )
     {
+        long time = System.currentTimeMillis();
+
+        List<Activity> items = new ArrayList<Activity>();
+
         DateTime dt = new DateTime();
         DateMidnight from = dt.withDayOfMonth( 1 ).toDateMidnight();
         DateMidnight to = from.plusMonths( 1 );
 
-        Collection<Activity> allActivities = activityPlanService.getActivitiesByProvider( unit );
-
-        ActivityPlan plan = new ActivityPlan();
-
-        List<org.hisp.dhis.web.api.model.Activity> items = new ArrayList<org.hisp.dhis.web.api.model.Activity>();
-
-        if ( DEBUG )
-            log.debug( "Filtering through " + allActivities.size() + " activities" );
-
-        for ( Activity activity : allActivities )
-        {
-            long dueTime = activity.getDueDate().getTime();
-
-            if ( to.isBefore( dueTime ) )
-            {
-                continue;
-            }
-
-            if ( DEBUG )
-                log.debug( "Activity " + activity.getBeneficiary().getFirstName() + ", "
-                    + activity.getTask().getProgramStage().getName() );
-
-            if ( from.isBefore( dueTime ) )
-            {
-                items.add( getActivityModel( activity ) );
-            }
-            else if ( !activity.getTask().isCompleted() )
-            {
-                org.hisp.dhis.web.api.model.Activity a = getActivityModel( activity );
-                a.setLate( true );
-                items.add( a );
-            }
-
-            if ( items.size() > 10 )
-                break;
-        }
-
-        if ( DEBUG )
-            log.debug( "Found " + items.size() + " current activities" );
+        List<ProgramStageInstance> instances = programStageInstanceService.get( unit, from.toDate(), to.toDate(), null );
+
+        for ( ProgramStageInstance instance : instances )
+        {
+            items.add( getActivity( instance, false ) );
+        }
+
+        instances = programStageInstanceService.get( unit, null, from.minusDays( 1 ).toDate(), false );
+
+        for ( ProgramStageInstance instance : instances )
+        {
+            items.add( getActivity( instance, true ) );
+        }
 
         if ( items.isEmpty() )
         {
@@ -150,27 +125,96 @@
         }
 
         Collections.sort( items, activityComparator );
+
+        ActivityPlan plan = new ActivityPlan();
+
         plan.setActivitiesList( items );
 
+        if ( DEBUG )
+            log.debug( "Found " + items.size() + " current activities in " + (System.currentTimeMillis() - time)
+                + " ms." );
+
         return plan;
     }
 
-    private org.hisp.dhis.web.api.model.Activity getActivityModel( org.hisp.dhis.activityplan.Activity activity )
-    {
-        if ( activity == null )
+    // -------------------------------------------------------------------------
+    // DataValueService
+    // -------------------------------------------------------------------------
+
+    @Override
+    public void saveActivityReport( OrganisationUnit unit, ActivityValue activityValue )
+        throws NotAllowedException
+    {
+
+        ProgramStageInstance programStageInstance = programStageInstanceService.getProgramStageInstance( activityValue
+            .getProgramInstanceId() );
+
+        if ( programStageInstance == null )
+        {
+            throw NotAllowedException.INVALID_PROGRAM_STAGE;
+        }
+
+        programStageInstance.getProgramStage();
+        Collection<org.hisp.dhis.dataelement.DataElement> dataElements = new ArrayList<org.hisp.dhis.dataelement.DataElement>();
+
+        for ( ProgramStageDataElement de : programStageInstance.getProgramStage().getProgramStageDataElements() )
+        {
+            dataElements.add( de.getDataElement() );
+        }
+
+        programStageInstance.getProgramStage().getProgramStageDataElements();
+        Collection<Integer> dataElementIds = new ArrayList<Integer>( activityValue.getDataValues().size() );
+
+        for ( DataValue dv : activityValue.getDataValues() )
+        {
+            dataElementIds.add( dv.getId() );
+        }
+
+        if ( dataElements.size() != dataElementIds.size() )
+        {
+            throw NotAllowedException.INVALID_PROGRAM_STAGE;
+        }
+
+        Map<Integer, org.hisp.dhis.dataelement.DataElement> dataElementMap = new HashMap<Integer, org.hisp.dhis.dataelement.DataElement>();
+        for ( org.hisp.dhis.dataelement.DataElement dataElement : dataElements )
+        {
+            if ( !dataElementIds.contains( dataElement.getId() ) )
+            {
+                throw NotAllowedException.INVALID_PROGRAM_STAGE;
+            }
+            dataElementMap.put( dataElement.getId(), dataElement );
+        }
+
+        // Set ProgramStageInstance to completed
+        programStageInstance.setCompleted( true );
+        programStageInstanceService.updateProgramStageInstance( programStageInstance );
+        // Everything is fine, hence save
+        saveDataValues( activityValue, programStageInstance, dataElementMap, unit,
+            categoryService.getDefaultDataElementCategoryOptionCombo() );
+
+    }
+
+    // -------------------------------------------------------------------------
+    // Supportive method
+    // -------------------------------------------------------------------------
+
+    private Activity getActivity( ProgramStageInstance instance, boolean late )
+    {
+        if ( instance == null )
         {
             return null;
         }
-        org.hisp.dhis.web.api.model.Activity item = new org.hisp.dhis.web.api.model.Activity();
-        Patient patient = activity.getBeneficiary();
+        Activity activity = new Activity();
+        Patient patient = instance.getProgramInstance().getPatient();
 
-        item.setBeneficiary( getBeneficiaryModel( patient ) );
-        item.setDueDate( activity.getDueDate() );
-        item.setTask( getTask( activity.getTask() ) );
-        return item;
+        activity.setBeneficiary( getBeneficiaryModel( patient ) );
+        activity.setDueDate( instance.getDueDate() );
+        activity.setTask( getTask( instance ) );
+        activity.setLate( late );
+        return activity;
     }
 
-    public Task getTask( ProgramStageInstance stageInstance )
+    private Task getTask( ProgramStageInstance stageInstance )
     {
         if ( stageInstance == null )
         {
@@ -297,67 +341,6 @@
         return setting;
     }
 
-    // -------------------------------------------------------------------------
-    // DataValueService
-    // -------------------------------------------------------------------------
-
-    @Override
-    public void saveActivityReport( OrganisationUnit unit, ActivityValue activityValue )
-        throws NotAllowedException
-    {
-
-        ProgramStageInstance programStageInstance = programStageInstanceService.getProgramStageInstance( activityValue
-            .getProgramInstanceId() );
-
-        if ( programStageInstance == null )
-        {
-            throw NotAllowedException.INVALID_PROGRAM_STAGE;
-        }
-
-        programStageInstance.getProgramStage();
-        Collection<org.hisp.dhis.dataelement.DataElement> dataElements = new ArrayList<org.hisp.dhis.dataelement.DataElement>();
-
-        for ( ProgramStageDataElement de : programStageInstance.getProgramStage().getProgramStageDataElements() )
-        {
-            dataElements.add( de.getDataElement() );
-        }
-
-        programStageInstance.getProgramStage().getProgramStageDataElements();
-        Collection<Integer> dataElementIds = new ArrayList<Integer>( activityValue.getDataValues().size() );
-
-        for ( DataValue dv : activityValue.getDataValues() )
-        {
-            dataElementIds.add( dv.getId() );
-        }
-
-        if ( dataElements.size() != dataElementIds.size() )
-        {
-            throw NotAllowedException.INVALID_PROGRAM_STAGE;
-        }
-
-        Map<Integer, org.hisp.dhis.dataelement.DataElement> dataElementMap = new HashMap<Integer, org.hisp.dhis.dataelement.DataElement>();
-        for ( org.hisp.dhis.dataelement.DataElement dataElement : dataElements )
-        {
-            if ( !dataElementIds.contains( dataElement.getId() ) )
-            {
-                throw NotAllowedException.INVALID_PROGRAM_STAGE;
-            }
-            dataElementMap.put( dataElement.getId(), dataElement );
-        }
-
-        // Set ProgramStageInstance to completed
-        programStageInstance.setCompleted( true );
-        programStageInstanceService.updateProgramStageInstance( programStageInstance );
-        // Everything is fine, hence save
-        saveDataValues( activityValue, programStageInstance, dataElementMap, unit,
-            categoryService.getDefaultDataElementCategoryOptionCombo() );
-
-    }
-
-    // -------------------------------------------------------------------------
-    // Supportive method
-    // -------------------------------------------------------------------------
-
     private void saveDataValues( ActivityValue activityValue, ProgramStageInstance programStageInstance,
         Map<Integer, DataElement> dataElementMap, OrganisationUnit orgUnit, DataElementCategoryOptionCombo optionCombo )
     {
@@ -428,12 +411,6 @@
     }
 
     @Required
-    public void setActivityPlanService( ActivityPlanService activityPlanService )
-    {
-        this.activityPlanService = activityPlanService;
-    }
-
-    @Required
     public void setPatientAttValueService( PatientAttributeValueService patientAttValueService )
     {
         this.patientAttValueService = patientAttValueService;

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/resources/META-INF/dhis/beans.xml'
--- dhis-2/dhis-web/dhis-web-api/src/main/resources/META-INF/dhis/beans.xml	2011-01-18 17:44:59 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/resources/META-INF/dhis/beans.xml	2011-01-21 18:28:26 +0000
@@ -32,7 +32,6 @@
   </bean>
 
   <bean id="org.hisp.dhis.web.api.service.ActivityReportingService" class="org.hisp.dhis.web.api.service.ActivityReportingServiceImpl">
-    <property name="activityPlanService" ref="org.hisp.dhis.activityplan.ActivityPlanService" />
     <property name="patientAttValueService" ref="org.hisp.dhis.patientattributevalue.PatientAttributeValueService" />
     <property name="patientAttService" ref="org.hisp.dhis.patient.PatientAttributeService" />
     <property name="programStageInstanceService" ref="org.hisp.dhis.program.ProgramStageInstanceService" />