← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 14238: WIP trackedentityinstance query. Removed old query solution.

 

------------------------------------------------------------
revno: 14238
committer: Lars Helge Øverland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2014-03-17 12:30:30 +0100
message:
  WIP trackedentityinstance query. Removed old query solution.
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceService.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-dxf2/src/main/java/org/hisp/dhis/dxf2/events/trackedentity/TrackedEntityInstanceService.java
  dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/events/TrackedEntityInstanceServiceTest.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/resources/META-INF/dhis/beans.xml
  dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/MimicingHashMap.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/TrackedEntityInstanceService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceService.java	2014-03-17 10:04:44 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceService.java	2014-03-17 11:30:30 +0000
@@ -56,7 +56,7 @@
     Grid getTrackedEntityInstances( TrackedEntityInstanceQueryParams params );
     
     TrackedEntityInstanceQueryParams getFromUrl( Set<String> items, String program, String trackedEntity, 
-        Set<String> organisationUnits, String ouMode, Integer page, Integer pageSize );
+        Set<String> ou, String ouMode, Integer page, Integer pageSize );
     
     /**
      * Adds an {@link TrackedEntityInstance}

=== 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-03-17 08:22:01 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/trackedentity/AbstractTrackedEntityInstanceService.java	2014-03-17 11:30:30 +0000
@@ -28,12 +28,17 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
 import org.hisp.dhis.common.IdentifiableObjectManager;
 import org.hisp.dhis.dxf2.importsummary.ImportConflict;
 import org.hisp.dhis.dxf2.importsummary.ImportStatus;
 import org.hisp.dhis.dxf2.importsummary.ImportSummary;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
-import org.hisp.dhis.program.Program;
 import org.hisp.dhis.relationship.Relationship;
 import org.hisp.dhis.relationship.RelationshipService;
 import org.hisp.dhis.relationship.RelationshipType;
@@ -43,12 +48,6 @@
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.util.Assert;
 
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Set;
-
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
  */
@@ -76,51 +75,6 @@
     // -------------------------------------------------------------------------
 
     @Override
-    public TrackedEntityInstances getTrackedEntityInstances()
-    {
-        List<org.hisp.dhis.trackedentity.TrackedEntityInstance> entityInstances = new ArrayList<org.hisp.dhis.trackedentity.TrackedEntityInstance>(
-            entityInstanceService.getAllTrackedEntityInstances() );
-        return getTrackedEntityInstances( entityInstances );
-    }
-
-    @Override
-    public TrackedEntityInstances getTrackedEntityInstances( OrganisationUnit organisationUnit )
-    {
-        List<org.hisp.dhis.trackedentity.TrackedEntityInstance> entityInstances = new ArrayList<org.hisp.dhis.trackedentity.TrackedEntityInstance>(
-            entityInstanceService.getTrackedEntityInstances( organisationUnit, null, null ) );
-        return getTrackedEntityInstances( entityInstances );
-    }
-
-    @Override
-    public TrackedEntityInstances getTrackedEntityInstances( Program program )
-    {
-        List<org.hisp.dhis.trackedentity.TrackedEntityInstance> entityInstances = new ArrayList<org.hisp.dhis.trackedentity.TrackedEntityInstance>(
-            entityInstanceService.getTrackedEntityInstances( program ) );
-        return getTrackedEntityInstances( entityInstances );
-    }
-
-    @Override
-    public TrackedEntityInstances getTrackedEntityInstances( OrganisationUnit organisationUnit, Program program )
-    {
-        List<org.hisp.dhis.trackedentity.TrackedEntityInstance> entityInstances = new ArrayList<org.hisp.dhis.trackedentity.TrackedEntityInstance>(
-            entityInstanceService.getTrackedEntityInstances( organisationUnit, program ) );
-        return getTrackedEntityInstances( entityInstances );
-    }
-
-    @Override
-    public TrackedEntityInstances getTrackedEntityInstances( Collection<org.hisp.dhis.trackedentity.TrackedEntityInstance> entityInstances )
-    {
-        TrackedEntityInstances trackedEntityInstances = new TrackedEntityInstances();
-
-        for ( org.hisp.dhis.trackedentity.TrackedEntityInstance entityInstance : entityInstances )
-        {
-            trackedEntityInstances.getTrackedEntityInstances().add( getTrackedEntityInstance( entityInstance ) );
-        }
-
-        return trackedEntityInstances;
-    }
-
-    @Override
     public TrackedEntityInstance getTrackedEntityInstance( String uid )
     {
         return getTrackedEntityInstance( entityInstanceService.getTrackedEntityInstance( uid ) );

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/trackedentity/TrackedEntityInstanceService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/trackedentity/TrackedEntityInstanceService.java	2014-03-17 08:22:01 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/trackedentity/TrackedEntityInstanceService.java	2014-03-17 11:30:30 +0000
@@ -30,12 +30,9 @@
 
 import java.io.IOException;
 import java.io.InputStream;
-import java.util.Collection;
 
 import org.hisp.dhis.dxf2.importsummary.ImportSummaries;
 import org.hisp.dhis.dxf2.importsummary.ImportSummary;
-import org.hisp.dhis.organisationunit.OrganisationUnit;
-import org.hisp.dhis.program.Program;
 
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
@@ -46,16 +43,6 @@
     // READ
     // -------------------------------------------------------------------------
 
-    TrackedEntityInstances getTrackedEntityInstances();
-
-    TrackedEntityInstances getTrackedEntityInstances( OrganisationUnit organisationUnit );
-
-    TrackedEntityInstances getTrackedEntityInstances( Program program );
-
-    TrackedEntityInstances getTrackedEntityInstances( OrganisationUnit organisationUnit, Program program );
-
-    TrackedEntityInstances getTrackedEntityInstances( Collection<org.hisp.dhis.trackedentity.TrackedEntityInstance> entityInstances );
-
     TrackedEntityInstance getTrackedEntityInstance( String uid );
 
     TrackedEntityInstance getTrackedEntityInstance( org.hisp.dhis.trackedentity.TrackedEntityInstance entityInstance );

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/events/TrackedEntityInstanceServiceTest.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/events/TrackedEntityInstanceServiceTest.java	2014-03-17 08:22:01 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/events/TrackedEntityInstanceServiceTest.java	2014-03-17 11:30:30 +0000
@@ -28,6 +28,13 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+
+import java.util.HashSet;
+
 import org.hisp.dhis.DhisTest;
 import org.hisp.dhis.common.IdentifiableObjectManager;
 import org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance;
@@ -42,12 +49,6 @@
 import org.junit.Test;
 import org.springframework.beans.factory.annotation.Autowired;
 
-import java.util.Arrays;
-import java.util.HashSet;
-import java.util.List;
-
-import static org.junit.Assert.*;
-
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
  */
@@ -106,26 +107,6 @@
     }
 
     @Test
-    public void testGetPersons()
-    {
-        assertEquals( 4, trackedEntityInstanceService.getTrackedEntityInstances().getTrackedEntityInstances().size() );
-    }
-
-    @Test
-    public void testGetPersonByOrganisationUnit()
-    {
-        assertEquals( 2, trackedEntityInstanceService.getTrackedEntityInstances( organisationUnitA ).getTrackedEntityInstances().size() );
-        assertEquals( 2, trackedEntityInstanceService.getTrackedEntityInstances( organisationUnitB ).getTrackedEntityInstances().size() );
-    }
-
-    @Test
-    public void getPersonByPatients()
-    {
-        List<org.hisp.dhis.trackedentity.TrackedEntityInstance> patients = Arrays.asList( maleA, femaleB );
-        assertEquals( 2, trackedEntityInstanceService.getTrackedEntityInstances( patients ).getTrackedEntityInstances().size() );
-    }
-
-    @Test
     public void getPersonByUid()
     {
         assertEquals( maleA.getUid(), trackedEntityInstanceService.getTrackedEntityInstance( maleA.getUid() ).getTrackedEntityInstance() );
@@ -144,12 +125,6 @@
     }
 
     @Test
-    public void testGetPersonByProgram()
-    {
-        assertEquals( 2, trackedEntityInstanceService.getTrackedEntityInstances( programA ).getTrackedEntityInstances().size() );
-    }
-
-    @Test
     @Ignore
     public void testUpdatePerson()
     {

=== 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-17 10:04:44 +0000
+++ dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentity/DefaultTrackedEntityInstanceService.java	2014-03-17 11:30:30 +0000
@@ -43,6 +43,7 @@
 import org.hisp.dhis.i18n.I18n;
 import org.hisp.dhis.i18n.I18nFormat;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
+import org.hisp.dhis.organisationunit.OrganisationUnitService;
 import org.hisp.dhis.program.Program;
 import org.hisp.dhis.program.ProgramService;
 import org.hisp.dhis.relationship.Relationship;
@@ -86,6 +87,13 @@
     {
         this.attributeService = attributeService;
     }
+    
+    private TrackedEntityService trackedEntityService;
+
+    public void setTrackedEntityService( TrackedEntityService trackedEntityService )
+    {
+        this.trackedEntityService = trackedEntityService;
+    }
 
     private RelationshipService relationshipService;
 
@@ -107,27 +115,61 @@
     {
         this.programService = programService;
     }
+    
+    private OrganisationUnitService organisationUnitService;
+
+    public void setOrganisationUnitService( OrganisationUnitService organisationUnitService )
+    {
+        this.organisationUnitService = organisationUnitService;
+    }
 
     // -------------------------------------------------------------------------
     // Implementation methods
     // -------------------------------------------------------------------------
 
+    @Override
     public Grid getTrackedEntityInstances( TrackedEntityInstanceQueryParams params )
     {
         return trackedEntityInstanceStore.getTrackedEntityInstances( params );
     }
     
+    @Override
     public TrackedEntityInstanceQueryParams getFromUrl( Set<String> items, String program, String trackedEntity, 
-        Set<String> organisationUnits, String ouMode, Integer page, Integer pageSize )
+        Set<String> ou, String ouMode, Integer page, Integer pageSize )
     {
         TrackedEntityInstanceQueryParams params = new TrackedEntityInstanceQueryParams();
 
-        Program pr = programService.getProgram( program );
+        Program pr = program != null ? programService.getProgram( program ) : null;
 
-        if ( pr == null )
-        {
-            throw new IllegalQueryException( "Program does not exist: " + program );
-        }
+        if ( program != null && pr == null )
+        {
+            throw new IllegalQueryException( "Program does not exist: " + program );
+        }
+        
+        TrackedEntity te = trackedEntity != null ? trackedEntityService.getTrackedEntity( trackedEntity ) : null;
+        
+        if ( te == null )
+        {
+            throw new IllegalQueryException( "Program does not exist: " + program );
+        }
+        
+        Set<OrganisationUnit> ous = new HashSet<OrganisationUnit>();
+        
+        for ( String orgUnit : ou )
+        {
+            OrganisationUnit organisationUnit = organisationUnitService.getOrganisationUnit( orgUnit );
+            
+            if ( organisationUnit == null )
+            {
+                throw new IllegalQueryException( "Organisation unit does not exist: " + orgUnit );
+            }
+            
+            ous.add( organisationUnit );
+        }
+        
+        params.setOrganisationUnitMode( ouMode );
+        params.setPage( page );
+        params.setPageSize( pageSize );
         
         return params;
     }

=== modified file 'dhis-2/dhis-services/dhis-service-tracker/src/main/resources/META-INF/dhis/beans.xml'
--- dhis-2/dhis-services/dhis-service-tracker/src/main/resources/META-INF/dhis/beans.xml	2014-03-17 10:04:44 +0000
+++ dhis-2/dhis-services/dhis-service-tracker/src/main/resources/META-INF/dhis/beans.xml	2014-03-17 11:30:30 +0000
@@ -242,12 +242,16 @@
 			ref="org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValueService" />
 		<property name="attributeService"
 			ref="org.hisp.dhis.trackedentity.TrackedEntityAttributeService" />
+		<property name="trackedEntityService"
+		    ref="org.hisp.dhis.trackedentity.TrackedEntityService" />
 		<property name="relationshipTypeService"
 			ref="org.hisp.dhis.relationship.RelationshipTypeService" />
 		<property name="relationshipService"
 			ref="org.hisp.dhis.relationship.RelationshipService" />
 	    <property name="programService"
 	        ref="org.hisp.dhis.program.ProgramService" />
+	    <property name="organisationUnitService"
+	        ref="org.hisp.dhis.organisationunit.OrganisationUnitService" />
 	</bean>
 
 	<bean id="org.hisp.dhis.trackedentity.TrackedEntityAttributeService"

=== modified file 'dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/MimicingHashMap.java'
--- dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/MimicingHashMap.java	2013-08-23 16:05:01 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/MimicingHashMap.java	2014-03-17 11:30:30 +0000
@@ -39,11 +39,6 @@
 public class MimicingHashMap<K, V>
     extends HashMap<K, V>
 {
-    /**
-     * Determines if a de-serialized file is compatible with this class.
-     */
-    private static final long serialVersionUID = -1314511873888181449L;
-
     @Override
     @SuppressWarnings( "unchecked" )
     public V get( Object key )

=== 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-17 08:46:22 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/event/TrackedEntityInstanceController.java	2014-03-17 11:30:30 +0000
@@ -29,44 +29,40 @@
  */
 
 import java.io.IOException;
-import java.util.List;
+import java.util.HashSet;
 import java.util.Map;
+import java.util.Set;
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
-import org.hibernate.Criteria;
 import org.hibernate.SessionFactory;
-import org.hibernate.criterion.Conjunction;
-import org.hibernate.criterion.Disjunction;
-import org.hibernate.criterion.Order;
-import org.hibernate.criterion.Restrictions;
 import org.hisp.dhis.api.controller.WebOptions;
 import org.hisp.dhis.api.controller.exception.NotFoundException;
 import org.hisp.dhis.api.utils.ContextUtils;
+import org.hisp.dhis.common.DimensionalObject;
+import org.hisp.dhis.common.Grid;
 import org.hisp.dhis.common.IdentifiableObjectManager;
+import org.hisp.dhis.common.IllegalQueryException;
 import org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance;
 import org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstanceService;
-import org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstances;
 import org.hisp.dhis.dxf2.importsummary.ImportStatus;
 import org.hisp.dhis.dxf2.importsummary.ImportSummaries;
 import org.hisp.dhis.dxf2.importsummary.ImportSummary;
 import org.hisp.dhis.dxf2.utils.JacksonUtils;
-import org.hisp.dhis.organisationunit.OrganisationUnit;
-import org.hisp.dhis.program.Program;
-import org.hisp.dhis.trackedentity.TrackedEntityAttribute;
+import org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.HttpStatus;
 import org.springframework.http.MediaType;
 import org.springframework.security.access.prepost.PreAuthorize;
 import org.springframework.stereotype.Controller;
 import org.springframework.ui.Model;
+import org.springframework.web.bind.annotation.ExceptionHandler;
 import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.ResponseStatus;
-import org.springframework.web.client.HttpClientErrorException;
 
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
@@ -80,6 +76,9 @@
 
     @Autowired
     private TrackedEntityInstanceService trackedEntityInstanceService;
+    
+    @Autowired
+    private org.hisp.dhis.trackedentity.TrackedEntityInstanceService instanceService;
 
     @Autowired
     private IdentifiableObjectManager manager;
@@ -90,150 +89,28 @@
     // -------------------------------------------------------------------------
     // READ
     // -------------------------------------------------------------------------
-
-    @RequestMapping( value = "", method = RequestMethod.GET )
-    @PreAuthorize( "hasRole('ALL') or hasRole('F_ACCESS_PATIENT_ATTRIBUTES')" )
-    public String getTrackedEntityInstances( @RequestParam(value = "orgUnit", required = false) String orgUnitUid,
-        @RequestParam(value = "program", required = false) String programUid,
-        @RequestParam(value = "attribute", required = false) List<String> attributeFilters,
-        @RequestParam(required = false) Map<String, String> parameters, Model model )
-        throws Exception
-    {
-        WebOptions options = new WebOptions( parameters );
-        TrackedEntityInstances trackedEntityInstances = new TrackedEntityInstances();
-
-        if ( attributeFilters != null )
-        {
-            trackedEntityInstances = trackedEntityInstancesByFilter( attributeFilters, orgUnitUid );
-        }
-        else if ( orgUnitUid != null )
-        {
-            if ( programUid != null )
-            {
-                OrganisationUnit organisationUnit = getOrganisationUnit( orgUnitUid );
-                Program program = getProgram( programUid );
-
-                trackedEntityInstances = trackedEntityInstanceService.getTrackedEntityInstances( organisationUnit, program );
-            }
-            else
-            {
-                OrganisationUnit organisationUnit = getOrganisationUnit( orgUnitUid );
-                trackedEntityInstances = trackedEntityInstanceService.getTrackedEntityInstances( organisationUnit );
-            }
-        }
-        else
-        {
-            throw new HttpClientErrorException( HttpStatus.BAD_REQUEST, "Missing required orgUnit parameter." );
-        }
-
-        model.addAttribute( "model", trackedEntityInstances );
-        model.addAttribute( "viewClass", options.getViewClass( "basic" ) );
-
-        return "trackedEntityInstances";
-    }
-
-    @SuppressWarnings( "unchecked" )
-    private TrackedEntityInstances trackedEntityInstancesByFilter( List<String> attributeFilters, String orgUnitUid )
-    {
-        Criteria criteria = sessionFactory.getCurrentSession().createCriteria( org.hisp.dhis.trackedentity.TrackedEntityInstance.class );
-        criteria.createAlias( "attributeValues", "attributeValue" );
-        criteria.createAlias( "attributeValue.attribute", "attribute" );
-
-        Disjunction or = Restrictions.or();
-        criteria.add( or );
-
-        if ( orgUnitUid != null )
-        {
-            OrganisationUnit organisationUnit = manager.get( OrganisationUnit.class, orgUnitUid );
-
-            if ( organisationUnit == null )
-            {
-                throw new HttpClientErrorException( HttpStatus.BAD_REQUEST, "OrganisationUnit with UID " + orgUnitUid + " does not exist." );
-            }
-
-            criteria.createAlias( "organisationUnit", "organisationUnit" );
-            criteria.add( Restrictions.eq( "organisationUnit.uid", orgUnitUid ) );
-        }
-
-        // validate attributes, and build criteria
-        for ( String filter : attributeFilters )
-        {
-            String[] split = filter.split( ":" );
-
-            Conjunction and = Restrictions.and();
-            or.add( and );
-
-            if ( split.length != 3 )
-            {
-                throw new HttpClientErrorException( HttpStatus.BAD_REQUEST, "Filter " + filter + " is not in valid format. " +
-                    "Valid syntax is attribute=ATTRIBUTE_UID:OPERATOR:VALUE." );
-            }
-
-            TrackedEntityAttribute attribute = manager.get( TrackedEntityAttribute.class, split[0] );
-
-            if ( attribute == null )
-            {
-                throw new HttpClientErrorException( HttpStatus.BAD_REQUEST, "TrackedEntityAttribute with UID " + split[0] + " does not exist." );
-            }
-
-            if ( "like".equals( split[1].toLowerCase() ) )
-            {
-                and.add( Restrictions.and(
-                    Restrictions.eq( "attribute.uid", split[0] ),
-                    Restrictions.ilike( "attributeValue.value", "%" + split[2] + "%" )
-                ) );
-            }
-            else if ( "eq".equals( split[1].toLowerCase() ) )
-            {
-                and.add( Restrictions.and(
-                    Restrictions.eq( "attribute.uid", split[0] ),
-                    Restrictions.eq( "attributeValue.value", split[2] )
-                ) );
-            }
-            else if ( "ne".equals( split[1].toLowerCase() ) )
-            {
-                and.add( Restrictions.and(
-                    Restrictions.eq( "attribute.uid", split[0] ),
-                    Restrictions.ne( "attributeValue.value", split[2] )
-                ) );
-            }
-            else if ( "gt".equals( split[1].toLowerCase() ) )
-            {
-                and.add( Restrictions.and(
-                    Restrictions.eq( "attribute.uid", split[0] ),
-                    Restrictions.gt( "attributeValue.value", split[2] )
-                ) );
-            }
-            else if ( "lt".equals( split[1].toLowerCase() ) )
-            {
-                and.add( Restrictions.and(
-                    Restrictions.eq( "attribute.uid", split[0] ),
-                    Restrictions.lt( "attributeValue.value", split[2] )
-                ) );
-            }
-            else if ( "ge".equals( split[1].toLowerCase() ) )
-            {
-                and.add( Restrictions.and(
-                    Restrictions.eq( "attribute.uid", split[0] ),
-                    Restrictions.ge( "attributeValue.value", split[2] )
-                ) );
-            }
-            else if ( "in".equals( split[1].toLowerCase() ) )
-            {
-                String[] in = split[2].split( ";" );
-
-                and.add( Restrictions.and(
-                    Restrictions.eq( "attribute.uid", split[0] ),
-                    Restrictions.in( "attributeValue.value", in )
-                ) );
-            }
-        }
-
-        criteria.addOrder( Order.desc( "lastUpdated" ) );
-
-        return trackedEntityInstanceService.getTrackedEntityInstances( criteria.list() );
-    }
-
+    
+    @RequestMapping( method = RequestMethod.GET, produces = { "application/json", "application/javascript" } )
+    public String queryTrackedEntityInstances( // JSON, JSONP
+        @RequestParam Set<String> items,
+        @RequestParam(required=false) String program,
+        @RequestParam(required=false) String trackedEntity,
+        @RequestParam String ou,
+        @RequestParam(required=false, defaultValue=DimensionalObject.OU_MODE_SELECTED) String ouMode,
+        @RequestParam(required=false) Integer page,
+        @RequestParam(required=false) Integer pageSize,
+        Model model,
+        HttpServletResponse response ) throws Exception
+    {
+        Set<String> orgUnits = new HashSet<String>( ContextUtils.getQueryParamValues( ou ) );        
+        TrackedEntityInstanceQueryParams params = instanceService.getFromUrl( items, program, trackedEntity, orgUnits, ouMode, page, pageSize );
+        Grid grid = instanceService.getTrackedEntityInstances( params );
+        
+        model.addAttribute( "model", grid );
+        model.addAttribute( "viewClass", "detailed" );
+        return "grid";
+    }
+    
     @RequestMapping( value = "/{id}", method = RequestMethod.GET )
     @PreAuthorize( "hasRole('ALL') or hasRole('F_ACCESS_PATIENT_ATTRIBUTES')" )
     public String getTrackedEntityInstance( @PathVariable String id, @RequestParam Map<String, String> parameters, Model model )
@@ -358,38 +235,18 @@
         return trackedEntityInstance;
     }
 
-    private Program getProgram( String id )
-        throws NotFoundException
-    {
-        Program program = manager.get( Program.class, id );
-
-        if ( program == null )
-        {
-            throw new NotFoundException( "TrackedEntityInstance", id );
-        }
-
-        return program;
-    }
-
     private String getResourcePath( HttpServletRequest request, ImportSummary importSummary )
     {
         return ContextUtils.getContextPath( request ) + "/api/" + "trackedEntityInstances" + "/" + importSummary.getReference();
     }
-
-    private OrganisationUnit getOrganisationUnit( String orgUnitUid )
+    
+    // -------------------------------------------------------------------------
+    // Exception handling
+    // -------------------------------------------------------------------------
+  
+    @ExceptionHandler(IllegalQueryException.class)
+    public void handleError( IllegalQueryException ex, HttpServletResponse response )
     {
-        if ( orgUnitUid == null )
-        {
-            return null;
-        }
-
-        OrganisationUnit organisationUnit = manager.get( OrganisationUnit.class, orgUnitUid );
-
-        if ( organisationUnit == null )
-        {
-            throw new HttpClientErrorException( HttpStatus.BAD_REQUEST, "orgUnit is not a valid uid." );
-        }
-
-        return organisationUnit;
+        ContextUtils.conflictResponse( response, ex.getMessage() );
     }
 }
\ No newline at end of file