← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 15873: Web api, TEI query, impl support for multiple filter values in single query param

 

------------------------------------------------------------
revno: 15873
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Fri 2014-06-27 12:12:09 +0200
message:
  Web api, TEI query, impl support for multiple filter values in single query param
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/QueryOperator.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceQueryParams.java
  dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/DefaultEventAnalyticsService.java
  dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentity/DefaultTrackedEntityInstanceService.java
  dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentity/hibernate/HibernateTrackedEntityInstanceStore.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/common/QueryOperator.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/QueryOperator.java	2014-05-08 17:33:45 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/QueryOperator.java	2014-06-27 10:12:09 +0000
@@ -1,5 +1,36 @@
 package org.hisp.dhis.common;
 
+/*
+ * Copyright (c) 2004-2014, 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 Lars Helge Overland
+ */
 public enum QueryOperator
 {
     EQ, GT, GE, LT, LE, NE, LIKE, IN;

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceQueryParams.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceQueryParams.java	2014-06-14 17:58:11 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceQueryParams.java	2014-06-27 10:12:09 +0000
@@ -190,8 +190,8 @@
      * <li>
      * If a query item is specified as an attribute item as well as a filter 
      * item, the filter item will be removed. In that case, if the attribute 
-     * item does not have a filter value and the filter item has a filter value, 
-     * it will be applied to the attribute item. 
+     * item does not have any filters and the filter item has one or more filters, 
+     * these will be applied to the attribute item. 
      * </li>
      * </ul> 
      */
@@ -211,7 +211,7 @@
                 
                 if ( !attribute.hasFilter() && filter.hasFilter() )
                 {
-                    attribute.getFilters().add( filter.getFilters().iterator().next() );
+                    attribute.getFilters().addAll( filter.getFilters() );
                 }
                 
                 filterIter.remove();
@@ -482,6 +482,21 @@
     {
         return ( getPageWithDefault() - 1 ) * getPageSizeWithDefault();
     }
+
+    // -------------------------------------------------------------------------
+    // toString
+    // -------------------------------------------------------------------------
+
+    @Override
+    public String toString()
+    {
+        return "[Query: " + query + ", Attributes: " + attributes + ", filters: " + filters + 
+            ", program: " + program + ", program status " + programStatus + ", follow up: " + followUp + 
+            ", program start date: " + programStartDate + ", program end date: " + programEndDate + 
+            ", tracked entity: " + trackedEntity + ", org unit mode: " + organisationUnitMode + 
+            ", event start date: " + eventStartDate + ", event end date: " + eventEndDate + 
+            ", event status: " + eventStatus + "]";
+    }
     
     // -------------------------------------------------------------------------
     // Getters and setters

=== modified file 'dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/DefaultEventAnalyticsService.java'
--- dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/DefaultEventAnalyticsService.java	2014-05-10 11:18:29 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/DefaultEventAnalyticsService.java	2014-06-27 10:12:09 +0000
@@ -438,7 +438,7 @@
         
         QueryItem queryItem = getQueryItem( program, split[0] );
         
-        if ( split.length > 1 )
+        if ( split.length > 1 ) // Filters specified
         {   
             for ( int i = 1; i < split.length; i += 2 )
             {

=== modified file 'dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentity/DefaultTrackedEntityInstanceService.java'
--- dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentity/DefaultTrackedEntityInstanceService.java	2014-06-14 17:58:11 +0000
+++ dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentity/DefaultTrackedEntityInstanceService.java	2014-06-27 10:12:09 +0000
@@ -177,8 +177,8 @@
 
         // ---------------------------------------------------------------------
         // If params of type query and no attributes or filters defined, use
-        // attributes from program if exists, if not, use display-in-list
-        // attributes.
+        // attributes from program if program is defined, if not, use 
+        // display-in-list attributes.
         // ---------------------------------------------------------------------
 
         if ( params.isOrQuery() || !params.hasAttributes() )
@@ -222,7 +222,7 @@
         {
             grid.addHeader( new GridHeader( item.getItem().getUid(), item.getItem().getName() ) );
         }
-
+        
         List<Map<String, String>> entities = trackedEntityInstanceStore.getTrackedEntityInstances( params );
 
         // ---------------------------------------------------------------------
@@ -326,7 +326,7 @@
             violation = "Program must be defined when program end date is specified";
         }
 
-        if ( params.hasEventStatus() && (!params.hasEventStartDate() || !params.hasEventEndDate()) )
+        if ( params.hasEventStatus() && ( !params.hasEventStartDate() || !params.hasEventEndDate() ) )
         {
             violation = "Event start and end date must be specified when event status is specified";
         }
@@ -373,17 +373,17 @@
                 params.getAttributes().add( it );
             }
         }
-
+        
         if ( filter != null )
         {
             for ( String filt : filter )
             {
                 QueryItem it = getQueryItem( filt );
-
+                
                 params.getFilters().add( it );
             }
         }
-
+        
         if ( ou != null )
         {
             for ( String orgUnit : ou )
@@ -433,31 +433,33 @@
 
     /**
      * Creates a QueryItem from the given item string. Item is on format
-     * {attribute-id}:{operator}:{filter-value}. Only the attribute-id is mandatory.
+     * {attribute-id}:{operator}:{filter-value}[:{operator}:{filter-value}].
+     * Only the attribute-id is mandatory.
      */
     private QueryItem getQueryItem( String item )
     {
-        if ( !item.contains( DimensionalObjectUtils.DIMENSION_NAME_SEP ) )
+        String[] split = item.split( DimensionalObjectUtils.DIMENSION_NAME_SEP );
+
+        if ( split == null || ( split.length % 2 != 1 ) )
         {
-            return getItem( item, null, null );
+            throw new IllegalQueryException( "Query item or filter is invalid: " + item );
         }
-        else // Filter
+        
+        QueryItem queryItem = getItem( split[0] );
+        
+        if ( split.length > 1 ) // Filters specified
         {
-            String[] split = item.split( DimensionalObjectUtils.DIMENSION_NAME_SEP );
-
-            if ( split == null || split.length != 3 )
+            for ( int i = 1; i < split.length; i += 2 )
             {
-                throw new IllegalQueryException( "Item filter has invalid format: " + item );
-            }
-
-            return getItem( split[0], split[1], split[2] );
+                QueryOperator operator = QueryOperator.fromString( split[i] );
+                queryItem.getFilters().add( new QueryFilter( operator, split[i+1] ) );
+            }            
         }
+        
+        return queryItem;
     }
 
-    /**
-     * Creates a QueryItem from the given item, operator and filter strings.
-     */
-    private QueryItem getItem( String item, String operator, String filter )
+    private QueryItem getItem( String item )
     {
         TrackedEntityAttribute at = attributeService.getTrackedEntityAttribute( item );
 
@@ -465,17 +467,8 @@
         {
             throw new IllegalQueryException( "Attribute does not exist: " + item );
         }
-
-        if ( operator != null && filter != null )
-        {
-            QueryOperator op = QueryOperator.fromString( operator );
-            
-            return new QueryItem( at, op, filter, at.isNumericType() );
-        }
-        else
-        {
-            return new QueryItem( at, at.isNumericType() );
-        }
+        
+        return new QueryItem( at, at.isNumericType() );
     }
     
     /**

=== modified file 'dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentity/hibernate/HibernateTrackedEntityInstanceStore.java'
--- dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentity/hibernate/HibernateTrackedEntityInstanceStore.java	2014-06-24 14:23:58 +0000
+++ dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentity/hibernate/HibernateTrackedEntityInstanceStore.java	2014-06-27 10:12:09 +0000
@@ -234,7 +234,7 @@
             if ( !params.isOrQuery() && item.hasFilter() )
             {
                 for ( QueryFilter filter : item.getFilters() )
-                {
+                {                    
                     final String encodedFilter = statementBuilder.encode( filter.getFilter(), false );
 
                     final String queryCol = item.isNumeric() ? (col + ".value") : "lower(" + col + ".value)";