← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 21860: extend /api/metadata/export endpoint to support pr class fields, filter, order

 

------------------------------------------------------------
revno: 21860
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2016-02-03 13:57:11 +0700
message:
  extend /api/metadata/export endpoint to support pr class fields, filter, order
modified:
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/QueryParserException.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/DefaultMetadataExportService.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/MetadataExportParams.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/MetadataExportService.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/metadata/MetadataExportController.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/service/ContextService.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/service/DefaultContextService.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-core/src/main/java/org/hisp/dhis/query/QueryParserException.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/QueryParserException.java	2016-01-04 02:27:49 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/QueryParserException.java	2016-02-03 06:57:11 +0000
@@ -31,7 +31,7 @@
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
  */
-public class QueryParserException extends Exception
+public class QueryParserException extends RuntimeException
 {
     public QueryParserException( String message )
     {

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/DefaultMetadataExportService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/DefaultMetadataExportService.java	2016-01-27 09:06:30 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/DefaultMetadataExportService.java	2016-02-03 06:57:11 +0000
@@ -28,14 +28,17 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import com.google.common.collect.Sets;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.hisp.dhis.common.IdentifiableObject;
+import org.hisp.dhis.dxf2.common.OrderParams;
 import org.hisp.dhis.fieldfilter.FieldFilterService;
 import org.hisp.dhis.node.NodeUtils;
 import org.hisp.dhis.node.types.RootNode;
 import org.hisp.dhis.node.types.SimpleNode;
 import org.hisp.dhis.query.Query;
+import org.hisp.dhis.query.QueryParserException;
 import org.hisp.dhis.query.QueryService;
 import org.hisp.dhis.schema.Schema;
 import org.hisp.dhis.schema.SchemaService;
@@ -44,6 +47,7 @@
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
+import java.util.ArrayList;
 import java.util.Date;
 import java.util.HashMap;
 import java.util.List;
@@ -138,17 +142,82 @@
 
     @Override
     @SuppressWarnings( "unchecked" )
-    public MetadataExportParams getParamsFromMap( Map<String, String> parameters )
+    public MetadataExportParams getParamsFromMap( Map<String, List<String>> parameters )
     {
         MetadataExportParams params = new MetadataExportParams();
-
-        for ( String p : parameters.keySet() )
-        {
-            Schema schema = schemaService.getSchemaByPluralName( p );
-
-            if ( schema != null && schema.isIdentifiableObject() && "true".equalsIgnoreCase( parameters.get( p ) ) )
-            {
-                params.addClass( (Class<? extends IdentifiableObject>) schema.getKlass() );
+        Map<Class<? extends IdentifiableObject>, Map<String, List<String>>> map = new HashMap<>();
+
+        for ( String parameterKey : parameters.keySet() )
+        {
+            String[] parameter = parameterKey.split( ":" );
+            Schema schema = schemaService.getSchemaByPluralName( parameter[0] );
+
+            if ( schema == null || !schema.isIdentifiableObject() )
+            {
+                continue;
+            }
+
+            Class<? extends IdentifiableObject> klass = (Class<? extends IdentifiableObject>) schema.getKlass();
+
+            // class is enabled if value = true, or fields/filter/order is present
+            if ( "true".equalsIgnoreCase( parameters.get( parameterKey ).get( 0 ) ) || (parameter.length > 1 && ("fields".equalsIgnoreCase( parameter[1] )
+                || "filter".equalsIgnoreCase( parameter[1] ) || "order".equalsIgnoreCase( parameter[1] ))) )
+            {
+                if ( !map.containsKey( klass ) ) map.put( klass, new HashMap<>() );
+            }
+            else
+            {
+                continue;
+            }
+
+            if ( "fields".equalsIgnoreCase( parameter[1] ) )
+            {
+                if ( !map.get( klass ).containsKey( "fields" ) ) map.get( klass ).put( "fields", new ArrayList<>() );
+                map.get( klass ).get( "fields" ).addAll( parameters.get( parameterKey ) );
+            }
+
+            if ( "filter".equalsIgnoreCase( parameter[1] ) )
+            {
+                if ( !map.get( klass ).containsKey( "filter" ) ) map.get( klass ).put( "filter", new ArrayList<>() );
+                map.get( klass ).get( "filter" ).addAll( parameters.get( parameterKey ) );
+            }
+
+            if ( "order".equalsIgnoreCase( parameter[1] ) )
+            {
+                if ( !map.get( klass ).containsKey( "order" ) ) map.get( klass ).put( "order", new ArrayList<>() );
+                map.get( klass ).get( "order" ).addAll( parameters.get( parameterKey ) );
+            }
+        }
+
+        map.keySet().forEach( params::addClass );
+
+        for ( Class<? extends IdentifiableObject> klass : map.keySet() )
+        {
+            Map<String, List<String>> classMap = map.get( klass );
+            Schema schema = schemaService.getDynamicSchema( klass );
+
+            if ( classMap.containsKey( "fields" ) ) params.addFields( klass, classMap.get( "fields" ) );
+
+            if ( classMap.containsKey( "filter" ) && classMap.containsKey( "order" ) )
+            {
+                OrderParams orderParams = new OrderParams();
+                orderParams.setOrder( Sets.newHashSet( classMap.get( "order" ) ) );
+
+                Query query = queryService.getQueryFromUrl( klass, classMap.get( "filter" ), orderParams.getOrders( schema ) );
+                params.addQuery( query );
+            }
+            else if ( classMap.containsKey( "filter" ) )
+            {
+                Query query = queryService.getQueryFromUrl( klass, classMap.get( "filter" ), new ArrayList<>() );
+                params.addQuery( query );
+            }
+            else if ( classMap.containsKey( "order" ) )
+            {
+                OrderParams orderParams = new OrderParams();
+                orderParams.setOrder( Sets.newHashSet( classMap.get( "order" ) ) );
+
+                Query query = queryService.getQueryFromUrl( klass, new ArrayList<>(), orderParams.getOrders( schema ) );
+                params.addQuery( query );
             }
         }
 

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/MetadataExportParams.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/MetadataExportParams.java	2016-01-27 09:06:30 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/MetadataExportParams.java	2016-02-03 06:57:11 +0000
@@ -30,8 +30,10 @@
 
 import com.google.common.collect.Lists;
 import org.hisp.dhis.common.IdentifiableObject;
+import org.hisp.dhis.query.Order;
 import org.hisp.dhis.query.Query;
 
+import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
@@ -47,6 +49,8 @@
 
     private Map<Class<? extends IdentifiableObject>, Query> queries = new HashMap<>();
 
+    private Map<Class<? extends IdentifiableObject>, List<Order>> orders = new HashMap<>();
+
     private Map<Class<? extends IdentifiableObject>, List<String>> fields = new HashMap<>();
 
     private static final List<String> defaultFields = Lists.newArrayList( ":owner" );
@@ -74,10 +78,7 @@
     @SuppressWarnings( "unchecked" )
     public MetadataExportParams addQuery( Query query )
     {
-        if ( !query.getSchema().isIdentifiableObject() )
-        {
-            return this;
-        }
+        if ( !query.getSchema().isIdentifiableObject() ) return this;
 
         Class<? extends IdentifiableObject> klass = (Class<? extends IdentifiableObject>) query.getSchema().getKlass();
         classes.add( klass );
@@ -91,12 +92,22 @@
         return queries.get( klass );
     }
 
+    public MetadataExportParams addOrder( Class<? extends IdentifiableObject> klass, Order order )
+    {
+        if ( !orders.containsKey( klass ) ) orders.put( klass, new ArrayList<>() );
+
+        orders.get( klass ).add( order );
+        return this;
+    }
+
+    public Map<Class<? extends IdentifiableObject>, List<Order>> getOrders()
+    {
+        return orders;
+    }
+
     public MetadataExportParams addFields( Class<? extends IdentifiableObject> klass, List<String> classFields )
     {
-        if ( !fields.containsKey( klass ) )
-        {
-            fields.put( klass, classFields );
-        }
+        if ( !fields.containsKey( klass ) ) fields.put( klass, classFields );
 
         fields.get( klass ).addAll( classFields );
         return this;

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/MetadataExportService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/MetadataExportService.java	2016-01-27 09:06:30 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/MetadataExportService.java	2016-02-03 06:57:11 +0000
@@ -45,5 +45,5 @@
 
     void validate( MetadataExportParams params ) throws MetadataExportException;
 
-    MetadataExportParams getParamsFromMap( Map<String, String> parameters );
+    MetadataExportParams getParamsFromMap( Map<String, List<String>> parameters );
 }

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/metadata/MetadataExportController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/metadata/MetadataExportController.java	2016-01-27 09:06:30 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/metadata/MetadataExportController.java	2016-02-03 06:57:11 +0000
@@ -31,15 +31,13 @@
 import org.hisp.dhis.dxf2.metadata2.MetadataExportParams;
 import org.hisp.dhis.dxf2.metadata2.MetadataExportService;
 import org.hisp.dhis.node.types.RootNode;
+import org.hisp.dhis.webapi.service.ContextService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
-import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.ResponseBody;
 
-import java.util.Map;
-
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
  */
@@ -50,10 +48,13 @@
     @Autowired
     private MetadataExportService metadataExportService;
 
+    @Autowired
+    private ContextService contextService;
+
     @RequestMapping( value = "", method = RequestMethod.GET )
-    public @ResponseBody RootNode getMetadata( @RequestParam Map<String, String> rpParameters )
+    public @ResponseBody RootNode getMetadata()
     {
-        MetadataExportParams params = metadataExportService.getParamsFromMap( rpParameters );
+        MetadataExportParams params = metadataExportService.getParamsFromMap( contextService.getParameterValuesMap() );
         return metadataExportService.getMetadataAsNode( params );
     }
 }

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/service/ContextService.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/service/ContextService.java	2016-01-04 02:27:49 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/service/ContextService.java	2016-02-03 06:57:11 +0000
@@ -29,6 +29,8 @@
  */
 
 import javax.servlet.http.HttpServletRequest;
+import java.util.List;
+import java.util.Map;
 import java.util.Set;
 
 /**
@@ -75,4 +77,9 @@
      * @return List of parameter values, or empty if not found
      */
     Set<String> getParameterValues( String name );
+
+    /**
+     * Get all parameters as a map of key => values, supports more than one pr key (so values is a collection)
+     */
+    Map<String, List<String>> getParameterValuesMap();
 }

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/service/DefaultContextService.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/service/DefaultContextService.java	2016-01-04 02:27:49 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/service/DefaultContextService.java	2016-02-03 06:57:11 +0000
@@ -28,6 +28,7 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import com.google.common.collect.Lists;
 import com.google.common.collect.Sets;
 import org.springframework.stereotype.Service;
 import org.springframework.web.context.request.RequestContextHolder;
@@ -35,6 +36,9 @@
 
 import javax.servlet.http.HttpServletRequest;
 import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
 import java.util.Set;
 
 /**
@@ -115,4 +119,18 @@
 
         return parameter;
     }
+
+    @Override
+    public Map<String, List<String>> getParameterValuesMap()
+    {
+        Map<String, String[]> parameterMap = getRequest().getParameterMap();
+        Map<String, List<String>> map = new HashMap<>();
+
+        for ( String key : parameterMap.keySet() )
+        {
+            map.put( key, Lists.newArrayList( parameterMap.get( key ) ) );
+        }
+
+        return map;
+    }
 }