dhis2-devs team mailing list archive
-
dhis2-devs team
-
Mailing list archive
-
Message #20590
[Branch ~dhis2-devs-core/dhis2/trunk] Rev 9442: Updated analytics request format
------------------------------------------------------------
revno: 9442
committer: Lars Helge Øverland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Sat 2013-01-05 17:55:32 +0100
message:
Updated analytics request format
modified:
dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/DataQueryParams.java
dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/data/QueryPlannerTest.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/DataQueryParams.java'
--- dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/DataQueryParams.java 2012-12-30 16:23:41 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/DataQueryParams.java 2013-01-05 16:55:32 +0000
@@ -30,6 +30,7 @@
import static org.hisp.dhis.analytics.AggregationType.AVERAGE_DISAGGREGATION;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
@@ -52,9 +53,12 @@
public static final String CATEGORYOPTIONCOMBO_DIM_ID = "coc";
public static final String PERIOD_DIM_ID = "pe";
public static final String ORGUNIT_DIM_ID = "ou";
- public static final String VALUE_ID = "value";
-
+ public static final String VALUE_ID = "value";
public static final String LEVEL_PREFIX = "uidlevel";
+
+ private static final String DIMENSION_SEP = ";";
+ private static final String DIMENSION_NAME_SEP = ":";
+ private static final String OPTION_SEP = ",";
private Map<String, List<String>> dimensions = new HashMap<String, List<String>>();
@@ -243,9 +247,53 @@
}
}
- // -------------------------------------------------------------------------
- // Logic
- // -------------------------------------------------------------------------
+ /**
+ * Creates an instance based on a URL.
+ */
+ public static DataQueryParams getFromUrl( String dimensions, String filters, boolean categories )
+ {
+ DataQueryParams params = new DataQueryParams();
+
+ params.getDimensions().putAll( getDimension( dimensions ) );
+ params.getFilters().putAll( getDimension( filters ) );
+ params.setCategories( categories );
+
+ return params;
+ }
+
+ // -------------------------------------------------------------------------
+ // Supportive methods
+ // -------------------------------------------------------------------------
+
+ /**
+ * Gets a mapping between dimension name and dimension options for the given
+ * query parameter.
+ */
+ private static Map<String, List<String>> getDimension( String requestParam )
+ {
+ Map<String, List<String>> map = new HashMap<String, List<String>>();
+
+ if ( requestParam == null || requestParam.isEmpty() )
+ {
+ return map;
+ }
+
+ String[] dimensions = requestParam.split( DIMENSION_SEP );
+
+ for ( String dimension : dimensions )
+ {
+ String[] elements = dimension.split( DIMENSION_NAME_SEP );
+
+ if ( elements[0] != null && !elements[0].isEmpty() && elements[1] != null && !elements[1].isEmpty() )
+ {
+ List<String> options = Arrays.asList( elements[1].split( OPTION_SEP ) );
+
+ map.put( elements[0], options );
+ }
+ }
+
+ return map;
+ }
/**
* Returns the dimension names as a list.
@@ -254,7 +302,7 @@
{
return new ArrayList<String>( dimensions.keySet() );
}
-
+
// -------------------------------------------------------------------------
// hashCode, equals and toString
// -------------------------------------------------------------------------
=== modified file 'dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/data/QueryPlannerTest.java'
--- dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/data/QueryPlannerTest.java 2012-12-30 16:23:41 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/data/QueryPlannerTest.java 2013-01-05 16:55:32 +0000
@@ -107,6 +107,21 @@
// Tests
// -------------------------------------------------------------------------
+ public void testGetFromUrl()
+ {
+ String dimensions = "de:s46m5MS0hxu,fClA2Erf6IO,UOlfIjgN8X6,I78gJm4KBo7,n6aMJNLdvep;pe:2012,2012S1,2012S2";
+ String filters = "ou:ImspTQPwCqd";
+
+ DataQueryParams params = DataQueryParams.getFromUrl( dimensions, filters, false );
+
+ assertEquals( 2, params.getDimensions().size() );
+ assertEquals( 1, params.getFilters().size() );
+
+ assertTrue( params.getDimensionNames().contains( "de" ) );
+ assertTrue( params.getDimensionNames().contains( "pe" ) );
+ assertTrue( params.getFilterNames().contains( "ou" ) );
+ }
+
public void testGetDataPeriodAggregationPeriodMap()
{
DataQueryParams params = new DataQueryParams();
=== 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 2012-12-21 14:12:04 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/AnalyticsController.java 2013-01-05 16:55:32 +0000
@@ -27,8 +27,6 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-import java.io.InputStream;
-
import javax.servlet.http.HttpServletResponse;
import org.hisp.dhis.analytics.AnalyticsService;
@@ -36,18 +34,19 @@
import org.hisp.dhis.api.utils.ContextUtils;
import org.hisp.dhis.api.utils.ContextUtils.CacheStrategy;
import org.hisp.dhis.common.Grid;
-import org.hisp.dhis.dxf2.utils.JacksonUtils;
import org.hisp.dhis.system.grid.GridUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class AnalyticsController
{
private static final String RESOURCE_PATH = "/analytics";
+
@Autowired
private AnalyticsService analyticsService;
@@ -61,11 +60,17 @@
// -------------------------------------------------------------------------
@RequestMapping( value = RESOURCE_PATH, method = RequestMethod.GET, produces = { "application/json", "application/javascript" } )
- public String getJson( InputStream in, // JSON, JSONP
+ public String getJson( // JSON, JSONP
+ @RequestParam String dimensions,
+ @RequestParam(required = false) String filters,
+ @RequestParam(required = false) boolean categories,
Model model,
HttpServletResponse response ) throws Exception
{
- DataQueryParams params = JacksonUtils.fromJson( in, DataQueryParams.class );
+ DataQueryParams params = DataQueryParams.getFromUrl( dimensions, filters, categories );
+System.out.println("dim " + dimensions);
+System.out.println("fl " + filters);
+System.out.println("par " + params);
if ( !valid( params, response ) )
{
@@ -80,11 +85,14 @@
}
@RequestMapping( value = RESOURCE_PATH + ".xml", method = RequestMethod.GET )
- public void getXml( InputStream in,
+ public void getXml(
+ @RequestParam String dimensions,
+ @RequestParam(required = false) String filters,
+ @RequestParam(required = false) boolean categories,
Model model,
HttpServletResponse response ) throws Exception
{
- DataQueryParams params = JacksonUtils.fromJson( in, DataQueryParams.class );
+ DataQueryParams params = DataQueryParams.getFromUrl( dimensions, filters, categories );
if ( !valid( params, response ) )
{
@@ -97,11 +105,14 @@
}
@RequestMapping( value = RESOURCE_PATH + ".csv", method = RequestMethod.GET )
- public void getCsv( InputStream in,
+ public void getCsv(
+ @RequestParam String dimensions,
+ @RequestParam(required = false) String filters,
+ @RequestParam(required = false) boolean categories,
Model model,
HttpServletResponse response ) throws Exception
{
- DataQueryParams params = JacksonUtils.fromJson( in, DataQueryParams.class );
+ DataQueryParams params = DataQueryParams.getFromUrl( dimensions, filters, categories );
if ( !valid( params, response ) )
{
@@ -114,11 +125,14 @@
}
@RequestMapping( value = RESOURCE_PATH + ".html", method = RequestMethod.GET )
- public void getHtml( InputStream in,
+ public void getHtml(
+ @RequestParam String dimensions,
+ @RequestParam(required = false) String filters,
+ @RequestParam(required = false) boolean categories,
Model model,
HttpServletResponse response ) throws Exception
{
- DataQueryParams params = JacksonUtils.fromJson( in, DataQueryParams.class );
+ DataQueryParams params = DataQueryParams.getFromUrl( dimensions, filters, categories );
if ( !valid( params, response ) )
{
@@ -133,7 +147,7 @@
// -------------------------------------------------------------------------
// Supportive methods
// -------------------------------------------------------------------------
-
+
private boolean valid( DataQueryParams params, HttpServletResponse response )
{
if ( params == null || params.getDimensions().isEmpty() )