← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 15198: TEI query. Improved usability of Java API. Using enum for query operator. Added utility methods. ...

 

Merge authors:
  Lars Helge Øverland (larshelge)
------------------------------------------------------------
revno: 15198 [merge]
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2014-05-08 19:34:50 +0200
message:
  TEI query. Improved usability of Java API. Using enum for query operator. Added utility methods. Improved documentation.
added:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/QueryOperator.java
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/QueryFilter.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/QueryItem.java
  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-analytics/src/main/java/org/hisp/dhis/analytics/event/data/DefaultEventAnalyticsService.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/enrollment/AbstractEnrollmentService.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/trackedentity/AbstractTrackedEntityInstanceService.java
  dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentity/DefaultTrackedEntityInstanceService.java
  dhis-2/dhis-web/dhis-web-light/src/main/java/org/hisp/dhis/light/message/action/AddRecipientAction.java
  dhis-2/dhis-web/dhis-web-light/src/main/java/org/hisp/dhis/light/messaging/action/FindUserAction.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/QueryFilter.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/QueryFilter.java	2014-04-21 11:05:22 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/QueryFilter.java	2014-05-08 17:33:45 +0000
@@ -1,8 +1,5 @@
 package org.hisp.dhis.common;
 
-import java.util.HashMap;
-import java.util.Map;
-
 /*
  * Copyright (c) 2004-2014, University of Oslo
  * All rights reserved.
@@ -31,6 +28,9 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import java.util.HashMap;
+import java.util.Map;
+
 /**
  * @author Lars Helge Overland
  */
@@ -38,18 +38,18 @@
 {
     public static final String OPTION_SEP = ";";
     
-    public static final Map<String, String> OPERATOR_MAP = new HashMap<String, String>() { {
-        put( "eq", "=" );
-        put( "gt", ">" );
-        put( "ge", ">=" );
-        put( "lt", "<" );
-        put( "le", "<=" );
-        put( "ne", "!=" );
-        put( "like", "like" );
-        put( "in", "in" );
+    public static final Map<QueryOperator, String> OPERATOR_MAP = new HashMap<QueryOperator, String>() { {
+        put( QueryOperator.EQ, "=" );
+        put( QueryOperator.GT, ">" );
+        put( QueryOperator.GE, ">=" );
+        put( QueryOperator.LT, "<" );
+        put( QueryOperator.LE, "<=" );
+        put( QueryOperator.NE, "!=" );
+        put( QueryOperator.LIKE, "like" );
+        put( QueryOperator.IN, "in" );
     } };
     
-    protected String operator;
+    protected QueryOperator operator;
 
     protected String filter;
 
@@ -61,7 +61,7 @@
     {
     }
     
-    public QueryFilter( String operator, String filter )
+    public QueryFilter( QueryOperator operator, String filter )
     {
         this.operator = operator;
         this.filter = filter;
@@ -73,7 +73,7 @@
     
     public boolean isFilter()
     {
-        return operator != null && !operator.isEmpty() && filter != null && !filter.isEmpty();
+        return operator != null && filter != null && !filter.isEmpty();
     }
     
     public String getSqlOperator()
@@ -83,7 +83,7 @@
             return null;
         }
         
-        return OPERATOR_MAP.get( operator.toLowerCase() );
+        return OPERATOR_MAP.get( operator );
     }
     
     public String getSqlFilter( String encodedFilter )
@@ -92,12 +92,12 @@
         {
             return null;
         }
-                
-        if ( operator.equalsIgnoreCase( "like" ) )
+
+        if ( QueryOperator.LIKE.equals( operator ) )
         {
             return "'%" + encodedFilter + "%'";
         }
-        else if ( operator.equalsIgnoreCase( "in" ) )
+        else if ( QueryOperator.IN.equals( operator ) )
         {
             String[] split = encodedFilter.split( OPTION_SEP );
             
@@ -185,12 +185,12 @@
     // Getters and setters
     // -------------------------------------------------------------------------
     
-    public String getOperator()
+    public QueryOperator getOperator()
     {
         return operator;
     }
 
-    public void setOperator( String operator )
+    public void setOperator( QueryOperator operator )
     {
         this.operator = operator;
     }

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/QueryItem.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/QueryItem.java	2014-04-21 11:05:22 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/QueryItem.java	2014-05-08 17:33:45 +0000
@@ -61,7 +61,7 @@
         this.numeric = numeric;
     }
     
-    public QueryItem( IdentifiableObject item, String operator, String filter, boolean numeric )
+    public QueryItem( IdentifiableObject item, QueryOperator operator, String filter, boolean numeric )
     {
         this.item = item;
         this.numeric = numeric;

=== added 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	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/QueryOperator.java	2014-05-08 17:33:45 +0000
@@ -0,0 +1,16 @@
+package org.hisp.dhis.common;
+
+public enum QueryOperator
+{
+    EQ, GT, GE, LT, LE, NE, LIKE, IN;
+    
+    public static final QueryOperator fromString( String string )
+    {
+        if ( string == null || string.isEmpty() )
+        {
+            return null;
+        }
+        
+        return valueOf( string.toUpperCase() );
+    }
+}

=== 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-21 11:05:22 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceQueryParams.java	2014-05-08 17:33:45 +0000
@@ -159,8 +159,30 @@
     // -------------------------------------------------------------------------
     
     /**
-     * //TODO allow attributes only once and allow multiple filters per item
-     * 
+     * Adds a query item as attribute to the parameters.
+     */
+    public void addAttribute( QueryItem attribute )
+    {
+        this.attributes.add( attribute );
+    }
+    
+    /**
+     * Adds a query item as filter to the parameters.
+     */
+    public void addFilter( QueryItem filter )
+    {
+        this.filters.add( filter );
+    }
+    
+    /**
+     * Adds an organisation unit to the parameters.
+     */
+    public void addOrganisationUnit( OrganisationUnit unit )
+    {
+        this.organisationUnits.add( unit );
+    }
+
+    /**
      * Performs a set of operations on this params.
      * 
      * <ul>
@@ -210,8 +232,35 @@
         
         return setMap;
     }
+
+    /**
+     * Add the given attributes to this params if they are not already present.
+     */
+    public void addAttributesIfNotExist( List<QueryItem> attrs )
+    {
+        for ( QueryItem attr : attrs )
+        {
+            if ( attributes != null && !attributes.contains( attr ) )
+            {
+                attributes.add( attr );            
+            }
+        }
+    }
     
     /**
+     * Adds the given filters to this params if they are not already present.
+     */
+    public void addFiltersIfNotExist( List<QueryItem> filtrs )
+    {
+        for ( QueryItem filter : filtrs )
+        {
+            if ( filters != null && !filters.contains( filter ) )
+            {
+                filters.add( filter );
+            }
+        }
+    }
+    /**
      * Indicates whether this is a logical OR query, meaning that a query string
      * is specified and instances which matches this query on one or more attributes
      * should be included in the response. The opposite is an item-specific query,
@@ -278,35 +327,7 @@
         
         return duplicates;
     }
-        
-    /**
-     * Add the given attributes to this params if they are not already present.
-     */
-    public void addAttributesIfNotExist( List<QueryItem> attrs )
-    {
-        for ( QueryItem attr : attrs )
-        {
-            if ( attributes != null && !attributes.contains( attr ) )
-            {
-                attributes.add( attr );            
-            }
-        }
-    }
-    
-    /**
-     * Adds the given filters to this params if they are not already present.
-     */
-    public void addFiltersIfNotExist( List<QueryItem> filtrs )
-    {
-        for ( QueryItem filter : filtrs )
-        {
-            if ( filters != null && !filters.contains( filter ) )
-            {
-                filters.add( filter );
-            }
-        }
-    }
-    
+           
     /**
      * Indicates whether this params specifies any attributes and/or filters.
      */

=== 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-05-08 13:39:17 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceService.java	2014-05-08 17:33:45 +0000
@@ -45,6 +45,49 @@
 import org.hisp.dhis.validation.ValidationCriteria;
 
 /**
+ * <p>This interface is responsible for retrieving tracked entity instances (TEI).
+ * The query methods accepts a TrackedEntityInstanceQueryParams object which
+ * encapsulates all arguments.</p> 
+ * 
+ * <p>The TEIs are returned as a Grid object, which is a two-dimensional list with 
+ * headers. The TEI attribute values are returned in the same order as specified
+ * in the arguments. The grid has a set of columns which are always present
+ * starting at index 0, followed by attributes specified for the query. All
+ * values in the grid are of type String. The order is:</p>
+ * 
+ * <ul>
+ * <li>0: Tracked entity instance UID</li>
+ * <li>1: Created time stamp</li>
+ * <li>2: Last updated time stamp</li>
+ * <li>3: Organisation unit UID</li>
+ * <li>4: Tracked entity UID</li>
+ * <ul>
+ * 
+ * <p>Attributes specified in the query follows on the next column indexes.
+ * Example usage for retrieving TEIs with two attributes using one attribute as 
+ * filter:</p>
+ * 
+ * <pre>
+ * <code>
+ * TrackedEntityInstanceQueryParams params = new TrackedEntityInstanceQueryParams();
+ *
+ * params.addAttribute( new QueryItem( gender, QueryOperator.EQ, "Male", false ) );
+ * params.addAttribute( new QueryItem( age, QueryOperator.LT, "5", true ) );
+ * params.addFilter( new QueryItem( weight, QueryOperator.GT, "2500", true ) );
+ * params.addOrganistionUnit( unit );
+ * 
+ * Grid instances = teiService.getTrackedEntityInstances( params );
+ * 
+ * for ( List&lt;Object&gt; row : instances.getRows() )
+ * {
+ *     String tei = row.get( 0 );
+ *     String ou = row.get( 3 );
+ *     String gender = row.get( 5 );
+ *     String age = row.get( 6 );
+ * }
+ * </code>
+ * </pre>
+ * 
  * @author Abyot Asalefew Gizaw
  */
 public interface TrackedEntityInstanceService

=== 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-06 18:53:52 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/DefaultEventAnalyticsService.java	2014-05-08 17:33:45 +0000
@@ -62,6 +62,7 @@
 import org.hisp.dhis.common.Pager;
 import org.hisp.dhis.common.QueryFilter;
 import org.hisp.dhis.common.QueryItem;
+import org.hisp.dhis.common.QueryOperator;
 import org.hisp.dhis.dataelement.DataElement;
 import org.hisp.dhis.dataelement.DataElementService;
 import org.hisp.dhis.i18n.I18nFormat;
@@ -440,7 +441,8 @@
         {   
             for ( int i = 1; i < split.length; i += 2 )
             {
-                queryItem.getFilters().add( new QueryFilter( split[i], split[i+1] ) );
+                QueryOperator operator = QueryOperator.fromString( split[i] );
+                queryItem.getFilters().add( new QueryFilter( operator, split[i+1] ) );
             }
         }
 

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/enrollment/AbstractEnrollmentService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/enrollment/AbstractEnrollmentService.java	2014-04-15 03:54:06 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/enrollment/AbstractEnrollmentService.java	2014-05-08 17:33:45 +0000
@@ -30,10 +30,12 @@
 
 import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
+
 import org.hisp.dhis.common.Grid;
 import org.hisp.dhis.common.IdentifiableObjectManager;
 import org.hisp.dhis.common.OrganisationUnitSelectionMode;
 import org.hisp.dhis.common.QueryItem;
+import org.hisp.dhis.common.QueryOperator;
 import org.hisp.dhis.dxf2.events.trackedentity.Attribute;
 import org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance;
 import org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstanceService;
@@ -218,9 +220,6 @@
 
         for ( ProgramInstance programInstance : programInstances )
         {
-            // check for null, both for pi, and for pi.entityInstance, there are DBs
-            // out there where trackedentityinstanceid == null
-            // even if the program is of type 1/2.
             if ( programInstance != null && programInstance.getEntityInstance() != null )
             {
                 enrollments.getEnrollments().add( getEnrollment( programInstance ) );
@@ -503,17 +502,17 @@
 
         TrackedEntityInstanceQueryParams params = new TrackedEntityInstanceQueryParams();
 
-        QueryItem queryItem = new QueryItem( attribute, "eq", value, false );
-        params.getAttributes().add( queryItem );
+        QueryItem queryItem = new QueryItem( attribute, QueryOperator.EQ, value, false );
+        params.addAttribute( queryItem );
 
         if ( attribute.getOrgunitScope() && attribute.getProgramScope() )
         {
             params.setProgram( program );
-            params.getOrganisationUnits().add( organisationUnit );
+            params.addOrganisationUnit( organisationUnit );
         }
         else if ( attribute.getOrgunitScope() )
         {
-            params.getOrganisationUnits().add( organisationUnit );
+            params.addOrganisationUnit( organisationUnit );
         }
         else if ( attribute.getProgramScope() )
         {

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/trackedentity/AbstractTrackedEntityInstanceService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/trackedentity/AbstractTrackedEntityInstanceService.java	2014-04-15 03:54:06 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/trackedentity/AbstractTrackedEntityInstanceService.java	2014-05-08 17:33:45 +0000
@@ -29,10 +29,12 @@
  */
 
 import com.google.common.collect.Lists;
+
 import org.hisp.dhis.common.Grid;
 import org.hisp.dhis.common.IdentifiableObjectManager;
 import org.hisp.dhis.common.OrganisationUnitSelectionMode;
 import org.hisp.dhis.common.QueryItem;
+import org.hisp.dhis.common.QueryOperator;
 import org.hisp.dhis.dxf2.importsummary.ImportConflict;
 import org.hisp.dhis.dxf2.importsummary.ImportStatus;
 import org.hisp.dhis.dxf2.importsummary.ImportSummary;
@@ -338,8 +340,8 @@
 
         TrackedEntityInstanceQueryParams params = new TrackedEntityInstanceQueryParams();
 
-        QueryItem queryItem = new QueryItem( attribute, "eq", value, false );
-        params.getAttributes().add( queryItem );
+        QueryItem queryItem = new QueryItem( attribute, QueryOperator.EQ, value, false );
+        params.addAttribute( queryItem );
 
         if ( attribute.getOrgunitScope() )
         {

=== 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-05-08 13:39:17 +0000
+++ dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentity/DefaultTrackedEntityInstanceService.java	2014-05-08 17:33:45 +0000
@@ -55,6 +55,7 @@
 import org.hisp.dhis.common.OrganisationUnitSelectionMode;
 import org.hisp.dhis.common.Pager;
 import org.hisp.dhis.common.QueryItem;
+import org.hisp.dhis.common.QueryOperator;
 import org.hisp.dhis.event.EventStatus;
 import org.hisp.dhis.i18n.I18nFormat;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
@@ -441,7 +442,9 @@
 
         if ( operator != null && filter != null )
         {
-            return new QueryItem( at, operator, filter, at.isNumericType() );
+            QueryOperator op = QueryOperator.fromString( operator );
+            
+            return new QueryItem( at, op, filter, at.isNumericType() );
         }
         else
         {

=== modified file 'dhis-2/dhis-web/dhis-web-light/src/main/java/org/hisp/dhis/light/message/action/AddRecipientAction.java'
--- dhis-2/dhis-web/dhis-web-light/src/main/java/org/hisp/dhis/light/message/action/AddRecipientAction.java	2014-05-06 06:43:36 +0000
+++ dhis-2/dhis-web/dhis-web-light/src/main/java/org/hisp/dhis/light/message/action/AddRecipientAction.java	2014-05-08 17:33:45 +0000
@@ -1,10 +1,36 @@
 package org.hisp.dhis.light.message.action;
 
+/*
+ * 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.
+ */
+
 import java.util.HashSet;
 import java.util.Set;
 
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
 import org.hisp.dhis.user.User;
 import org.hisp.dhis.user.UserService;
 
@@ -18,8 +44,6 @@
 public class AddRecipientAction
     implements Action
 {
-    private static final Log log = LogFactory.getLog( AddRecipientAction.class );
-
     public AddRecipientAction()
     {
     }

=== modified file 'dhis-2/dhis-web/dhis-web-light/src/main/java/org/hisp/dhis/light/messaging/action/FindUserAction.java'
--- dhis-2/dhis-web/dhis-web-light/src/main/java/org/hisp/dhis/light/messaging/action/FindUserAction.java	2014-05-06 06:43:36 +0000
+++ dhis-2/dhis-web/dhis-web-light/src/main/java/org/hisp/dhis/light/messaging/action/FindUserAction.java	2014-05-08 17:33:45 +0000
@@ -32,8 +32,6 @@
 import java.util.HashSet;
 import java.util.Set;
 
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
 import org.hisp.dhis.user.User;
 import org.hisp.dhis.user.UserService;
 
@@ -42,8 +40,6 @@
 public class FindUserAction
     implements Action
 {
-    private static final Log log = LogFactory.getLog( FindUserAction.class );
-
     private static final String REDIRECT = "redirect";
 
     private UserService userService;