← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 14896: TEI query, changed from programDates to programStartDate and programEndDate for URL syntax

 

------------------------------------------------------------
revno: 14896
committer: Lars Helge Øverland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Fri 2014-04-18 11:52:56 +0200
message:
  TEI query, changed from programDates to programStartDate and programEndDate for URL syntax
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-04-17 20:10:10 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceQueryParams.java	2014-04-18 09:52:56 +0000
@@ -29,13 +29,13 @@
  */
 
 import java.util.ArrayList;
+import java.util.Date;
 import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Set;
 
 import org.hisp.dhis.common.OrganisationUnitSelectionMode;
-import org.hisp.dhis.common.QueryFilter;
 import org.hisp.dhis.common.QueryItem;
 import org.hisp.dhis.common.SetMap;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
@@ -96,9 +96,14 @@
     private Boolean followUp;
     
     /**
-     * Enrollment dates for the given program.
-     */
-    private List<QueryFilter> programDates = new ArrayList<QueryFilter>();
+     * Start date for enrollment in the given program.
+     */
+    private Date programStartDate;
+    
+    /**
+     * End date for enrollment in the given program.
+     */
+    private Date programEndDate;
     
     /**
      * Tracked entity of the instances in the response.
@@ -343,12 +348,19 @@
     }
     
     /**
-     * Indicates whether this params specifies any program dates.
-     * @return
-     */
-    public boolean hasProgramDates()
-    {
-        return programDates != null && !programDates.isEmpty();
+     * Indicates whether this params specifies a program start date.
+     */
+    public boolean hasProgramStartDate()
+    {
+        return programStartDate != null;
+    }
+    
+    /**
+     * Indicates whether this params specifies a program end date.
+     */
+    public boolean hasProgramEndDate()
+    {
+        return programEndDate != null;
     }
     
     /**
@@ -473,14 +485,24 @@
         this.followUp = followUp;
     }
 
-    public List<QueryFilter> getProgramDates()
-    {
-        return programDates;
-    }
-
-    public void setProgramDates( List<QueryFilter> programDates )
-    {
-        this.programDates = programDates;
+    public Date getProgramStartDate()
+    {
+        return programStartDate;
+    }
+
+    public void setProgramStartDate( Date programStartDate )
+    {
+        this.programStartDate = programStartDate;
+    }
+
+    public Date getProgramEndDate()
+    {
+        return programEndDate;
+    }
+
+    public void setProgramEndDate( Date programEndDate )
+    {
+        this.programEndDate = programEndDate;
     }
 
     public TrackedEntity getTrackedEntity()

=== 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-04-17 20:10:10 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceService.java	2014-04-18 09:52:56 +0000
@@ -29,6 +29,7 @@
  */
 
 import java.util.Collection;
+import java.util.Date;
 import java.util.List;
 import java.util.Set;
 
@@ -75,7 +76,8 @@
      * @param program the Program identifier.
      * @param programStatus the ProgramStatus in the given orogram.
      * @param followUp indicates follow up status in the given Program.
-     * @param programDate the set of filters for Program enrollment dates.
+     * @param programStartDate the start date for enrollment in the given Program.
+     * @param programEndDate the end date for enrollment in the given Program.
      * @param trackedEntity the TrackedEntity uid.
      * @param skipMeta indicates whether to include meta data in the response.
      * @param page the page number.
@@ -83,7 +85,7 @@
      * @return a TrackedEntityInstanceQueryParams.
      */
     TrackedEntityInstanceQueryParams getFromUrl( String query, Set<String> attribute, Set<String> filter, 
-        Set<String> ou, OrganisationUnitSelectionMode ouMode, String program, ProgramStatus programStatus, Boolean followUp, Set<String> programDate,
+        Set<String> ou, OrganisationUnitSelectionMode ouMode, String program, ProgramStatus programStatus, Boolean followUp, Date programStartDate, Date programEndDate,
         String trackedEntity, boolean skipMeta, Integer page, Integer pageSize );
     
     /**

=== 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-17 20:10:10 +0000
+++ dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentity/DefaultTrackedEntityInstanceService.java	2014-04-18 09:52:56 +0000
@@ -55,7 +55,6 @@
 import org.hisp.dhis.common.IllegalQueryException;
 import org.hisp.dhis.common.OrganisationUnitSelectionMode;
 import org.hisp.dhis.common.Pager;
-import org.hisp.dhis.common.QueryFilter;
 import org.hisp.dhis.common.QueryItem;
 import org.hisp.dhis.i18n.I18n;
 import org.hisp.dhis.i18n.I18nFormat;
@@ -296,9 +295,14 @@
             violation = "Program must be defined when follow up status is defined";
         }
         
-        if ( params.hasProgramDates() && !params.hasProgram() )
-        {
-            violation = "Program must be defined when program dates are specified";
+        if ( params.hasProgramStartDate() && !params.hasProgram() )
+        {
+            violation = "Program must be defined when program start date is specified";
+        }
+        
+        if ( params.hasProgramEndDate() && !params.hasProgram() )
+        {
+            violation = "Program must be defined when program end date is specified";
         }
 
         if ( params.isOrQuery() && params.hasFilters() )
@@ -326,7 +330,7 @@
     
     @Override
     public TrackedEntityInstanceQueryParams getFromUrl( String query, Set<String> attribute, Set<String> filter, Set<String> ou, 
-        OrganisationUnitSelectionMode ouMode, String program, ProgramStatus programStatus, Boolean followUp, Set<String> programDate, 
+        OrganisationUnitSelectionMode ouMode, String program, ProgramStatus programStatus, Boolean followUp, Date programStartDate, Date programEndDate,
         String trackedEntity, boolean skipMeta, Integer page, Integer pageSize )
     {
         TrackedEntityInstanceQueryParams params = new TrackedEntityInstanceQueryParams();
@@ -372,17 +376,7 @@
         {
             throw new IllegalQueryException( "Program does not exist: " + program );
         }
-        
-        if ( programDate != null )
-        {
-            for ( String date : programDate )
-            {
-                QueryFilter queryFilter = getQueryFilter( date );
                 
-                params.getProgramDates().add( queryFilter );
-            }
-        }
-        
         TrackedEntity te = trackedEntity != null ? trackedEntityService.getTrackedEntity( trackedEntity ) : null;
         
         if ( trackedEntity != null && te == null )
@@ -394,6 +388,8 @@
         params.setProgram( pr );
         params.setProgramStatus( programStatus );
         params.setFollowUp( followUp );
+        params.setProgramStartDate( programStartDate );
+        params.setProgramEndDate( programEndDate );
         params.setTrackedEntity( te );
         params.setOrganisationUnitMode( ouMode );
         params.setSkipMeta( skipMeta );
@@ -402,18 +398,6 @@
         
         return params;
     }
-
-    private QueryFilter getQueryFilter( String filter )
-    {
-        String[] split = filter.split( DimensionalObjectUtils.DIMENSION_NAME_SEP );
-        
-        if ( split == null || split.length != 2 )
-        {
-            throw new IllegalQueryException( "Program date filter has invalid format: " + filter );
-        }
-        
-        return new QueryFilter( split[0], split[1] );
-    }
     
     private QueryItem getQueryItem( String item )
     {

=== 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-04-18 08:58:14 +0000
+++ dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentity/hibernate/HibernateTrackedEntityInstanceStore.java	2014-04-18 09:52:56 +0000
@@ -29,6 +29,7 @@
  */
 
 import static org.hisp.dhis.common.IdentifiableObjectUtils.getIdentifiers;
+import static org.hisp.dhis.system.util.DateUtils.getMediumDateString;
 import static org.hisp.dhis.system.util.TextUtils.getCommaDelimitedString;
 import static org.hisp.dhis.system.util.TextUtils.getTokens;
 import static org.hisp.dhis.system.util.TextUtils.removeLastAnd;
@@ -64,7 +65,6 @@
 import org.hibernate.criterion.Restrictions;
 import org.hisp.dhis.common.Grid;
 import org.hisp.dhis.common.OrganisationUnitSelectionMode;
-import org.hisp.dhis.common.QueryFilter;
 import org.hisp.dhis.common.QueryItem;
 import org.hisp.dhis.common.SetMap;
 import org.hisp.dhis.common.hibernate.HibernateIdentifiableObjectStore;
@@ -241,9 +241,9 @@
         final String wordStart = statementBuilder.getRegexpWordStart();
         final String wordEnd = statementBuilder.getRegexpWordEnd();
 
-        String sql = "from trackedentityinstance tei "
-            + "inner join trackedentity te on tei.trackedentityid = te.trackedentityid "
-            + "inner join organisationunit ou on tei.organisationunitid = ou.organisationunitid ";
+        String sql = "from trackedentityinstance tei " + 
+            "inner join trackedentity te on tei.trackedentityid = te.trackedentityid " + 
+            "inner join organisationunit ou on tei.organisationunitid = ou.organisationunitid ";
 
         for ( QueryItem item : params.getAttributesAndFilters() )
         {
@@ -251,9 +251,9 @@
 
             final String joinClause = item.hasFilter() ? "inner join" : "left join";
 
-            sql += joinClause + " trackedentityattributevalue as " + col + " " + "on " + col
-                + ".trackedentityinstanceid = tei.trackedentityinstanceid " + "and " + col
-                + ".trackedentityattributeid = " + item.getItem().getId() + " ";
+            sql += joinClause + 
+                " trackedentityattributevalue as " + col + " " + "on " + col + ".trackedentityinstanceid = tei.trackedentityinstanceid " + 
+                "and " + col + ".trackedentityattributeid = " + item.getItem().getId() + " ";
 
             final String filter = statementBuilder.encode( item.getFilter(), false );
 
@@ -291,8 +291,7 @@
         else if ( params.isOrganisationUnitMode( OrganisationUnitSelectionMode.ALL ) )
         {
         }
-        else
-        // SELECTED (default)
+        else // SELECTED (default)
         {
             sql += hlp.whereAnd() + " tei.organisationunitid in ("
                 + getCommaDelimitedString( getIdentifiers( params.getOrganisationUnits() ) ) + ") ";
@@ -300,9 +299,10 @@
 
         if ( params.hasProgram() )
         {
-            sql += hlp.whereAnd() + " exists (" + "select trackedentityinstanceid from programinstance pi "
-                + "where pi.trackedentityinstanceid=tei.trackedentityinstanceid " + "and pi.programid = "
-                + params.getProgram().getId() + " ";
+            sql += hlp.whereAnd() + 
+                " exists (" + "select trackedentityinstanceid from programinstance pi " + 
+                "where pi.trackedentityinstanceid=tei.trackedentityinstanceid " + 
+                "and pi.programid = " + params.getProgram().getId() + " ";
 
             if ( params.hasProgramStatus() )
             {
@@ -314,14 +314,14 @@
                 sql += "and pi.followup = " + params.getFollowUp() + " ";
             }
 
-            if ( params.hasProgramDates() )
-            {
-                for ( QueryFilter date : params.getProgramDates() )
-                {
-                    String filter = statementBuilder.encode( date.getFilter(), false );
-
-                    sql += "and pi.enrollmentdate " + date.getSqlOperator() + " " + date.getSqlFilter( filter ) + " ";
-                }
+            if ( params.hasProgramStartDate() )
+            {
+                sql += "and pi.enrollmentdate >= '" + getMediumDateString( params.getProgramStartDate() ) + "' ";
+            }
+            
+            if ( params.hasProgramEndDate() )
+            {
+                sql += "and pi.enrollmentdate <= '" + getMediumDateString( params.getProgramEndDate() ) + "' ";
             }
 
             sql += ") ";
@@ -343,8 +343,9 @@
                 {
                     final String col = statementBuilder.columnQuote( item.getItemId() );
 
-                    sql += "lower(" + col + ".value) " + regexp + " '" + wordStart + StringUtils.lowerCase( query )
-                        + wordEnd + "' or ";
+                    sql += 
+                        "lower(" + col + ".value) " + regexp + " '" + wordStart + 
+                        StringUtils.lowerCase( query ) + wordEnd + "' or ";
                 }
 
                 sql = removeLastOr( sql ) + ") and ";

=== 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-04-17 20:10:10 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/event/TrackedEntityInstanceController.java	2014-04-18 09:52:56 +0000
@@ -29,6 +29,7 @@
  */
 
 import java.io.IOException;
+import java.util.Date;
 import java.util.HashSet;
 import java.util.Map;
 import java.util.Set;
@@ -55,6 +56,7 @@
 import org.hisp.dhis.system.grid.GridUtils;
 import org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.format.annotation.DateTimeFormat;
 import org.springframework.http.HttpStatus;
 import org.springframework.http.MediaType;
 import org.springframework.security.access.prepost.PreAuthorize;
@@ -106,7 +108,8 @@
         @RequestParam(required=false) String program,
         @RequestParam(required=false) ProgramStatus programStatus,
         @RequestParam(required=false) Boolean followUp,
-        @RequestParam(required=false) Set<String> programDate,
+        @RequestParam(required=false) @DateTimeFormat( pattern = "yyyy-MM-dd" ) Date programStartDate,
+        @RequestParam(required=false) @DateTimeFormat( pattern = "yyyy-MM-dd" ) Date programEndDate,
         @RequestParam(required=false) String trackedEntity,
         @RequestParam(required=false) boolean skipMeta,
         @RequestParam(required=false) Integer page,
@@ -116,7 +119,7 @@
     {
         Set<String> orgUnits = new HashSet<String>( ContextUtils.getQueryParamValues( ou ) );        
         TrackedEntityInstanceQueryParams params = instanceService.getFromUrl( query, attribute, filter, orgUnits, ouMode, 
-            program, programStatus, followUp, programDate, trackedEntity, skipMeta, page, pageSize );
+            program, programStatus, followUp, programStartDate, programEndDate, trackedEntity, skipMeta, page, pageSize );
         
         contextUtils.configureResponse( response, ContextUtils.CONTENT_TYPE_JSON, CacheStrategy.NO_CACHE );
         Grid grid = instanceService.getTrackedEntityInstances( params );
@@ -136,7 +139,8 @@
         @RequestParam(required=false) String program,
         @RequestParam(required=false) ProgramStatus programStatus,
         @RequestParam(required=false) Boolean followUp,
-        @RequestParam(required=false) Set<String> programDate,
+        @RequestParam(required=false) @DateTimeFormat( pattern = "yyyy-MM-dd" ) Date programStartDate,
+        @RequestParam(required=false) @DateTimeFormat( pattern = "yyyy-MM-dd" ) Date programEndDate,
         @RequestParam(required=false) String trackedEntity,
         @RequestParam(required=false) boolean skipMeta,
         @RequestParam(required=false) Integer page,
@@ -146,7 +150,7 @@
     {
         Set<String> orgUnits = new HashSet<String>( ContextUtils.getQueryParamValues( ou ) );        
         TrackedEntityInstanceQueryParams params = instanceService.getFromUrl( query, attribute, filter, orgUnits, ouMode, 
-            program, programStatus, followUp, programDate, trackedEntity, skipMeta, page, pageSize );
+            program, programStatus, followUp, programStartDate, programEndDate, trackedEntity, skipMeta, page, pageSize );
         
         contextUtils.configureResponse( response, ContextUtils.CONTENT_TYPE_XML, CacheStrategy.NO_CACHE );
         Grid grid = instanceService.getTrackedEntityInstances( params );
@@ -163,7 +167,8 @@
         @RequestParam(required=false) String program,
         @RequestParam(required=false) ProgramStatus programStatus,
         @RequestParam(required=false) Boolean followUp,
-        @RequestParam(required=false) Set<String> programDate,
+        @RequestParam(required=false) @DateTimeFormat( pattern = "yyyy-MM-dd" ) Date programStartDate,
+        @RequestParam(required=false) @DateTimeFormat( pattern = "yyyy-MM-dd" ) Date programEndDate,
         @RequestParam(required=false) String trackedEntity,
         @RequestParam(required=false) boolean skipMeta,
         @RequestParam(required=false) Integer page,
@@ -173,7 +178,7 @@
     {
         Set<String> orgUnits = new HashSet<String>( ContextUtils.getQueryParamValues( ou ) );        
         TrackedEntityInstanceQueryParams params = instanceService.getFromUrl( query, attribute, filter, orgUnits, ouMode, 
-            program, programStatus, followUp, programDate, trackedEntity, skipMeta, page, pageSize );
+            program, programStatus, followUp, programStartDate, programEndDate, trackedEntity, skipMeta, page, pageSize );
         
         contextUtils.configureResponse( response, ContextUtils.CONTENT_TYPE_EXCEL, CacheStrategy.NO_CACHE );
         Grid grid = instanceService.getTrackedEntityInstances( params );
@@ -190,7 +195,8 @@
         @RequestParam(required=false) String program,
         @RequestParam(required=false) ProgramStatus programStatus,
         @RequestParam(required=false) Boolean followUp,
-        @RequestParam(required=false) Set<String> programDate,
+        @RequestParam(required=false) @DateTimeFormat( pattern = "yyyy-MM-dd" ) Date programStartDate,
+        @RequestParam(required=false) @DateTimeFormat( pattern = "yyyy-MM-dd" ) Date programEndDate,
         @RequestParam(required=false) String trackedEntity,
         @RequestParam(required=false) boolean skipMeta,
         @RequestParam(required=false) Integer page,
@@ -200,7 +206,7 @@
     {
         Set<String> orgUnits = new HashSet<String>( ContextUtils.getQueryParamValues( ou ) );        
         TrackedEntityInstanceQueryParams params = instanceService.getFromUrl( query, attribute, filter, orgUnits, ouMode, 
-            program, programStatus, followUp, programDate, trackedEntity, skipMeta, page, pageSize );
+            program, programStatus, followUp, programStartDate, programEndDate, trackedEntity, skipMeta, page, pageSize );
         
         contextUtils.configureResponse( response, ContextUtils.CONTENT_TYPE_CSV, CacheStrategy.NO_CACHE );
         Grid grid = instanceService.getTrackedEntityInstances( params );