← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 20969: support deep paths in QueryParser, adds more tests also

 

------------------------------------------------------------
revno: 20969
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Fri 2015-11-06 15:31:07 +0700
message:
  support deep paths in QueryParser, adds more tests also
modified:
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/Criteria.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/DefaultQueryParser.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/QueryParser.java
  dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/query/QueryParserTest.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/Criteria.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/Criteria.java	2015-11-05 05:04:18 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/Criteria.java	2015-11-06 08:31:07 +0000
@@ -65,10 +65,12 @@
 
             Restriction restriction = (Restriction) criterion;
 
+            /* Commented out for now, validation of this happens in QueryParser
             if ( !schema.haveProperty( restriction.getPath() ) )
             {
                 continue;
             }
+            */
 
             this.criterions.add( restriction );
         }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/DefaultQueryParser.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/DefaultQueryParser.java	2015-11-05 08:40:46 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/DefaultQueryParser.java	2015-11-06 08:31:07 +0000
@@ -31,6 +31,8 @@
 import org.hisp.dhis.query.operators.MatchMode;
 import org.hisp.dhis.schema.Property;
 import org.hisp.dhis.schema.Schema;
+import org.hisp.dhis.schema.SchemaService;
+import org.springframework.beans.factory.annotation.Autowired;
 
 import java.util.Collection;
 import java.util.List;
@@ -40,9 +42,13 @@
  */
 public class DefaultQueryParser implements QueryParser
 {
+    @Autowired
+    private SchemaService schemaService;
+
     @Override
-    public Query parse( Schema schema, List<String> filters ) throws QueryParserException
+    public Query parse( Class<?> klass, List<String> filters ) throws QueryParserException
     {
+        Schema schema = schemaService.getDynamicSchema( klass );
         Query query = Query.from( schema );
 
         for ( String filter : filters )
@@ -68,9 +74,14 @@
         return query;
     }
 
-    private Restriction getRestriction( Schema schema, String path, String operator, Object arg )
+    private Restriction getRestriction( Schema schema, String path, String operator, Object arg ) throws QueryParserException
     {
-        Property property = schema.getProperty( path );
+        Property property = getProperty( schema, path );
+
+        if ( property == null )
+        {
+            throw new QueryParserException( "Unknown path property: " + path );
+        }
 
         switch ( operator )
         {
@@ -130,4 +141,46 @@
 
         return null;
     }
+
+    private Property getProperty( Schema schema, String path ) throws QueryParserException
+    {
+        String[] paths = path.split( "\\." );
+        Schema currentSchema = schema;
+
+        for ( int i = 0; i < paths.length; i++ )
+        {
+            if ( !currentSchema.haveProperty( paths[i] ) )
+            {
+                return null;
+            }
+
+            Property property = currentSchema.getProperty( paths[i] );
+
+            if ( property == null )
+            {
+                throw new QueryParserException( "Unknown path property: " + paths[i] + " (" + path + ")" );
+            }
+
+            if ( property.isSimple() )
+            {
+                if ( i != (paths.length - 1) )
+                {
+                    throw new QueryParserException( "Simple type was found before finished parsing path expression, please check your path string." );
+                }
+
+                return property;
+            }
+
+            if ( property.isCollection() )
+            {
+                currentSchema = schemaService.getDynamicSchema( property.getItemKlass() );
+            }
+            else
+            {
+                currentSchema = schemaService.getDynamicSchema( property.getKlass() );
+            }
+        }
+
+        return null;
+    }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/QueryParser.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/QueryParser.java	2015-11-05 08:40:46 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/QueryParser.java	2015-11-06 08:31:07 +0000
@@ -28,8 +28,6 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import org.hisp.dhis.schema.Schema;
-
 import java.util.List;
 
 /**
@@ -37,5 +35,5 @@
  */
 public interface QueryParser
 {
-    Query parse( Schema schema, List<String> filters ) throws QueryParserException;
+    Query parse( Class<?> klass, List<String> filters ) throws QueryParserException;
 }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/query/QueryParserTest.java'
--- dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/query/QueryParserTest.java	2015-11-05 08:40:46 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/query/QueryParserTest.java	2015-11-06 08:31:07 +0000
@@ -32,8 +32,6 @@
 import org.hisp.dhis.dataelement.DataElement;
 import org.hisp.dhis.query.operators.EqualOperator;
 import org.hisp.dhis.query.operators.NullOperator;
-import org.hisp.dhis.schema.Schema;
-import org.hisp.dhis.schema.SchemaService;
 import org.junit.Test;
 import org.springframework.beans.factory.annotation.Autowired;
 
@@ -51,39 +49,62 @@
     @Autowired
     private QueryParser queryParser;
 
-    @Autowired
-    private SchemaService schemaService;
-
     @Test( expected = QueryParserException.class )
     public void failedFilters() throws QueryParserException
     {
-        Schema schema = schemaService.getSchema( DataElement.class );
-        queryParser.parse( schema, Arrays.asList( "id", "name" ) );
+        queryParser.parse( DataElement.class, Arrays.asList( "id", "name" ) );
     }
 
     @Test
     public void eqOperator() throws QueryParserException
     {
-        Schema schema = schemaService.getSchema( DataElement.class );
-        Query query = queryParser.parse( schema, Arrays.asList( "id:eq:1", "id:eq:2" ) );
-        assertEquals( 2, query.getCriterions().size() );
-
-        Restriction restriction = (Restriction) query.getCriterions().get( 0 );
-        assertEquals( "id", restriction.getPath() );
-        assertEquals( "1", restriction.getOperator().getArgs().get( 0 ) );
-        assertTrue( restriction.getOperator() instanceof EqualOperator );
-
-        restriction = (Restriction) query.getCriterions().get( 1 );
-        assertEquals( "id", restriction.getPath() );
-        assertEquals( "2", restriction.getOperator().getArgs().get( 0 ) );
-        assertTrue( restriction.getOperator() instanceof EqualOperator );
+        Query query = queryParser.parse( DataElement.class, Arrays.asList( "id:eq:1", "id:eq:2" ) );
+        assertEquals( 2, query.getCriterions().size() );
+
+        Restriction restriction = (Restriction) query.getCriterions().get( 0 );
+        assertEquals( "id", restriction.getPath() );
+        assertEquals( "1", restriction.getOperator().getArgs().get( 0 ) );
+        assertTrue( restriction.getOperator() instanceof EqualOperator );
+
+        restriction = (Restriction) query.getCriterions().get( 1 );
+        assertEquals( "id", restriction.getPath() );
+        assertEquals( "2", restriction.getOperator().getArgs().get( 0 ) );
+        assertTrue( restriction.getOperator() instanceof EqualOperator );
+    }
+
+    @Test
+    public void eqOperatorDeepPath1() throws QueryParserException
+    {
+        Query query = queryParser.parse( DataElement.class, Arrays.asList( "dataElementGroups.id:eq:1", "dataElementGroups.id:eq:2" ) );
+        assertEquals( 2, query.getCriterions().size() );
+
+        Restriction restriction = (Restriction) query.getCriterions().get( 0 );
+        assertEquals( "dataElementGroups.id", restriction.getPath() );
+        assertEquals( "1", restriction.getOperator().getArgs().get( 0 ) );
+        assertTrue( restriction.getOperator() instanceof EqualOperator );
+
+        restriction = (Restriction) query.getCriterions().get( 1 );
+        assertEquals( "dataElementGroups.id", restriction.getPath() );
+        assertEquals( "2", restriction.getOperator().getArgs().get( 0 ) );
+        assertTrue( restriction.getOperator() instanceof EqualOperator );
+    }
+
+    @Test( expected = QueryParserException.class )
+    public void eqOperatorDeepPathFail() throws QueryParserException
+    {
+        queryParser.parse( DataElement.class, Arrays.asList( "dataElementGroups.id.name:eq:1", "dataElementGroups.id.abc:eq:2" ) );
+    }
+
+    @Test( expected = QueryParserException.class )
+    public void eqOperatorDeepPathFailNotSimpleType() throws QueryParserException
+    {
+        queryParser.parse( DataElement.class, Arrays.asList( "dataElementGroups:eq:1", "dataElementGroups:eq:2" ) );
     }
 
     @Test
     public void nullOperator() throws QueryParserException
     {
-        Schema schema = schemaService.getSchema( DataElement.class );
-        Query query = queryParser.parse( schema, Arrays.asList( "id:null", "name:null" ) );
+        Query query = queryParser.parse( DataElement.class, Arrays.asList( "id:null", "name:null" ) );
         assertEquals( 2, query.getCriterions().size() );
 
         Restriction restriction = (Restriction) query.getCriterions().get( 0 );