← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 19052: support field filtering on /api/schemas endpoint

 

------------------------------------------------------------
revno: 19052
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2015-04-29 13:48:31 +0700
message:
  support field filtering on /api/schemas endpoint
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/NodeUtils.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/types/CollectionNode.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/fieldfilter/DefaultFieldFilterService.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/SchemaController.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/node/NodeUtils.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/NodeUtils.java	2015-03-11 08:04:51 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/NodeUtils.java	2015-04-29 06:48:31 +0000
@@ -39,6 +39,15 @@
  */
 public final class NodeUtils
 {
+    public static RootNode createRootNode( String name )
+    {
+        RootNode rootNode = new RootNode( name );
+        rootNode.setDefaultNamespace( DxfNamespaces.DXF_2_0 );
+        rootNode.setNamespace( DxfNamespaces.DXF_2_0 );
+
+        return rootNode;
+    }
+
     public static RootNode createRootNode( Node node )
     {
         RootNode rootNode = new RootNode( node );

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/types/CollectionNode.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/types/CollectionNode.java	2015-01-17 07:41:26 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/types/CollectionNode.java	2015-04-29 06:48:31 +0000
@@ -48,6 +48,12 @@
         super( name, NodeType.COLLECTION );
     }
 
+    public CollectionNode( String name, boolean wrapping )
+    {
+        super( name, NodeType.COLLECTION );
+        this.wrapping = wrapping;
+    }
+
     public boolean isWrapping()
     {
         return wrapping;

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/fieldfilter/DefaultFieldFilterService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/fieldfilter/DefaultFieldFilterService.java	2015-03-24 03:01:32 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/fieldfilter/DefaultFieldFilterService.java	2015-04-29 06:48:31 +0000
@@ -239,10 +239,13 @@
                     }
                     else
                     {
-                        for ( Object collectionObject : collection )
+                        if ( collection != null )
                         {
-                            SimpleNode simpleNode = child.addChild( new SimpleNode( property.getName(), collectionObject ) );
-                            simpleNode.setProperty( property );
+                            for ( Object collectionObject : collection )
+                            {
+                                SimpleNode simpleNode = child.addChild( new SimpleNode( property.getName(), collectionObject ) );
+                                simpleNode.setProperty( property );
+                            }
                         }
                     }
                 }

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/SchemaController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/SchemaController.java	2015-04-24 06:42:41 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/SchemaController.java	2015-04-29 06:48:31 +0000
@@ -28,14 +28,20 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import com.google.common.collect.Lists;
+import org.hisp.dhis.dxf2.fieldfilter.FieldFilterService;
 import org.hisp.dhis.dxf2.render.RenderService;
 import org.hisp.dhis.dxf2.schema.SchemaValidator;
 import org.hisp.dhis.dxf2.schema.ValidationViolation;
 import org.hisp.dhis.dxf2.schema.ValidationViolations;
+import org.hisp.dhis.node.NodeUtils;
+import org.hisp.dhis.node.types.CollectionNode;
+import org.hisp.dhis.node.types.RootNode;
 import org.hisp.dhis.schema.Property;
 import org.hisp.dhis.schema.Schema;
 import org.hisp.dhis.schema.SchemaService;
 import org.hisp.dhis.schema.Schemas;
+import org.hisp.dhis.webapi.service.ContextService;
 import org.hisp.dhis.webapi.service.LinkService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.HttpStatus;
@@ -50,6 +56,7 @@
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import java.io.IOException;
+import java.util.Arrays;
 import java.util.List;
 
 /**
@@ -71,24 +78,51 @@
     @Autowired
     private LinkService linkService;
 
+    @Autowired
+    private FieldFilterService fieldFilterService;
+
+    @Autowired
+    protected ContextService contextService;
+
     @RequestMapping
-    public @ResponseBody Schemas getSchemas()
+    public @ResponseBody RootNode getSchemas()
     {
+        List<String> fields = Lists.newArrayList( contextService.getParameterValues( "fields" ) );
+
+        if ( fields.isEmpty() )
+        {
+            fields.add( "*,properties[*,constants[*]]" );
+        }
+
         Schemas schemas = new Schemas( schemaService.getSortedSchemas() );
         linkService.generateSchemaLinks( schemas.getSchemas() );
 
-        return schemas;
+        RootNode rootNode = NodeUtils.createRootNode( "schemas" );
+        CollectionNode collectionNode = fieldFilterService.filter( Schema.class, schemas.getSchemas(), fields );
+        collectionNode.setWrapping( false );
+        rootNode.addChild( collectionNode );
+
+        return rootNode;
     }
 
     @RequestMapping( value = "/{type}", method = RequestMethod.GET )
-    public @ResponseBody Schema getSchema( @PathVariable String type )
+    public @ResponseBody RootNode getSchema( @PathVariable String type )
     {
+        List<String> fields = Lists.newArrayList( contextService.getParameterValues( "fields" ) );
+
+        if ( fields.isEmpty() )
+        {
+            fields.add( "*,properties[*,constants[*]]" );
+        }
+
         Schema schema = getSchemaFromType( type );
 
         if ( schema != null )
         {
             linkService.generateSchemaLinks( schema );
-            return schema;
+
+            CollectionNode collectionNode = fieldFilterService.filter( Schema.class, Arrays.asList( schema ), fields );
+            return NodeUtils.createRootNode( collectionNode.getChildren().get( 0 ) );
         }
 
         throw new HttpClientErrorException( HttpStatus.NOT_FOUND, "Type " + type + " does not exist." );