← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 2764: Made Grid implement JRDataSource

 

------------------------------------------------------------
revno: 2764
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2011-01-26 13:14:04 +0100
message:
  Made Grid implement JRDataSource
added:
  dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/grid/MockJRField.java
modified:
  dhis-2/dhis-api/pom.xml
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/Grid.java
  dhis-2/dhis-support/dhis-support-system/pom.xml
  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/test/java/org/hisp/dhis/system/grid/GridTest.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/pom.xml'
--- dhis-2/dhis-api/pom.xml	2011-01-23 00:12:53 +0000
+++ dhis-2/dhis-api/pom.xml	2011-01-26 12:14:04 +0000
@@ -30,6 +30,10 @@
       <groupId>jfree</groupId>
       <artifactId>jfreechart</artifactId>
     </dependency>
+    <dependency>
+      <groupId>net.sf.jasperreports</groupId>
+      <artifactId>jasperreports</artifactId>
+    </dependency>
   </dependencies>
   
   <properties>

=== 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	2011-01-26 10:01:53 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/Grid.java	2011-01-26 12:14:04 +0000
@@ -29,10 +29,13 @@
 
 import java.util.List;
 
+import net.sf.jasperreports.engine.JRDataSource;
+
 /**
  * @author Lars Helge Overland
  */
 public interface Grid
+    extends JRDataSource
 {
     /**
      * Returns the grid title.

=== modified file 'dhis-2/dhis-support/dhis-support-system/pom.xml'
--- dhis-2/dhis-support/dhis-support-system/pom.xml	2011-01-23 00:12:53 +0000
+++ dhis-2/dhis-support/dhis-support-system/pom.xml	2011-01-26 12:14:04 +0000
@@ -100,6 +100,10 @@
       <groupId>net.sourceforge.jexcelapi</groupId>
       <artifactId>jxl</artifactId>
     </dependency>
+    <dependency>
+      <groupId>net.sf.jasperreports</groupId>
+      <artifactId>jasperreports</artifactId>
+    </dependency>
   </dependencies>
   <properties>
     <rootDir>../../</rootDir>

=== 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-01-26 10:01:53 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/grid/ListGrid.java	2011-01-26 12:14:04 +0000
@@ -32,6 +32,9 @@
 import java.util.ArrayList;
 import java.util.List;
 
+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.Grid;
 import org.hisp.dhis.common.GridHeader;
@@ -70,9 +73,14 @@
     private List<List<String>> grid;
     
     /**
-     * Indicating the current row in the grid.
-     */
-    private int currentRowIndex = -1;
+     * Indicating the current row in the grid for writing data.
+     */
+    private int currentRowWriteIndex = -1;
+    
+    /**
+     * Indicating the current row in the grid for reading data.
+     */
+    private int currentRowReadIndex = -1;
     
     /**
      * Default constructor.
@@ -173,14 +181,14 @@
     {
         grid.add( new ArrayList<String>() );
         
-        currentRowIndex++;
+        currentRowWriteIndex++;
         
         return this;
     }
     
     public Grid addValue( String value )
     {
-        grid.get( currentRowIndex ).add( value );
+        grid.get( currentRowWriteIndex ).add( value );
         
         return this;
     }
@@ -335,7 +343,36 @@
         
         return this;
     }
+
+    // ---------------------------------------------------------------------
+    // JRDataSource implementation
+    // ---------------------------------------------------------------------
+
+    public boolean next()
+        throws JRException
+    {
+        int height = getHeight();
+        
+        return ++currentRowReadIndex < height; 
+    }
     
+    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;
+    }
+
     // ---------------------------------------------------------------------
     // Supportive methods
     // ---------------------------------------------------------------------

=== modified file 'dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/grid/GridTest.java'
--- dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/grid/GridTest.java	2011-01-26 10:01:53 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/grid/GridTest.java	2011-01-26 12:14:04 +0000
@@ -29,6 +29,7 @@
 
 import static junit.framework.Assert.assertEquals;
 import static junit.framework.Assert.assertTrue;
+import static junit.framework.Assert.assertFalse;
 
 import java.util.ArrayList;
 import java.util.List;
@@ -237,4 +238,30 @@
         assertTrue( column.contains( "29.0" ) );
         assertTrue( column.contains( "41.0" ) );
     }
+
+    @Test
+    public void testJRDataSource() throws Exception
+    {
+        assertTrue( grid.next() );
+        assertEquals( "11", (String)grid.getFieldValue( new MockJRField( "colA" ) ) );
+        assertEquals( "12", (String)grid.getFieldValue( new MockJRField( "colB" ) ) );
+        assertEquals( "13", (String)grid.getFieldValue( new MockJRField( "colC" ) ) );
+
+        assertTrue( grid.next() );
+        assertEquals( "21", (String)grid.getFieldValue( new MockJRField( "colA" ) ) );
+        assertEquals( "22", (String)grid.getFieldValue( new MockJRField( "colB" ) ) );
+        assertEquals( "23", (String)grid.getFieldValue( new MockJRField( "colC" ) ) );
+
+        assertTrue( grid.next() );
+        assertEquals( "31", (String)grid.getFieldValue( new MockJRField( "colA" ) ) );
+        assertEquals( "32", (String)grid.getFieldValue( new MockJRField( "colB" ) ) );
+        assertEquals( "33", (String)grid.getFieldValue( new MockJRField( "colC" ) ) );
+
+        assertTrue( grid.next() );
+        assertEquals( "41", (String)grid.getFieldValue( new MockJRField( "colA" ) ) );
+        assertEquals( "42", (String)grid.getFieldValue( new MockJRField( "colB" ) ) );
+        assertEquals( "43", (String)grid.getFieldValue( new MockJRField( "colC" ) ) );
+        
+        assertFalse( grid.next() );
+    }
 }

=== added file 'dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/grid/MockJRField.java'
--- dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/grid/MockJRField.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/grid/MockJRField.java	2011-01-26 12:14:04 +0000
@@ -0,0 +1,87 @@
+package org.hisp.dhis.system.grid;
+
+/*
+ * Copyright (c) 2004-2010, 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 net.sf.jasperreports.engine.JRField;
+import net.sf.jasperreports.engine.JRPropertiesHolder;
+import net.sf.jasperreports.engine.JRPropertiesMap;
+
+public class MockJRField
+    implements JRField
+{
+    private String name;
+    
+    public MockJRField( String name )
+    {
+        this.name = name;
+    }
+    
+    public String getDescription()
+    {
+        return null;
+    }
+
+    public String getName()
+    {
+        return name;
+    }
+
+    public Class<?> getValueClass()
+    {
+        return null;
+    }
+
+    public String getValueClassName()
+    {
+        return null;
+    }
+
+    public void setDescription( String arg0 )
+    {
+    }
+
+    public JRPropertiesHolder getParentProperties()
+    {
+        return null;
+    }
+
+    public JRPropertiesMap getPropertiesMap()
+    {
+        return null;
+    }
+
+    public boolean hasProperties()
+    {
+        return false;
+    }
+    
+    public Object clone()
+    {
+        return null;
+    }
+}