← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 9983: Re-impl resource tables using spring jdbctemplate batchupdate

 

------------------------------------------------------------
revno: 9983
committer: Lars Helge Øverland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2013-03-05 11:28:10 +0100
message:
  Re-impl resource tables using spring jdbctemplate batchupdate
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/resourcetable/ResourceTableStore.java
  dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/resourcetable/DefaultResourceTableService.java
  dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/resourcetable/jdbc/JdbcResourceTableStore.java
  dhis-2/dhis-services/dhis-service-administration/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/resourcetable/ResourceTableStore.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/resourcetable/ResourceTableStore.java	2012-12-27 18:15:23 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/resourcetable/ResourceTableStore.java	2013-03-05 10:28:10 +0000
@@ -47,6 +47,15 @@
     final String TABLE_NAME_PERIOD_STRUCTURE = "_periodstructure";
     final String TABLE_NAME_PERIOD_NO_DISAGGREGATION_STRUCTURE = "_period_no_disagg_structure";
     
+    /**
+     * Performs a batch update.
+     * 
+     * @param columns the number of columns in the table to update.
+     * @param tableName the name of the table to update.
+     * @param batchArgs the arguments to use for the update statement.
+     */
+    void batchUpdate( int columns, String tableName, List<Object[]> batchArgs );
+    
     // -------------------------------------------------------------------------
     // OrganisationUnitStructure
     // -------------------------------------------------------------------------

=== modified file 'dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/resourcetable/DefaultResourceTableService.java'
--- dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/resourcetable/DefaultResourceTableService.java	2012-04-03 22:10:58 +0000
+++ dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/resourcetable/DefaultResourceTableService.java	2013-03-05 10:28:10 +0000
@@ -27,6 +27,9 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import static org.hisp.dhis.resourcetable.ResourceTableStore.TABLE_NAME_CATEGORY_OPTION_COMBO_NAME;
+import static org.hisp.dhis.resourcetable.ResourceTableStore.TABLE_NAME_DATA_ELEMENT_STRUCTURE;
+import static org.hisp.dhis.resourcetable.ResourceTableStore.TABLE_NAME_ORGANISATION_UNIT_STRUCTURE;
 import static org.hisp.dhis.resourcetable.ResourceTableStore.TABLE_NAME_PERIOD_STRUCTURE;
 
 import java.util.ArrayList;
@@ -37,8 +40,6 @@
 import java.util.List;
 import java.util.Map;
 
-import org.amplecode.quick.BatchHandler;
-import org.amplecode.quick.BatchHandlerFactory;
 import org.hisp.dhis.common.comparator.IdentifiableObjectNameComparator;
 import org.hisp.dhis.dataelement.DataElement;
 import org.hisp.dhis.dataelement.DataElementCategory;
@@ -52,7 +53,6 @@
 import org.hisp.dhis.indicator.IndicatorGroup;
 import org.hisp.dhis.indicator.IndicatorGroupSet;
 import org.hisp.dhis.indicator.IndicatorService;
-import org.hisp.dhis.jdbc.batchhandler.GenericBatchHandler;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.organisationunit.OrganisationUnitGroup;
 import org.hisp.dhis.organisationunit.OrganisationUnitGroupService;
@@ -134,13 +134,6 @@
         this.sqlViewService = sqlViewService;
     }
 
-    private BatchHandlerFactory batchHandlerFactory;
-
-    public void setBatchHandlerFactory( BatchHandlerFactory batchHandlerFactory )
-    {
-        this.batchHandlerFactory = batchHandlerFactory;
-    }
-
     // -------------------------------------------------------------------------
     // All
     // -------------------------------------------------------------------------
@@ -172,8 +165,7 @@
         
         resourceTableStore.createOrganisationUnitStructure( maxLevel );
 
-        BatchHandler<Object> batchHandler = batchHandlerFactory.createBatchHandler( GenericBatchHandler.class ).
-            setTableName( ResourceTableStore.TABLE_NAME_ORGANISATION_UNIT_STRUCTURE ).init();
+        List<Object[]> batchArgs = new ArrayList<Object[]>();
         
         for ( int i = 0; i < maxLevel; i++ )
         {
@@ -183,10 +175,10 @@
 
             for ( OrganisationUnit unit : units )
             {
-                List<String> structure = new ArrayList<String>();
+                List<Object> values = new ArrayList<Object>();
 
-                structure.add( String.valueOf( unit.getId() ) );
-                structure.add( String.valueOf( level ) );
+                values.add( unit.getId() );
+                values.add( level );
 
                 Map<Integer, Integer> identifiers = new HashMap<Integer, Integer>();
                 Map<Integer, String> uids = new HashMap<Integer, String>();
@@ -201,15 +193,15 @@
                
                 for ( int k = 1 ; k <= maxLevel ; k ++ )
                 {
-                    structure.add( identifiers.get( k ) != null ? String.valueOf( identifiers.get( k ) ) : null );
-                    structure.add( uids.get( k ) );
+                    values.add( identifiers.get( k ) != null ? identifiers.get( k ) : null );
+                    values.add( uids.get( k ) );
                 }
                 
-                batchHandler.addObject( structure );
+                batchArgs.add( values.toArray() );
             }
         }
 
-        batchHandler.flush();
+        resourceTableStore.batchUpdate( ( maxLevel * 2 ) + 2, TABLE_NAME_ORGANISATION_UNIT_STRUCTURE, batchArgs );
     }
 
     // -------------------------------------------------------------------------
@@ -222,25 +214,19 @@
 
         Collection<DataElementCategoryOptionCombo> combos = categoryService.getAllDataElementCategoryOptionCombos();
 
-        BatchHandler<Object> batchHandler = batchHandlerFactory.createBatchHandler( GenericBatchHandler.class ).
-            setTableName( ResourceTableStore.TABLE_NAME_CATEGORY_OPTION_COMBO_NAME ).init();
-        
-        
-        //TODO
-        
-        
+        List<Object[]> batchArgs = new ArrayList<Object[]>();
         
         for ( DataElementCategoryOptionCombo combo : combos )
         {
-            final List<String> values = new ArrayList<String>();
-            
-            values.add( String.valueOf( combo.getId() ) );
+            List<Object> values = new ArrayList<Object>();
+
+            values.add( combo.getId() );
             values.add( combo.getName() );
             
-            batchHandler.addObject( values );
+            batchArgs.add( values.toArray() );
         }
         
-        batchHandler.flush();
+        resourceTableStore.batchUpdate( 2, TABLE_NAME_CATEGORY_OPTION_COMBO_NAME, batchArgs );
     }
 
     // -------------------------------------------------------------------------
@@ -267,14 +253,13 @@
         // Populate table
         // ---------------------------------------------------------------------
 
-        BatchHandler<Object> batchHandler = batchHandlerFactory.createBatchHandler( GenericBatchHandler.class ).
-            setTableName( CreateDataElementGroupSetTableStatement.TABLE_NAME ).init();
-
+        List<Object[]> batchArgs = new ArrayList<Object[]>();
+        
         for ( DataElement dataElement : dataElements )
         {
-            final List<String> values = new ArrayList<String>();
+            List<Object> values = new ArrayList<Object>();
 
-            values.add( String.valueOf( dataElement.getId() ) );
+            values.add( dataElement.getId() );
             values.add( dataElement.getName() );
             
             for ( DataElementGroupSet groupSet : groupSets )
@@ -285,10 +270,10 @@
                 values.add( group != null ? group.getUid() : null );
             }
             
-            batchHandler.addObject( values );
+            batchArgs.add( values.toArray() );
         }
         
-        batchHandler.flush();
+        resourceTableStore.batchUpdate( ( groupSets.size() * 2 ) + 2, CreateDataElementGroupSetTableStatement.TABLE_NAME, batchArgs );
     }
 
     // -------------------------------------------------------------------------
@@ -315,14 +300,13 @@
         // Populate table
         // ---------------------------------------------------------------------
 
-        BatchHandler<Object> batchHandler = batchHandlerFactory.createBatchHandler( GenericBatchHandler.class ).
-            setTableName( CreateIndicatorGroupSetTableStatement.TABLE_NAME ).init();
-
+        List<Object[]> batchArgs = new ArrayList<Object[]>();
+        
         for ( Indicator indicator : indicators )
         {
-            final List<String> values = new ArrayList<String>();
-            
-            values.add( String.valueOf( indicator.getId() ) );
+            List<Object> values = new ArrayList<Object>();
+
+            values.add( indicator.getId() );
             values.add( indicator.getName() );
             
             for ( IndicatorGroupSet groupSet : groupSets )
@@ -333,10 +317,10 @@
                 values.add( group != null ? group.getUid() : null );
             }
             
-            batchHandler.addObject( values );
+            batchArgs.add( values.toArray() );
         }
         
-        batchHandler.flush();        
+        resourceTableStore.batchUpdate( ( groupSets.size() * 2 ) + 2, CreateIndicatorGroupSetTableStatement.TABLE_NAME, batchArgs );
     }
     
     // -------------------------------------------------------------------------
@@ -365,14 +349,13 @@
         // Populate table
         // ---------------------------------------------------------------------
 
-        BatchHandler<Object> batchHandler = batchHandlerFactory.createBatchHandler( GenericBatchHandler.class ).
-            setTableName( CreateOrganisationUnitGroupSetTableStatement.TABLE_NAME ).init();
-
+        List<Object[]> batchArgs = new ArrayList<Object[]>();
+        
         for ( OrganisationUnit unit : units )
         {
-            final List<String> values = new ArrayList<String>();
+            List<Object> values = new ArrayList<Object>();
 
-            values.add( String.valueOf( unit.getId() ) );
+            values.add( unit.getId() );
             values.add( unit.getName() );
 
             for ( OrganisationUnitGroupSet groupSet : groupSets )
@@ -383,10 +366,10 @@
                 values.add( group != null ? group.getUid() : null );
             }
 
-            batchHandler.addObject( values );
+            batchArgs.add( values.toArray() );
         }
 
-        batchHandler.flush();
+        resourceTableStore.batchUpdate( ( groupSets.size() * 2 ) + 2, CreateOrganisationUnitGroupSetTableStatement.TABLE_NAME, batchArgs );
     }
     
     // -------------------------------------------------------------------------
@@ -412,14 +395,13 @@
         // Populate table
         // ---------------------------------------------------------------------
 
-        BatchHandler<Object> batchHandler = batchHandlerFactory.createBatchHandler( GenericBatchHandler.class ).
-            setTableName( CreateCategoryTableStatement.TABLE_NAME ).init();
+        List<Object[]> batchArgs = new ArrayList<Object[]>();
         
         for ( DataElementCategoryOptionCombo categoryOptionCombo : categoryOptionCombos )
         {
-            final List<String> values = new ArrayList<String>();
-            
-            values.add( String.valueOf( categoryOptionCombo.getId() ) );
+            List<Object> values = new ArrayList<Object>();
+
+            values.add( categoryOptionCombo.getId() );
             values.add( categoryOptionCombo.getName() );
             
             for ( DataElementCategory category : categories )
@@ -427,13 +409,13 @@
                 DataElementCategoryOption categoryOption = category.getCategoryOption( categoryOptionCombo );
                 
                 values.add( categoryOption != null ? categoryOption.getName() : null );
-                values.add( categoryOption != null ? String.valueOf( categoryOption.getId() ) : null );
+                values.add( categoryOption != null ? categoryOption.getId() : null );
             }
             
-            batchHandler.addObject( values );
+            batchArgs.add( values.toArray() );
         }
         
-        batchHandler.flush();
+        resourceTableStore.batchUpdate( ( categories.size() * 2 ) + 2, CreateCategoryTableStatement.TABLE_NAME, batchArgs );
     }
 
     // -------------------------------------------------------------------------
@@ -454,24 +436,23 @@
         // Populate table
         // ---------------------------------------------------------------------
 
-        BatchHandler<Object> batchHandler = batchHandlerFactory.createBatchHandler( GenericBatchHandler.class ).
-            setTableName( ResourceTableStore.TABLE_NAME_DATA_ELEMENT_STRUCTURE ).init();
+        List<Object[]> batchArgs = new ArrayList<Object[]>();
         
         for ( DataElement dataElement : dataElements )
         {
-            final List<String> values = new ArrayList<String>();
-            
+            List<Object> values = new ArrayList<Object>();
+
             final PeriodType periodType = dataElement.getPeriodType();
             
-            values.add( String.valueOf( dataElement.getId() ) );
+            values.add( dataElement.getId() );
             values.add( dataElement.getName() );
-            values.add( periodType != null ? String.valueOf( periodType.getId() ) : null );
+            values.add( periodType != null ? periodType.getId() : null );
             values.add( periodType != null ? periodType.getName() : null );
             
-            batchHandler.addObject( values );
+            batchArgs.add( values.toArray() );
         }
         
-        batchHandler.flush();
+        resourceTableStore.batchUpdate( 4, TABLE_NAME_DATA_ELEMENT_STRUCTURE, batchArgs );
     }
 
     // -------------------------------------------------------------------------
@@ -492,19 +473,18 @@
         // Populate table
         // ---------------------------------------------------------------------
         
-        BatchHandler<Object> batchHandler = batchHandlerFactory.createBatchHandler( GenericBatchHandler.class ).
-            setTableName( TABLE_NAME_PERIOD_STRUCTURE ).init();
+        List<Object[]> batchArgs = new ArrayList<Object[]>();
         
         for ( Period period : periods )
         {
             final Date startDate = period.getStartDate();
             final PeriodType rowType = period.getPeriodType();
-            
-            final List<String> values = new ArrayList<String>();
-            
-            values.add( String.valueOf( period.getId() ) );
+
+            List<Object> values = new ArrayList<Object>();
+
+            values.add( period.getId() );
             values.add( period.getIsoDate() );
-            values.add( String.valueOf( rowType.getFrequencyOrder() ) );
+            values.add( rowType.getFrequencyOrder() );
             
             for ( PeriodType periodType : PeriodType.PERIOD_TYPES )
             {
@@ -518,9 +498,9 @@
                 }
             }
             
-            batchHandler.addObject( values );
+            batchArgs.add( values.toArray() );
         }
 
-        batchHandler.flush();
+        resourceTableStore.batchUpdate( PeriodType.PERIOD_TYPES.size() + 3, TABLE_NAME_PERIOD_STRUCTURE, batchArgs );
     }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/resourcetable/jdbc/JdbcResourceTableStore.java'
--- dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/resourcetable/jdbc/JdbcResourceTableStore.java	2012-12-27 18:15:23 +0000
+++ dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/resourcetable/jdbc/JdbcResourceTableStore.java	2013-03-05 10:28:10 +0000
@@ -65,6 +65,29 @@
     }
 
     // -------------------------------------------------------------------------
+    // ResourceTableStore implementation
+    // -------------------------------------------------------------------------
+
+    public void batchUpdate( int columns, String tableName, List<Object[]> batchArgs )
+    {
+        if ( columns == 0 || tableName == null )
+        {
+            return;
+        }
+        
+        StringBuilder builder = new StringBuilder( "insert into " + tableName + " values (" );
+        
+        for ( int i = 0; i < columns; i++ )
+        {
+            builder.append( "?," );
+        }
+        
+        builder.deleteCharAt( builder.length() - 1 ).append( ")" );
+        
+        jdbcTemplate.batchUpdate( builder.toString(), batchArgs );
+    }
+    
+    // -------------------------------------------------------------------------
     // OrganisationUnitStructure
     // -------------------------------------------------------------------------
 

=== modified file 'dhis-2/dhis-services/dhis-service-administration/src/main/resources/META-INF/dhis/beans.xml'
--- dhis-2/dhis-services/dhis-service-administration/src/main/resources/META-INF/dhis/beans.xml	2013-03-04 14:44:15 +0000
+++ dhis-2/dhis-services/dhis-service-administration/src/main/resources/META-INF/dhis/beans.xml	2013-03-05 10:28:10 +0000
@@ -17,7 +17,6 @@
     <property name="indicatorService" ref="org.hisp.dhis.indicator.IndicatorService" />
     <property name="periodService" ref="org.hisp.dhis.period.PeriodService" />
     <property name="sqlViewService" ref="org.hisp.dhis.sqlview.SqlViewService" />
-    <property name="batchHandlerFactory" ref="batchHandlerFactory" />
   </bean>
   
   <!-- Data integrity -->