← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 2902: Improved performance in JRDataSource impl by using an internal map

 

------------------------------------------------------------
revno: 2902
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2011-02-23 22:26:24 +0100
message:
  Improved performance in JRDataSource impl by using an internal map
modified:
  dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/grid/ListGrid.java
  dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/result/GridJasperResult.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-support/dhis-support-system/src/main/java/org/hisp/dhis/system/grid/ListGrid.java'
--- dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/grid/ListGrid.java	2011-02-23 21:08:31 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/grid/ListGrid.java	2011-02-23 21:26:24 +0000
@@ -32,7 +32,9 @@
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Comparator;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
 import net.sf.jasperreports.engine.JRException;
 import net.sf.jasperreports.engine.JRField;
@@ -85,6 +87,11 @@
     private int currentRowReadIndex = -1;
     
     /**
+     * Represents a mapping between column names and the index of the column in the grid.
+     */
+    private Map<String, Integer> columnIndexMap = new HashMap<String, Integer>();
+    
+    /**
      * Default constructor.
      */
     public ListGrid()
@@ -132,7 +139,16 @@
         
         return this;
     }
-    
+
+    public Grid addHeader( GridHeader header )
+    {
+        headers.add( header );
+        
+        updateColumnIndexMap();
+        
+        return this;
+    }
+       
     public List<GridHeader> getHeaders()
     {
         return headers;
@@ -152,14 +168,7 @@
         
         return tempHeaders;
     }
-    
-    public Grid addHeader( GridHeader header )
-    {
-        headers.add( header );
-        
-        return this;
-    }
-        
+     
     public int getHeight()
     {        
         return ( grid != null && grid.size() > 0 ) ? grid.size() : 0;
@@ -288,6 +297,8 @@
             row.remove( columnIndex );
         }
         
+        updateColumnIndexMap();
+        
         return this;
     }
     
@@ -385,18 +396,9 @@
     public Object getFieldValue( JRField field )
         throws JRException
     {
-        int headerIndex = -1;
-        
-        for ( int i = 0; i < headers.size(); i++ )
-        {
-            if ( headers.get( i ).getColumn() != null && headers.get( i ).getColumn().equals( field.getName() ) )
-            {
-                headerIndex = i;
-                break;
-            }
-        }
-        
-        return headerIndex != -1 ? getRow( currentRowReadIndex ).get( headerIndex ) : null;
+        Integer index = columnIndexMap.get( field.getName() );
+        
+        return index != null ? getRow( currentRowReadIndex ).get( index ) : null;
     }
 
     // -------------------------------------------------------------------------
@@ -428,6 +430,20 @@
         }
     }
     
+    /**
+     * Updates the mapping between header columns and grid indexes. This method
+     * should be invoked whenever the columns are manipulated.
+     */
+    private void updateColumnIndexMap()
+    {
+        columnIndexMap.clear();
+        
+        for ( int i = 0; i < headers.size(); i++ )
+        {
+            columnIndexMap.put( headers.get( i ).getColumn(), i );
+        }
+    }
+    
     // -------------------------------------------------------------------------
     // toString
     // -------------------------------------------------------------------------

=== modified file 'dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/result/GridJasperResult.java'
--- dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/result/GridJasperResult.java	2011-02-23 21:08:31 +0000
+++ dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/result/GridJasperResult.java	2011-02-23 21:26:24 +0000
@@ -89,8 +89,6 @@
         
         grid = _grid != null ? _grid : grid; 
 
-        System.out.println( "GRID IN JASPER RESULT " + grid );
-
         // ---------------------------------------------------------------------
         // Configure response
         // ---------------------------------------------------------------------