← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 20964: Add new QueryParser + tests, will replace (object field) ParserService, wip

 

------------------------------------------------------------
revno: 20964
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2015-11-05 15:40:46 +0700
message:
  Add new QueryParser + tests, will replace (object field) ParserService, wip
added:
  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/main/java/org/hisp/dhis/query/QueryParserException.java
  dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/query/QueryParserTest.java
modified:
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/operators/Operator.java
  dhis-2/dhis-services/dhis-service-core/src/main/resources/META-INF/dhis/beans.xml


--
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
=== added 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	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/DefaultQueryParser.java	2015-11-05 08:40:46 +0000
@@ -0,0 +1,133 @@
+package org.hisp.dhis.query;
+
+/*
+ * Copyright (c) 2004-2015, 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 org.hisp.dhis.query.operators.MatchMode;
+import org.hisp.dhis.schema.Property;
+import org.hisp.dhis.schema.Schema;
+
+import java.util.Collection;
+import java.util.List;
+
+/**
+ * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
+ */
+public class DefaultQueryParser implements QueryParser
+{
+    @Override
+    public Query parse( Schema schema, List<String> filters ) throws QueryParserException
+    {
+        Query query = Query.from( schema );
+
+        for ( String filter : filters )
+        {
+            String[] split = filter.split( ":" );
+
+            if ( !(split.length >= 2) )
+            {
+                throw new QueryParserException( "Invalid filter => " + filter );
+            }
+
+            if ( split.length >= 3 )
+            {
+                int index = split[0].length() + ":".length() + split[1].length() + ":".length();
+                query.add( getRestriction( schema, split[0], split[1], filter.substring( index ) ) );
+            }
+            else
+            {
+                query.add( getRestriction( schema, split[0], split[1], null ) );
+            }
+        }
+
+        return query;
+    }
+
+    private Restriction getRestriction( Schema schema, String path, String operator, Object arg )
+    {
+        Property property = schema.getProperty( path );
+
+        switch ( operator )
+        {
+            case "eq":
+            {
+                return Restrictions.eq( path, QueryUtils.getValue( property.getKlass(), arg ) );
+            }
+            case "ne":
+            {
+                return Restrictions.ne( path, QueryUtils.getValue( property.getKlass(), arg ) );
+            }
+            case "neq":
+            {
+                return Restrictions.ne( path, QueryUtils.getValue( property.getKlass(), arg ) );
+            }
+            case "gt":
+            {
+                return Restrictions.gt( path, QueryUtils.getValue( property.getKlass(), arg ) );
+            }
+            case "lt":
+            {
+                return Restrictions.lt( path, QueryUtils.getValue( property.getKlass(), arg ) );
+            }
+            case "gte":
+            {
+                return Restrictions.ge( path, QueryUtils.getValue( property.getKlass(), arg ) );
+            }
+            case "ge":
+            {
+                return Restrictions.ge( path, QueryUtils.getValue( property.getKlass(), arg ) );
+            }
+            case "lte":
+            {
+                return Restrictions.le( path, QueryUtils.getValue( property.getKlass(), arg ) );
+            }
+            case "le":
+            {
+                return Restrictions.le( path, QueryUtils.getValue( property.getKlass(), arg ) );
+            }
+            case "like":
+            {
+                return Restrictions.ilike( path, String.valueOf( arg ), MatchMode.ANYWHERE );
+            }
+            case "ilike":
+            {
+                return Restrictions.ilike( path, String.valueOf( arg ), MatchMode.ANYWHERE );
+            }
+            case "in":
+            {
+                return Restrictions.in( path, QueryUtils.getValue( Collection.class, arg ) );
+            }
+            case "null":
+            {
+                return Restrictions.isNull( path );
+            }
+        }
+
+        return null;
+    }
+}

=== added 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	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/QueryParser.java	2015-11-05 08:40:46 +0000
@@ -0,0 +1,41 @@
+package org.hisp.dhis.query;
+
+/*
+ * Copyright (c) 2004-2015, 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 org.hisp.dhis.schema.Schema;
+
+import java.util.List;
+
+/**
+ * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
+ */
+public interface QueryParser
+{
+    Query parse( Schema schema, List<String> filters ) throws QueryParserException;
+}

=== added 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	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/QueryParserException.java	2015-11-05 08:40:46 +0000
@@ -0,0 +1,40 @@
+package org.hisp.dhis.query;
+
+/*
+ * Copyright (c) 2004-2015, 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.
+ */
+
+/**
+ * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
+ */
+public class QueryParserException extends Exception
+{
+    public QueryParserException( String message )
+    {
+        super( message );
+    }
+}

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/operators/Operator.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/operators/Operator.java	2015-11-05 03:55:41 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/operators/Operator.java	2015-11-05 08:40:46 +0000
@@ -62,6 +62,11 @@
         Collections.addAll( this.args, args );
     }
 
+    public List<Object> getArgs()
+    {
+        return args;
+    }
+
     protected <T> T getValue( Class<T> klass, int idx )
     {
         return QueryUtils.getValue( klass, args.get( idx ) );

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/resources/META-INF/dhis/beans.xml'
--- dhis-2/dhis-services/dhis-service-core/src/main/resources/META-INF/dhis/beans.xml	2015-11-02 04:27:25 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/resources/META-INF/dhis/beans.xml	2015-11-05 08:40:46 +0000
@@ -19,10 +19,12 @@
 
   <bean id="org.hisp.dhis.schema.SchemaService" class="org.hisp.dhis.schema.DefaultSchemaService" />
 
+  <bean id="org.hisp.dhis.query.QueryParser" class="org.hisp.dhis.query.DefaultQueryParser" />
+
+  <bean id="org.hisp.dhis.query.QueryService" class="org.hisp.dhis.query.DefaultQueryService" />
+
   <bean id="org.hisp.dhis.query.CriteriaQueryEngine" class="org.hisp.dhis.query.CriteriaQueryEngine" />
 
-  <bean id="org.hisp.dhis.query.QueryService" class="org.hisp.dhis.query.DefaultQueryService" />
-
   <bean id="org.hisp.dhis.schema.PropertyIntrospectorService" class="org.hisp.dhis.schema.Jackson2PropertyIntrospectorService" />
 
   <bean id="org.hisp.dhis.security.acl.AclService" class="org.hisp.dhis.security.acl.DefaultAclService" />

=== added 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	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/query/QueryParserTest.java	2015-11-05 08:40:46 +0000
@@ -0,0 +1,97 @@
+package org.hisp.dhis.query;
+
+/*
+ * Copyright (c) 2004-2015, 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 org.hisp.dhis.DhisSpringTest;
+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;
+
+import java.util.Arrays;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
+ */
+public class QueryParserTest
+    extends DhisSpringTest
+{
+    @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" ) );
+    }
+
+    @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 );
+    }
+
+    @Test
+    public void nullOperator() throws QueryParserException
+    {
+        Schema schema = schemaService.getSchema( DataElement.class );
+        Query query = queryParser.parse( schema, Arrays.asList( "id:null", "name:null" ) );
+        assertEquals( 2, query.getCriterions().size() );
+
+        Restriction restriction = (Restriction) query.getCriterions().get( 0 );
+        assertEquals( "id", restriction.getPath() );
+        assertTrue( restriction.getOperator() instanceof NullOperator );
+
+        restriction = (Restriction) query.getCriterions().get( 1 );
+        assertEquals( "name", restriction.getPath() );
+        assertTrue( restriction.getOperator() instanceof NullOperator );
+    }
+}
\ No newline at end of file