← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 14275: TEI query, validation

 

------------------------------------------------------------
revno: 14275
committer: Lars Helge Øverland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2014-03-18 12:26:41 +0100
message:
  TEI query, validation
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-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 08:10:10 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceQueryParams.java	2014-03-18 11:26:41 +0000
@@ -51,6 +51,8 @@
     public static final String TRACKED_ENTITY_ATTRIBUTE_ID = "teattribute";
     public static final String TRACKED_ENTITY_ATTRIBUTE_VALUE_ID = "tevalue";
     
+    private String query;
+    
     private List<QueryItem> items = new ArrayList<QueryItem>();
 
     private Set<OrganisationUnit> organisationUnits = new HashSet<OrganisationUnit>();
@@ -92,6 +94,27 @@
         return setMap;
     }
     
+    public boolean isOrQuery()
+    {
+        return hasQuery();
+    }
+    
+    /**
+     * Indicates whether this params specifies a query.
+     */
+    public boolean hasQuery()
+    {
+        return query != null && !query.isEmpty();
+    }
+
+    /**
+     * Indicates whether this params specifies any query items.
+     */
+    public boolean hasItems()
+    {
+        return items != null && !items.isEmpty();
+    }
+    
     /**
      * Indicates whether this params specifies a program.
      */
@@ -152,6 +175,16 @@
     // Getters and setters
     // -------------------------------------------------------------------------
 
+    public String getQuery()
+    {
+        return query;
+    }
+
+    public void setQuery( String query )
+    {
+        this.query = query;
+    }
+
     public List<QueryItem> getItems()
     {
         return items;

=== 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 08:10:10 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceService.java	2014-03-18 11:26:41 +0000
@@ -55,7 +55,7 @@
 
     Grid getTrackedEntityInstances( TrackedEntityInstanceQueryParams params );
     
-    TrackedEntityInstanceQueryParams getFromUrl( Set<String> items, Set<String> ou, String ouMode, 
+    TrackedEntityInstanceQueryParams getFromUrl( String query, Set<String> items, Set<String> ou, String ouMode, 
         String program, String trackedEntity, 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-03-18 08:10:10 +0000
+++ dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentity/DefaultTrackedEntityInstanceService.java	2014-03-18 11:26:41 +0000
@@ -44,6 +44,8 @@
 import java.util.Set;
 
 import org.apache.commons.lang.StringUtils;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 import org.hisp.dhis.common.DimensionalObjectUtils;
 import org.hisp.dhis.common.Grid;
 import org.hisp.dhis.common.GridHeader;
@@ -72,6 +74,8 @@
 public class DefaultTrackedEntityInstanceService
     implements TrackedEntityInstanceService
 {
+    private static final Log log = LogFactory.getLog( DefaultTrackedEntityInstanceService.class );
+    
     // -------------------------------------------------------------------------
     // Dependencies
     // -------------------------------------------------------------------------
@@ -139,6 +143,8 @@
     @Override
     public Grid getTrackedEntityInstances( TrackedEntityInstanceQueryParams params )
     {
+        validate( params );
+        
         Grid grid = new ListGrid();
         
         grid.addHeader( new GridHeader( TRACKED_ENTITY_INSTANCE_ID, "Instance" ) );
@@ -172,8 +178,36 @@
         return grid;
     }
     
+    public void validate( TrackedEntityInstanceQueryParams params )
+        throws IllegalQueryException
+    {
+        String violation = null;
+        
+        if ( params == null )
+        {
+            throw new IllegalQueryException( "Params cannot be null" );
+        }
+        
+        if ( params.hasQuery() && params.hasItems() )
+        {
+            violation = "Query and items cannot be specified simultaneously";
+        }
+        
+        if ( params.hasProgram() && params.hasTrackedEntity() )
+        {
+            violation = "Program and tracked entity cannot be specified simultaneously";
+        }
+
+        if ( violation != null )
+        {
+            log.warn( "Validation failed: " + violation );
+            
+            throw new IllegalQueryException( violation );
+        }
+    }
+    
     @Override
-    public TrackedEntityInstanceQueryParams getFromUrl( Set<String> items, Set<String> ou, String ouMode, 
+    public TrackedEntityInstanceQueryParams getFromUrl( String query, Set<String> items, Set<String> ou, String ouMode, 
         String program, String trackedEntity, Integer page, Integer pageSize )
     {
         TrackedEntityInstanceQueryParams params = new TrackedEntityInstanceQueryParams();
@@ -213,6 +247,7 @@
             throw new IllegalQueryException( "Tracked entity does not exist: " + program );
         }
         
+        params.setQuery( query );
         params.setProgram( pr );
         params.setTrackedEntity( te );
         params.setOrganisationUnitMode( ouMode );

=== 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 08:10:10 +0000
+++ 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
@@ -97,7 +97,8 @@
     
     @RequestMapping( method = RequestMethod.GET, produces = { ContextUtils.CONTENT_TYPE_JSON, ContextUtils.CONTENT_TYPE_JAVASCRIPT } )
     public String queryTrackedEntityInstancesJson(
-        @RequestParam Set<String> item,
+        @RequestParam(required=false) String query,
+        @RequestParam(required=false) Set<String> item,
         @RequestParam String ou,
         @RequestParam(required=false, defaultValue=DimensionalObject.OU_MODE_SELECTED) String ouMode,
         @RequestParam(required=false) String program,
@@ -108,7 +109,7 @@
         HttpServletResponse response ) throws Exception
     {
         Set<String> orgUnits = new HashSet<String>( ContextUtils.getQueryParamValues( ou ) );        
-        TrackedEntityInstanceQueryParams params = instanceService.getFromUrl( item, orgUnits, ouMode, program, trackedEntity, page, pageSize );
+        TrackedEntityInstanceQueryParams params = instanceService.getFromUrl( query, item, orgUnits, ouMode, program, trackedEntity, page, pageSize );
         
         contextUtils.configureResponse( response, ContextUtils.CONTENT_TYPE_JSON, CacheStrategy.NO_CACHE );
         Grid grid = instanceService.getTrackedEntityInstances( params );
@@ -120,7 +121,8 @@
 
     @RequestMapping( method = RequestMethod.GET, produces = ContextUtils.CONTENT_TYPE_EXCEL )
     public void queryTrackedEntityInstancesXls(
-        @RequestParam Set<String> item,
+        @RequestParam(required=false) String query,
+        @RequestParam(required=false) Set<String> item,
         @RequestParam String ou,
         @RequestParam(required=false, defaultValue=DimensionalObject.OU_MODE_SELECTED) String ouMode,
         @RequestParam(required=false) String program,
@@ -131,7 +133,7 @@
         HttpServletResponse response ) throws Exception
     {
         Set<String> orgUnits = new HashSet<String>( ContextUtils.getQueryParamValues( ou ) );        
-        TrackedEntityInstanceQueryParams params = instanceService.getFromUrl( item, orgUnits, ouMode, program, trackedEntity, page, pageSize );
+        TrackedEntityInstanceQueryParams params = instanceService.getFromUrl( query, item, orgUnits, ouMode, program, trackedEntity, page, pageSize );
         
         contextUtils.configureResponse( response, ContextUtils.CONTENT_TYPE_EXCEL, CacheStrategy.NO_CACHE );
         Grid grid = instanceService.getTrackedEntityInstances( params );
@@ -140,7 +142,8 @@
 
     @RequestMapping( method = RequestMethod.GET, produces = ContextUtils.CONTENT_TYPE_CSV )
     public void queryTrackedEntityInstancesCsv(
-        @RequestParam Set<String> item,
+        @RequestParam(required=false) String query,
+        @RequestParam(required=false) Set<String> item,
         @RequestParam String ou,
         @RequestParam(required=false, defaultValue=DimensionalObject.OU_MODE_SELECTED) String ouMode,
         @RequestParam(required=false) String program,
@@ -151,7 +154,7 @@
         HttpServletResponse response ) throws Exception
     {
         Set<String> orgUnits = new HashSet<String>( ContextUtils.getQueryParamValues( ou ) );        
-        TrackedEntityInstanceQueryParams params = instanceService.getFromUrl( item, orgUnits, ouMode, program, trackedEntity, page, pageSize );
+        TrackedEntityInstanceQueryParams params = instanceService.getFromUrl( query, item, orgUnits, ouMode, program, trackedEntity, page, pageSize );
         
         contextUtils.configureResponse( response, ContextUtils.CONTENT_TYPE_CSV, CacheStrategy.NO_CACHE );
         Grid grid = instanceService.getTrackedEntityInstances( params );