← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 16819: Event analytics, charts. Preserving order of options from filters.

 

------------------------------------------------------------
revno: 16819
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2014-09-25 17:46:23 +0200
message:
  Event analytics, charts. Preserving order of options from filters.
added:
  dhis-2/dhis-api/src/test/java/org/hisp/dhis/common/DimensionalObjectTest.java
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/BaseDimensionalObject.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
  resources/sql/div.sql


--
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/BaseDimensionalObject.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/BaseDimensionalObject.java	2014-08-15 07:40:20 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/BaseDimensionalObject.java	2014-09-25 15:46:23 +0000
@@ -29,6 +29,7 @@
  */
 
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.List;
 
 import org.hisp.dhis.common.view.DimensionalView;
@@ -158,6 +159,26 @@
             AnalyticsType.EVENT : AnalyticsType.AGGREGATE;
     }
     
+    /**
+     * Returns the items in the filter as a list. Order of items are preserved.
+     * Requires that the filter has the IN operator and that at least one item
+     * is specified in the filter, returns null if not.
+     */
+    public List<String> getFilterItemsAsList()
+    {
+        final String inOp = QueryOperator.IN.getValue().toLowerCase();
+        final int opLen = inOp.length() + 1;
+        
+        if ( filter == null || !filter.toLowerCase().startsWith( inOp ) || filter.length() < opLen )
+        {
+            return null;
+        }
+        
+        String filterItems = filter.substring( opLen, filter.length() );
+        
+        return new ArrayList<>( Arrays.asList( filterItems.split( DimensionalObjectUtils.OPTION_SEP ) ) );
+    }
+    
     //--------------------------------------------------------------------------
     // Getters and setters
     //--------------------------------------------------------------------------
@@ -244,6 +265,6 @@
     @Override
     public String toString()
     {
-        return "[" + uid + ", type: " + dimensionType + ", items: " + items + "]";
+        return "[" + uid + ", type: " + dimensionType + ", items: " + items + ", filter: " + filter + "]";
     }
 }

=== 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-09-25 10:17:34 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/DimensionalObjectUtils.java	2014-09-25 15:46:23 +0000
@@ -37,9 +37,9 @@
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
-import java.util.Set;
 import java.util.regex.Pattern;
 
+import org.apache.commons.collections.ListUtils;
 import org.apache.commons.lang.StringUtils;
 
 /**
@@ -296,11 +296,20 @@
         }
             
         BaseDimensionalObject dim = (BaseDimensionalObject) dimension;
-        Set<Object> values = grid.getUniqueValues( dim.getDimension() );
-        List<NameableObject> items = NameableObjectUtils.getNameableObjects( values, naForNull );
+        
+        List<String> filterItems = dim.getFilterItemsAsList();
+        
+        List<Object> values = new ArrayList<>( grid.getUniqueValues( dim.getDimension() ) );
+        
+        // Use order of items in filter if specified
+        
+        List<?> itemList = filterItems != null ? ListUtils.retainAll( filterItems, values ) : values;
+                
+        List<NameableObject> items = NameableObjectUtils.getNameableObjects( itemList, naForNull );
+        
         dim.setItems( items );
     }
-    
+        
     /**
      * Accepts filter strings on the format:
      * </p>

=== 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-09-25 10:17:34 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/NameableObjectUtils.java	2014-09-25 15:46:23 +0000
@@ -121,7 +121,7 @@
      *        replacement for null values.
      * @return a list of BaseNameableObejcts.
      */
-    public static List<NameableObject> getNameableObjects( Collection<Object> values, boolean naForNull )
+    public static List<NameableObject> getNameableObjects( Collection<?> values, boolean naForNull )
     {
         List<NameableObject> objects = new ArrayList<>();
         

=== added file 'dhis-2/dhis-api/src/test/java/org/hisp/dhis/common/DimensionalObjectTest.java'
--- dhis-2/dhis-api/src/test/java/org/hisp/dhis/common/DimensionalObjectTest.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-api/src/test/java/org/hisp/dhis/common/DimensionalObjectTest.java	2014-09-25 15:46:23 +0000
@@ -0,0 +1,54 @@
+package org.hisp.dhis.common;
+
+/*
+ * Copyright (c) 2004-2014, 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 java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+/**
+ * @author Lars Helge Overland
+ */
+public class DimensionalObjectTest
+{
+    @Test
+    public void testGetFilterItemsAsList()
+    {
+        BaseDimensionalObject objectA = new BaseDimensionalObject( "dimA", DimensionType.TRACKED_ENTITY_DATAELEMENT, null, null, "IN:uidA;uidB;uidC" );
+        List<String> expectedA = new ArrayList<>( Arrays.asList( "uidA", "uidB", "uidC" ) );
+        assertEquals( expectedA, objectA.getFilterItemsAsList() );
+        
+        BaseDimensionalObject objectB = new BaseDimensionalObject( "dimA", DimensionType.TRACKED_ENTITY_DATAELEMENT, null, null, "EQ:uidA" );
+        assertEquals( null, objectB.getFilterItemsAsList() );
+    }
+}

=== modified file 'resources/sql/div.sql'
--- resources/sql/div.sql	2014-09-25 14:56:12 +0000
+++ resources/sql/div.sql	2014-09-25 15:46:23 +0000
@@ -163,7 +163,8 @@
 
 -- Data value exploded view
 
-select de.name as dename, de.uid as deuid, pe.startdate as pestart, pe.enddate as peend, pt.name as ptname, ou.name as ouname, ou.uid as ouuid, coc.uid as cocuid, aoc.uid as aocuid, dv.value as dvval
+select de.name as dename, de.uid as deuid, pe.startdate as pestart, pe.enddate as peend, pt.name as ptname, 
+ou.name as ouname, ou.uid as ouuid, coc.uid as cocuid, coc.categoryoptioncomboid as cocid, aoc.uid as aocuid, aoc.categoryoptioncomboid as aocid, dv.value as dvval
 from datavalue dv
 inner join dataelement de on (dv.dataelementid=de.dataelementid)
 inner join period pe on (dv.periodid=pe.periodid)