← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 16813: Event analytics. For analysis with option sets, including item for no value selected within optio...

 

------------------------------------------------------------
revno: 16813
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2014-09-25 12:17:34 +0200
message:
  Event analytics. For analysis with option sets, including item for no value selected within option set as [n/a]
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/DimensionalObjectUtils.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/NameableObjectUtils.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/eventchart/EventChart.java
  dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/event/data/EventAnalyticsServiceTest.java
  dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/grid/GridUtils.java
  dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/TextUtils.java
  dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/TextUtilsTest.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/DimensionalObjectUtils.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/DimensionalObjectUtils.java	2014-08-15 07:40:20 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/DimensionalObjectUtils.java	2014-09-25 10:17:34 +0000
@@ -284,9 +284,11 @@
      * analytics type must be equal to EVENT.
      * 
      * @param dimension the dimension.
+     * @param naForNull indicates whether a [n/a] string should be used as
+     *        replacement for null values.
      * @param grid the grid with data values.
      */
-    public static void setDimensionItemsForFilters( DimensionalObject dimension, Grid grid )
+    public static void setDimensionItemsForFilters( DimensionalObject dimension, Grid grid, boolean naForNull )
     {
         if ( dimension == null || grid == null || !AnalyticsType.EVENT.equals( dimension.getAnalyticsType() ) )
         {
@@ -295,7 +297,7 @@
             
         BaseDimensionalObject dim = (BaseDimensionalObject) dimension;
         Set<Object> values = grid.getUniqueValues( dim.getDimension() );
-        List<NameableObject> items = NameableObjectUtils.getNameableObjects( values );
+        List<NameableObject> items = NameableObjectUtils.getNameableObjects( values, naForNull );
         dim.setItems( items );
     }
     

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/NameableObjectUtils.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/NameableObjectUtils.java	2014-08-15 07:40:20 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/NameableObjectUtils.java	2014-09-25 10:17:34 +0000
@@ -40,6 +40,8 @@
  */
 public class NameableObjectUtils
 {
+    public static final String NULL_REPLACEMENT = "[n/a]";
+    
     /**
      * Returns a list of NameableObjects.
      *
@@ -115,14 +117,21 @@
      * the value of each list item.
      * 
      * @param values the list of object values.
+     * @param naForNull indicates whether a [n/a] string should be used as
+     *        replacement for null values.
      * @return a list of BaseNameableObejcts.
      */
-    public static List<NameableObject> getNameableObjects( Collection<Object> values )
+    public static List<NameableObject> getNameableObjects( Collection<Object> values, boolean naForNull )
     {
         List<NameableObject> objects = new ArrayList<>();
         
         for ( Object value : values )
         {
+            if ( value == null && naForNull )
+            {
+                value = NULL_REPLACEMENT;
+            }
+            
             if ( value != null )
             {
                 String val = String.valueOf( value );

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/eventchart/EventChart.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/eventchart/EventChart.java	2014-09-12 11:14:27 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/eventchart/EventChart.java	2014-09-25 10:17:34 +0000
@@ -134,7 +134,7 @@
         DimensionalObject object = getDimensionalObject( series, relativePeriodDate, user, true,
             organisationUnitsAtLevel, organisationUnitsInGroups, format );
 
-        DimensionalObjectUtils.setDimensionItemsForFilters( object, dataItemGrid );
+        DimensionalObjectUtils.setDimensionItemsForFilters( object, dataItemGrid, true );
         
         return object != null ? object.getItems() : null;
     }
@@ -146,7 +146,7 @@
         DimensionalObject object = getDimensionalObject( category, relativePeriodDate, user, true,
             organisationUnitsAtLevel, organisationUnitsInGroups, format );
 
-        DimensionalObjectUtils.setDimensionItemsForFilters( object, dataItemGrid );
+        DimensionalObjectUtils.setDimensionItemsForFilters( object, dataItemGrid, true );
         
         return object != null ? object.getItems() : null;
     }

=== modified file 'dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/event/data/EventAnalyticsServiceTest.java'
--- dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/event/data/EventAnalyticsServiceTest.java	2014-08-15 07:40:20 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/event/data/EventAnalyticsServiceTest.java	2014-09-25 10:17:34 +0000
@@ -232,12 +232,13 @@
         grid.addRow().addValue( "1" );
         grid.addRow().addValue( "2" );
         grid.addRow().addValue( "3" );
+        grid.addRow().addValue( null );
         
         chart.populateAnalyticalProperties();
         
         DimensionalObject dim = chart.getColumns().get( 0 );
         
-        DimensionalObjectUtils.setDimensionItemsForFilters( dim, grid );
+        DimensionalObjectUtils.setDimensionItemsForFilters( dim, grid, true );
         
         assertNotNull( dim );
         assertEquals( tea.getDimension(), dim.getDimension() );
@@ -246,7 +247,7 @@
         assertEquals( tead.getFilter(), dim.getFilter() );
         
         List<NameableObject> items = dim.getItems();
-        assertEquals( 3, items.size() );
+        assertEquals( 4, items.size() );
         assertNotNull( items.get( 0 ).getUid() );
         assertNotNull( items.get( 0 ).getName() );
         assertNotNull( items.get( 0 ).getCode() );

=== modified file 'dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/grid/GridUtils.java'
--- dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/grid/GridUtils.java	2014-08-15 07:40:20 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/grid/GridUtils.java	2014-09-25 10:17:34 +0000
@@ -28,6 +28,7 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import static org.apache.commons.lang.StringUtils.trimToEmpty;
 import static org.hisp.dhis.common.DimensionalObject.DIMENSION_SEP;
 import static org.hisp.dhis.system.util.CsvUtils.NEWLINE;
 import static org.hisp.dhis.system.util.CsvUtils.SEPARATOR_B;
@@ -41,7 +42,6 @@
 import static org.hisp.dhis.system.util.PDFUtils.getTitleCell;
 import static org.hisp.dhis.system.util.PDFUtils.openDocument;
 import static org.hisp.dhis.system.util.PDFUtils.resetPaddings;
-import static org.apache.commons.lang.StringUtils.*;
 
 import java.io.OutputStream;
 import java.io.StringWriter;
@@ -72,6 +72,7 @@
 import org.apache.velocity.VelocityContext;
 import org.hisp.dhis.common.Grid;
 import org.hisp.dhis.common.GridHeader;
+import org.hisp.dhis.common.NameableObjectUtils;
 import org.hisp.dhis.system.util.CodecUtils;
 import org.hisp.dhis.system.util.DateUtils;
 import org.hisp.dhis.system.util.Encoder;
@@ -79,6 +80,7 @@
 import org.hisp.dhis.system.util.ListUtils;
 import org.hisp.dhis.system.util.MathUtils;
 import org.hisp.dhis.system.util.StreamUtils;
+import org.hisp.dhis.system.util.TextUtils;
 import org.hisp.dhis.system.velocity.VelocityManager;
 import org.htmlparser.Node;
 import org.htmlparser.NodeFilter;
@@ -101,8 +103,7 @@
 {
     private static final Log log = LogFactory.getLog( GridUtils.class );
     
-    private static final String EMPTY = "";
-    
+    private static final String EMPTY = "";    
     private static final String XLS_SHEET_PREFIX = "Sheet ";
     
     private static final NodeFilter HTML_ROW_FILTER = new OrFilter( new TagNameFilter( "td" ), new TagNameFilter( "th" ) );    
@@ -615,7 +616,9 @@
 
         for ( List<Object> row : grid.getRows() )
         {
-            String key = StringUtils.join( ListUtils.getAtIndexes( row, metaIndexes ), DIMENSION_SEP );
+            List<Object> metaDataRowItems = ListUtils.getAtIndexes( row, metaIndexes );
+            
+            String key = TextUtils.join( metaDataRowItems, DIMENSION_SEP, NameableObjectUtils.NULL_REPLACEMENT );
             
             map.put( key, Double.parseDouble( trimToEmpty( String.valueOf( row.get( valueIndex ) ) ) ) );
         }

=== modified file 'dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/TextUtils.java'
--- dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/TextUtils.java	2014-08-15 07:40:20 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/TextUtils.java	2014-09-25 10:17:34 +0000
@@ -31,6 +31,7 @@
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.List;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
@@ -282,6 +283,32 @@
     }
 
     /**
+     * Joins the elements of the provided array into a single String containing 
+     * the provided list of elements.
+     * 
+     * @param list the list of objects to join.
+     * @param separator the separator string.
+     * @param nullReplacement the value to replace nulls in list with.
+     * @return the joined string.
+     */
+    public static <T> String join( List<T> list, String separator, T nullReplacement )
+    {
+        if ( list == null )
+        {
+            return null;
+        }
+        
+        List<T> objects = new ArrayList<T>( list );
+        
+        if ( nullReplacement != null )
+        {
+            Collections.replaceAll( objects, null, nullReplacement );
+        }
+        
+        return StringUtils.join( objects, separator );
+    }
+    
+    /**
      * Transforms a collection of Integers into a comma delimited String. If the
      * given collection of elements are null or is empty, an empty String is
      * returned.

=== modified file 'dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/TextUtilsTest.java'
--- dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/TextUtilsTest.java	2014-08-15 07:40:20 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/TextUtilsTest.java	2014-09-25 10:17:34 +0000
@@ -116,4 +116,14 @@
         assertEquals( "tom, john", TextUtils.removeLastComma( "tom, john, " ) );
         assertEquals( "tom, john", TextUtils.removeLastComma( "tom, john,  " ) );
     }
+    
+    @Test
+    public void testJoinReplaceNull()
+    {
+        assertEquals( "green-red-blue", TextUtils.join( Arrays.asList( "green", "red", "blue" ), "-", "[n/a]" ) );
+        assertEquals( "green-[n/a]-blue", TextUtils.join( Arrays.asList( "green", null, "blue" ), "-", "[n/a]" ) );
+        assertEquals( "green-red-[n/a]", TextUtils.join( Arrays.asList( "green", "red", null ), "-", "[n/a]" ) );
+        assertEquals( "greenred[n/a]", TextUtils.join( Arrays.asList( "green", "red", null ), null, "[n/a]" ) );
+        assertEquals( "greenred", TextUtils.join( Arrays.asList( "green", "red", null ), null, null ) );
+    }
 }