← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 14912: EventStore. Made code more compact. Fixed bug introduced by not checking if status is null.

 

------------------------------------------------------------
revno: 14912
committer: Lars Helge Øverland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Sat 2014-04-19 14:01:55 +0200
message:
  EventStore. Made code more compact. Fixed bug introduced by not checking if status is null.
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/event/EventStatus.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/JdbcEventStore.java
  dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/events/RegistrationMultiEventsServiceTest.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/event/EventStatus.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/event/EventStatus.java	2014-04-18 10:14:28 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/event/EventStatus.java	2014-04-19 12:01:55 +0000
@@ -63,5 +63,15 @@
 
         throw new IllegalArgumentException();
     }
+    
+    public static boolean isExistingEvent( EventStatus status )
+    {
+        if ( status == null )
+        {
+            return false;
+        }
+        
+        return ACTIVE.equals( status ) || COMPLETED.equals( status ) || VISITED.equals( status );
+    }
 }
 

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/JdbcEventStore.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/JdbcEventStore.java	2014-04-18 18:17:51 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/JdbcEventStore.java	2014-04-19 12:01:55 +0000
@@ -37,6 +37,7 @@
 import java.util.Date;
 import java.util.List;
 
+import org.apache.commons.lang.StringUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance;
@@ -50,7 +51,6 @@
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.jdbc.core.JdbcTemplate;
 import org.springframework.jdbc.support.rowset.SqlRowSet;
-import org.springframework.util.StringUtils;
 
 import com.fasterxml.jackson.core.type.TypeReference;
 import com.fasterxml.jackson.databind.ObjectMapper;
@@ -119,23 +119,16 @@
                 event.setProgramStage( rowSet.getString( "ps_uid" ) );
                 event.setStoredBy( rowSet.getString( "psi_completeduser" ) );
                 event.setOrgUnit( rowSet.getString( "ou_uid" ) );
-                if ( rowSet.getString( "psi_executiondate" ) != null )
-                {
-                    event.setEventDate( rowSet.getString( "psi_executiondate" ) );
-                }
-                else
-                {
-                    event.setEventDate( rowSet.getString( "psi_duedate" ) );
-                }
-
+                event.setEventDate( StringUtils.defaultIfEmpty( rowSet.getString( "psi_executiondate" ), rowSet.getString( "psi_duedate" ) ) );
+                
                 if ( rowSet.getBoolean( "ps_capturecoordinates" ) )
                 {
-                    Double psi_longitude = rowSet.getDouble( "psi_longitude" );
-                    Double psi_latitude = rowSet.getDouble( "psi_latitude" );
+                    Double longitude = rowSet.getDouble( "psi_longitude" );
+                    Double latitude = rowSet.getDouble( "psi_latitude" );
 
-                    if ( !StringUtils.isEmpty( psi_longitude ) && !StringUtils.isEmpty( psi_latitude ) )
+                    if ( longitude != null && latitude != null )
                     {
-                        Coordinate coordinate = new Coordinate( psi_longitude, psi_latitude );
+                        Coordinate coordinate = new Coordinate( longitude, latitude );
 
                         try
                         {
@@ -183,27 +176,30 @@
     {
         SqlHelper hlp = new SqlHelper();
 
-        String sql = "select p.uid as p_uid, ps.uid as ps_uid, ps.capturecoordinates as ps_capturecoordinates, pa.uid as pa_uid, psi.uid as psi_uid, psi.status as psi_status, ou.uid as ou_uid, "
-            + "psi.executiondate as psi_executiondate, psi.duedate as psi_duedate, psi.completeduser as psi_completeduser, psi.longitude as psi_longitude, psi.latitude as psi_latitude,"
-            + " pdv.value as pdv_value, pdv.storedby as pdv_storedby, pdv.providedelsewhere as pdv_providedelsewhere, de.uid as de_uid"
-            + " from program p"
-            + " left join programstage ps on ps.programid=p.programid"
-            + " left join programstageinstance psi on ps.programstageid=psi.programstageid"
-            + " left join programinstance pi on pi.programinstanceid=psi.programinstanceid";
+        String sql = 
+            "select p.uid as p_uid, ps.uid as ps_uid, ps.capturecoordinates as ps_capturecoordinates, pa.uid as pa_uid, psi.uid as psi_uid, psi.status as psi_status, ou.uid as ou_uid, " +
+            "psi.executiondate as psi_executiondate, psi.duedate as psi_duedate, psi.completeduser as psi_completeduser, psi.longitude as psi_longitude, psi.latitude as psi_latitude, " +
+            "pdv.value as pdv_value, pdv.storedby as pdv_storedby, pdv.providedelsewhere as pdv_providedelsewhere, de.uid as de_uid " +
+            "from program p " +
+            "left join programstage ps on ps.programid=p.programid " +
+            "left join programstageinstance psi on ps.programstageid=psi.programstageid " +
+            "left join programinstance pi on pi.programinstanceid=psi.programinstanceid ";
 
-        if ( status == EventStatus.VISITED || status == EventStatus.COMPLETED )
+        if ( status == null || EventStatus.isExistingEvent( status ) )
         {
-            sql += " left join organisationunit ou on (psi.organisationunitid=ou.organisationunitid) ";
+            sql += "left join organisationunit ou on (psi.organisationunitid=ou.organisationunitid) ";
         }
         else
         {
-            sql += " left join trackedentityinstance tei on tei.trackedentityinstanceid=pi.trackedentityinstanceid "
-                + " left join organisationunit ou on (tei.organisationunitid=ou.organisationunitid) ";
+            sql += 
+                "left join trackedentityinstance tei on tei.trackedentityinstanceid=pi.trackedentityinstanceid " +
+                "left join organisationunit ou on (tei.organisationunitid=ou.organisationunitid) ";
         }
 
-        sql += " left join trackedentitydatavalue pdv on psi.programstageinstanceid=pdv.programstageinstanceid"
-            + " left join dataelement de on pdv.dataelementid=de.dataelementid "
-            + " left join trackedentityinstance pa on pa.trackedentityinstanceid=pi.trackedentityinstanceid ";
+        sql += 
+            "left join trackedentitydatavalue pdv on psi.programstageinstanceid=pdv.programstageinstanceid " +
+            "left join dataelement de on pdv.dataelementid=de.dataelementid " +
+            "left join trackedentityinstance pa on pa.trackedentityinstanceid=pi.trackedentityinstanceid ";
 
         if ( trackedEntityInstanceId != null )
         {

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/events/RegistrationMultiEventsServiceTest.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/events/RegistrationMultiEventsServiceTest.java	2014-04-19 03:54:45 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/events/RegistrationMultiEventsServiceTest.java	2014-04-19 12:01:55 +0000
@@ -255,14 +255,14 @@
         assertEquals( ImportStatus.SUCCESS, importSummary.getStatus() );
 
         sessionFactory.getCurrentSession().flush();
+        assertEquals( 2, eventService.getEvents( programA, organisationUnitA ).getEvents().size() );
+
+        event = createEvent( programA.getUid(), programStageB.getUid(), organisationUnitA.getUid(),
+            trackedEntityInstanceMaleA.getTrackedEntityInstance(), dataElementB.getUid() );
+        importSummary = eventService.addEvent( event );
+        assertEquals( ImportStatus.SUCCESS, importSummary.getStatus() );
+
         assertEquals( 3, eventService.getEvents( programA, organisationUnitA ).getEvents().size() );
-
-        event = createEvent( programA.getUid(), programStageB.getUid(), organisationUnitA.getUid(),
-            trackedEntityInstanceMaleA.getTrackedEntityInstance(), dataElementB.getUid() );
-        importSummary = eventService.addEvent( event );
-        assertEquals( ImportStatus.SUCCESS, importSummary.getStatus() );
-
-        assertEquals( 4, eventService.getEvents( programA, organisationUnitA ).getEvents().size() );
     }
 
     @Test
@@ -283,7 +283,7 @@
         assertEquals( ImportStatus.SUCCESS, importSummary.getStatus() );
 
         sessionFactory.getCurrentSession().flush();
-        assertEquals( 3, eventService.getEvents( programA, organisationUnitA ).getEvents().size() );
+        assertEquals( 2, eventService.getEvents( programA, organisationUnitA ).getEvents().size() );
 
         event = createEvent( programA.getUid(), programStageB.getUid(), organisationUnitA.getUid(),
             trackedEntityInstanceMaleA.getTrackedEntityInstance(), dataElementB.getUid() );
@@ -291,14 +291,14 @@
         importSummary = eventService.addEvent( event );
         assertEquals( ImportStatus.SUCCESS, importSummary.getStatus() );
 
-        assertEquals( 3, eventService.getEvents( programA, organisationUnitA ).getEvents().size() );
+        assertEquals( 2, eventService.getEvents( programA, organisationUnitA ).getEvents().size() );
 
         event = createEvent( programA.getUid(), programStageA.getUid(), organisationUnitA.getUid(),
             trackedEntityInstanceMaleA.getTrackedEntityInstance(), dataElementA.getUid() );
         importSummary = eventService.addEvent( event );
         assertEquals( ImportStatus.SUCCESS, importSummary.getStatus() );
 
-        assertEquals( 3, eventService.getEvents( programA, organisationUnitA ).getEvents().size() );
+        assertEquals( 2, eventService.getEvents( programA, organisationUnitA ).getEvents().size() );
     }
 
     private Enrollment createEnrollment( String program, String person )