← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 18448: Event analytics, added support for legend filter

 

------------------------------------------------------------
revno: 18448
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Fri 2015-02-27 19:08:30 +0100
message:
  Event analytics, added support for legend filter
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/QueryFilter.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/QueryItem.java
  dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/DefaultEventAnalyticsService.java
  dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/event/data/EventAnalyticsServiceTest.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/QueryFilter.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/QueryFilter.java	2015-01-17 07:41:26 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/QueryFilter.java	2015-02-27 18:08:30 +0000
@@ -104,7 +104,7 @@
         }
         else if ( QueryOperator.IN.equals( operator ) )
         {
-            String[] split = encodedFilter.split( OPTION_SEP );
+            String[] split =  getFilterItems( encodedFilter );
             
             final StringBuffer buffer = new StringBuffer( "(" );        
             
@@ -119,6 +119,14 @@
         return "'" + encodedFilter + "'";
     }
     
+    /**
+     * Returns the items of the filter. Items are separated with the ";" character.
+     */
+    public static String[] getFilterItems( String filter )
+    {
+        return filter.split( OPTION_SEP );
+    }
+    
     // -------------------------------------------------------------------------
     // hashCode, equals and toString
     // -------------------------------------------------------------------------

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/QueryItem.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/QueryItem.java	2015-02-25 16:35:09 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/QueryItem.java	2015-02-27 18:08:30 +0000
@@ -114,6 +114,11 @@
         return Double.class.equals( ObjectUtils.VALUE_TYPE_JAVA_CLASS_MAP.get( valueType ) );
     }
     
+    public boolean hasLegendSet()
+    {
+        return legendSet != null;
+    }
+    
     public boolean hasFilter()
     {
         return filters != null && !filters.isEmpty();

=== modified file 'dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/DefaultEventAnalyticsService.java'
--- dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/DefaultEventAnalyticsService.java	2015-02-25 15:59:31 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/DefaultEventAnalyticsService.java	2015-02-27 18:08:30 +0000
@@ -77,6 +77,7 @@
 import org.hisp.dhis.dataelement.DataElement;
 import org.hisp.dhis.dataelement.DataElementService;
 import org.hisp.dhis.i18n.I18nFormat;
+import org.hisp.dhis.legend.Legend;
 import org.hisp.dhis.legend.LegendService;
 import org.hisp.dhis.legend.LegendSet;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
@@ -552,18 +553,22 @@
         QueryItem queryItem = getQueryItemFromDimension( split[0] );
         
         if ( split.length > 1 ) // Filters specified
-        {   
+        {
             for ( int i = 1; i < split.length; i += 2 )
             {
+                // Legends comes as identifiers, replace with names if legend set
+                
+                String filterItem = queryItem.hasLegendSet() ? replaceLegendUidsWithNames( split[i+1] ) : split[i+1];
+                
                 QueryOperator operator = QueryOperator.fromString( split[i] );
-                QueryFilter filter = new QueryFilter( operator, split[i+1] );
+                QueryFilter filter = new QueryFilter( operator, filterItem );
                 queryItem.getFilters().add( filter );
             }
         }
         
         return queryItem;
     }
-
+    
     private Map<String, String> getUidNameMap( EventQueryParams params )
     {
         Map<String, String> map = new HashMap<>();
@@ -705,4 +710,29 @@
         
         throw new IllegalQueryException( "Value identifier does not reference any data element or attribute which are numeric type and part of the program: " + value );        
     }
+
+    /**
+     * Replaces legend identifiers with names.
+     */
+    private String replaceLegendUidsWithNames( String filter )
+    {
+        if ( filter != null )
+        {
+            String[] filterItems = QueryFilter.getFilterItems( filter );
+            
+            for ( String uid : filterItems )
+            {
+                Legend legend = legendService.getLegend( uid );
+
+                if ( legend == null )
+                {
+                    throw new IllegalQueryException( "Legend does not exist: " + uid );
+                }
+                
+                filter = filter.replace( uid, legend.getName() );
+            }
+        }
+        
+        return filter;
+    }
 }

=== 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	2015-02-25 20:40:28 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/event/data/EventAnalyticsServiceTest.java	2015-02-27 18:08:30 +0000
@@ -50,6 +50,9 @@
 import org.hisp.dhis.dataelement.DataElement;
 import org.hisp.dhis.dataelement.DataElementService;
 import org.hisp.dhis.eventchart.EventChart;
+import org.hisp.dhis.legend.Legend;
+import org.hisp.dhis.legend.LegendService;
+import org.hisp.dhis.legend.LegendSet;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.organisationunit.OrganisationUnitService;
 import org.hisp.dhis.period.Period;
@@ -88,6 +91,11 @@
     
     private TrackedEntityAttribute atA;
     private TrackedEntityAttribute atB;
+    
+    private LegendSet legendSetA;
+    
+    private Legend legendA;
+    private Legend legendB;
 
     @Autowired
     private EventAnalyticsService analyticsService;
@@ -110,6 +118,9 @@
     @Autowired
     private TrackedEntityAttributeService attributeService;
     
+    @Autowired
+    private LegendService legendService;
+    
     @Override
     public void setUpTest()
     {
@@ -133,7 +144,6 @@
         
         attributeService.addTrackedEntityAttribute( atA );
         attributeService.addTrackedEntityAttribute( atB );
-
                 
         prA = createProgram( 'A', new HashSet<ProgramStage>(), asSet( atA, atB ), asSet( ouA, ouB ) );
         programService.addProgram( prA );
@@ -145,6 +155,19 @@
         
         programStageDataElementService.addProgramStageDataElement( new ProgramStageDataElement( psA, deA, false ) );
         programStageDataElementService.addProgramStageDataElement( new ProgramStageDataElement( psA, deB, false ) );
+        
+        legendA = createLegend( 'A', 0d, 10d );
+        legendB = createLegend( 'B', 10d, 20d );
+
+        legendService.addLegend( legendA );
+        legendService.addLegend( legendB );
+        
+        legendSetA = createLegendSet( 'A' );
+        
+        legendSetA.getLegends().add( legendA );
+        legendSetA.getLegends().add( legendB );
+        
+        legendService.addLegendSet( legendSetA );
     }
 
     @Test
@@ -274,4 +297,20 @@
         assertNotNull( items.get( 0 ).getCode() );
         assertNotNull( items.get( 0 ).getShortName() );
     }
+
+    @Test
+    public void testGetFromUrlLegendSet()
+    {
+        Set<String> dimensionParams = new HashSet<>();
+        dimensionParams.add( "dx:" + deA.getUid() + "-" + legendSetA.getUid() + ":IN:" + legendA.getUid() + ";" + legendB.getId() );
+        dimensionParams.add( atA.getUid() );
+        
+        Set<String> filterParams = new HashSet<>();
+        filterParams.add( "pe:201401;201402" );
+        
+        EventQueryParams params = analyticsService.getFromUrl( prA.getUid(), null, 
+            null, null, dimensionParams, filterParams, null, null, false, false, false, null, null, null, null, null );
+        
+        assertEquals( prA, params.getProgram() );
+    }
 }