← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 14744: TEI query, improved usability. Allowed for specifying a query item once as attribute and once as ...

 

------------------------------------------------------------
revno: 14744
committer: Lars Helge Øverland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2014-04-08 16:44:25 +0200
message:
  TEI query, improved usability. Allowed for specifying a query item once as attribute and once as filter.
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceQueryParams.java
  dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentity/DefaultTrackedEntityInstanceService.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/trackedentity/TrackedEntityInstanceQueryParams.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceQueryParams.java	2014-04-07 16:50:00 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceQueryParams.java	2014-04-08 14:44:25 +0000
@@ -30,6 +30,7 @@
 
 import java.util.ArrayList;
 import java.util.HashSet;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Set;
 
@@ -131,6 +132,43 @@
     // -------------------------------------------------------------------------
     
     /**
+     * Performs a set of operations on this params.
+     * 
+     * <ul>
+     * <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.
+     * </li>
+     * </ul> 
+     */
+    public void conform()
+    {
+        Iterator<QueryItem> filterIter = filters.iterator();
+        
+        while ( filterIter.hasNext() )
+        {
+            QueryItem filter = filterIter.next();
+        
+            int index = attributes.indexOf( filter ); // Filter present as attr
+            
+            if ( index >= 0 )
+            {
+                QueryItem attribute = attributes.get( index );
+                
+                if ( !attribute.hasFilter() )
+                {
+                    attribute.setOperator( filter.getOperator() );
+                    attribute.setFilter( filter.getFilter() );
+                }
+                
+                filterIter.remove();
+            }
+        }
+    }
+    
+    /**
      * Returns a mapping between level and organisation units.
      */
     public SetMap<Integer, OrganisationUnit> getLevelOrgUnitMap()
@@ -176,25 +214,43 @@
     }
 
     /**
-     * Returns a list of query items which appear more than once as attributes
-     * or filters.
-     */
-    public List<QueryItem> getDuplicateAttributesAndFilters()
-    {
-        Set<QueryItem> items = new HashSet<QueryItem>();
-        List<QueryItem> duplicates = new ArrayList<QueryItem>();
-        
-        for ( QueryItem item : getAttributesAndFilters() )
-        {
-            if ( !items.add( item ) )
-            {
-                duplicates.add( item );
-            }
-        }
-        
-        return duplicates;
-    }
-    
+     * Returns a list of attributes which appear more than once.
+     */
+    public List<QueryItem> getDuplicateAttributes()
+    {
+        Set<QueryItem> items = new HashSet<QueryItem>();
+        List<QueryItem> duplicates = new ArrayList<QueryItem>();
+        
+        for ( QueryItem item : getAttributes() )
+        {
+            if ( !items.add( item ) )
+            {
+                duplicates.add( item );
+            }
+        }
+        
+        return duplicates;
+    }
+
+    /**
+     * Returns a list of attributes which appear more than once.
+     */
+    public List<QueryItem> getDuplicateFilters()
+    {
+        Set<QueryItem> items = new HashSet<QueryItem>();
+        List<QueryItem> duplicates = new ArrayList<QueryItem>();
+        
+        for ( QueryItem item : getFilters() )
+        {
+            if ( !items.add( item ) )
+            {
+                duplicates.add( item );
+            }
+        }
+        
+        return duplicates;
+    }
+        
     /**
      * Indicates whether this params specifies any attributes and/or filters.
      */

=== 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-04-08 10:06:11 +0000
+++ dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentity/DefaultTrackedEntityInstanceService.java	2014-04-08 14:44:25 +0000
@@ -147,12 +147,16 @@
     // Implementation methods
     // -------------------------------------------------------------------------
     
-    //TODO queries with multiple words
     //TODO lower index on attribute value?
     
     @Override
     public Grid getTrackedEntityInstances( TrackedEntityInstanceQueryParams params )
     {
+        if ( params != null )
+        {
+            params.conform();
+        }
+        
         validate( params );
 
         // ---------------------------------------------------------------------
@@ -290,11 +294,16 @@
             violation = "Program must be defined when program dates are specified";
         }
         
-        if ( !params.getDuplicateAttributesAndFilters().isEmpty() )
+        if ( !params.getDuplicateAttributes().isEmpty() )
         {
-            violation = "Attributes and filters cannot be specified more than once: " + params.getDuplicateAttributesAndFilters();
+            violation = "Attributes cannot be specified more than once: " + params.getDuplicateAttributes();
         }
         
+        if ( !params.getDuplicateFilters().isEmpty() )
+        {
+            violation = "Filters cannot be specified more than once: " + params.getDuplicateFilters();
+        }
+                
         if ( violation != null )
         {
             log.warn( "Validation failed: " + violation );