← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 10562: Merged DataBrowserUtils into DataBrowserStore. The former is not really a good candidate for a ut...

 

------------------------------------------------------------
revno: 10562
committer: Lars Helge Øverland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2013-04-15 18:48:56 +0200
message:
  Merged DataBrowserUtils into DataBrowserStore. The former is not really a good candidate for a util class as it contains browsing queries.
removed:
  dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/databrowser/util/
  dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/databrowser/util/DataBrowserUtils.java
modified:
  dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/databrowser/jdbc/JDBCDataBrowserStore.java
  dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/statistics/jdbc/JdbcStatisticsProvider.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-administration/src/main/java/org/hisp/dhis/databrowser/jdbc/JDBCDataBrowserStore.java'
--- dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/databrowser/jdbc/JDBCDataBrowserStore.java	2012-09-13 11:16:27 +0000
+++ dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/databrowser/jdbc/JDBCDataBrowserStore.java	2013-04-15 16:48:56 +0000
@@ -1,11 +1,18 @@
 package org.hisp.dhis.databrowser.jdbc;
 
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.ArrayList;
 import java.util.List;
 
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 import org.hisp.dhis.common.Grid;
 import org.hisp.dhis.common.GridHeader;
 import org.hisp.dhis.databrowser.DataBrowserGridStore;
-import org.hisp.dhis.databrowser.util.DataBrowserUtils;
+import org.hisp.dhis.databrowser.MetaValue;
 import org.hisp.dhis.jdbc.StatementBuilder;
 import org.hisp.dhis.organisationunit.OrganisationUnitService;
 import org.hisp.dhis.period.PeriodType;
@@ -17,9 +24,10 @@
  * @version $Id JDBCDataBrowserStore.java 2010-04-06 jpp, ddhieu$
  */
 public class JDBCDataBrowserStore
-    extends DataBrowserUtils
     implements DataBrowserGridStore
 {
+    private static final Log log = LogFactory.getLog( JDBCDataBrowserStore.class );
+    
     // -------------------------------------------------------------------------
     // Dependencies
     // -------------------------------------------------------------------------
@@ -47,8 +55,6 @@
 
     // -------------------------------------------------------------------------
     // DataBrowserStore implementation
-    //
-    // Basic
     // -------------------------------------------------------------------------
 
     public Grid getDataSetsBetweenPeriods( List<Integer> betweenPeriodIds, PeriodType periodType, boolean isZeroAdded )
@@ -302,8 +308,6 @@
 
     }
 
-    // This method retrieves raw data for a given orgunit, periods,
-
     public Integer setRawDataElementsForOrgUnitBetweenPeriods( Grid grid, Integer orgUnitId,
         List<Integer> betweenPeriodIds, List<Integer> metaIds, boolean isZeroAdded )
     {
@@ -315,8 +319,179 @@
     }
 
     // -------------------------------------------------------------------------
+    // Private methods
+    // -------------------------------------------------------------------------
+
+    private static void setMetaStructure( Grid grid, StringBuffer sqlsb, List<Integer> metaIds, JdbcTemplate jdbcTemplate )
+    {
+        try
+        {
+            Integer metaId = null;
+            String metaName = null;
+            ResultSet resultSet = getScrollableResult( sqlsb.toString(), jdbcTemplate );
+
+            while ( resultSet.next() )
+            {
+                metaId = resultSet.getInt( 1 );
+                metaName = resultSet.getString( 2 );
+
+                metaIds.add( metaId );
+                grid.addRow().addValue( new MetaValue( metaId, metaName ) );
+            }
+        }
+        catch ( SQLException e )
+        {
+            log.error( "Failed to add meta value\n" + sqlsb.toString() );
+            throw new RuntimeException( "Failed to add meta value\n", e );
+        }
+        catch ( Exception e )
+        {
+            throw new RuntimeException( "Oops. Something else went wrong in setMetaStructure()", e );
+        }
+    }
+
+    private static void setHeaderStructure( Grid grid, ResultSet resultSet, List<Integer> headerIds, boolean isZeroAdded )
+    {
+        try
+        {
+            Integer headerId = null;
+            String headerName = null;
+
+            while ( resultSet.next() )
+            {
+                headerId = resultSet.getInt( 4 );
+                headerName = resultSet.getString( 5 );
+
+                GridHeader header = new GridHeader( headerName, headerId + "", String.class.getName(), false, false );
+
+                if ( !headerIds.contains( headerId ) )
+                {
+                    headerIds.add( headerId );
+                    grid.addHeader( header );
+
+                    for ( List<Object> row : grid.getRows() )
+                    {
+                        row.add( isZeroAdded ? "0" : "" );
+                    }
+                }
+            }
+        }
+        catch ( SQLException e )
+        {
+            throw new RuntimeException( "Failed to add header\n", e );
+        }
+        catch ( Exception e )
+        {
+            throw new RuntimeException( "Oops. Something else went wrong in setHeaderStructure()", e );
+        }
+    }
+
+    private static int fillUpDataBasic( Grid grid, StringBuffer sqlsb, boolean isZeroAdded, JdbcTemplate jdbcTemplate )
+    {
+        int countRows = 0;
+
+        try
+        {
+            ResultSet resultSet = getScrollableResult( sqlsb.toString(), jdbcTemplate );
+
+            while ( resultSet.next() )
+            {
+                MetaValue metaValue = new MetaValue( resultSet.getInt( 1 ), resultSet.getString( 2 ) );
+
+                grid.addRow().addValue( metaValue ).addValue( checkValue( resultSet.getString( 3 ), isZeroAdded ) );
+            }
+        }
+        catch ( SQLException e )
+        {
+            log.error( "Error executing" + sqlsb.toString() );
+            throw new RuntimeException( "Failed to get aggregated data value\n", e );
+        }
+        catch ( Exception e )
+        {
+            throw new RuntimeException( "Oops. Something else went wrong", e );
+        }
+
+        return countRows;
+    }
+
+    private static int fillUpDataAdvance( Grid grid, StringBuffer sqlsb, List<Integer> metaIds, boolean isZeroAdded,
+        JdbcTemplate jdbcTemplate )
+    {
+        int countRows = 0;
+        int rowIndex = -1;
+        int columnIndex = -1;
+        int oldWidth = grid.getWidth();
+
+        try
+        {
+            ResultSet rs = getScrollableResult( sqlsb.toString(), jdbcTemplate );
+
+            List<Integer> headerIds = new ArrayList<Integer>();
+            setHeaderStructure( grid, rs, headerIds, isZeroAdded );
+
+            if ( rs.first() != true )
+            {
+                return countRows;
+            }
+
+            rs.beforeFirst();
+
+            while ( rs.next() )
+            {
+                rowIndex = metaIds.indexOf( rs.getInt( 1 ) );
+                columnIndex = headerIds.indexOf( rs.getInt( 4 ) ) + oldWidth;
+
+                grid.getRow( rowIndex ).set( columnIndex, checkValue( rs.getString( 3 ), isZeroAdded ) );
+
+                countRows++;
+            }
+        }
+        catch ( SQLException e )
+        {
+            log.error( "Error executing" + sqlsb.toString() );
+            throw new RuntimeException( "Failed to get aggregated data value\n", e );
+
+        }
+        catch ( Exception e )
+        {
+            throw new RuntimeException( "Oops. Somthing else went wrong", e );
+        }
+
+        return countRows;
+    }
+
+    // -------------------------------------------------------------------------
     // Supportive methods
     // -------------------------------------------------------------------------
+
+    /**
+     * Uses StatementManager to obtain a scroll-able, read-only ResultSet based
+     * on the query string.
+     * 
+     * @param sql the query
+     * @param holder the StatementHolder object
+     * @return null or the ResultSet
+     */
+    private static ResultSet getScrollableResult( String sql, JdbcTemplate jdbcTemplate )
+        throws SQLException
+    {
+        Connection con = jdbcTemplate.getDataSource().getConnection();
+        Statement stm = con.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY );
+        stm.execute( sql );
+        log.debug( sql );
+
+        return stm.getResultSet();
+    }
+
+    private static String checkValue( String value, boolean isZeroAdded )
+    {
+        if ( value == null )
+        {
+            return "null";
+        }
+        return (value.equals( "0" ) && !isZeroAdded) ? "" : value;
+    }
+    
     /**
      * Splits a list of integers by by comma. Use this method if you have a list
      * that will be used in f.ins. a WHERE xxx IN (list) clause in SQL.
@@ -410,5 +585,4 @@
 
         return desc_query.toString();
     }
-
 }

=== removed directory 'dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/databrowser/util'
=== removed file 'dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/databrowser/util/DataBrowserUtils.java'
--- dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/databrowser/util/DataBrowserUtils.java	2012-07-20 09:12:57 +0000
+++ dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/databrowser/util/DataBrowserUtils.java	1970-01-01 00:00:00 +0000
@@ -1,221 +0,0 @@
-package org.hisp.dhis.databrowser.util;
-
-/*
- * Copyright (c) 2004-2011, 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 java.sql.Connection;
-import java.sql.ResultSet;
-import java.sql.SQLException;
-import java.sql.Statement;
-import java.util.ArrayList;
-import java.util.List;
-
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-import org.hisp.dhis.common.Grid;
-import org.hisp.dhis.common.GridHeader;
-import org.hisp.dhis.databrowser.MetaValue;
-import org.springframework.jdbc.core.JdbcTemplate;
-
-/**
- * @author Dang Duy Hieu
- * @version $Id$
- */
-public class DataBrowserUtils
-{
-    protected static final Log log = LogFactory.getLog( DataBrowserUtils.class );
-
-    public static void setMetaStructure( Grid grid, StringBuffer sqlsb, List<Integer> metaIds, JdbcTemplate jdbcTemplate )
-    {
-        try
-        {
-            Integer metaId = null;
-            String metaName = null;
-            ResultSet resultSet = getScrollableResult( sqlsb.toString(), jdbcTemplate );
-
-            while ( resultSet.next() )
-            {
-                metaId = resultSet.getInt( 1 );
-                metaName = resultSet.getString( 2 );
-
-                metaIds.add( metaId );
-                grid.addRow().addValue( new MetaValue( metaId, metaName ) );
-            }
-        }
-        catch ( SQLException e )
-        {
-            log.error( "Failed to add meta value\n" + sqlsb.toString() );
-            throw new RuntimeException( "Failed to add meta value\n", e );
-        }
-        catch ( Exception e )
-        {
-            throw new RuntimeException( "Oops. Something else went wrong in setMetaStructure()", e );
-        }
-    }
-
-    public static void setHeaderStructure( Grid grid, ResultSet resultSet, List<Integer> headerIds, boolean isZeroAdded )
-    {
-        try
-        {
-            Integer headerId = null;
-            String headerName = null;
-
-            while ( resultSet.next() )
-            {
-                headerId = resultSet.getInt( 4 );
-                headerName = resultSet.getString( 5 );
-
-                GridHeader header = new GridHeader( headerName, headerId + "", String.class.getName(), false, false );
-
-                if ( !headerIds.contains( headerId ) )
-                {
-                    headerIds.add( headerId );
-                    grid.addHeader( header );
-
-                    for ( List<Object> row : grid.getRows() )
-                    {
-                        row.add( isZeroAdded ? "0" : "" );
-                    }
-                }
-            }
-        }
-        catch ( SQLException e )
-        {
-            throw new RuntimeException( "Failed to add header\n", e );
-        }
-        catch ( Exception e )
-        {
-            throw new RuntimeException( "Oops. Something else went wrong in setHeaderStructure()", e );
-        }
-    }
-
-    public static int fillUpDataBasic( Grid grid, StringBuffer sqlsb, boolean isZeroAdded, JdbcTemplate jdbcTemplate )
-    {
-        int countRows = 0;
-
-        try
-        {
-            ResultSet resultSet = getScrollableResult( sqlsb.toString(), jdbcTemplate );
-
-            while ( resultSet.next() )
-            {
-                MetaValue metaValue = new MetaValue( resultSet.getInt( 1 ), resultSet.getString( 2 ) );
-
-                grid.addRow().addValue( metaValue ).addValue( checkValue( resultSet.getString( 3 ), isZeroAdded ) );
-            }
-        }
-        catch ( SQLException e )
-        {
-            log.error( "Error executing" + sqlsb.toString() );
-            throw new RuntimeException( "Failed to get aggregated data value\n", e );
-        }
-        catch ( Exception e )
-        {
-            throw new RuntimeException( "Oops. Something else went wrong", e );
-        }
-
-        return countRows;
-    }
-
-    public static int fillUpDataAdvance( Grid grid, StringBuffer sqlsb, List<Integer> metaIds, boolean isZeroAdded,
-        JdbcTemplate jdbcTemplate )
-    {
-        int countRows = 0;
-        int rowIndex = -1;
-        int columnIndex = -1;
-        int oldWidth = grid.getWidth();
-
-        try
-        {
-            ResultSet rs = getScrollableResult( sqlsb.toString(), jdbcTemplate );
-
-            List<Integer> headerIds = new ArrayList<Integer>();
-            setHeaderStructure( grid, rs, headerIds, isZeroAdded );
-
-            if ( rs.first() != true )
-            {
-                return countRows;
-            }
-
-            rs.beforeFirst();
-
-            while ( rs.next() )
-            {
-                rowIndex = metaIds.indexOf( rs.getInt( 1 ) );
-                columnIndex = headerIds.indexOf( rs.getInt( 4 ) ) + oldWidth;
-
-                grid.getRow( rowIndex ).set( columnIndex, checkValue( rs.getString( 3 ), isZeroAdded ) );
-
-                countRows++;
-            }
-        }
-        catch ( SQLException e )
-        {
-            log.error( "Error executing" + sqlsb.toString() );
-            throw new RuntimeException( "Failed to get aggregated data value\n", e );
-
-        }
-        catch ( Exception e )
-        {
-            throw new RuntimeException( "Oops. Somthing else went wrong", e );
-        }
-
-        return countRows;
-    }
-
-    // -------------------------------------------------------------------------
-    // Supportive methods
-    // -------------------------------------------------------------------------
-
-    /**
-     * Uses StatementManager to obtain a scroll-able, read-only ResultSet based
-     * on the query string.
-     * 
-     * @param sql the query
-     * @param holder the StatementHolder object
-     * @return null or the ResultSet
-     */
-    private static ResultSet getScrollableResult( String sql, JdbcTemplate jdbcTemplate )
-        throws SQLException
-    {
-        Connection con = jdbcTemplate.getDataSource().getConnection();
-        Statement stm = con.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY );
-        stm.execute( sql );
-        log.debug( sql );
-
-        return stm.getResultSet();
-    }
-
-    private static String checkValue( String value, boolean isZeroAdded )
-    {
-        if ( value == null )
-        {
-            return "null";
-        }
-        return (value.equals( "0" ) && !isZeroAdded) ? "" : value;
-    }
-}

=== modified file 'dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/statistics/jdbc/JdbcStatisticsProvider.java'
--- dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/statistics/jdbc/JdbcStatisticsProvider.java	2012-07-12 12:39:43 +0000
+++ dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/statistics/jdbc/JdbcStatisticsProvider.java	2013-04-15 16:48:56 +0000
@@ -60,18 +60,18 @@
     {
         final Map<Objects, Integer> objectCounts = new HashMap<Objects, Integer>();
         
-        objectCounts.put( Objects.DATAELEMENT, jdbcTemplate.queryForInt( "SELECT COUNT(*) FROM dataelement" ) );
-        objectCounts.put( Objects.DATAELEMENTGROUP, jdbcTemplate.queryForInt( "SELECT COUNT(*) FROM dataelementgroup" ) );
-        objectCounts.put( Objects.INDICATORTYPE, jdbcTemplate.queryForInt( "SELECT COUNT(*) FROM indicatortype" ) );
-        objectCounts.put( Objects.INDICATOR, jdbcTemplate.queryForInt( "SELECT COUNT(*) FROM indicator" ) );
-        objectCounts.put( Objects.INDICATORGROUP, jdbcTemplate.queryForInt( "SELECT COUNT(*) FROM indicatorgroup" ) );
-        objectCounts.put( Objects.DATASET, jdbcTemplate.queryForInt( "SELECT COUNT(*) FROM dataset" ) );
-        objectCounts.put( Objects.DATADICTIONARY, jdbcTemplate.queryForInt( "SELECT COUNT(*) FROM datadictionary" ) );
-        objectCounts.put( Objects.SOURCE, jdbcTemplate.queryForInt( "SELECT COUNT(*) FROM organisationunit" ) );
-        objectCounts.put( Objects.VALIDATIONRULE, jdbcTemplate.queryForInt( "SELECT COUNT(*) FROM validationrule" ) );
-        objectCounts.put( Objects.PERIOD, jdbcTemplate.queryForInt( "SELECT COUNT(*) FROM period" ) );
-        objectCounts.put( Objects.USER, jdbcTemplate.queryForInt( "SELECT COUNT(*) FROM users" ) );
-        objectCounts.put( Objects.DATAVALUE, jdbcTemplate.queryForInt( "SELECT COUNT(*) FROM datavalue" ) );
+        objectCounts.put( Objects.DATAELEMENT, jdbcTemplate.queryForObject( "SELECT COUNT(*) FROM dataelement", Integer.class ) );
+        objectCounts.put( Objects.DATAELEMENTGROUP, jdbcTemplate.queryForObject( "SELECT COUNT(*) FROM dataelementgroup", Integer.class ) );
+        objectCounts.put( Objects.INDICATORTYPE, jdbcTemplate.queryForObject( "SELECT COUNT(*) FROM indicatortype", Integer.class ) );
+        objectCounts.put( Objects.INDICATOR, jdbcTemplate.queryForObject( "SELECT COUNT(*) FROM indicator", Integer.class ) );
+        objectCounts.put( Objects.INDICATORGROUP, jdbcTemplate.queryForObject( "SELECT COUNT(*) FROM indicatorgroup", Integer.class ) );
+        objectCounts.put( Objects.DATASET, jdbcTemplate.queryForObject( "SELECT COUNT(*) FROM dataset", Integer.class ) );
+        objectCounts.put( Objects.DATADICTIONARY, jdbcTemplate.queryForObject( "SELECT COUNT(*) FROM datadictionary", Integer.class ) );
+        objectCounts.put( Objects.SOURCE, jdbcTemplate.queryForObject( "SELECT COUNT(*) FROM organisationunit", Integer.class ) );
+        objectCounts.put( Objects.VALIDATIONRULE, jdbcTemplate.queryForObject( "SELECT COUNT(*) FROM validationrule", Integer.class ) );
+        objectCounts.put( Objects.PERIOD, jdbcTemplate.queryForObject( "SELECT COUNT(*) FROM period", Integer.class ) );
+        objectCounts.put( Objects.USER, jdbcTemplate.queryForObject( "SELECT COUNT(*) FROM users", Integer.class ) );
+        objectCounts.put( Objects.DATAVALUE, jdbcTemplate.queryForObject( "SELECT COUNT(*) FROM datavalue", Integer.class ) );
         
         return objectCounts;
     }