← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 6466: Added sql result set utility methods to Grid object

 

------------------------------------------------------------
revno: 6466
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2012-04-03 10:51:42 +0200
message:
  Added sql result set utility methods to Grid object
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/Grid.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/sqlview/SqlViewTable.java
  dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/grid/ListGrid.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/common/Grid.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/Grid.java	2012-02-20 10:20:01 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/Grid.java	2012-04-03 08:51:42 +0000
@@ -27,6 +27,7 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import java.sql.ResultSet;
 import java.util.List;
 
 import net.sf.jasperreports.engine.JRDataSource;
@@ -203,4 +204,19 @@
      * @param addHeader indicates whether to add a grid header for the regression column.
      */
     Grid addCumulativeColumn( int columnIndex, boolean addHeader );
+    
+    /**
+     * Adds a set of headers based on the column names of the given SQL result set.
+     * 
+     * @param rs the result set.
+     */
+    void addHeaders( ResultSet rs );
+    
+    /**
+     * Moves the cursor the next row and adds values for each column of the given
+     * SQL result set.
+     * 
+     * @param rs the result set.
+     */
+    void addRow( ResultSet rs );
 }

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/sqlview/SqlViewTable.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/sqlview/SqlViewTable.java	2010-08-19 09:25:55 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/sqlview/SqlViewTable.java	2012-04-03 08:51:42 +0000
@@ -8,7 +8,6 @@
 
 /**
  * @author Dang Duy Hieu
- * @version $Id ResourceViewerTable.java July 12, 2010$
  */
 public class SqlViewTable
 {
@@ -81,9 +80,9 @@
                 headers.add( rsmd.getColumnLabel( i ) );
             }
         }
-        catch ( SQLException e )
+        catch ( SQLException ex )
         {
-            e.printStackTrace();
+            throw new RuntimeException( ex );
         }
     }
 
@@ -93,24 +92,21 @@
         {
             int columnNo = rs.getMetaData().getColumnCount();
 
-            rs.beforeFirst();
+            while ( rs.next() )
             {
-                while ( rs.next() )
+                List<Object> rows = new ArrayList<Object>();
+
+                for ( int i = 1; i <= columnNo; i++ )
                 {
-                    List<Object> rows = new ArrayList<Object>();
-
-                    for ( int i = 1; i <= columnNo; i++ )
-                    {
-                        rows.add( rs.getObject( i ) );
-                    }
-
-                    records.add( rows );
+                    rows.add( rs.getObject( i ) );
                 }
+
+                records.add( rows );
             }
         }
-        catch ( SQLException e )
+        catch ( SQLException ex )
         {
-            e.printStackTrace();
+            throw new RuntimeException( ex );
         }
     }
 
@@ -170,15 +166,4 @@
         }
         return true;
     }
-
-    // -------------------------------------------------------------------------
-    // toString()
-    // -------------------------------------------------------------------------
-
-    public String toString()
-    {
-        return null;
-
-    }
-
 }

=== 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	2012-03-12 14:27:51 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/grid/ListGrid.java	2012-04-03 08:51:42 +0000
@@ -27,13 +27,21 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import com.fasterxml.jackson.annotation.JsonProperty;
-import com.fasterxml.jackson.annotation.JsonView;
-import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
-import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
-import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
+import static org.hisp.dhis.system.util.MathUtils.getRounded;
+
+import java.sql.ResultSet;
+import java.sql.ResultSetMetaData;
+import java.sql.SQLException;
+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;
+
 import org.apache.commons.math.stat.regression.SimpleRegression;
 import org.hisp.dhis.common.Dxf2Namespace;
 import org.hisp.dhis.common.Grid;
@@ -41,10 +49,11 @@
 import org.hisp.dhis.common.view.DetailedView;
 import org.hisp.dhis.system.util.MathUtils;
 
-import javax.xml.bind.annotation.XmlElement;
-import java.util.*;
-
-import static org.hisp.dhis.system.util.MathUtils.getRounded;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.fasterxml.jackson.annotation.JsonView;
+import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
+import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
+import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
 
 /**
  * @author Lars Helge Overland
@@ -493,6 +502,51 @@
     }
 
     // -------------------------------------------------------------------------
+    // SQL utility methods
+    // -------------------------------------------------------------------------
+
+    public void addHeaders( ResultSet rs )
+    {
+        try
+        {
+            ResultSetMetaData rsmd = rs.getMetaData();
+
+            int columnNo = rsmd.getColumnCount();
+
+            for ( int i = 1; i <= columnNo; i++ )
+            {
+                addHeader( new GridHeader( rsmd.getColumnLabel( i ), false, false ) );
+            }
+        }
+        catch ( SQLException ex )
+        {
+            throw new RuntimeException( ex );
+        }
+    }
+
+    public void addRow( ResultSet rs )
+    {
+        try
+        {
+            int columnNo = rs.getMetaData().getColumnCount();
+
+            while ( rs.next() )
+            {
+                addRow();
+                
+                for ( int i = 1; i <= columnNo; i++ )
+                {
+                    addValue( rs.getObject( i ) );
+                }
+            }
+        }
+        catch ( SQLException ex )
+        {
+            throw new RuntimeException( ex );
+        }
+    }
+    
+    // -------------------------------------------------------------------------
     // Supportive methods
     // -------------------------------------------------------------------------