← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 11801: filter events by orgunit/program

 

------------------------------------------------------------
revno: 11801
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2013-08-26 15:01:59 +0200
message:
  filter events by orgunit/program
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-dxf2/src/main/java/org/hisp/dhis/dxf2/event/BaseEventService.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/EventService.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/api/controller/event/EventController.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/exception/NotFoundException.java


--
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	2013-08-23 15:56:19 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramStageInstanceService.java	2013-08-26 13:01:59 +0000
@@ -64,6 +64,8 @@
 
     Collection<ProgramStageInstance> getProgramStageInstances( ProgramStage programStage );
 
+    Collection<ProgramStageInstance> getProgramStageInstances( ProgramStage programStage, OrganisationUnit organisationUnit);
+
     Collection<ProgramStageInstance> getProgramStageInstances( Collection<ProgramInstance> programInstances );
 
     Collection<ProgramStageInstance> getProgramStageInstances( Date dueDate );

=== 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-08-23 15:56:19 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramStageInstanceStore.java	2013-08-26 13:01:59 +0000
@@ -81,6 +81,8 @@
 
     List<ProgramStageInstance> get( Patient patient, Boolean completed );
 
+    List<ProgramStageInstance> get( ProgramStage programStage, OrganisationUnit orgunit );
+
     List<ProgramStageInstance> get( ProgramStage programStage, OrganisationUnit orgunit, Date startDate, Date endDate,
         int min, int max );
 
@@ -126,7 +128,7 @@
     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-dxf2/src/main/java/org/hisp/dhis/dxf2/event/BaseEventService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/BaseEventService.java	2013-08-26 11:46:08 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/BaseEventService.java	2013-08-26 13:01:59 +0000
@@ -344,12 +344,28 @@
     @Override
     public Events getEvents( Program program )
     {
+        return getEvents( program, null );
+    }
+
+    @Override
+    public Events getEvents( Program program, OrganisationUnit organisationUnit )
+    {
         Events events = new Events();
 
         ProgramStage programStage = program.getProgramStageByStage( 1 );
 
-        List<ProgramStageInstance> programStageInstances =
-            new ArrayList<ProgramStageInstance>( programStageInstanceService.getProgramStageInstances( programStage ) );
+        List<ProgramStageInstance> programStageInstances = null;
+
+        if ( organisationUnit != null )
+        {
+            programStageInstances = new ArrayList<ProgramStageInstance>(
+                programStageInstanceService.getProgramStageInstances( programStage, organisationUnit ) );
+        }
+        else
+        {
+            programStageInstances = new ArrayList<ProgramStageInstance>(
+                programStageInstanceService.getProgramStageInstances( programStage ) );
+        }
 
         List<Event> convertedEvents = convertProgramStageInstances( programStageInstances );
 

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/EventService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/EventService.java	2013-08-26 11:46:08 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/EventService.java	2013-08-26 13:01:59 +0000
@@ -30,6 +30,7 @@
 
 import org.hisp.dhis.dxf2.importsummary.ImportSummaries;
 import org.hisp.dhis.dxf2.importsummary.ImportSummary;
+import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.program.Program;
 import org.hisp.dhis.program.ProgramStageInstance;
 
@@ -51,6 +52,8 @@
 
     Events getEvents( Program program );
 
+    Events getEvents( Program program, OrganisationUnit organisationUnit );
+
     Event getEvent( String uid );
 
     Event getEvent( ProgramStageInstance programStageInstance );

=== 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-08-23 16:05:01 +0000
+++ dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/program/DefaultProgramStageInstanceService.java	2013-08-26 13:01:59 +0000
@@ -28,15 +28,6 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Date;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-
 import org.hisp.dhis.common.Grid;
 import org.hisp.dhis.common.GridHeader;
 import org.hisp.dhis.dataelement.DataElement;
@@ -59,6 +50,15 @@
 import org.hisp.dhis.user.CurrentUserService;
 import org.springframework.transaction.annotation.Transactional;
 
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
 /**
  * @author Abyot Asalefew
  * @version $Id$
@@ -160,6 +160,12 @@
         return programStageInstanceStore.get( programStage );
     }
 
+    @Override
+    public Collection<ProgramStageInstance> getProgramStageInstances( ProgramStage programStage, OrganisationUnit organisationUnit )
+    {
+        return programStageInstanceStore.get( programStage, organisationUnit );
+    }
+
     public void updateProgramStageInstance( ProgramStageInstance programStageInstance )
     {
         programStageInstance.setAutoFields();
@@ -579,7 +585,7 @@
                 }
             }
         }
-        
+
         return outboundSmsList;
     }
 
@@ -587,7 +593,7 @@
     {
         return programStageInstanceStore.get( patient );
     }
-    
+
     // -------------------------------------------------------------------------
     // Supportive methods
     // -------------------------------------------------------------------------

=== 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-08-23 16:05:01 +0000
+++ dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/program/hibernate/HibernateProgramStageInstanceStore.java	2013-08-26 13:01:59 +0000
@@ -28,20 +28,6 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import java.sql.ResultSet;
-import java.sql.SQLException;
-import java.sql.Types;
-import java.util.ArrayList;
-import java.util.Calendar;
-import java.util.Collection;
-import java.util.Date;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-
 import org.hibernate.Criteria;
 import org.hibernate.Query;
 import org.hibernate.criterion.Order;
@@ -84,6 +70,20 @@
 import org.springframework.jdbc.core.RowMapper;
 import org.springframework.jdbc.support.rowset.SqlRowSet;
 
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Types;
+import java.util.ArrayList;
+import java.util.Calendar;
+import java.util.Collection;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
 /**
  * @author Abyot Asalefew
  */
@@ -250,6 +250,13 @@
         return getQuery( hql ).setEntity( "patient", patient ).setBoolean( "completed", completed ).list();
     }
 
+    @Override
+    @SuppressWarnings( "unchecked" )
+    public List<ProgramStageInstance> get( ProgramStage programStage, OrganisationUnit organisationUnit )
+    {
+        return getCriteria( Restrictions.eq( "programStage", programStage ), Restrictions.eq( "organisationUnit", organisationUnit ) ).list();
+    }
+
     @SuppressWarnings( "unchecked" )
     public List<ProgramStageInstance> get( ProgramStage programStage, OrganisationUnit orgunit, Date startDate,
         Date endDate, int min, int max )
@@ -423,10 +430,10 @@
                 String programStageName = rs.getString( "programStageName" );
                 String daysSinceDueDate = rs.getString( "days_since_due_date" );
                 String dueDate = rs.getString( "duedate" ).split( " " )[0];// just
-                                                                           // get
-                                                                           // date,
-                                                                           // remove
-                                                                           // timestamp
+                // get
+                // date,
+                // remove
+                // timestamp
 
                 message = message.replace( PatientReminder.TEMPLATE_MESSSAGE_PATIENT_NAME, patientName );
                 message = message.replace( PatientReminder.TEMPLATE_MESSSAGE_PROGRAM_NAME, programName );
@@ -846,9 +853,9 @@
                 Restrictions.and( Restrictions.eq( "completed", false ), Restrictions.isNull( "executionDate" ),
                     Restrictions.between( "dueDate", startDate, endDate ),
                     Restrictions.in( "regOrgunit.id", orgunitIds ) ), Restrictions.and(
-                    Restrictions.eq( "status", ProgramStageInstance.SKIPPED_STATUS ),
-                    Restrictions.between( "dueDate", startDate, endDate ),
-                    Restrictions.in( "regOrgunit.id", orgunitIds ) ) ) );
+                Restrictions.eq( "status", ProgramStageInstance.SKIPPED_STATUS ),
+                Restrictions.between( "dueDate", startDate, endDate ),
+                Restrictions.in( "regOrgunit.id", orgunitIds ) ) ) );
         }
         else
         {
@@ -1065,7 +1072,7 @@
         sql += where; // filters
         sql = sql.substring( 0, sql.length() - 1 ) + " "; // Remove last comma
         sql += (min != null && max != null) ? statementBuilder.limitRecord( min, max ) : "";
-        
+
         return sql;
     }
 
@@ -1081,26 +1088,26 @@
 
         switch ( status )
         {
-        case ProgramStageInstance.COMPLETED_STATUS:
-            criteria.add( Restrictions.eq( "completed", true ) );
-            criteria.add( Restrictions.between( "executionDate", startDate, endDate ) );
-            break;
-        case ProgramStageInstance.VISITED_STATUS:
-            criteria.add( Restrictions.eq( "completed", false ) );
-            criteria.add( Restrictions.between( "executionDate", startDate, endDate ) );
-            break;
-        case ProgramStageInstance.FUTURE_VISIT_STATUS:
-            criteria.add( Restrictions.between( "programInstance.enrollmentDate", startDate, endDate ) );
-            criteria.add( Restrictions.isNull( "executionDate" ) );
-            criteria.add( Restrictions.ge( "dueDate", new Date() ) );
-            break;
-        case ProgramStageInstance.LATE_VISIT_STATUS:
-            criteria.add( Restrictions.between( "programInstance.enrollmentDate", startDate, endDate ) );
-            criteria.add( Restrictions.isNull( "executionDate" ) );
-            criteria.add( Restrictions.lt( "dueDate", new Date() ) );
-            break;
-        default:
-            break;
+            case ProgramStageInstance.COMPLETED_STATUS:
+                criteria.add( Restrictions.eq( "completed", true ) );
+                criteria.add( Restrictions.between( "executionDate", startDate, endDate ) );
+                break;
+            case ProgramStageInstance.VISITED_STATUS:
+                criteria.add( Restrictions.eq( "completed", false ) );
+                criteria.add( Restrictions.between( "executionDate", startDate, endDate ) );
+                break;
+            case ProgramStageInstance.FUTURE_VISIT_STATUS:
+                criteria.add( Restrictions.between( "programInstance.enrollmentDate", startDate, endDate ) );
+                criteria.add( Restrictions.isNull( "executionDate" ) );
+                criteria.add( Restrictions.ge( "dueDate", new Date() ) );
+                break;
+            case ProgramStageInstance.LATE_VISIT_STATUS:
+                criteria.add( Restrictions.between( "programInstance.enrollmentDate", startDate, endDate ) );
+                criteria.add( Restrictions.isNull( "executionDate" ) );
+                criteria.add( Restrictions.lt( "dueDate", new Date() ) );
+                break;
+            default:
+                break;
         }
 
         return criteria;
@@ -1110,8 +1117,7 @@
      * Aggregate report Position Orgunit Rows - Period Columns - Data Filter
      * Aggregate report Position Orgunit Columns - Period Rows - Data Filter
      * This result is not included orgunits without any data
-     * 
-     **/
+     */
     private String getAggregateReportSQL12( ProgramStage programStage, Collection<Integer> roots, String facilityLB,
         String filterSQL, Integer deGroupBy, Integer deSum, Collection<Period> periods, String aggregateType,
         Integer limit, Boolean useCompletedEvents, I18nFormat format )
@@ -1206,8 +1212,7 @@
 
     /**
      * Aggregate report Position Orgunit Rows - Period Rows - Data Filter
-     * 
-     **/
+     */
     private String getAggregateReportSQL3( int position, ProgramStage programStage, Collection<Integer> roots,
         String facilityLB, String filterSQL, Integer deGroupBy, Integer deSum, Collection<Period> periods,
         String aggregateType, Integer limit, Boolean useCompletedEvents, I18nFormat format )
@@ -1298,8 +1303,7 @@
 
     /**
      * Aggregate report Orgunit Filter - Period Rows - Data Filter
-     * 
-     **/
+     */
     private String getAggregateReportSQL4( int position, ProgramStage programStage, Collection<Integer> roots,
         String facilityLB, String filterSQL, Integer deGroupBy, Integer deSum, Collection<Period> periods,
         String aggregateType, Integer limit, Boolean useCompletedEvents, I18nFormat format )
@@ -1390,8 +1394,7 @@
 
     /**
      * Aggregate report Position Orgunit Rows -Period Filter - Data Filter
-     * 
-     **/
+     */
     private String getAggregateReportSQL5( int position, ProgramStage programStage, Collection<Integer> roots,
         String facilityLB, String filterSQL, Integer deGroupBy, Integer deSum, Period period, String aggregateType,
         Integer limit, Boolean useCompletedEvents, I18nFormat format )
@@ -1466,7 +1469,7 @@
     /**
      * Aggregate report Position Orgunit Filter - Period Rows - Data Columns
      * with group-by
-     **/
+     */
     private String getAggregateReportSQL6( ProgramStage programStage, Integer root, String facilityLB,
         String filterSQL, Integer deGroupBy, Integer deSum, Collection<Period> periods, String aggregateType,
         Integer limit, Boolean useCompletedEvents, I18nFormat format )
@@ -1593,7 +1596,7 @@
     /**
      * Aggregate report Position Orgunit Filter - Period Rows - Data Columns
      * without group-by
-     **/
+     */
     private String getAggregateReportSQL6WithoutGroup( ProgramStage programStage, Integer root, String facilityLB,
         String filterSQL, Integer deSum, Collection<Period> periods, String aggregateType, Integer limit,
         Boolean useCompletedEvents, I18nFormat format )
@@ -1674,8 +1677,7 @@
 
     /**
      * Aggregate report Position Orgunit Rows - Period Filter - Data Columns
-     * 
-     **/
+     */
     private String getAggregateReportSQL7( ProgramStage programStage, Collection<Integer> roots, String facilityLB,
         String filterSQL, Integer deGroupBy, Integer deSum, Period period, String aggregateType, Integer limit,
         Boolean useCompletedEvents, I18nFormat format )
@@ -1785,8 +1787,7 @@
 
     /**
      * Aggregate report Position Orgunit Rows - Period Filter - Data Columns
-     * 
-     **/
+     */
     private String getAggregateReportSQL7WithoutGroup( ProgramStage programStage, Collection<Integer> roots,
         String facilityLB, String filterSQL, Integer deSum, Period period, String aggregateType, Integer limit,
         Boolean useCompletedEvents, I18nFormat format )
@@ -1851,8 +1852,7 @@
 
     /**
      * Aggregate report Position Data Rows
-     * 
-     **/
+     */
     private String getAggregateReportSQL8( ProgramStage programStage, Collection<Integer> roots, String facilityLB,
         String filterSQL, Integer deGroupBy, Period period, String aggregateType, Integer limit,
         Boolean useCompletedEvents, I18nFormat format )
@@ -1904,7 +1904,7 @@
     /**
      * Aggregate report Position Orgunit Filter - Period Columns - Data Rows
      * with group-by
-     **/
+     */
     private String getAggregateReportSQL9( ProgramStage programStage, Integer root, String facilityLB,
         String filterSQL, Integer deGroupBy, Integer deSum, Collection<Period> periods, String aggregateType,
         Integer limit, Boolean useCompletedEvents, Boolean useFormNameDataElement, I18nFormat format )
@@ -2193,7 +2193,6 @@
 
     /**
      * Return the Ids of organisation units which events happened.
-     * 
      */
     private Collection<Integer> getServiceOrgunit( Collection<Integer> orgunitIds, Period period )
     {

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/event/EventController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/event/EventController.java	2013-08-26 11:46:08 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/event/EventController.java	2013-08-26 13:01:59 +0000
@@ -38,6 +38,7 @@
 import org.hisp.dhis.dxf2.importsummary.ImportSummaries;
 import org.hisp.dhis.dxf2.importsummary.ImportSummary;
 import org.hisp.dhis.dxf2.utils.JacksonUtils;
+import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.program.Program;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.HttpStatus;
@@ -80,15 +81,27 @@
     // -------------------------------------------------------------------------
 
     @RequestMapping( value = "", method = RequestMethod.GET )
-    public String getEvents( @RequestParam( "program" ) String programUid, @RequestParam Map<String, String> parameters,
-        Model model, HttpServletRequest request, HttpServletResponse response ) throws Exception
+    public String getEvents( @RequestParam( "program" ) String programUid, @RequestParam( value = "orgUnit", required = false ) String orgUnitUid,
+        @RequestParam Map<String, String> parameters, Model model, HttpServletRequest request,
+        HttpServletResponse response ) throws Exception
     {
         WebOptions options = new WebOptions( parameters );
         Program program = manager.get( Program.class, programUid );
+        OrganisationUnit organisationUnit = null;
 
         if ( program == null )
         {
-            throw new NotFoundException( programUid );
+            throw new NotFoundException( "Program", programUid );
+        }
+
+        if ( orgUnitUid != null )
+        {
+            organisationUnit = manager.get( OrganisationUnit.class, orgUnitUid );
+
+            if ( organisationUnit == null )
+            {
+                throw new NotFoundException( "OrganisationUnit", programUid );
+            }
         }
 
         if ( program.isRegistration() || !program.isSingleEvent() )
@@ -96,7 +109,7 @@
             throw new HttpClientErrorException( HttpStatus.BAD_REQUEST, "Only single event with no registration is currently supported." );
         }
 
-        Events events = eventService.getEvents( program );
+        Events events = eventService.getEvents( program, organisationUnit );
 
         if ( options.hasLinks() )
         {

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/exception/NotFoundException.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/exception/NotFoundException.java	2013-08-23 16:00:30 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/exception/NotFoundException.java	2013-08-26 13:01:59 +0000
@@ -42,4 +42,9 @@
     {
         super( "Object not found for uid: " + uid );
     }
+
+    public NotFoundException( String type, String uid )
+    {
+        super( type + " not found for uid: " + uid );
+    }
 }