dhis2-devs team mailing list archive
-
dhis2-devs team
-
Mailing list archive
-
Message #20759
[Branch ~dhis2-devs-core/dhis2/trunk] Rev 9640: Impl support for measure criteria in queries through query parameter measureCriteria. Valid optio...
------------------------------------------------------------
revno: 9640
committer: Lars Helge Øverland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2013-01-28 15:31:44 +0200
message:
Impl support for measure criteria in queries through query parameter measureCriteria. Valid options are EQ, GT, GE, LT, LE.
added:
dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/MeasureFilter.java
dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/DataQueryParamsTest.java
modified:
dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/AnalyticsService.java
dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/DataQueryParams.java
dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/DefaultAnalyticsService.java
dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/JdbcAnalyticsManager.java
dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/data/AnalyticsServiceTest.java
dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/AnalyticsController.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-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/AnalyticsService.java'
--- dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/AnalyticsService.java 2013-01-27 21:03:02 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/AnalyticsService.java 2013-01-28 13:31:44 +0000
@@ -39,5 +39,6 @@
Map<String, Double> getAggregatedDataValueMap( DataQueryParams params, String tableName ) throws Exception;
- DataQueryParams getFromUrl( Set<String> dimensionParams, Set<String> filterParams, boolean categories, AggregationType aggregationType, I18nFormat format );
+ DataQueryParams getFromUrl( Set<String> dimensionParams, Set<String> filterParams,
+ boolean categories, AggregationType aggregationType, String measureCriteria, I18nFormat format );
}
=== modified file 'dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/DataQueryParams.java'
--- dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/DataQueryParams.java 2013-01-28 12:50:32 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/DataQueryParams.java 2013-01-28 13:31:44 +0000
@@ -46,6 +46,7 @@
import org.hisp.dhis.system.util.CollectionUtils;
import org.hisp.dhis.system.util.ListMap;
import org.hisp.dhis.system.util.MapMap;
+import org.hisp.dhis.system.util.MathUtils;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
@@ -76,6 +77,8 @@
private AggregationType aggregationType;
+ private Map<MeasureFilter, Double> measureCriteria = new HashMap<MeasureFilter, Double>();
+
// -------------------------------------------------------------------------
// Transient properties
// -------------------------------------------------------------------------
@@ -102,6 +105,7 @@
this.filters = new ArrayList<Dimension>( params.getFilters() );
this.categories = params.isCategories();
this.aggregationType = params.getAggregationType();
+ this.measureCriteria = params.getMeasureCriteria();
this.tableName = params.getTableName();
this.periodType = params.getPeriodType();
@@ -476,6 +480,37 @@
return new ArrayList<String>();
}
+ /**
+ * Retrieves the measure criteria form the given string. Criteria are separated
+ * by the option separator, while the criterion filter and value are separated
+ * with the dimension name separator.
+ */
+ public static Map<MeasureFilter, Double> getMeasureCriteriaFromParam( String param )
+ {
+ if ( param == null )
+ {
+ return null;
+ }
+
+ Map<MeasureFilter, Double> map = new HashMap<MeasureFilter, Double>();
+
+ String[] criteria = param.split( OPTION_SEP );
+
+ for ( String c : criteria )
+ {
+ String[] criterion = c.split( DIMENSION_NAME_SEP );
+
+ if ( criterion != null && criterion.length == 2 && MathUtils.isNumeric( criterion[1] ) )
+ {
+ MeasureFilter filter = MeasureFilter.valueOf( criterion[0] );
+ Double value = Double.valueOf( criterion[1] );
+ map.put( filter, value );
+ }
+ }
+
+ return map;
+ }
+
// -------------------------------------------------------------------------
// Supportive methods
// -------------------------------------------------------------------------
@@ -621,6 +656,16 @@
this.aggregationType = aggregationType;
}
+ public Map<MeasureFilter, Double> getMeasureCriteria()
+ {
+ return measureCriteria;
+ }
+
+ public void setMeasureCriteria( Map<MeasureFilter, Double> measureCriteria )
+ {
+ this.measureCriteria = measureCriteria;
+ }
+
// -------------------------------------------------------------------------
// Get and set methods for transient properties
// -------------------------------------------------------------------------
=== added file 'dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/MeasureFilter.java'
--- dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/MeasureFilter.java 1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/MeasureFilter.java 2013-01-28 13:31:44 +0000
@@ -0,0 +1,33 @@
+package org.hisp.dhis.analytics;
+
+/*
+ * Copyright (c) 2004-2012, 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.
+ */
+
+public enum MeasureFilter
+{
+ EQ, GT, GE, LT, LE
+}
=== modified file 'dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/DefaultAnalyticsService.java'
--- dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/DefaultAnalyticsService.java 2013-01-27 21:03:02 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/DefaultAnalyticsService.java 2013-01-28 13:31:44 +0000
@@ -282,7 +282,8 @@
return map;
}
- public DataQueryParams getFromUrl( Set<String> dimensionParams, Set<String> filterParams, boolean categories, AggregationType aggregationType, I18nFormat format )
+ public DataQueryParams getFromUrl( Set<String> dimensionParams, Set<String> filterParams,
+ boolean categories, AggregationType aggregationType, String measureCriteria, I18nFormat format )
{
DataQueryParams params = new DataQueryParams();
@@ -316,6 +317,11 @@
}
}
}
+
+ if ( measureCriteria != null && !measureCriteria.isEmpty() )
+ {
+ params.setMeasureCriteria( DataQueryParams.getMeasureCriteriaFromParam( measureCriteria ) );
+ }
return params;
}
=== modified file 'dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/JdbcAnalyticsManager.java'
--- dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/JdbcAnalyticsManager.java 2013-01-28 12:50:32 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/JdbcAnalyticsManager.java 2013-01-28 13:31:44 +0000
@@ -35,6 +35,7 @@
import static org.hisp.dhis.analytics.DataQueryParams.VALUE_ID;
import static org.hisp.dhis.common.IdentifiableObjectUtils.getUids;
import static org.hisp.dhis.system.util.TextUtils.getQuotedCommaDelimitedString;
+import static org.hisp.dhis.analytics.MeasureFilter.*;
import java.util.Collection;
import java.util.HashMap;
@@ -49,10 +50,12 @@
import org.hisp.dhis.analytics.AnalyticsManager;
import org.hisp.dhis.analytics.DataQueryParams;
import org.hisp.dhis.analytics.Dimension;
+import org.hisp.dhis.analytics.MeasureFilter;
import org.hisp.dhis.common.IdentifiableObject;
import org.hisp.dhis.period.Period;
import org.hisp.dhis.period.PeriodType;
import org.hisp.dhis.system.util.ListMap;
+import org.hisp.dhis.system.util.MathUtils;
import org.hisp.dhis.system.util.SqlHelper;
import org.hisp.dhis.system.util.TextUtils;
import org.springframework.beans.factory.annotation.Autowired;
@@ -145,6 +148,13 @@
while ( rowSet.next() )
{
+ Double value = rowSet.getDouble( VALUE_ID );
+
+ if ( !measureCriteriaSatisfied( params, value ) )
+ {
+ continue;
+ }
+
StringBuilder key = new StringBuilder();
for ( Dimension dim : selectDimensions )
@@ -154,8 +164,6 @@
key.deleteCharAt( key.length() - 1 );
- Double value = rowSet.getDouble( VALUE_ID );
-
map.put( key.toString(), value );
}
@@ -195,8 +203,52 @@
}
}
}
-
- private static String getCommaDelimitedString( Collection<Dimension> dimensions )
+
+ // -------------------------------------------------------------------------
+ // Supportive methods
+ // -------------------------------------------------------------------------
+
+ private boolean measureCriteriaSatisfied( DataQueryParams params, Double value )
+ {
+ if ( value == null )
+ {
+ return false;
+ }
+
+ for ( MeasureFilter filter : params.getMeasureCriteria().keySet() )
+ {
+ Double criterion = params.getMeasureCriteria().get( filter );
+
+ if ( EQ.equals( filter ) && !MathUtils.isEqual( value, criterion ) )
+ {
+ return false;
+ }
+
+ if ( GT.equals( filter ) && Double.compare( value, criterion ) <= 0 )
+ {
+ return false;
+ }
+
+ if ( GE.equals( filter ) && Double.compare( value, criterion ) < 0 )
+ {
+ return false;
+ }
+
+ if ( LT.equals( filter ) && Double.compare( value, criterion ) >= 0 )
+ {
+ return false;
+ }
+
+ if ( LE.equals( filter ) && Double.compare( value, criterion ) > 0 )
+ {
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ private String getCommaDelimitedString( Collection<Dimension> dimensions )
{
final StringBuilder builder = new StringBuilder();
=== added file 'dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/DataQueryParamsTest.java'
--- dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/DataQueryParamsTest.java 1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/DataQueryParamsTest.java 2013-01-28 13:31:44 +0000
@@ -0,0 +1,64 @@
+package org.hisp.dhis.analytics;
+
+/*
+ * Copyright (c) 2004-2012, 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.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.junit.Test;
+import static org.junit.Assert.*;
+
+public class DataQueryParamsTest
+{
+ @Test
+ public void testGetDimensionFromParam()
+ {
+ assertEquals( DataQueryParams.DATAELEMENT_DIM_ID, DataQueryParams.getDimensionFromParam( "de:D348asd782j,kj78HnH6hgT,9ds9dS98s2" ) );
+ }
+
+ @Test
+ public void testGetDimensionOptionsFromParam()
+ {
+ List<String> expected = new ArrayList<String>( Arrays.asList( "D348asd782j", "kj78HnH6hgT", "9ds9dS98s2" ) );
+
+ assertEquals( expected, DataQueryParams.getDimensionOptionsFromParam( "de:D348asd782j,kj78HnH6hgT,9ds9dS98s2" ) );
+ }
+
+ @Test
+ public void test()
+ {
+ Map<MeasureFilter, Double> expected = new HashMap<MeasureFilter, Double>();
+ expected.put( MeasureFilter.GT, 100d );
+ expected.put( MeasureFilter.LT, 200d );
+
+ assertEquals( expected, DataQueryParams.getMeasureCriteriaFromParam( "GT:100,LT:200" ) );
+ }
+}
=== modified file 'dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/data/AnalyticsServiceTest.java'
--- dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/data/AnalyticsServiceTest.java 2013-01-27 21:03:02 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/data/AnalyticsServiceTest.java 2013-01-28 13:31:44 +0000
@@ -130,7 +130,7 @@
Set<String> filterParams = new HashSet<String>();
filterParams.add( "ou:" + BASE_UID + "A," + BASE_UID + "B," + BASE_UID + "C," + BASE_UID + "D," + BASE_UID + "E" );
- DataQueryParams params = analyticsService.getFromUrl( dimensionParams, filterParams, false, null, null );
+ DataQueryParams params = analyticsService.getFromUrl( dimensionParams, filterParams, false, null, null, null );
assertEquals( 4, params.getDataElements().size() );
assertEquals( 3, params.getPeriods().size() );
=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/AnalyticsController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/AnalyticsController.java 2013-01-28 10:55:48 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/AnalyticsController.java 2013-01-28 13:31:44 +0000
@@ -73,10 +73,11 @@
@RequestParam(required = false) Set<String> filter,
@RequestParam(required = false) boolean categories,
@RequestParam(required = false) AggregationType aggregationType,
+ @RequestParam(required = false) String measureCriteria,
Model model,
HttpServletResponse response ) throws Exception
{
- DataQueryParams params = analyticsService.getFromUrl( dimension, filter, categories, aggregationType, i18nManager.getI18nFormat() );
+ DataQueryParams params = analyticsService.getFromUrl( dimension, filter, categories, aggregationType, measureCriteria, i18nManager.getI18nFormat() );
if ( !valid( params, response ) )
{
@@ -96,10 +97,11 @@
@RequestParam(required = false) Set<String> filter,
@RequestParam(required = false) boolean categories,
@RequestParam(required = false) AggregationType aggregationType,
+ @RequestParam(required = false) String measureCriteria,
Model model,
HttpServletResponse response ) throws Exception
{
- DataQueryParams params = analyticsService.getFromUrl( dimension, filter, categories, aggregationType, i18nManager.getI18nFormat() );
+ DataQueryParams params = analyticsService.getFromUrl( dimension, filter, categories, aggregationType, measureCriteria, i18nManager.getI18nFormat() );
if ( !valid( params, response ) )
{
@@ -117,10 +119,11 @@
@RequestParam(required = false) Set<String> filter,
@RequestParam(required = false) boolean categories,
@RequestParam(required = false) AggregationType aggregationType,
+ @RequestParam(required = false) String measureCriteria,
Model model,
HttpServletResponse response ) throws Exception
{
- DataQueryParams params = analyticsService.getFromUrl( dimension, filter, categories, aggregationType, i18nManager.getI18nFormat() );
+ DataQueryParams params = analyticsService.getFromUrl( dimension, filter, categories, aggregationType, measureCriteria, i18nManager.getI18nFormat() );
if ( !valid( params, response ) )
{
@@ -138,10 +141,11 @@
@RequestParam(required = false) Set<String> filter,
@RequestParam(required = false) boolean categories,
@RequestParam(required = false) AggregationType aggregationType,
+ @RequestParam(required = false) String measureCriteria,
Model model,
HttpServletResponse response ) throws Exception
{
- DataQueryParams params = analyticsService.getFromUrl( dimension, filter, categories, aggregationType, i18nManager.getI18nFormat() );
+ DataQueryParams params = analyticsService.getFromUrl( dimension, filter, categories, aggregationType, measureCriteria, i18nManager.getI18nFormat() );
if ( !valid( params, response ) )
{