dhis2-devs team mailing list archive
-
dhis2-devs team
-
Mailing list archive
-
Message #10490
[Branch ~dhis2-devs-core/dhis2/trunk] Rev 2873: Added functions for sorting and limiting to Grid object
Merge authors:
Lars Helge Øverland (larshelge)
------------------------------------------------------------
revno: 2873 [merge]
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Fri 2011-02-18 17:09:26 +0100
message:
Added functions for sorting and limiting 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/reporttable/ReportTable.java
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/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 12:14:04 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/Grid.java 2011-02-17 22:21:12 +0000
@@ -165,6 +165,21 @@
Grid removeColumn( GridHeader header );
/**
+ * Limits the grid from top by the given argument number.
+ *
+ * @param limit the top limit.
+ */
+ Grid limitGrid( int limit );
+
+ /**
+ * Sorts the grid ascending on the column at the given columnIndex.
+ *
+ * @param columnIndex the column index, starting on 1.
+ * @param descending indicates whether to sort ascending or descending.
+ */
+ Grid sortGrid( int columnIndex, boolean descending );
+
+ /**
* Adds a regression column to the grid. Column must hold numeric data.
*
* @param columnIndex the index of the base column.
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/reporttable/ReportTable.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/reporttable/ReportTable.java 2011-02-17 18:48:30 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/reporttable/ReportTable.java 2011-02-17 22:48:17 +0000
@@ -388,40 +388,18 @@
addIfEmpty( columns ); // Allow for all or none crosstab dimensions
addIfEmpty( rows );
- if ( !isDoIndicators() )
- {
- indexColumns.add( INDICATOR_ID );
- indexNameColumns.add( INDICATOR_NAME );
- }
-
- if ( !isDoPeriods() )
- {
- indexColumns.add( PERIOD_ID );
- indexNameColumns.add( PERIOD_NAME );
- }
-
- if ( !isDoUnits() )
- {
- indexColumns.add( ORGANISATIONUNIT_ID );
- indexNameColumns.add( ORGANISATIONUNIT_NAME );
- }
+ add( indexColumns, INDICATOR_ID, doIndicators );
+ add( indexColumns, PERIOD_ID, doPeriods );
+ add( indexColumns, ORGANISATIONUNIT_ID, doUnits );
+ add( indexNameColumns, INDICATOR_NAME, doIndicators );
+ add( indexNameColumns, PERIOD_NAME, doPeriods );
+ add( indexNameColumns, ORGANISATIONUNIT_NAME, doUnits );
}
- /**
- * Adds an empty list of IdentifiableObjects to the given list if empty.
- */
- private void addIfEmpty( List<List<IdentifiableObject>> list )
- {
- if ( list != null && list.size() == 0 )
- {
- list.add( Arrays.asList( new IdentifiableObject[0] ) );
- }
- }
-
// -------------------------------------------------------------------------
// Public methods
// -------------------------------------------------------------------------
-
+
/**
* Updates the existing table name with the current name.
*/
@@ -667,6 +645,17 @@
{
return TABLE_PREFIX + databaseEncode( name );
}
+
+ /**
+ * Adds an empty list of IdentifiableObjects to the given list if empty.
+ */
+ private void addIfEmpty( List<List<IdentifiableObject>> list )
+ {
+ if ( list != null && list.size() == 0 )
+ {
+ list.add( Arrays.asList( new IdentifiableObject[0] ) );
+ }
+ }
/**
* Returns the number of empty lists among the argument lists.
@@ -703,6 +692,17 @@
}
/**
+ * Adds the given object to the given list if not skip argument is true.
+ */
+ private static <T> void add( List<T> list, T object, boolean skip )
+ {
+ if ( !skip )
+ {
+ list.add( object );
+ }
+ }
+
+ /**
* Removes duplicates from the given list while maintaining the order.
*/
private static <T> List<T> removeDuplicates( List<T> list )
=== 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-15 18:56:16 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/grid/ListGrid.java 2011-02-17 22:23:32 +0000
@@ -30,6 +30,8 @@
import static org.hisp.dhis.system.util.MathUtils.getRounded;
import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
import java.util.List;
import net.sf.jasperreports.engine.JRException;
@@ -301,6 +303,27 @@
return this;
}
+ public Grid limitGrid( int limit )
+ {
+ grid = grid.subList( 0, limit );
+
+ return this;
+ }
+
+ public Grid sortGrid( int columnIndex, boolean descending )
+ {
+ columnIndex--;
+
+ if ( columnIndex < 0 || columnIndex >= getWidth() )
+ {
+ return this; // Silently ignore
+ }
+
+ Collections.sort( grid, new GridRowComparator( columnIndex, descending ) );
+
+ return this;
+ }
+
public Grid addRegressionColumn( int columnIndex )
{
verifyGridState();
@@ -344,9 +367,9 @@
return this;
}
- // ---------------------------------------------------------------------
+ // -------------------------------------------------------------------------
// JRDataSource implementation
- // ---------------------------------------------------------------------
+ // -------------------------------------------------------------------------
public boolean next()
throws JRException
@@ -373,9 +396,9 @@
return headerIndex != -1 ? getRow( currentRowReadIndex ).get( headerIndex ) : null;
}
- // ---------------------------------------------------------------------
+ // -------------------------------------------------------------------------
// Supportive methods
- // ---------------------------------------------------------------------
+ // -------------------------------------------------------------------------
/**
* Verifies that all grid rows are of the same length, and that the number
@@ -402,14 +425,14 @@
}
}
- // ---------------------------------------------------------------------
+ // -------------------------------------------------------------------------
// toString
- // ---------------------------------------------------------------------
+ // -------------------------------------------------------------------------
@Override
public String toString()
{
- StringBuffer buffer = new StringBuffer( "[" );
+ StringBuffer buffer = new StringBuffer( "[\n" );
if ( headers != null && headers.size() > 0 )
{
@@ -430,4 +453,28 @@
return buffer.append( "]" ).toString();
}
+
+ // -------------------------------------------------------------------------
+ // Comparator
+ // -------------------------------------------------------------------------
+
+ protected static class GridRowComparator
+ implements Comparator<List<String>>
+ {
+ private int columnIndex;
+ private boolean descending;
+
+ protected GridRowComparator( int columnIndex, boolean descending )
+ {
+ this.columnIndex = columnIndex;
+ this.descending = descending;
+ }
+
+ @Override
+ public int compare( List<String> list1, List<String> list2 )
+ {
+ return descending ? list2.get( columnIndex ).compareTo(
+ list1.get( columnIndex ) ) : list1.get( columnIndex ).compareTo( list2.get( columnIndex ) );
+ }
+ }
}
=== 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 12:14:04 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/grid/GridTest.java 2011-02-17 22:21:12 +0000
@@ -39,6 +39,8 @@
import org.junit.Before;
import org.junit.Test;
+import org.hisp.dhis.system.grid.ListGrid;
+
/**
* @author Lars Helge Overland
* @version $Id$
@@ -213,6 +215,58 @@
assertEquals( 2, grid.getWidth() );
}
+
+ @Test
+ public void testLimit()
+ {
+ assertEquals( 4, grid.getRows().size() );
+
+ grid.limitGrid( 2 );
+
+ assertEquals( 2, grid.getRows().size() );
+
+ List<String> rowA = grid.getRow( 0 );
+ assertTrue( rowA.contains( "11" ) );
+
+ List<String> rowB = grid.getRow( 1 );
+ assertTrue( rowB.contains( "21" ) );
+ }
+
+ @Test
+ public void testSortA()
+ {
+ Grid grid = new ListGrid();
+
+ grid.addRow().addValue( "1" ).addValue( "a" );
+ grid.addRow().addValue( "2" ).addValue( "b" );
+ grid.addRow().addValue( "3" ).addValue( "c" );
+
+ grid.sortGrid( 2, true );
+
+ List<String> rowA = grid.getRow( 0 );
+ assertTrue( rowA.contains( "c" ) );
+
+ List<String> rowB = grid.getRow( 1 );
+ assertTrue( rowB.contains( "b" ) );
+ }
+
+ @Test
+ public void testSortB()
+ {
+ Grid grid = new ListGrid();
+
+ grid.addRow().addValue( "3" ).addValue( "a" );
+ grid.addRow().addValue( "2" ).addValue( "b" );
+ grid.addRow().addValue( "1" ).addValue( "c" );
+
+ grid.sortGrid( 1, false );
+
+ List<String> rowA = grid.getRow( 0 );
+ assertTrue( rowA.contains( "1" ) );
+
+ List<String> rowB = grid.getRow( 1 );
+ assertTrue( rowB.contains( "2" ) );
+ }
@Test
public void testAddRegressionColumn()