← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 16492: Data export. Changed from using spring jdbc queryForRowSet to using RowCallbackHandler in order t...

 

------------------------------------------------------------
revno: 16492
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Sun 2014-08-24 23:24:08 +0200
message:
  Data export. Changed from using spring jdbc queryForRowSet to using RowCallbackHandler in order to stream output
modified:
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/SpringDataValueSetStore.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-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/SpringDataValueSetStore.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/SpringDataValueSetStore.java	2014-08-24 21:04:36 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/SpringDataValueSetStore.java	2014-08-24 21:24:08 +0000
@@ -34,6 +34,8 @@
 
 import java.io.OutputStream;
 import java.io.Writer;
+import java.sql.ResultSet;
+import java.sql.SQLException;
 import java.util.Collection;
 import java.util.Date;
 import java.util.HashSet;
@@ -52,7 +54,7 @@
 import org.hisp.dhis.system.util.StreamUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.jdbc.core.JdbcTemplate;
-import org.springframework.jdbc.support.rowset.SqlRowSet;
+import org.springframework.jdbc.core.RowCallbackHandler;
 
 import com.csvreader.CsvWriter;
 
@@ -80,9 +82,7 @@
     {
         DataValueSet dataValueSet = new StreamingDataValueSet( XMLFactory.getXMLWriter( out ) );
 
-        SqlRowSet sqlRowSet = jdbcTemplate.queryForRowSet( getDataValueSql( dataSets, periods, orgUnits ) );
-
-        writeDataValueSet( sqlRowSet, dataSets, completeDate, period, orgUnit, periods, orgUnits, dataValueSet );
+        writeDataValueSet( getDataValueSql( dataSets, periods, orgUnits ), dataSets, completeDate, period, orgUnit, periods, orgUnits, dataValueSet );
 
         StreamUtils.closeOutputStream( out );
     }
@@ -93,9 +93,7 @@
     {
         DataValueSet dataValueSet = new StreamingJsonDataValueSet( outputStream );
 
-        SqlRowSet sqlRowSet = jdbcTemplate.queryForRowSet( getDataValueSql( dataSets, periods, orgUnits ) );
-
-        writeDataValueSet( sqlRowSet, dataSets, completeDate, period, orgUnit, periods, orgUnits, dataValueSet );
+        writeDataValueSet( getDataValueSql( dataSets, periods, orgUnits ), dataSets, completeDate, period, orgUnit, periods, orgUnits, dataValueSet );
 
         StreamUtils.closeOutputStream( outputStream );
     }
@@ -105,9 +103,7 @@
     {
         DataValueSet dataValueSet = new StreamingCsvDataValueSet( new CsvWriter( writer, CSV_DELIM ) );
 
-        SqlRowSet sqlRowSet = jdbcTemplate.queryForRowSet( getDataValueSql( dataSets, periods, orgUnits ) );
-
-        writeDataValueSet( sqlRowSet, dataSets, null, null, null, periods, orgUnits, dataValueSet );
+        writeDataValueSet( getDataValueSql( dataSets, periods, orgUnits ), dataSets, null, null, null, periods, orgUnits, dataValueSet );
     }
 
     @Override
@@ -126,43 +122,44 @@
             "join categoryoptioncombo aoc on (dv.attributeoptioncomboid=aoc.categoryoptioncomboid) " +
             "where dv.lastupdated >= '" + DateUtils.getLongDateString( lastUpdated ) + "'";
 
-        SqlRowSet rowSet = jdbcTemplate.queryForRowSet( sql );
-
-        writeDataValueSet( rowSet, null, null, null, null, null, null, dataValueSet );
+        writeDataValueSet( sql, null, null, null, null, null, null, dataValueSet );
     }
 
-    private void writeDataValueSet( SqlRowSet rowSet, Set<DataSet> dataSets, Date completeDate, Period period,
-        OrganisationUnit orgUnit, Set<Period> periods, Set<OrganisationUnit> orgUnits, DataValueSet dataValueSet )
+    private void writeDataValueSet( String sql, Set<DataSet> dataSets, Date completeDate, Period period,
+        OrganisationUnit orgUnit, Set<Period> periods, Set<OrganisationUnit> orgUnits, final DataValueSet dataValueSet )
     {
         dataValueSet.setDataSet( dataSets.size() == 1 ? dataSets.iterator().next().getUid() : null );
         dataValueSet.setCompleteDate( getLongDateString( completeDate ) );
         dataValueSet.setPeriod( period != null ? period.getIsoDate() : null );
         dataValueSet.setOrgUnit( orgUnit != null ? orgUnit.getUid() : null );
 
-        Calendar calendar = PeriodType.getCalendar();
+        final Calendar calendar = PeriodType.getCalendar();
         
-        while ( rowSet.next() )
+        jdbcTemplate.query( sql, new RowCallbackHandler()
         {
-            DataValue dataValue = dataValueSet.getDataValueInstance();
-            PeriodType pt = PeriodType.getPeriodTypeByName( rowSet.getString( "ptname" ) );
-
-            dataValue.setDataElement( rowSet.getString( "deuid" ) );
-            dataValue.setPeriod( periodCache.getIsoPeriod( pt, rowSet.getDate( "pestart" ), calendar ) );
-            dataValue.setOrgUnit( rowSet.getString( "ouuid" ) );
-            dataValue.setCategoryOptionCombo( rowSet.getString( "cocuid" ) );
-            dataValue.setAttributeOptionCombo( rowSet.getString( "aocuid" ) );
-            dataValue.setValue( rowSet.getString( "value" ) );
-            dataValue.setStoredBy( rowSet.getString( "storedby" ) );
-            dataValue.setCreated( getLongDateString( rowSet.getDate( "created" ) ) );
-            dataValue.setLastUpdated( getLongDateString( rowSet.getDate( "lastupdated" ) ) );
-            dataValue.setComment( rowSet.getString( "comment" ) );
-            dataValue.setFollowup( rowSet.getBoolean( "followup" ) );
-            dataValue.close();
-        }
-
+            public void processRow( ResultSet rs ) throws SQLException
+            {
+                DataValue dataValue = dataValueSet.getDataValueInstance();
+                PeriodType pt = PeriodType.getPeriodTypeByName( rs.getString( "ptname" ) );
+
+                dataValue.setDataElement( rs.getString( "deuid" ) );
+                dataValue.setPeriod( periodCache.getIsoPeriod( pt, rs.getDate( "pestart" ), calendar ) );
+                dataValue.setOrgUnit( rs.getString( "ouuid" ) );
+                dataValue.setCategoryOptionCombo( rs.getString( "cocuid" ) );
+                dataValue.setAttributeOptionCombo( rs.getString( "aocuid" ) );
+                dataValue.setValue( rs.getString( "value" ) );
+                dataValue.setStoredBy( rs.getString( "storedby" ) );
+                dataValue.setCreated( getLongDateString( rs.getDate( "created" ) ) );
+                dataValue.setLastUpdated( getLongDateString( rs.getDate( "lastupdated" ) ) );
+                dataValue.setComment( rs.getString( "comment" ) );
+                dataValue.setFollowup( rs.getBoolean( "followup" ) );
+                dataValue.close();
+            }            
+        } );
+        
         dataValueSet.close();
     }
-    
+        
     //--------------------------------------------------------------------------
     // DataValueSetStore implementation
     //--------------------------------------------------------------------------