← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 13164: Event analytics, centralized some sql filtering code. Renamed PersonQueryParams to TrackedEntityQ...

 

Merge authors:
  Lars Helge Øverland (larshelge)
------------------------------------------------------------
revno: 13164 [merge]
committer: Lars Helge Øverland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Fri 2013-12-06 16:16:51 +0100
message:
  Event analytics, centralized some sql filtering code. Renamed PersonQueryParams to TrackedEntityQueryParams.
renamed:
  dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patient/PersonQueryParams.java => dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patient/TrackedEntityQueryParams.java
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/QueryItem.java
  dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/JdbcEventAnalyticsManager.java
  dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patient/hibernate/HibernatePatientStore.java
  dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patient/TrackedEntityQueryParams.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/QueryItem.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/QueryItem.java	2013-10-14 20:58:12 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/QueryItem.java	2013-12-06 15:16:51 +0000
@@ -31,13 +31,13 @@
 import java.util.HashMap;
 import java.util.Map;
 
-import org.hisp.dhis.common.IdentifiableObject;
-
 /**
  * @author Lars Helge Overland
  */
 public class QueryItem
 {
+    public static final String OPTION_SEP = ";";
+    
     public static final Map<String, String> OPERATOR_MAP = new HashMap<String, String>() { {
         put( "eq", "=" );
         put( "gt", ">" );
@@ -90,6 +90,34 @@
         return OPERATOR_MAP.get( operator.toLowerCase() );
     }
     
+    public String getSqlFilter( String encodedFilter )
+    {
+        if ( operator == null || encodedFilter == null )
+        {
+            return null;
+        }
+                
+        if ( operator.equalsIgnoreCase( "like" ) )
+        {
+            return "'%" + encodedFilter + "%'";
+        }
+        else if ( operator.equalsIgnoreCase( "in" ) )
+        {
+            String[] split = encodedFilter.split( OPTION_SEP );
+            
+            final StringBuffer buffer = new StringBuffer( "(" );        
+            
+            for ( String el : split )
+            {
+                buffer.append( "'" ).append( el.toString() ).append( "'," );
+            }
+            
+            return buffer.deleteCharAt( buffer.length() - 1 ).append( ")" ).toString();
+        }
+        
+        return "'" + encodedFilter + "'";
+    }
+    
     @Override
     public String toString()
     {

=== modified file 'dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/JdbcEventAnalyticsManager.java'
--- dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/JdbcEventAnalyticsManager.java	2013-12-04 14:42:51 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/JdbcEventAnalyticsManager.java	2013-12-06 14:42:43 +0000
@@ -36,11 +36,8 @@
 import static org.hisp.dhis.system.util.TextUtils.removeLast;
 import static org.hisp.dhis.system.util.TextUtils.trimEnd;
 
-import java.util.Arrays;
-
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
-import org.hisp.dhis.analytics.DataQueryParams;
 import org.hisp.dhis.analytics.event.EventAnalyticsManager;
 import org.hisp.dhis.analytics.event.EventQueryParams;
 import org.hisp.dhis.common.DimensionalObject;
@@ -400,28 +397,8 @@
      */
     private String getSqlFilter( QueryItem item )
     {
-        String operator = item.getOperator();
-        String filter = item.getFilter();
-        
-        if ( operator == null || filter == null )
-        {
-            return null;
-        }
-        
-        operator = operator.toLowerCase();
-        filter = statementBuilder.encode( filter, false );
-        
-        if ( operator.equals( "like" ) )
-        {
-            return "'%" + filter + "%'";
-        }
-        else if ( operator.equals( "in" ) )
-        {
-            String[] split = filter.split( DataQueryParams.OPTION_SEP );
-                        
-            return "(" + getQuotedCommaDelimitedString( Arrays.asList( split ) ) + ")";
-        }
-        
-        return "'" + filter + "'";
+        String encodedFilter = statementBuilder.encode( item.getFilter(), false );
+        
+        return item.getSqlFilter( encodedFilter );
     }
 }

=== renamed file 'dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patient/PersonQueryParams.java' => 'dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patient/TrackedEntityQueryParams.java'
--- dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patient/PersonQueryParams.java	2013-11-01 11:46:41 +0000
+++ dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patient/TrackedEntityQueryParams.java	2013-12-06 13:29:05 +0000
@@ -39,39 +39,17 @@
 /**
  * @author Lars Helge Overland
  */
-public class PersonQueryParams
+public class TrackedEntityQueryParams
 {
     private Program program;
 
     private List<QueryItem> attributes = new ArrayList<QueryItem>();
     
-    private List<QueryItem> identifiers = new ArrayList<QueryItem>();
-    
     private List<OrganisationUnit> organisationUnits = new ArrayList<OrganisationUnit>();
 
     private OrganisationUnitSelectionMode orgUnitSelectionMode;
     
     private Integer enrollmentStatus; // 0, 1 or null
-
-    // -------------------------------------------------------------------------
-    // Logic
-    // -------------------------------------------------------------------------
-
-    /**
-     * Indicates whether any identifiers are part of this query.
-     */
-    public boolean hasIdentifiers()
-    {
-        return identifiers != null && !identifiers.isEmpty();
-    }
-
-    /**
-     * Indicates whether any attributes are part of this query.
-     */
-    public boolean hasAttributes()
-    {
-        return attributes != null && !attributes.isEmpty();
-    }
     
     // -------------------------------------------------------------------------
     // Getters and setters
@@ -97,16 +75,6 @@
         this.attributes = attributes;
     }
 
-    public List<QueryItem> getIdentifiers()
-    {
-        return identifiers;
-    }
-
-    public void setIdentifiers( List<QueryItem> identifiers )
-    {
-        this.identifiers = identifiers;
-    }
-
     public List<OrganisationUnit> getOrganisationUnits()
     {
         return organisationUnits;

=== modified file 'dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patient/hibernate/HibernatePatientStore.java'
--- dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patient/hibernate/HibernatePatientStore.java	2013-11-26 08:29:22 +0000
+++ dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patient/hibernate/HibernatePatientStore.java	2013-12-06 13:29:05 +0000
@@ -57,6 +57,7 @@
 import org.hibernate.criterion.Projections;
 import org.hibernate.criterion.Restrictions;
 import org.hisp.dhis.common.Grid;
+import org.hisp.dhis.common.QueryItem;
 import org.hisp.dhis.common.hibernate.HibernateIdentifiableObjectStore;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.organisationunit.OrganisationUnitService;
@@ -66,11 +67,13 @@
 import org.hisp.dhis.patient.PatientIdentifierType;
 import org.hisp.dhis.patient.PatientService;
 import org.hisp.dhis.patient.PatientStore;
+import org.hisp.dhis.patient.TrackedEntityQueryParams;
 import org.hisp.dhis.period.Period;
 import org.hisp.dhis.program.Program;
 import org.hisp.dhis.program.ProgramInstance;
 import org.hisp.dhis.program.ProgramStageInstance;
 import org.hisp.dhis.system.grid.GridUtils;
+import org.hisp.dhis.system.util.SqlHelper;
 import org.hisp.dhis.system.util.TextUtils;
 import org.hisp.dhis.validation.ValidationCriteria;
 import org.springframework.jdbc.core.RowMapper;
@@ -171,7 +174,7 @@
     public Collection<Patient> getByOrgUnitProgram( OrganisationUnit organisationUnit, Program program, Integer min,
         Integer max )
     {
-        String hql = "select pt from Patient pt " + "inner join pt.programInstances pi "
+        String hql = "select pt from Patient pt inner join pt.programInstances pi "
             + "where pt.organisationUnit = :organisationUnit " + "and pi.program = :program "
             + "and pi.status = :status";
 
@@ -183,6 +186,26 @@
         return query.list();
     }
 
+    @SuppressWarnings( "unchecked" )
+    public List<Patient> query( TrackedEntityQueryParams params )
+    {
+        SqlHelper hlp = new SqlHelper();
+        
+        String hql = 
+            "select pt from Patient pt " +
+            "inner join PatientAttributeValue av " +
+            "inner join PatientAttribute at ";
+        
+        for ( QueryItem at : params.getAttributes() )
+        {
+            hlp.whereAnd();
+        }
+        
+        Query query = getQuery( hql );
+        
+        return query.list();
+    }
+
     @Override
     @SuppressWarnings( "unchecked" )
     public Collection<Patient> getByProgram( Program program, Integer min, Integer max )