← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 16117: Event analytics, unit testing, supportive methods

 

------------------------------------------------------------
revno: 16117
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Sun 2014-07-13 15:53:40 +0200
message:
  Event analytics, unit testing, supportive methods
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/chart/BaseChart.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/BaseAnalyticalObject.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/BaseDimensionalObject.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/DimensionalObject.java
  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-api/src/test/java/org/hisp/dhis/common/BaseAnalyticalObjectTest.java
  dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/event/data/EventAnalyticsServiceTest.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/sms/J2MEDataValueSMSListener.java
  dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/chart/impl/DefaultChartService.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/chart/BaseChart.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/chart/BaseChart.java	2014-07-12 23:24:13 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/chart/BaseChart.java	2014-07-13 13:53:40 +0000
@@ -36,6 +36,7 @@
 import org.hisp.dhis.common.BaseAnalyticalObject;
 import org.hisp.dhis.common.DimensionalObject;
 import org.hisp.dhis.common.DxfNamespaces;
+import org.hisp.dhis.common.Grid;
 import org.hisp.dhis.common.IdentifiableObject;
 import org.hisp.dhis.common.IdentifiableObjectUtils;
 import org.hisp.dhis.common.NameableObject;
@@ -129,6 +130,8 @@
 
     protected transient List<OrganisationUnit> organisationUnitsInGroups = new ArrayList<OrganisationUnit>();
 
+    protected transient Grid dataItemGrid = null;
+    
     // -------------------------------------------------------------------------
     // Abstract methods
     // -------------------------------------------------------------------------
@@ -214,6 +217,18 @@
         this.relativePeriods = relativePeriods;
     }
 
+    @JsonIgnore
+    public Grid getDataItemGrid()
+    {
+        return dataItemGrid;
+    }
+
+    @JsonIgnore
+    public void setDataItemGrid( Grid dataItemGrid )
+    {
+        this.dataItemGrid = dataItemGrid;
+    }
+    
     // -------------------------------------------------------------------------
     // Getters and setters
     // -------------------------------------------------------------------------

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/BaseAnalyticalObject.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/BaseAnalyticalObject.java	2014-07-12 14:18:34 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/BaseAnalyticalObject.java	2014-07-13 13:53:40 +0000
@@ -28,14 +28,31 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import com.fasterxml.jackson.annotation.JsonIgnore;
-import com.fasterxml.jackson.annotation.JsonProperty;
-import com.fasterxml.jackson.annotation.JsonView;
-import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
-import com.fasterxml.jackson.databind.annotation.JsonSerialize;
-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.common.DimensionalObject.CATEGORYOPTIONCOMBO_DIM_ID;
+import static org.hisp.dhis.common.DimensionalObject.DATAELEMENT_DIM_ID;
+import static org.hisp.dhis.common.DimensionalObject.DATAELEMENT_OPERAND_ID;
+import static org.hisp.dhis.common.DimensionalObject.DATASET_DIM_ID;
+import static org.hisp.dhis.common.DimensionalObject.DATA_X_DIM_ID;
+import static org.hisp.dhis.common.DimensionalObject.DIMENSION_SEP;
+import static org.hisp.dhis.common.DimensionalObject.INDICATOR_DIM_ID;
+import static org.hisp.dhis.common.DimensionalObject.ORGUNIT_DIM_ID;
+import static org.hisp.dhis.common.DimensionalObject.PERIOD_DIM_ID;
+import static org.hisp.dhis.common.DimensionalObject.STATIC_DIMS;
+import static org.hisp.dhis.organisationunit.OrganisationUnit.KEY_LEVEL;
+import static org.hisp.dhis.organisationunit.OrganisationUnit.KEY_ORGUNIT_GROUP;
+import static org.hisp.dhis.organisationunit.OrganisationUnit.KEY_USER_ORGUNIT;
+import static org.hisp.dhis.organisationunit.OrganisationUnit.KEY_USER_ORGUNIT_CHILDREN;
+import static org.hisp.dhis.organisationunit.OrganisationUnit.KEY_USER_ORGUNIT_GRANDCHILDREN;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
 import org.apache.commons.lang.StringUtils;
 import org.hisp.dhis.common.adapter.JacksonPeriodDeserializer;
 import org.hisp.dhis.common.adapter.JacksonPeriodSerializer;
@@ -63,17 +80,14 @@
 import org.hisp.dhis.trackedentity.TrackedEntityDataElementDimension;
 import org.hisp.dhis.user.User;
 
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.Date;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-import static org.hisp.dhis.common.DimensionalObject.*;
-import static org.hisp.dhis.organisationunit.OrganisationUnit.*;
+import com.fasterxml.jackson.annotation.JsonIgnore;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.fasterxml.jackson.annotation.JsonView;
+import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
+import com.fasterxml.jackson.databind.annotation.JsonSerialize;
+import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
+import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
+import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
 
 /**
  * This class contains associations to dimensional meta-data. Should typically
@@ -227,7 +241,7 @@
             this.transientOrganisationUnits.add( organisationUnit );
         }
     }
-
+    
     /**
      * Assembles a DimensionalObject based on the persisted properties of this
      * AnalyticalObject. Collapses indicators, data elements, data element
@@ -654,11 +668,13 @@
             RelativePeriods.setName( period, null, dynamicNames, format );
         }
     }
-
+    
     /**
      * Splits the keys of the given map on the dimension identifier separator,
      * sorts the identifiers, writes them out as a key and puts the key back into
      * the map.
+     * 
+     * @param valueMap the mapping of keys and values.
      */
     public static void sortKeys( Map<String, Double> valueMap )
     {

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/BaseDimensionalObject.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/BaseDimensionalObject.java	2014-06-25 14:38:50 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/BaseDimensionalObject.java	2014-07-13 13:53:40 +0000
@@ -28,6 +28,11 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import java.util.ArrayList;
+import java.util.List;
+
+import org.hisp.dhis.common.view.DimensionalView;
+
 import com.fasterxml.jackson.annotation.JsonProperty;
 import com.fasterxml.jackson.annotation.JsonView;
 import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
@@ -36,11 +41,6 @@
 import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
 import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
 
-import org.hisp.dhis.common.view.DimensionalView;
-
-import java.util.ArrayList;
-import java.util.List;
-
 @JacksonXmlRootElement( localName = "dimensionalObject", namespace = DxfNamespaces.DXF_2_0 )
 public class BaseDimensionalObject
     extends BaseNameableObject implements DimensionalObject
@@ -149,7 +149,15 @@
     {
         return dimensionName != null ? dimensionName : uid;
     }
-
+    
+    public AnalyticsType getAnalyticsType()
+    {
+        return 
+            DimensionType.TRACKED_ENTITY_ATTRIBUTE.equals( dimensionType ) ||
+            DimensionType.TRACKED_ENTITY_DATAELEMENT.equals( dimensionType ) ?
+            AnalyticsType.EVENT : AnalyticsType.AGGREGATE;
+    }
+    
     //--------------------------------------------------------------------------
     // Getters and setters
     //--------------------------------------------------------------------------
@@ -236,6 +244,6 @@
     @Override
     public String toString()
     {
-        return "[" + uid + ", type: " + dimensionType + ", " + items + "]";
+        return "[" + uid + ", type: " + dimensionType + ", items: " + items + "]";
     }
 }

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/DimensionalObject.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/DimensionalObject.java	2014-04-21 11:05:22 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/DimensionalObject.java	2014-07-13 13:53:40 +0000
@@ -109,4 +109,9 @@
      * Gets the filter. Contains operator and filter. Applicable for events.
      */
     String getFilter();
+
+    /**
+     * Indicates the analytics type of this dimensional object.
+     */
+    AnalyticsType getAnalyticsType();
 }

=== 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-04-04 13:57:02 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/DimensionalObjectUtils.java	2014-07-13 13:53:40 +0000
@@ -37,6 +37,7 @@
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 import java.util.regex.Pattern;
 
 /**
@@ -271,4 +272,26 @@
         
         return null;
     }
+
+    /**
+     * Sets items on the given dimension based on the unique values of the matching 
+     * column in the given grid. Items are BaseNameableObjects where the name, 
+     * code and short name properties are set to the column value. The dimension
+     * analytics type must be equal to EVENT.
+     * 
+     * @param dimension the dimension.
+     * @param grid the grid with data values.
+     */
+    public static void setDimensionItemsForFilters( DimensionalObject dimension, Grid grid )
+    {
+        if ( dimension == null || grid == null || !AnalyticsType.EVENT.equals( dimension.getAnalyticsType() ) )
+        {
+            return;
+        }
+            
+        BaseDimensionalObject dim = (BaseDimensionalObject) dimension;
+        Set<Object> values = grid.getUniqueValues( dim.getDimension() );
+        List<NameableObject> items = NameableObjectUtils.getNameableObjects( values );
+        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-03-18 08:10:10 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/NameableObjectUtils.java	2014-07-13 13:53:40 +0000
@@ -106,4 +106,31 @@
     {
         return asTypedList( collection );
     }
+    
+    /**
+     * Returns a list of BaseNameableObjects based on the given list of values,
+     * where the name, code and short name of each BaseNameableObject is set to
+     * the value of each list item.
+     * 
+     * @param values the list of object values.
+     * @return a list of BaseNameableObejcts.
+     */
+    public static List<NameableObject> getNameableObjects( Collection<Object> values )
+    {
+        List<NameableObject> objects = new ArrayList<>();
+        
+        for ( Object value : values )
+        {
+            if ( value != null )
+            {
+                String val = String.valueOf( value );
+                
+                BaseNameableObject nameableObject = new BaseNameableObject( val, val, val );
+                nameableObject.setShortName( val );
+                objects.add( nameableObject );
+            }
+        }
+        
+        return objects;
+    }
 }

=== 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-07-12 14:18:34 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/eventchart/EventChart.java	2014-07-13 13:53:40 +0000
@@ -36,6 +36,7 @@
 import org.hisp.dhis.common.AnalyticsType;
 import org.hisp.dhis.common.BaseIdentifiableObject;
 import org.hisp.dhis.common.DimensionalObject;
+import org.hisp.dhis.common.DimensionalObjectUtils;
 import org.hisp.dhis.common.DxfNamespaces;
 import org.hisp.dhis.common.IdentifiableObject;
 import org.hisp.dhis.common.NameableObject;
@@ -99,6 +100,8 @@
         List<OrganisationUnit> organisationUnitsAtLevel, List<OrganisationUnit> organisationUnitsInGroups,
         I18nFormat format )
     {
+        this.user = user;
+        this.format = format;
     }
 
     @Override
@@ -122,21 +125,25 @@
 
     public List<NameableObject> series()
     {
-        String seriesDim = columnDimensions.get( 0 );
+        String series = columnDimensions.get( 0 );
         
-        DimensionalObject object = getDimensionalObject( seriesDim, relativePeriodDate, user, true,
+        DimensionalObject object = getDimensionalObject( series, relativePeriodDate, user, true,
             organisationUnitsAtLevel, organisationUnitsInGroups, format );
 
+        DimensionalObjectUtils.setDimensionItemsForFilters( object, dataItemGrid );
+        
         return object != null ? object.getItems() : null;
     }
 
     public List<NameableObject> category()
     {
-        String categoryDim = rowDimensions.get( 0 );
+        String category = rowDimensions.get( 0 );
         
-        DimensionalObject object = getDimensionalObject( categoryDim, relativePeriodDate, user, true,
+        DimensionalObject object = getDimensionalObject( category, relativePeriodDate, user, true,
             organisationUnitsAtLevel, organisationUnitsInGroups, format );
 
+        DimensionalObjectUtils.setDimensionItemsForFilters( object, dataItemGrid );
+        
         return object != null ? object.getItems() : null;
     }
     

=== modified file 'dhis-2/dhis-api/src/test/java/org/hisp/dhis/common/BaseAnalyticalObjectTest.java'
--- dhis-2/dhis-api/src/test/java/org/hisp/dhis/common/BaseAnalyticalObjectTest.java	2014-03-18 08:10:10 +0000
+++ dhis-2/dhis-api/src/test/java/org/hisp/dhis/common/BaseAnalyticalObjectTest.java	2014-07-13 13:53:40 +0000
@@ -36,7 +36,11 @@
 import org.hisp.dhis.dataelement.DataElement;
 import org.hisp.dhis.dataelement.DataElementGroup;
 import org.hisp.dhis.dataset.DataSet;
+import org.hisp.dhis.eventchart.EventChart;
+import org.hisp.dhis.trackedentity.TrackedEntityAttribute;
+import org.hisp.dhis.trackedentity.TrackedEntityAttributeDimension;
 import org.junit.Test;
+
 import static org.junit.Assert.*;
 
 /**
@@ -91,6 +95,30 @@
     }
     
     @Test
+    public void testPopulateAnalyticalProperties()
+    {
+        TrackedEntityAttribute tea = new TrackedEntityAttribute();
+
+        TrackedEntityAttributeDimension tead = new TrackedEntityAttributeDimension( tea, "EQ:10" );
+        
+        EventChart chart = new EventChart();
+        chart.getColumnDimensions().add( tea.getUid() );
+        chart.getAttributeDimensions().add( tead );
+        
+        chart.populateAnalyticalProperties();
+        
+        assertEquals( 1, chart.getColumns().size() );
+        
+        DimensionalObject dim = chart.getColumns().get( 0 );
+        
+        assertNotNull( dim );
+        assertEquals( tea.getDimension(), dim.getDimension() );
+        assertEquals( DimensionType.TRACKED_ENTITY_ATTRIBUTE, dim.getDimensionType() );
+        assertEquals( AnalyticsType.EVENT, dim.getAnalyticsType() );
+        assertEquals( tead.getFilter(), dim.getFilter() );
+    }
+
+    @Test
     public void testGetIdentifier()
     {
         DataElementGroup oA = new DataElementGroup();

=== 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-07-12 22:07:01 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/event/data/EventAnalyticsServiceTest.java	2014-07-13 13:53:40 +0000
@@ -33,12 +33,19 @@
 import static org.junit.Assert.assertNotNull;
 
 import java.util.HashSet;
+import java.util.List;
 import java.util.Set;
 
 import org.hisp.dhis.DhisSpringTest;
 import org.hisp.dhis.analytics.event.EventAnalyticsService;
 import org.hisp.dhis.analytics.event.EventQueryParams;
+import org.hisp.dhis.common.AnalyticsType;
+import org.hisp.dhis.common.DimensionType;
 import org.hisp.dhis.common.DimensionalObject;
+import org.hisp.dhis.common.DimensionalObjectUtils;
+import org.hisp.dhis.common.Grid;
+import org.hisp.dhis.common.GridHeader;
+import org.hisp.dhis.common.NameableObject;
 import org.hisp.dhis.dataelement.DataElement;
 import org.hisp.dhis.dataelement.DataElementService;
 import org.hisp.dhis.eventchart.EventChart;
@@ -52,6 +59,7 @@
 import org.hisp.dhis.program.ProgramStageDataElement;
 import org.hisp.dhis.program.ProgramStageDataElementService;
 import org.hisp.dhis.program.ProgramStageService;
+import org.hisp.dhis.system.grid.ListGrid;
 import org.hisp.dhis.trackedentity.TrackedEntityAttribute;
 import org.hisp.dhis.trackedentity.TrackedEntityAttributeDimension;
 import org.hisp.dhis.trackedentity.TrackedEntityAttributeService;
@@ -203,4 +211,41 @@
         assertEquals( 2, params.getPeriods().size() );
         assertEquals( 2, params.getFilterOrganisationUnits().size() );
     }
+
+    @Test
+    public void testSetItemsForDimensionFilters()
+    {
+        TrackedEntityAttribute tea = new TrackedEntityAttribute();
+
+        TrackedEntityAttributeDimension tead = new TrackedEntityAttributeDimension( tea, "EQ:2" );
+        
+        EventChart chart = new EventChart();
+        chart.getColumnDimensions().add( tea.getUid() );
+        chart.getAttributeDimensions().add( tead );
+        
+        Grid grid = new ListGrid();
+        grid.addHeader( new GridHeader( tea.getUid(), tea.getName() ) );
+        grid.addRow().addValue( "1" );
+        grid.addRow().addValue( "2" );
+        grid.addRow().addValue( "3" );
+        
+        chart.populateAnalyticalProperties();
+        
+        DimensionalObject dim = chart.getColumns().get( 0 );
+        
+        DimensionalObjectUtils.setDimensionItemsForFilters( dim, grid );
+        
+        assertNotNull( dim );
+        assertEquals( tea.getDimension(), dim.getDimension() );
+        assertEquals( DimensionType.TRACKED_ENTITY_ATTRIBUTE, dim.getDimensionType() );
+        assertEquals( AnalyticsType.EVENT, dim.getAnalyticsType() );
+        assertEquals( tead.getFilter(), dim.getFilter() );
+        
+        List<NameableObject> items = dim.getItems();
+        assertEquals( 3, items.size() );
+        assertNotNull( items.get( 0 ).getUid() );
+        assertNotNull( items.get( 0 ).getName() );
+        assertNotNull( items.get( 0 ).getCode() );
+        assertNotNull( items.get( 0 ).getShortName() );
+    }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/sms/J2MEDataValueSMSListener.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/sms/J2MEDataValueSMSListener.java	2014-06-27 16:27:09 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/sms/J2MEDataValueSMSListener.java	2014-07-13 13:53:40 +0000
@@ -405,8 +405,6 @@
             reportBack += notInReport;
         }
         
-        System.out.println("Success message :" + command.getSuccessMessage());
-        
         if ( command.getSuccessMessage() != null && !StringUtils.isEmpty( command.getSuccessMessage() ) )
         {
             reportBack = command.getSuccessMessage();

=== modified file 'dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/chart/impl/DefaultChartService.java'
--- dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/chart/impl/DefaultChartService.java	2014-07-13 09:53:10 +0000
+++ dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/chart/impl/DefaultChartService.java	2014-07-13 13:53:40 +0000
@@ -736,6 +736,8 @@
         {
             Grid grid = eventAnalyticsService.getAggregatedEventData( chart, chart.getFormat() );
             
+            chart.setDataItemGrid( grid );
+                        
             valueMap = GridUtils.getMetaValueMapping( grid, ( grid.getWidth() - 1 ) );
         }
 
@@ -753,7 +755,7 @@
                 categoryIndex++;
 
                 String key = series.getUid() + DIMENSION_SEP + category.getUid();
-
+                
                 // Replace potential operand separator with dimension separator
 
                 key = key.replace( DataElementOperand.SEPARATOR, DIMENSION_SEP );