← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 19239: minor refactor in Query, introduce Criterion as super class of expressions, first step to adding ...

 

------------------------------------------------------------
revno: 19239
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2015-06-02 11:25:37 +0700
message:
  minor refactor in Query, introduce Criterion as super class of expressions, first step to adding proper support for OR/AND
added:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/query/Criterion.java
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/query/Query.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/query/Restriction.java
  dhis-2/dhis-api/src/test/java/org/hisp/dhis/query/QueryTest.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/main/java/org/hisp/dhis/query/DefaultQueryService.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
=== added file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/query/Criterion.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/query/Criterion.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/query/Criterion.java	2015-06-02 04:25:37 +0000
@@ -0,0 +1,36 @@
+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 interface Criterion
+{
+}

=== 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-03-20 03:13:40 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/query/Query.java	2015-06-02 04:25:37 +0000
@@ -46,7 +46,7 @@
 
     private List<Order> orders = new ArrayList<>();
 
-    private List<Restriction> restrictions = new ArrayList<>();
+    private List<Criterion> criterions = new ArrayList<>();
 
     private Integer firstResult;
 
@@ -72,19 +72,9 @@
         return orders;
     }
 
-    public void setOrders( List<Order> orders )
-    {
-        this.orders = orders;
-    }
-
-    public List<Restriction> getRestrictions()
-    {
-        return restrictions;
-    }
-
-    public void setRestrictions( List<Restriction> restrictions )
-    {
-        this.restrictions = restrictions;
+    public List<Criterion> getCriterions()
+    {
+        return criterions;
     }
 
     public Integer getFirstResult()
@@ -110,11 +100,18 @@
     }
 
     // Builder
-    public Query add( Restriction... restrictions )
+    public Query add( Criterion... criterions )
     {
-        for ( Restriction restriction : restrictions )
+        for ( Criterion criterion : criterions )
         {
-            if ( restriction == null || !schema.haveProperty( restriction.getPath() ) )
+            if ( !Restriction.class.isInstance( criterion ) )
+            {
+                continue;
+            }
+
+            Restriction restriction = (Restriction) criterion;
+
+            if ( !schema.haveProperty( restriction.getPath() ) )
             {
                 continue;
             }
@@ -125,15 +122,15 @@
                 continue;
             }
 
-            this.restrictions.add( restriction );
+            this.criterions.add( restriction );
         }
 
         return this;
     }
 
-    public Query add( Collection<Restriction> restrictions )
+    public Query add( Collection<Criterion> criterions )
     {
-        this.restrictions.addAll( restrictions );
+        this.criterions.addAll( criterions );
         return this;
     }
 
@@ -191,7 +188,7 @@
             .add( "firstResult", firstResult )
             .add( "maxResults", maxResults )
             .add( "orders", orders )
-            .add( "restrictions", restrictions )
+            .add( "criterions", criterions )
             .toString();
     }
 }

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/query/Restriction.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/query/Restriction.java	2015-02-23 13:06:26 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/query/Restriction.java	2015-06-02 04:25:37 +0000
@@ -36,7 +36,7 @@
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
  */
-public class Restriction
+public class Restriction implements Criterion
 {
     /**
      * Path to property you want to restrict only, one first-level properties are currently supported.

=== modified file 'dhis-2/dhis-api/src/test/java/org/hisp/dhis/query/QueryTest.java'
--- dhis-2/dhis-api/src/test/java/org/hisp/dhis/query/QueryTest.java	2015-02-20 04:03:39 +0000
+++ dhis-2/dhis-api/src/test/java/org/hisp/dhis/query/QueryTest.java	2015-06-02 04:25:37 +0000
@@ -78,7 +78,7 @@
         query.add( Restrictions.like( "name", "anc" ) );
         query.add( Restrictions.eq( "code", "anc" ) );
 
-        assertEquals( 3, query.getRestrictions().size() );
+        assertEquals( 3, query.getCriterions().size() );
     }
 
     @Test
@@ -94,7 +94,7 @@
         query.add( new Restriction( "lastUpdated", Operator.BETWEEN, new Date() ) );
         query.add( new Restriction( "lastUpdated", Operator.BETWEEN, new Date(), 1, 2, 3, 4 ) );
 
-        assertEquals( 0, query.getRestrictions().size() );
+        assertEquals( 0, query.getCriterions().size() );
     }
 
     @Test

=== 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 03:30:57 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/CriteriaQueryEngine.java	2015-06-02 04:25:37 +0000
@@ -91,7 +91,7 @@
 
         // create a copy of this query using only the restrictions
         Query countQuery = Query.from( query.getSchema() );
-        countQuery.add( query.getRestrictions() );
+        countQuery.add( query.getCriterions() );
 
         if ( schema == null )
         {
@@ -131,8 +131,15 @@
             criteria.setMaxResults( query.getMaxResults() );
         }
 
-        for ( Restriction restriction : query.getRestrictions() )
+        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>() );

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/DefaultQueryService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/DefaultQueryService.java	2015-03-20 03:00:33 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/DefaultQueryService.java	2015-06-02 04:25:37 +0000
@@ -83,7 +83,7 @@
     public Query getQueryFromUrl( Class<?> klass, List<String> filters, List<Order> orders )
     {
         Query query = Query.from( schemaService.getDynamicSchema( klass ) );
-        query.add( getRestrictions( query.getSchema(), filters ) );
+        query.add( getCriterions( query.getSchema(), filters ) );
         query.addOrders( orders );
 
         return query;
@@ -93,25 +93,25 @@
     // Helpers
     //--------------------------------------------------------------------------
 
-    private List<Restriction> getRestrictions( Schema schema, List<String> filters )
+    private List<Criterion> getCriterions( Schema schema, List<String> filters )
     {
-        List<Restriction> restrictions = new ArrayList<>();
-        List<String> candidates = getRestrictionCandidates( schema, filters );
+        List<Criterion> criterions = new ArrayList<>();
+        List<String> candidates = getCandidates( schema, filters );
 
         if ( candidates.isEmpty() )
         {
-            return restrictions;
+            return criterions;
         }
 
         for ( String candidate : candidates )
         {
-            restrictions.add( getRestriction( schema, candidate ) );
+            criterions.add( getRestriction( schema, candidate ) );
         }
 
-        return restrictions;
+        return criterions;
     }
 
-    private List<String> getRestrictionCandidates( Schema schema, List<String> filters )
+    private List<String> getCandidates( Schema schema, List<String> filters )
     {
         List<String> candidates = new ArrayList<>();