← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 21006: support deep filtering in InMemoryQueryEngine + add more tests

 

------------------------------------------------------------
revno: 21006
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2015-11-10 13:17:17 +0700
message:
  support deep filtering in InMemoryQueryEngine + add more tests
modified:
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/InMemoryQueryEngine.java
  dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/query/InMemoryQueryEngineTest.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/InMemoryQueryEngine.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/InMemoryQueryEngine.java	2015-11-10 03:44:53 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/InMemoryQueryEngine.java	2015-11-10 06:17:17 +0000
@@ -28,13 +28,17 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import com.google.common.collect.Lists;
 import org.hisp.dhis.common.IdentifiableObject;
 import org.hisp.dhis.common.PagerUtils;
 import org.hisp.dhis.schema.Property;
 import org.hisp.dhis.schema.Schema;
+import org.hisp.dhis.schema.SchemaService;
 import org.hisp.dhis.system.util.ReflectionUtils;
+import org.springframework.beans.factory.annotation.Autowired;
 
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.List;
 import java.util.stream.Collectors;
 
@@ -43,6 +47,9 @@
  */
 public class InMemoryQueryEngine<T extends IdentifiableObject> implements QueryEngine
 {
+    @Autowired
+    private SchemaService schemaService;
+
     @Override
     public List<T> query( Query query )
     {
@@ -103,40 +110,50 @@
 
     private boolean test( Query query, T object )
     {
+        List<Boolean> testResults = new ArrayList<>();
+
         for ( Criterion criterion : query.getCriterions() )
         {
+            Boolean testResult = false;
+
             // normal Restriction, just assume Conjunction
             if ( Restriction.class.isInstance( criterion ) )
             {
                 Restriction restriction = (Restriction) criterion;
                 Object value = getValue( query, object, restriction.getPath() );
 
-                if ( !restriction.getOperator().test( value ) )
-                {
-                    return false;
+                if ( !Collection.class.isInstance( value ) )
+                {
+                    testResult = restriction.getOperator().test( value );
+                }
+                else
+                {
+                    Collection<?> collection = (Collection<?>) value;
+
+                    for ( Object item : collection )
+                    {
+                        if ( restriction.getOperator().test( item ) )
+                        {
+                            testResult = true;
+                        }
+                    }
                 }
             }
             else if ( Conjunction.class.isInstance( criterion ) )
             {
                 Conjunction conjunction = (Conjunction) criterion;
-
-                if ( !testAnd( query, object, conjunction.getCriterions() ) )
-                {
-                    return false;
-                }
+                testResult = testAnd( query, object, conjunction.getCriterions() );
             }
             else if ( Disjunction.class.isInstance( criterion ) )
             {
                 Disjunction disjunction = (Disjunction) criterion;
-
-                if ( !testOr( query, object, disjunction.getCriterions() ) )
-                {
-                    return false;
-                }
+                testResult = testOr( query, object, disjunction.getCriterions() );
             }
+
+            testResults.add( testResult );
         }
 
-        return true;
+        return !testResults.contains( Boolean.FALSE );
     }
 
     private boolean testAnd( Query query, T object, List<Criterion> criterions )
@@ -148,8 +165,25 @@
                 Restriction restriction = (Restriction) criterion;
                 Object value = getValue( query, object, restriction.getPath() );
 
-                if ( !restriction.getOperator().test( value ) )
-                {
+                if ( !Collection.class.isInstance( value ) )
+                {
+                    if ( !restriction.getOperator().test( value ) )
+                    {
+                        return false;
+                    }
+                }
+                else
+                {
+                    Collection<?> collection = (Collection<?>) value;
+
+                    for ( Object item : collection )
+                    {
+                        if ( restriction.getOperator().test( item ) )
+                        {
+                            return true;
+                        }
+                    }
+
                     return false;
                 }
             }
@@ -167,9 +201,24 @@
                 Restriction restriction = (Restriction) criterion;
                 Object value = getValue( query, object, restriction.getPath() );
 
-                if ( restriction.getOperator().test( value ) )
-                {
-                    return true;
+                if ( !Collection.class.isInstance( value ) )
+                {
+                    if ( restriction.getOperator().test( value ) )
+                    {
+                        return true;
+                    }
+                }
+                else
+                {
+                    Collection<?> collection = (Collection<?>) value;
+
+                    for ( Object item : collection )
+                    {
+                        if ( restriction.getOperator().test( item ) )
+                        {
+                            return true;
+                        }
+                    }
                 }
             }
         }
@@ -193,17 +242,46 @@
 
             object = ReflectionUtils.invokeMethod( object, property.getGetterMethod() );
 
-            if ( property.isSimple() )
+            if ( i == (paths.length - 1) )
             {
-                if ( i != (paths.length - 1) )
+                if ( property.isCollection() )
                 {
-                    throw new QueryException( "Simple property was found before finished parsing path expression, please check your path string." );
+                    return Lists.newArrayList( object );
                 }
 
                 return object;
             }
+
+            if ( property.isCollection() )
+            {
+                currentSchema = schemaService.getDynamicSchema( property.getItemKlass() );
+            }
+            else
+            {
+                currentSchema = schemaService.getDynamicSchema( property.getKlass() );
+            }
+
+            if ( property.isCollection() && i == (paths.length - 2) )
+            {
+                property = currentSchema.getProperty( paths[paths.length - 1] );
+
+                if ( property == null )
+                {
+                    throw new QueryException( "No property found for path " + path );
+                }
+
+                Collection<?> collection = (Collection<?>) object;
+                List<Object> items = new ArrayList<>();
+
+                for ( Object item : collection )
+                {
+                    items.add( ReflectionUtils.invokeMethod( item, property.getGetterMethod() ) );
+                }
+
+                return items;
+            }
         }
 
-        return null;
+        throw new QueryException( "No values found for path " + path );
     }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/query/InMemoryQueryEngineTest.java'
--- dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/query/InMemoryQueryEngineTest.java	2015-11-10 03:44:53 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/query/InMemoryQueryEngineTest.java	2015-11-10 06:17:17 +0000
@@ -39,7 +39,6 @@
 import org.hisp.dhis.schema.SchemaService;
 import org.jfree.data.time.Year;
 import org.junit.Before;
-import org.junit.Ignore;
 import org.junit.Test;
 import org.springframework.beans.factory.annotation.Autowired;
 
@@ -521,8 +520,7 @@
     }
 
     @Test
-    @Ignore
-    public void testEqDeepPath()
+    public void testEqIdDeepPath()
     {
         Query query = Query.from( schemaService.getDynamicSchema( DataElementGroup.class ) );
         query.setObjects( dataElementGroups );
@@ -532,4 +530,43 @@
         assertEquals( 1, objects.size() );
         assertEquals( "abcdefghijA", objects.get( 0 ).getUid() );
     }
+
+    @Test
+    public void testLikeNameDeepPath()
+    {
+        Query query = Query.from( schemaService.getDynamicSchema( DataElementGroup.class ) );
+        query.setObjects( dataElementGroups );
+        query.add( Restrictions.like( "dataElements.name", "ElementD", MatchMode.END ) );
+        List<? extends IdentifiableObject> objects = queryEngine.query( query );
+
+        assertEquals( 1, objects.size() );
+        assertEquals( "abcdefghijB", objects.get( 0 ).getUid() );
+    }
+
+    @Test
+    public void testLikeNamesDeepPath()
+    {
+        Query query = Query.from( schemaService.getDynamicSchema( DataElementGroup.class ) );
+        query.setObjects( dataElementGroups );
+
+        Disjunction disjunction = query.disjunction();
+        disjunction.add( Restrictions.like( "dataElements.name", "ElementD", MatchMode.END ) );
+        disjunction.add( Restrictions.like( "dataElements.name", "ElementA", MatchMode.END ) );
+        List<? extends IdentifiableObject> objects = queryEngine.query( query );
+
+        assertEquals( 2, objects.size() );
+        assertTrue( collectionContainsUid( objects, "abcdefghijA" ) );
+        assertTrue( collectionContainsUid( objects, "abcdefghijB" ) );
+    }
+
+    @Test
+    public void testCollectionSize()
+    {
+        Query query = Query.from( schemaService.getDynamicSchema( DataElementGroup.class ) );
+        query.setObjects( dataElementGroups );
+        query.add( Restrictions.eq( "dataElements", 3 ) );
+        List<? extends IdentifiableObject> objects = queryEngine.query( query );
+
+        assertEquals( 2, objects.size() );
+    }
 }