← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 11864: use sql to get data from programstageinstances (in eventservice)

 

------------------------------------------------------------
revno: 11864
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2013-09-02 19:02:33 +0200
message:
  use sql to get data from programstageinstances (in eventservice)
added:
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/DefaultEventStore.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/EventStore.java
modified:
  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/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-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-09-02 10:37:36 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/BaseEventService.java	2013-09-02 17:02:33 +0000
@@ -52,6 +52,7 @@
 import org.hisp.dhis.program.ProgramStageService;
 import org.hisp.dhis.user.CurrentUserService;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.transaction.annotation.Transactional;
 
 import java.util.ArrayList;
 import java.util.Collection;
@@ -63,6 +64,7 @@
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
  */
+@Transactional
 public abstract class BaseEventService implements EventService
 {
     // -------------------------------------------------------------------------
@@ -97,6 +99,9 @@
     private InputValidationService inputValidationService;
 
     @Autowired
+    private EventStore eventStore;
+
+    @Autowired
     private I18nManager i18nManager;
 
     private I18nFormat format;
@@ -369,18 +374,17 @@
     @Override
     public Events getEvents( Program program, OrganisationUnit organisationUnit, Date start, Date end )
     {
+        ProgramStage programStage = program.getProgramStageByStage( 1 );
+        List<Event> eventList = eventStore.getAll( program, programStage, organisationUnit, start, end );
         Events events = new Events();
-
-        ProgramStage programStage = program.getProgramStageByStage( 1 );
-
-        List<ProgramStageInstance> programStageInstances;
-
-        programStageInstances = new ArrayList<ProgramStageInstance>(
+        events.setEvents( eventList );
+
+        /*
+        List<ProgramStageInstance> programStageInstances = new ArrayList<ProgramStageInstance>(
             programStageInstanceService.getProgramStageInstances( programStage, organisationUnit, start, end ) );
 
         List<Event> convertedEvents = convertProgramStageInstances( programStageInstances );
-
-        events.setEvents( convertedEvents );
+        */
 
         return events;
     }
@@ -491,10 +495,10 @@
         event.setCompleted( programStageInstance.isCompleted() );
         event.setEvent( programStageInstance.getUid() );
         event.setEventDate( programStageInstance.getExecutionDate().toString() );
+        event.setStoredBy( programStageInstance.getCompletedUser() );
         event.setOrgUnit( programStageInstance.getOrganisationUnit().getUid() );
         event.setProgram( programStageInstance.getProgramInstance().getProgram().getUid() );
         event.setProgramStage( programStageInstance.getProgramStage().getUid() );
-        event.setStoredBy( programStageInstance.getCompletedUser() );
 
         Collection<PatientDataValue> patientDataValues = patientDataValueService.getPatientDataValues( programStageInstance );
 

=== added file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/DefaultEventStore.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/DefaultEventStore.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/DefaultEventStore.java	2013-09-02 17:02:33 +0000
@@ -0,0 +1,103 @@
+package org.hisp.dhis.dxf2.event;
+
+/*
+ * Copyright (c) 2004-2013, University of Oslo
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ *
+ * Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * Neither the name of the HISP project nor the names of its contributors may
+ * be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+import org.hisp.dhis.organisationunit.OrganisationUnit;
+import org.hisp.dhis.program.Program;
+import org.hisp.dhis.program.ProgramStage;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.jdbc.core.JdbcTemplate;
+import org.springframework.jdbc.support.rowset.SqlRowSet;
+
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+
+/**
+ * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
+ */
+public class DefaultEventStore implements EventStore
+{
+    @Autowired
+    private JdbcTemplate jdbcTemplate;
+
+    @Override
+    public List<Event> getAll( Program program, ProgramStage programStage, OrganisationUnit organisationUnit, Date startDate, Date endDate )
+    {
+        List<Event> events = new ArrayList<Event>();
+        String sql = buildSql( programStage.getId(), organisationUnit.getId(), startDate.toString(), endDate.toString() );
+
+        SqlRowSet rowSet = jdbcTemplate.queryForRowSet( sql );
+
+        Event event = new Event();
+        event.setEvent( "not_valid" );
+
+        while ( rowSet.next() )
+        {
+            if ( !event.getEvent().equals( rowSet.getString( "psi_uid" ) ) )
+            {
+                event = new Event();
+
+                event.setCompleted( rowSet.getBoolean( "psi_completed" ) );
+                event.setEvent( rowSet.getString( "psi_uid" ) );
+                event.setProgram( program.getUid() );
+                event.setProgramStage( programStage.getUid() );
+                event.setStoredBy( rowSet.getString( "psi_completeduser" ) );
+                event.setOrgUnit( rowSet.getString( "ou_uid" ) );
+                event.setEventDate( rowSet.getString( "psi_executiondate" ) );
+
+                events.add( event );
+            }
+
+            DataValue dataValue = new DataValue();
+            dataValue.setValue( rowSet.getString( "pdv_value" ) );
+            dataValue.setProvidedElsewhere( rowSet.getBoolean( "pdv_providedelsewhere" ) );
+            dataValue.setDataElement( rowSet.getString( "de_uid" ) );
+
+            event.getDataValues().add( dataValue );
+        }
+
+        return events;
+    }
+
+    private String buildSql( int programStageId, int orgUnitId, String startDate, String endDate )
+    {
+        String sql = "select psi.uid as psi_uid, ou.uid as ou_uid, psi.executiondate as psi_executiondate," +
+            " psi.completeduser as psi_completeduser, psi.completed as psi_completed," +
+            " pdv.value as pdv_value, pdv.providedelsewhere as pdv_providedelsewhere, de.uid as de_uid" +
+            " from programstageinstance psi" +
+            " left join organisationunit ou on (psi.organisationunitid=ou.organisationunitid and ou.organisationunitid=" + orgUnitId + ")" +
+            " left join patientdatavalue pdv on psi.programstageinstanceid=pdv.programstageinstanceid" +
+            " left join dataelement de on pdv.dataelementid=de.dataelementid" +
+            " where psi.programstageid=" + programStageId +
+            " and (psi.executiondate >= '" + startDate + "' and psi.executiondate <= '" + endDate + "') order by psi_uid";
+
+        return sql;
+    }
+}

=== added file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/EventStore.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/EventStore.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/EventStore.java	2013-09-02 17:02:33 +0000
@@ -0,0 +1,44 @@
+package org.hisp.dhis.dxf2.event;
+
+/*
+ * Copyright (c) 2004-2013, University of Oslo
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ *
+ * Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * Neither the name of the HISP project nor the names of its contributors may
+ * be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+import org.hisp.dhis.organisationunit.OrganisationUnit;
+import org.hisp.dhis.program.Program;
+import org.hisp.dhis.program.ProgramStage;
+
+import java.util.Date;
+import java.util.List;
+
+/**
+ * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
+ */
+public interface EventStore
+{
+    List<Event> getAll( Program program, ProgramStage programStage, OrganisationUnit organisationUnit, Date startDate, Date endDate );
+}

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/resources/META-INF/dhis/beans.xml'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/resources/META-INF/dhis/beans.xml	2013-05-23 04:30:18 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/resources/META-INF/dhis/beans.xml	2013-09-02 17:02:33 +0000
@@ -14,6 +14,8 @@
   <bean id="org.hisp.dhis.dxf2.datavalueset.DataValueSetStore"
     class="org.hisp.dhis.dxf2.datavalueset.SpringDataValueSetStore" />
 
+  <bean id="org.hisp.dhis.dxf2.event.EventStore" class="org.hisp.dhis.dxf2.event.DefaultEventStore" />
+
   <bean id="org.hisp.dhis.dxf2.event.EventService" class="org.hisp.dhis.dxf2.event.JacksonEventService" />
 
   <bean id="org.hisp.dhis.dxf2.InputValidationService" class="org.hisp.dhis.dxf2.DefaultInputValidationService" />