← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 19282: changed default for object filter to use AND, support AND/OR mixes in query-service

 

------------------------------------------------------------
revno: 19282
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2015-06-04 08:55:31 +0700
message:
  changed default for object filter to use AND, support AND/OR mixes in query-service
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/query/Query.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/CriteriaQueryEngine.java
  dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/query/QueryServiceTest.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/objectfilter/DefaultObjectFilterService.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/objectfilter/FilterOps.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/objectfilter/Filters.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/query/Query.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/query/Query.java	2015-06-03 03:42:15 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/query/Query.java	2015-06-04 01:55:31 +0000
@@ -111,11 +111,27 @@
 
     public Disjunction addDisjunction()
     {
+        Disjunction disjunction = new Disjunction( schema );
+        add( disjunction );
+
+        return disjunction;
+    }
+
+    public Disjunction disjunction()
+    {
         return new Disjunction( schema );
     }
 
     public Conjunction addConjunction()
     {
+        Conjunction conjunction = new Conjunction( schema );
+        add( conjunction );
+
+        return conjunction;
+    }
+
+    public Conjunction conjunction()
+    {
         return new Conjunction( schema );
     }
 

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/CriteriaQueryEngine.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/CriteriaQueryEngine.java	2015-06-02 04:25:37 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/CriteriaQueryEngine.java	2015-06-04 01:55:31 +0000
@@ -30,7 +30,6 @@
 
 import org.hibernate.Criteria;
 import org.hibernate.criterion.Criterion;
-import org.hibernate.criterion.Disjunction;
 import org.hibernate.criterion.Projections;
 import org.hibernate.criterion.Restrictions;
 import org.hisp.dhis.common.IdentifiableObject;
@@ -119,8 +118,6 @@
 
     private Criteria buildCriteria( Criteria criteria, Query query )
     {
-        Map<Operator, List<Restriction>> restrictions = new HashMap<>();
-
         if ( query.getFirstResult() != null )
         {
             criteria.setFirstResult( query.getFirstResult() );
@@ -133,38 +130,7 @@
 
         for ( org.hisp.dhis.query.Criterion criterion : query.getCriterions() )
         {
-            if ( !Restriction.class.isInstance( criterion ) )
-            {
-                continue;
-            }
-
-            Restriction restriction = (Restriction) criterion;
-
-            if ( !restrictions.containsKey( restriction.getOperator() ) )
-            {
-                restrictions.put( restriction.getOperator(), new ArrayList<Restriction>() );
-            }
-
-            restrictions.get( restriction.getOperator() ).add( restriction );
-        }
-
-        for ( Map.Entry<Operator, List<Restriction>> restriction : restrictions.entrySet() )
-        {
-            if ( restriction.getValue().size() == 1 )
-            {
-                criteria.add( getHibernateCriterion( query.getSchema(), restriction.getValue().get( 0 ) ) );
-            }
-            else
-            {
-                Disjunction disjunction = Restrictions.disjunction();
-
-                for ( Restriction r : restriction.getValue() )
-                {
-                    disjunction.add( getHibernateCriterion( query.getSchema(), r ) );
-                }
-
-                criteria.add( disjunction );
-            }
+            addCriterion( criteria, criterion, query.getSchema() );
         }
 
         for ( Order order : query.getOrders() )
@@ -175,6 +141,64 @@
         return criteria;
     }
 
+    private void addJunction( org.hibernate.criterion.Junction junction, org.hisp.dhis.query.Criterion criterion, Schema schema )
+    {
+        if ( Restriction.class.isInstance( criterion ) )
+        {
+            Restriction restriction = (Restriction) criterion;
+            junction.add( getHibernateCriterion( schema, restriction ) );
+        }
+        else if ( Junction.class.isInstance( criterion ) )
+        {
+            org.hibernate.criterion.Junction j = null;
+
+            if ( Disjunction.class.isInstance( criterion ) )
+            {
+                j = Restrictions.disjunction();
+            }
+            else if ( Conjunction.class.isInstance( criterion ) )
+            {
+                j = Restrictions.conjunction();
+            }
+
+            junction.add( j );
+
+            for ( org.hisp.dhis.query.Criterion c : ((Junction) criterion).getCriterions() )
+            {
+                addJunction( junction, c, schema );
+            }
+        }
+    }
+
+    private void addCriterion( Criteria criteria, org.hisp.dhis.query.Criterion criterion, Schema schema )
+    {
+        if ( Restriction.class.isInstance( criterion ) )
+        {
+            Restriction restriction = (Restriction) criterion;
+            criteria.add( getHibernateCriterion( schema, restriction ) );
+        }
+        else if ( Junction.class.isInstance( criterion ) )
+        {
+            org.hibernate.criterion.Junction junction = null;
+
+            if ( Disjunction.class.isInstance( criterion ) )
+            {
+                junction = Restrictions.disjunction();
+            }
+            else if ( Conjunction.class.isInstance( criterion ) )
+            {
+                junction = Restrictions.conjunction();
+            }
+
+            criteria.add( junction );
+
+            for ( org.hisp.dhis.query.Criterion c : ((Junction) criterion).getCriterions() )
+            {
+                addJunction( junction, c, schema );
+            }
+        }
+    }
+
     @SuppressWarnings( "unchecked" )
     private Criterion getHibernateCriterion( Schema schema, Restriction restriction )
     {

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/query/QueryServiceTest.java'
--- dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/query/QueryServiceTest.java	2015-06-03 03:47:22 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/query/QueryServiceTest.java	2015-06-04 01:55:31 +0000
@@ -62,11 +62,22 @@
     private void createDataElements()
     {
         DataElement dataElementA = createDataElement( 'A' );
+        dataElementA.setNumberType( DataElement.VALUE_TYPE_NUMBER );
+
         DataElement dataElementB = createDataElement( 'B' );
+        dataElementB.setNumberType( DataElement.VALUE_TYPE_BOOL );
+
         DataElement dataElementC = createDataElement( 'C' );
+        dataElementC.setNumberType( DataElement.VALUE_TYPE_INT );
+
         DataElement dataElementD = createDataElement( 'D' );
+        dataElementD.setNumberType( DataElement.VALUE_TYPE_NUMBER );
+
         DataElement dataElementE = createDataElement( 'E' );
+        dataElementE.setNumberType( DataElement.VALUE_TYPE_BOOL );
+
         DataElement dataElementF = createDataElement( 'F' );
+        dataElementF.setNumberType( DataElement.VALUE_TYPE_INT );
 
         dataElementA.setCreated( Year.parseYear( "2001" ).getStart() );
         dataElementB.setCreated( Year.parseYear( "2002" ).getStart() );
@@ -496,4 +507,59 @@
         assertEquals( "deabcdefghE", result.getItems().get( 4 ).getUid() );
         assertEquals( "deabcdefghF", result.getItems().get( 5 ).getUid() );
     }
+
+    @Test
+    public void testDoubleEqConjunction()
+    {
+        createDataElements();
+        Query query = Query.from( schemaService.getDynamicSchema( DataElement.class ) );
+
+        Conjunction conjunction = query.conjunction();
+        conjunction.add( Restrictions.eq( "id", "deabcdefghD" ) );
+        conjunction.add( Restrictions.eq( "id", "deabcdefghF" ) );
+        query.add( conjunction );
+
+        Result result = queryService.query( query );
+
+        assertEquals( 0, result.size() );
+    }
+
+    @Test
+    public void testDoubleEqDisjunction()
+    {
+        createDataElements();
+        Query query = Query.from( schemaService.getDynamicSchema( DataElement.class ) );
+
+        Disjunction disjunction = query.disjunction();
+        disjunction.add( Restrictions.eq( "id", "deabcdefghD" ) );
+        disjunction.add( Restrictions.eq( "id", "deabcdefghF" ) );
+        query.add( disjunction );
+
+        Result result = queryService.query( query );
+
+        assertEquals( 2, result.size() );
+
+        assertTrue( collectionContainsUid( result.getItems(), "deabcdefghD" ) );
+        assertTrue( collectionContainsUid( result.getItems(), "deabcdefghF" ) );
+    }
+
+    @Test
+    public void testDateRange()
+    {
+        createDataElements();
+        Query query = Query.from( schemaService.getDynamicSchema( DataElement.class ) );
+
+        Conjunction conjunction = query.conjunction();
+        conjunction.add( Restrictions.ge( "created", Year.parseYear( "2002" ).getStart() ) );
+        conjunction.add( Restrictions.le( "created", Year.parseYear( "2004" ).getStart() ) );
+        query.add( conjunction );
+
+        Result result = queryService.query( query );
+
+        assertEquals( 3, result.size() );
+
+        assertTrue( collectionContainsUid( result.getItems(), "deabcdefghB" ) );
+        assertTrue( collectionContainsUid( result.getItems(), "deabcdefghC" ) );
+        assertTrue( collectionContainsUid( result.getItems(), "deabcdefghD" ) );
+    }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/objectfilter/DefaultObjectFilterService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/objectfilter/DefaultObjectFilterService.java	2015-05-30 13:36:07 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/objectfilter/DefaultObjectFilterService.java	2015-06-04 01:55:31 +0000
@@ -169,23 +169,11 @@
      */
     private boolean evaluateFilterOps( Object value, FilterOps filterOps )
     {
-        for ( String operator : filterOps.getFilters().keySet() )
+        for ( Op op : filterOps.getFilters() )
         {
-            boolean include = false;
-
-            List<Op> ops = filterOps.getFilters().get( operator );
-
-            for ( Op op : ops )
-            {
-                OpStatus status = op.evaluate( value );
-
-                if ( OpStatus.INCLUDE.equals( status ) )
-                {
-                    include = true;
-                }
-            }
-
-            if ( !include )
+            OpStatus status = op.evaluate( value );
+
+            if ( OpStatus.EXCLUDE.equals( status ) )
             {
                 return true;
             }

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/objectfilter/FilterOps.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/objectfilter/FilterOps.java	2015-01-17 07:41:26 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/objectfilter/FilterOps.java	2015-06-04 01:55:31 +0000
@@ -28,40 +28,28 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import com.google.common.collect.Lists;
-import com.google.common.collect.Maps;
 import org.hisp.dhis.dxf2.objectfilter.ops.Op;
 
+import java.util.ArrayList;
 import java.util.List;
-import java.util.Map;
 
 /**
-* @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
-*/
+ * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
+ */
 public class FilterOps
 {
-    private Map<String, List<Op>> filters = Maps.newHashMap();
+    private List<Op> filters = new ArrayList<>();
 
     FilterOps()
     {
     }
 
-    public void addFilter( String opStr, Op op )
-    {
-        if ( !filters.containsKey( opStr ) )
-        {
-            filters.put( opStr, Lists.<Op>newArrayList() );
-        }
-
-        filters.get( opStr ).add( op );
-    }
-
-    public Map<String, List<Op>> getFilters()
+    public List<Op> getFilters()
     {
         return filters;
     }
 
-    public void setFilters( Map<String, List<Op>> filters )
+    public void setFilters( List<Op> filters )
     {
         this.filters = filters;
     }

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/objectfilter/Filters.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/objectfilter/Filters.java	2015-01-17 07:41:26 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/objectfilter/Filters.java	2015-06-04 01:55:31 +0000
@@ -34,8 +34,8 @@
 import java.util.Map;
 
 /**
-* @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
-*/
+ * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
+ */
 public class Filters
 {
     private Map<String, Object> filters = Maps.newHashMap();
@@ -67,7 +67,7 @@
                 op.setValue( value );
             }
 
-            filterOps.addFilter( operator, op );
+            filterOps.getFilters().add( op );
         }
     }