← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 21521: TEI query. Centralized handling of org units to make the grid and full response consistent.

 

------------------------------------------------------------
revno: 21521
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2015-12-23 13:56:31 +0100
message:
  TEI query. Centralized handling of org units to make the grid and full response consistent.
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramStageInstance.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceQueryParams.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/program/DefaultProgramInstanceService.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/trackedentity/DefaultTrackedEntityInstanceService.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/trackedentity/hibernate/HibernateTrackedEntityInstanceStore.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/program/ProgramStageInstance.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramStageInstance.java	2015-10-05 17:45:17 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramStageInstance.java	2015-12-23 12:56:31 +0000
@@ -94,33 +94,21 @@
     // Getters and setters
     // -------------------------------------------------------------------------
 
-    /**
-     * @return the programInstance
-     */
     public ProgramInstance getProgramInstance()
     {
         return programInstance;
     }
 
-    /**
-     * @param programInstance the programInstance to set
-     */
     public void setProgramInstance( ProgramInstance programInstance )
     {
         this.programInstance = programInstance;
     }
 
-    /**
-     * @return the programStage
-     */
     public ProgramStage getProgramStage()
     {
         return programStage;
     }
 
-    /**
-     * @param programStage the programStage to set
-     */
     public void setProgramStage( ProgramStage programStage )
     {
         this.programStage = programStage;
@@ -136,41 +124,26 @@
         this.completedUser = completedUser;
     }
 
-    /**
-     * @return the dueDate
-     */
     public Date getDueDate()
     {
         return dueDate;
     }
 
-    /**
-     * @param dueDate the dueDate to set
-     */
     public void setDueDate( Date dueDate )
     {
         this.dueDate = dueDate;
     }
 
-    /**
-     * @return the executionDate
-     */
     public Date getExecutionDate()
     {
         return executionDate;
     }
 
-    /**
-     * @param executionDate the executionDate to set
-     */
     public void setExecutionDate( Date executionDate )
     {
         this.executionDate = executionDate;
     }
 
-    /**
-     * @return the completed
-     */
     public boolean isCompleted()
     {
         return (status == EventStatus.COMPLETED) ? true : false;

=== 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	2015-12-01 16:14:18 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceQueryParams.java	2015-12-23 12:56:31 +0000
@@ -1,5 +1,7 @@
 package org.hisp.dhis.trackedentity;
 
+import static org.hisp.dhis.common.OrganisationUnitSelectionMode.CHILDREN;
+
 /*
  * Copyright (c) 2004-2015, University of Oslo
  * All rights reserved.
@@ -42,6 +44,7 @@
 import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.program.Program;
 import org.hisp.dhis.program.ProgramStatus;
+import org.hisp.dhis.user.User;
 
 /**
  * @author Lars Helge Overland
@@ -121,9 +124,9 @@
     private TrackedEntity trackedEntity;
     
     /**
-     * Selection mode for the specified organisation units.
+     * Selection mode for the specified organisation units, default is ACCESSIBLE.
      */
-    private OrganisationUnitSelectionMode organisationUnitMode;
+    private OrganisationUnitSelectionMode organisationUnitMode = OrganisationUnitSelectionMode.ACCESSIBLE;
     
     /**
      * Status of any events in the specified program.
@@ -164,6 +167,15 @@
      * Indicates whether paging should be skipped.
      */
     private boolean skipPaging;
+
+    // -------------------------------------------------------------------------
+    // Transient properties
+    // -------------------------------------------------------------------------
+
+    /**
+     * Current user for query.
+     */
+    private transient User user;
     
     // -------------------------------------------------------------------------
     // Constructors
@@ -236,6 +248,36 @@
             }
         }
     }
+
+    /**
+     * Prepares the organisation units of the given parameters to simplify querying.
+     * Mode ACCESSIBLE is converted to DESCENDANTS for organisation units linked
+     * to the given user, and mode CHILDREN is converted to CHILDREN for organisation
+     * units including all their children. Mode can be DESCENDANTS, SELECTED, ALL
+     * only after invoking this method.
+     * 
+     * @param user the user.
+     */
+    public void handleOrganisationUnits()
+    {
+        if ( user != null && isOrganisationUnitMode( OrganisationUnitSelectionMode.ACCESSIBLE ) )
+        {
+            setOrganisationUnits( user.getOrganisationUnits() );
+            setOrganisationUnitMode( OrganisationUnitSelectionMode.DESCENDANTS );
+        }
+        else if ( isOrganisationUnitMode( CHILDREN ) )
+        {
+            Set<OrganisationUnit> organisationUnits = new HashSet<>( getOrganisationUnits() );
+            
+            for ( OrganisationUnit organisationUnit : getOrganisationUnits() )
+            {
+                organisationUnits.addAll( organisationUnit.getChildren() );
+            }
+
+            setOrganisationUnits( organisationUnits );
+            setOrganisationUnitMode( OrganisationUnitSelectionMode.SELECTED );
+        }
+    }
     
     /**
      * Add the given attributes to this params if they are not already present.
@@ -714,4 +756,14 @@
     {
         this.skipPaging = skipPaging;
     }
+
+    public User getUser()
+    {
+        return user;
+    }
+
+    public void setUser( User user )
+    {
+        this.user = user;
+    }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/program/DefaultProgramInstanceService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/program/DefaultProgramInstanceService.java	2015-12-14 15:03:55 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/program/DefaultProgramInstanceService.java	2015-12-23 12:56:31 +0000
@@ -294,7 +294,7 @@
 
         User user = currentUserService.getCurrentUser();
 
-        if ( !params.hasOrganisationUnits() && !(params.isOrganisationUnitMode( ALL ) || params.isOrganisationUnitMode( ACCESSIBLE )) )
+        if ( !params.hasOrganisationUnits() && !( params.isOrganisationUnitMode( ALL ) || params.isOrganisationUnitMode( ACCESSIBLE ) ) )
         {
             violation = "At least one organisation unit must be specified";
         }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/trackedentity/DefaultTrackedEntityInstanceService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/trackedentity/DefaultTrackedEntityInstanceService.java	2015-11-23 01:05:22 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/trackedentity/DefaultTrackedEntityInstanceService.java	2015-12-23 12:56:31 +0000
@@ -121,30 +121,8 @@
         decideAccess( params );
         validate( params );
 
-        // ---------------------------------------------------------------------
-        // Verify params
-        // ---------------------------------------------------------------------
-
-        User user = currentUserService.getCurrentUser();
-
-        if ( user != null && params.isOrganisationUnitMode( OrganisationUnitSelectionMode.ACCESSIBLE ) )
-        {
-            params.setOrganisationUnits( user.getDataViewOrganisationUnitsWithFallback() );
-            params.setOrganisationUnitMode( OrganisationUnitSelectionMode.DESCENDANTS );
-        }
-        else if ( params.isOrganisationUnitMode( CHILDREN ) )
-        {
-            Set<OrganisationUnit> organisationUnits = new HashSet<>();
-            organisationUnits.addAll( params.getOrganisationUnits() );
-
-            for ( OrganisationUnit organisationUnit : params.getOrganisationUnits() )
-            {
-                organisationUnits.addAll( organisationUnit.getChildren() );
-            }
-
-            params.setOrganisationUnits( organisationUnits );
-        }
-
+        params.setUser( currentUserService.getCurrentUser() );
+        
         if ( !params.isPaging() && !params.isSkipPaging() )
         {
             params.setDefaultPaging();
@@ -160,23 +138,8 @@
         decideAccess( params );
         validate( params );
 
-        // ---------------------------------------------------------------------
-        // Verify params
-        // ---------------------------------------------------------------------
-
-        User user = currentUserService.getCurrentUser();
-
-        if ( user != null && params.isOrganisationUnitMode( OrganisationUnitSelectionMode.ACCESSIBLE ) )
-        {
-            params.setOrganisationUnits( user.getDataViewOrganisationUnitsWithFallback() );
-            params.setOrganisationUnitMode( OrganisationUnitSelectionMode.DESCENDANTS );
-        }
-
-        if ( !params.isPaging() && !params.isSkipPaging() )
-        {
-            params.setDefaultPaging();
-        }
-
+        params.setUser( currentUserService.getCurrentUser() );
+        
         // ---------------------------------------------------------------------
         // If params of type query and no attributes or filters defined, use
         // attributes from program if program is defined, if not, use 

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/trackedentity/hibernate/HibernateTrackedEntityInstanceStore.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/trackedentity/hibernate/HibernateTrackedEntityInstanceStore.java	2015-12-01 16:14:18 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/trackedentity/hibernate/HibernateTrackedEntityInstanceStore.java	2015-12-23 12:56:31 +0000
@@ -55,10 +55,8 @@
 
 import java.util.ArrayList;
 import java.util.HashMap;
-import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
-import java.util.Set;
 
 import static org.hisp.dhis.common.IdentifiableObjectUtils.getIdentifiers;
 import static org.hisp.dhis.common.IdentifiableObjectUtils.getUids;
@@ -97,6 +95,7 @@
     public int countTrackedEntityInstances( TrackedEntityInstanceQueryParams params )
     {
         String hql = buildTrackedEntityInstanceHql( params );
+        
         Query query = getQuery( hql );
 
         return ((Number) query.iterate().next()).intValue();
@@ -107,6 +106,7 @@
     public List<TrackedEntityInstance> getTrackedEntityInstances( TrackedEntityInstanceQueryParams params )
     {
         String hql = buildTrackedEntityInstanceHql( params );
+        
         Query query = getQuery( hql );
 
         if ( params.isPaging() )
@@ -130,9 +130,12 @@
 
         if ( params.hasOrganisationUnits() )
         {
+            params.handleOrganisationUnits();
+            
             if ( params.isOrganisationUnitMode( OrganisationUnitSelectionMode.DESCENDANTS ) )
             {
                 String ouClause = "(";
+                
                 SqlHelper orHlp = new SqlHelper( true );
 
                 for ( OrganisationUnit organisationUnit : params.getOrganisationUnits() )
@@ -159,6 +162,7 @@
                     String filter = queryFilter.getSqlFilter( StringUtils.lowerCase( queryFilter.getFilter() ) );
 
                     hql += hlp.whereAnd() + " exists (from TrackedEntityAttributeValue teav where teav.entityInstance=tei";
+                    
                     hql += " and teav.attribute.uid='" + queryItem.getItemId() + "'";
 
                     if ( queryItem.isNumeric() )
@@ -177,6 +181,7 @@
         if ( params.hasProgram() )
         {
             hql += hlp.whereAnd() + "exists (from ProgramInstance pi where pi.entityInstance=tei";
+            
             hql += " and pi.program.uid = '" + params.getProgram().getUid() + "'";
 
             if ( params.hasProgramStatus() )
@@ -346,6 +351,8 @@
             sql += hlp.whereAnd() + " tei.trackedentityid = " + params.getTrackedEntity().getId() + " ";
         }
 
+        params.handleOrganisationUnits();
+        
         if ( params.isOrganisationUnitMode( OrganisationUnitSelectionMode.ALL ) )
         {
             // No restriction
@@ -359,19 +366,6 @@
 
             sql = removeLastOr( sql );
         }
-        else if ( params.isOrganisationUnitMode( OrganisationUnitSelectionMode.CHILDREN ) )
-        {
-            Set<OrganisationUnit> orgUnits = new HashSet<>();
-
-            for ( OrganisationUnit orgUnit : params.getOrganisationUnits() )
-            {
-                orgUnits.add( orgUnit );
-                orgUnits.addAll( orgUnit.getChildren() );
-            }
-
-            sql += hlp.whereAnd() + " tei.organisationunitid in ("
-                + getCommaDelimitedString( getIdentifiers( orgUnits ) ) + ") ";
-        }
         else // SELECTED (default)        
         {
             sql += hlp.whereAnd() + " tei.organisationunitid in ("