← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 14284: TEI query, impl support for including attributes in the response or only to be used as filters

 

------------------------------------------------------------
revno: 14284
committer: Lars Helge Øverland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2014-03-18 17:11:21 +0100
message:
  TEI query, impl support for including attributes in the response or only to be used as filters
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceQueryParams.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceService.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
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/event/TrackedEntityInstanceController.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-03-18 14:54:13 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceQueryParams.java	2014-03-18 16:11:21 +0000
@@ -53,8 +53,10 @@
     
     private String query;
     
-    private List<QueryItem> items = new ArrayList<QueryItem>();
+    private List<QueryItem> attributes = new ArrayList<QueryItem>();
 
+    private List<QueryItem> filters = new ArrayList<QueryItem>();
+    
     private Set<OrganisationUnit> organisationUnits = new HashSet<OrganisationUnit>();
     
     private Program program;
@@ -78,7 +80,7 @@
     // -------------------------------------------------------------------------
     // Logic
     // -------------------------------------------------------------------------
-
+    
     /**
      * Returns a mapping between level and organisation units.
      */
@@ -112,13 +114,40 @@
     {
         return query != null && !query.isEmpty();
     }
-
-    /**
-     * Indicates whether this params specifies any query items.
-     */
-    public boolean hasItems()
-    {
-        return items != null && !items.isEmpty();
+    
+    /**
+     * Returns a list of attributes and filters combined.
+     */
+    public List<QueryItem> getAttributesAndFilters()
+    {
+        List<QueryItem> items = new ArrayList<QueryItem>();
+        items.addAll( attributes );
+        items.addAll( filters );
+        return items;
+    }
+
+    /**
+     * Indicates whether this params specifies any attributes and/or filters.
+     */
+    public boolean hasAttributesOrFilters()
+    {
+        return hasAttributes() || hasFilters();
+    }
+
+    /**
+     * Indicates whether this params specifies any attributes.
+     */
+    public boolean hasAttributes()
+    {
+        return attributes != null && !attributes.isEmpty();
+    }
+    
+    /**
+     * Indicates whether this params specifies any filters.
+     */
+    public boolean hasFilters()
+    {
+        return filters != null && !filters.isEmpty();
     }
 
     /**
@@ -199,14 +228,24 @@
         this.query = query;
     }
 
-    public List<QueryItem> getItems()
-    {
-        return items;
-    }
-
-    public void setItems( List<QueryItem> items )
-    {
-        this.items = items;
+    public List<QueryItem> getAttributes()
+    {
+        return attributes;
+    }
+
+    public void setAttributes( List<QueryItem> attributes )
+    {
+        this.attributes = attributes;
+    }
+
+    public List<QueryItem> getFilters()
+    {
+        return filters;
+    }
+
+    public void setFilters( List<QueryItem> filters )
+    {
+        this.filters = filters;
     }
 
     public String getOrganisationUnitMode()

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceService.java	2014-03-18 11:26:41 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceService.java	2014-03-18 16:11:21 +0000
@@ -55,8 +55,8 @@
 
     Grid getTrackedEntityInstances( TrackedEntityInstanceQueryParams params );
     
-    TrackedEntityInstanceQueryParams getFromUrl( String query, Set<String> items, Set<String> ou, String ouMode, 
-        String program, String trackedEntity, Integer page, Integer pageSize );
+    TrackedEntityInstanceQueryParams getFromUrl( String query, Set<String> attribute, Set<String> filter, 
+        Set<String> ou, String ouMode, String program, String trackedEntity, Integer page, Integer pageSize );
     
     /**
      * Adds an {@link TrackedEntityInstance}

=== 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-03-18 14:54:13 +0000
+++ dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentity/DefaultTrackedEntityInstanceService.java	2014-03-18 16:11:21 +0000
@@ -145,27 +145,46 @@
     {
         validate( params );
         
-        if ( params.isOrQuery() )
+        // ---------------------------------------------------------------------
+        // If params of type query and no attributes or filters defined, use
+        // attributes from program if exists, if not, use all attributes.
+        // ---------------------------------------------------------------------
+
+        if ( params.isOrQuery() && !params.hasAttributesOrFilters() )
         {
-            params.getItems().clear();
-            params.getItems().addAll( getItemsForOrQuery( params ) );
+            if ( params.hasProgram() )
+            {
+                params.getAttributes().addAll( QueryItem.getQueryItems( params.getProgram().getTrackedEntityAttributes() ) );
+            }
+            else
+            {
+                params.getAttributes().addAll( QueryItem.getQueryItems( attributeService.getAllTrackedEntityAttributes() ) );
+            }
         }
-        
+
         Grid grid = new ListGrid();
-        
+
+        // ---------------------------------------------------------------------
+        // Grid headers
+        // ---------------------------------------------------------------------
+
         grid.addHeader( new GridHeader( TRACKED_ENTITY_INSTANCE_ID, "Instance" ) );
         grid.addHeader( new GridHeader( CREATED_ID, "Created" ) );
         grid.addHeader( new GridHeader( LAST_UPDATED_ID, "Last updated" ) );
         grid.addHeader( new GridHeader( ORG_UNIT_ID, "Org unit" ) );
         grid.addHeader( new GridHeader( TRACKED_ENTITY_ID, "Tracked entity" ) );
         
-        for ( QueryItem item : params.getItems() )
+        for ( QueryItem item : params.getAttributes() )
         {
             grid.addHeader( new GridHeader( item.getItem().getUid(), item.getItem().getName() ) );
         }
         
         List<Map<String, String>> entities = trackedEntityInstanceStore.getTrackedEntityInstances( params );
-        
+
+        // ---------------------------------------------------------------------
+        // Grid rows
+        // ---------------------------------------------------------------------
+
         for ( Map<String, String> entity : entities )
         {
             grid.addRow();
@@ -175,7 +194,7 @@
             grid.addValue( entity.get( ORG_UNIT_ID ) );
             grid.addValue( entity.get( TRACKED_ENTITY_ID ) );
             
-            for ( QueryItem item : params.getItems() )
+            for ( QueryItem item : params.getAttributes() )
             {
                 grid.addValue( entity.get( item.getItemId() ) );
             }
@@ -184,23 +203,6 @@
         return grid;
     }
     
-    /**
-     * Returns the appropriate items for a OR query with no items specified. If 
-     * the params specifies a program, the program attributes will be returned. 
-     * If not, all attributes will be returned.
-     */
-    private List<QueryItem> getItemsForOrQuery( TrackedEntityInstanceQueryParams params )
-    {
-        if ( params.hasProgram() )
-        {
-            return QueryItem.getQueryItems( params.getProgram().getTrackedEntityAttributes() );
-        }
-        else
-        {
-            return QueryItem.getQueryItems( attributeService.getAllTrackedEntityAttributes() );
-        }
-    }
-    
     public void validate( TrackedEntityInstanceQueryParams params )
         throws IllegalQueryException
     {
@@ -211,14 +213,9 @@
             throw new IllegalQueryException( "Params cannot be null" );
         }
         
-        if ( params.hasQuery() && params.hasItems() )
-        {
-            violation = "Query and item cannot be specified simultaneously";
-        }
-        
-        if ( !params.hasQuery() && !params.hasItems() )
-        {
-            violation = "At least one of query and item must be specified";
+        if ( !params.hasQuery() && !params.hasAttributes() )
+        {
+            violation = "At least one of query and attributes must be specified";
         }
 
         if ( !params.hasOrganisationUnits() )
@@ -240,18 +237,28 @@
     }
     
     @Override
-    public TrackedEntityInstanceQueryParams getFromUrl( String query, Set<String> items, Set<String> ou, String ouMode, 
+    public TrackedEntityInstanceQueryParams getFromUrl( String query, Set<String> attribute, Set<String> filter, Set<String> ou, String ouMode, 
         String program, String trackedEntity, Integer page, Integer pageSize )
     {
         TrackedEntityInstanceQueryParams params = new TrackedEntityInstanceQueryParams();
 
-        if ( items != null )
-        {
-            for ( String item : items )
-            {
-                QueryItem it = getQueryItem( item );
-                
-                params.getItems().add( it );
+        if ( attribute != null )
+        {
+            for ( String attr : attribute )
+            {
+                QueryItem it = getQueryItem( attr );
+                
+                params.getAttributes().add( it );
+            }
+        }
+        
+        if ( filter != null )
+        {
+            for ( String filt : filter )
+            {
+                QueryItem it = getQueryItem( filt );
+                
+                params.getFilters().add( it );
             }
         }
 

=== 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-03-18 14:54:13 +0000
+++ dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentity/hibernate/HibernateTrackedEntityInstanceStore.java	2014-03-18 16:11:21 +0000
@@ -135,7 +135,7 @@
             "ou.uid as " + ORG_UNIT_ID + ", " +
             "te.uid as " + TRACKED_ENTITY_ID + ", ";
         
-        for ( QueryItem item : params.getItems() )
+        for ( QueryItem item : params.getAttributes() )
         {
             String col = statementBuilder.columnQuote( item.getItemId() );
             
@@ -145,7 +145,8 @@
         sql = sql.substring( 0, sql.length() - 2 ) + " "; // Remove last comma
 
         // ---------------------------------------------------------------------
-        // From, join and restriction clause
+        // From, join and where clause. For attribute params, restriction is set
+        // in inner join. For query params, restriction is set in where clause.
         // ---------------------------------------------------------------------
         
         sql +=        
@@ -153,7 +154,7 @@
             "inner join trackedentity te on tei.trackedentityid = te.trackedentityid " +
             "inner join organisationunit ou on tei.organisationunitid = ou.organisationunitid ";
         
-        for ( QueryItem item : params.getItems() )
+        for ( QueryItem item : params.getAttributesAndFilters() )
         {
             String col = statementBuilder.columnQuote( item.getItemId() );
             
@@ -191,9 +192,9 @@
                 sql += hlp.whereAnd() + " ous.idlevel" + level + " in (" + getCommaDelimitedString( getIdentifiers( levelOuMap.get( level ) ) ) + ") or ";
             }
             
-            sql = sql.substring( 0, sql.length() - 3 ); // Reomove last or
+            sql = sql.substring( 0, sql.length() - 3 ); // Remove last or
         }
-        else // SELECTED
+        else // OU_MODE_SELECTED
         {
             sql += hlp.whereAnd() + " tei.organisationunitid in (" + getCommaDelimitedString( getIdentifiers( params.getOrganisationUnits() ) ) + ") ";
         }
@@ -206,11 +207,11 @@
                 "and pi.programid = " + params.getProgram().getId() + ") ";
         }
         
-        if ( params.isOrQuery() && params.hasItems() )
+        if ( params.isOrQuery() && params.hasAttributesOrFilters() )
         {
             sql += hlp.whereAnd() + " (";
             
-            for ( QueryItem item : params.getItems() )
+            for ( QueryItem item : params.getAttributesAndFilters() )
             {
                 String col = statementBuilder.columnQuote( item.getItemId() );
                 String query = statementBuilder.encode( params.getQuery(), false );
@@ -218,7 +219,7 @@
                 sql += col + ".value = '" + query + "' or ";
             }
             
-            sql = sql.substring( 0, sql.length() - 3 ) + ") "; // Reomove last or
+            sql = sql.substring( 0, sql.length() - 3 ) + ") "; // Remove last or
         }
 
         // ---------------------------------------------------------------------
@@ -250,7 +251,7 @@
             map.put( ORG_UNIT_ID, rowSet.getString( ORG_UNIT_ID ) );
             map.put( TRACKED_ENTITY_ID, rowSet.getString( TRACKED_ENTITY_ID ) );
             
-            for ( QueryItem item : params.getItems() )
+            for ( QueryItem item : params.getAttributes() )
             {
                 map.put( item.getItemId(), rowSet.getString( item.getItemId() ) );
             }

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/event/TrackedEntityInstanceController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/event/TrackedEntityInstanceController.java	2014-03-18 11:26:41 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/event/TrackedEntityInstanceController.java	2014-03-18 16:11:21 +0000
@@ -98,7 +98,8 @@
     @RequestMapping( method = RequestMethod.GET, produces = { ContextUtils.CONTENT_TYPE_JSON, ContextUtils.CONTENT_TYPE_JAVASCRIPT } )
     public String queryTrackedEntityInstancesJson(
         @RequestParam(required=false) String query,
-        @RequestParam(required=false) Set<String> item,
+        @RequestParam(required=false) Set<String> attribute,
+        @RequestParam(required=false) Set<String> filter,
         @RequestParam String ou,
         @RequestParam(required=false, defaultValue=DimensionalObject.OU_MODE_SELECTED) String ouMode,
         @RequestParam(required=false) String program,
@@ -109,7 +110,7 @@
         HttpServletResponse response ) throws Exception
     {
         Set<String> orgUnits = new HashSet<String>( ContextUtils.getQueryParamValues( ou ) );        
-        TrackedEntityInstanceQueryParams params = instanceService.getFromUrl( query, item, orgUnits, ouMode, program, trackedEntity, page, pageSize );
+        TrackedEntityInstanceQueryParams params = instanceService.getFromUrl( query, attribute, filter, orgUnits, ouMode, program, trackedEntity, page, pageSize );
         
         contextUtils.configureResponse( response, ContextUtils.CONTENT_TYPE_JSON, CacheStrategy.NO_CACHE );
         Grid grid = instanceService.getTrackedEntityInstances( params );
@@ -122,7 +123,8 @@
     @RequestMapping( method = RequestMethod.GET, produces = ContextUtils.CONTENT_TYPE_EXCEL )
     public void queryTrackedEntityInstancesXls(
         @RequestParam(required=false) String query,
-        @RequestParam(required=false) Set<String> item,
+        @RequestParam(required=false) Set<String> attribute,
+        @RequestParam(required=false) Set<String> filter,
         @RequestParam String ou,
         @RequestParam(required=false, defaultValue=DimensionalObject.OU_MODE_SELECTED) String ouMode,
         @RequestParam(required=false) String program,
@@ -133,7 +135,7 @@
         HttpServletResponse response ) throws Exception
     {
         Set<String> orgUnits = new HashSet<String>( ContextUtils.getQueryParamValues( ou ) );        
-        TrackedEntityInstanceQueryParams params = instanceService.getFromUrl( query, item, orgUnits, ouMode, program, trackedEntity, page, pageSize );
+        TrackedEntityInstanceQueryParams params = instanceService.getFromUrl( query, attribute, filter, orgUnits, ouMode, program, trackedEntity, page, pageSize );
         
         contextUtils.configureResponse( response, ContextUtils.CONTENT_TYPE_EXCEL, CacheStrategy.NO_CACHE );
         Grid grid = instanceService.getTrackedEntityInstances( params );
@@ -143,7 +145,8 @@
     @RequestMapping( method = RequestMethod.GET, produces = ContextUtils.CONTENT_TYPE_CSV )
     public void queryTrackedEntityInstancesCsv(
         @RequestParam(required=false) String query,
-        @RequestParam(required=false) Set<String> item,
+        @RequestParam(required=false) Set<String> attribute,
+        @RequestParam(required=false) Set<String> filter,
         @RequestParam String ou,
         @RequestParam(required=false, defaultValue=DimensionalObject.OU_MODE_SELECTED) String ouMode,
         @RequestParam(required=false) String program,
@@ -154,7 +157,7 @@
         HttpServletResponse response ) throws Exception
     {
         Set<String> orgUnits = new HashSet<String>( ContextUtils.getQueryParamValues( ou ) );        
-        TrackedEntityInstanceQueryParams params = instanceService.getFromUrl( query, item, orgUnits, ouMode, program, trackedEntity, page, pageSize );
+        TrackedEntityInstanceQueryParams params = instanceService.getFromUrl( query, attribute, filter, orgUnits, ouMode, program, trackedEntity, page, pageSize );
         
         contextUtils.configureResponse( response, ContextUtils.CONTENT_TYPE_CSV, CacheStrategy.NO_CACHE );
         Grid grid = instanceService.getTrackedEntityInstances( params );