← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 20054: Re-implemented org unit search. Added tests.

 

------------------------------------------------------------
revno: 20054
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2015-09-09 10:41:15 +0200
message:
  Re-implemented org unit search. Added tests.
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/BaseIdentifiableObject.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitService.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/DefaultOrganisationUnitService.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/hibernate/HibernateOrganisationUnitStore.java
  dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/organisationunit/OrganisationUnitStoreTest.java
  dhis-2/dhis-support/dhis-support-test/src/main/java/org/hisp/dhis/DhisConvenienceTest.java
  dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-organisationunit/src/main/java/org/hisp/dhis/oum/action/search/SearchOrganisationUnitsAction.java
  dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-organisationunit/src/main/webapp/dhis-web-maintenance-organisationunit/javascript/organisationUnitSearch.js
  dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-organisationunit/src/main/webapp/dhis-web-maintenance-organisationunit/organisationUnitSearch.vm


--
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/BaseIdentifiableObject.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/BaseIdentifiableObject.java	2015-07-14 07:21:33 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/BaseIdentifiableObject.java	2015-09-09 08:41:15 +0000
@@ -166,7 +166,7 @@
     @Override
     public int compareTo( IdentifiableObject object )
     {
-        return name == null ? (object.getName() == null ? 0 : -1) : name.compareTo( object.getName() );
+        return name == null ? ( object.getName() == null ? 0 : -1 ) : name.compareTo( object.getName() );
     }
 
     // -------------------------------------------------------------------------

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitService.java	2015-09-08 21:26:18 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitService.java	2015-09-09 08:41:15 +0000
@@ -152,9 +152,17 @@
      * Returns all OrganisationUnits with corresponding identifiers.
      *
      * @param uids the collection of uids.
-     * @return a collection of OrganisationUnits.
+     * @return a list of OrganisationUnits.
      */
     List<OrganisationUnit> getOrganisationUnitsByUid( Collection<String> uids );
+    
+    /**
+     * Returns a list of OrganisationUnits based on the given params.
+     * 
+     * @param params the params.
+     * @return a list of OrganisationUnits.
+     */
+    List<OrganisationUnit> getOrganisationUnitsByQuery( OrganisationUnitQueryParams params );
 
     /**
      * Returns an OrganisationUnit with a given name.

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/DefaultOrganisationUnitService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/DefaultOrganisationUnitService.java	2015-09-08 21:26:18 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/DefaultOrganisationUnitService.java	2015-09-09 08:41:15 +0000
@@ -217,6 +217,12 @@
     }
 
     @Override
+    public List<OrganisationUnit> getOrganisationUnitsByQuery( OrganisationUnitQueryParams params )
+    {
+        return organisationUnitStore.getOrganisationUnits( params );
+    }
+
+    @Override
     public OrganisationUnit getOrganisationUnit( String uid )
     {
         return i18n( i18nService, organisationUnitStore.getByUid( uid ) );

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/hibernate/HibernateOrganisationUnitStore.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/hibernate/HibernateOrganisationUnitStore.java	2015-09-08 21:26:18 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/hibernate/HibernateOrganisationUnitStore.java	2015-09-09 08:41:15 +0000
@@ -28,8 +28,6 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import static org.hisp.dhis.common.IdentifiableObjectUtils.getIdentifiers;
-
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.sql.Timestamp;
@@ -161,21 +159,19 @@
     {
         SqlHelper hlp = new SqlHelper();
         
-        String hql = "select distinct o from OrganisationUnit o ";
+        String hql = "select o from OrganisationUnit o ";
         
-        if ( params.hasGroups() )
-        {
-            hql += "left join o.groups g ";
-        }
-
         if ( params.getQuery() != null )
         {
-            hql += hlp.whereAnd() + " (lower(o.name) like :expression or o.code = :query or o.uid = :query)" ;
+            hql += hlp.whereAnd() + " (lower(o.name) like :queryLower or o.code = :query or o.uid = :query)" ;
         }
 
         if ( params.hasGroups() )
         {
-            hql += hlp.whereAnd() + " g.id in (:groupIds) ";
+            for ( OrganisationUnitGroup group : params.getGroups() )
+            {
+                hql += hlp.whereAnd() + " :" + group.getUid() + " in elements(o.groups) ";
+            }
         }
         
         if ( params.hasParents() )
@@ -184,7 +180,7 @@
             
             for ( OrganisationUnit parent : params.getParents() )
             {
-                hql += "o.parent like :" + parent.getUid() + " or ";
+                hql += "o.path like :" + parent.getUid() + " or ";
             }
             
             hql = TextUtils.removeLastOr( hql ) + ")";
@@ -194,13 +190,16 @@
         
         if ( params.getQuery() != null )
         {
-            query.setString( "expression", "%" + params.getQuery().toLowerCase() + "%" );
+            query.setString( "queryLower", "%" + params.getQuery().toLowerCase() + "%" );
             query.setString( "query", params.getQuery() );
         }
         
         if ( params.hasGroups() )
         {
-            query.setParameterList( "groupIds", getIdentifiers( params.getGroups() ) );
+            for ( OrganisationUnitGroup group : params.getGroups() )
+            {
+                query.setEntity( group.getUid(), group );
+            }
         }
         
         if ( params.hasParents() )

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/organisationunit/OrganisationUnitStoreTest.java'
--- dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/organisationunit/OrganisationUnitStoreTest.java	2015-06-16 05:11:29 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/organisationunit/OrganisationUnitStoreTest.java	2015-09-09 08:41:15 +0000
@@ -39,6 +39,8 @@
 import org.junit.Test;
 import org.springframework.beans.factory.annotation.Autowired;
 
+import com.google.common.collect.Sets;
+
 /**
  * @author Lars Helge Overland
  * @version $Id$
@@ -47,8 +49,87 @@
     extends DhisSpringTest
 {
     @Autowired
-    private OrganisationUnitLevelStore organisationUnitLevelStore;
-
+    private OrganisationUnitLevelStore orgUnitLevelStore;
+    
+    @Autowired
+    private OrganisationUnitStore orgUnitStore;
+    
+    @Autowired
+    private OrganisationUnitGroupStore orgUnitGroupStore;
+
+    // -------------------------------------------------------------------------
+    // OrganisationUnitLevel
+    // -------------------------------------------------------------------------
+
+    @Test
+    public void testGetOrganisationUnits()
+    {
+        OrganisationUnit ouA = createOrganisationUnit( 'A' );
+        OrganisationUnit ouB = createOrganisationUnit( 'B', ouA );
+        OrganisationUnit ouC = createOrganisationUnit( 'C', ouA );
+        OrganisationUnit ouD = createOrganisationUnit( 'D', ouB );
+        OrganisationUnit ouE = createOrganisationUnit( 'E', ouB );
+        OrganisationUnit ouF = createOrganisationUnit( 'F', ouC );
+        OrganisationUnit ouG = createOrganisationUnit( 'G', ouC );
+        
+        orgUnitStore.save( ouA );
+        orgUnitStore.save( ouB );
+        orgUnitStore.save( ouC );
+        orgUnitStore.save( ouD );
+        orgUnitStore.save( ouE );
+        orgUnitStore.save( ouF );
+        orgUnitStore.save( ouG );
+        
+        OrganisationUnitGroup ogA = createOrganisationUnitGroup( 'A' );
+        ogA.getMembers().addAll( Sets.newHashSet( ouD, ouF ) );
+        OrganisationUnitGroup ogB = createOrganisationUnitGroup( 'B' );
+        ogB.getMembers().addAll( Sets.newHashSet( ouE, ouG ) );
+        
+        orgUnitGroupStore.save( ogA );
+        orgUnitGroupStore.save( ogB );
+        
+        OrganisationUnitQueryParams params = new OrganisationUnitQueryParams();
+        params.setQuery( "UnitC" );
+        
+        List<OrganisationUnit> ous = orgUnitStore.getOrganisationUnits( params );
+
+        assertEquals( 1, ous.size() );
+        assertTrue( ous.contains( ouC ) );
+        
+        params = new OrganisationUnitQueryParams();
+        params.setQuery( "OrganisationUnitCodeA" );
+        
+        ous = orgUnitStore.getOrganisationUnits( params );
+        
+        assertTrue( ous.contains( ouA ) );
+        assertEquals( 1, ous.size() );
+
+        params = new OrganisationUnitQueryParams();
+        params.setParents( Sets.newHashSet( ouC, ouF ) );
+
+        ous = orgUnitStore.getOrganisationUnits( params );
+
+        assertEquals( 3, ous.size() );
+        assertTrue( ous.containsAll( Sets.newHashSet( ouC, ouF, ouG ) ) );
+
+        params = new OrganisationUnitQueryParams();
+        params.setGroups( Sets.newHashSet( ogA ) );
+
+        ous = orgUnitStore.getOrganisationUnits( params );
+
+        assertEquals( 2, ous.size() );
+        assertTrue( ous.containsAll( Sets.newHashSet( ouD, ouF ) ) );
+
+        params = new OrganisationUnitQueryParams();
+        params.setParents( Sets.newHashSet( ouC ) );        
+        params.setGroups( Sets.newHashSet( ogB ) );
+
+        ous = orgUnitStore.getOrganisationUnits( params );
+
+        assertEquals( 1, ous.size() );
+        assertTrue( ous.containsAll( Sets.newHashSet( ouG ) ) );        
+    }
+    
     // -------------------------------------------------------------------------
     // OrganisationUnitLevel
     // -------------------------------------------------------------------------
@@ -59,11 +140,11 @@
         OrganisationUnitLevel levelA = new OrganisationUnitLevel( 1, "National" );
         OrganisationUnitLevel levelB = new OrganisationUnitLevel( 2, "District" );
 
-        int idA = organisationUnitLevelStore.save( levelA );
-        int idB = organisationUnitLevelStore.save( levelB );
+        int idA = orgUnitLevelStore.save( levelA );
+        int idB = orgUnitLevelStore.save( levelB );
 
-        assertEquals( levelA, organisationUnitLevelStore.get( idA ) );
-        assertEquals( levelB, organisationUnitLevelStore.get( idB ) );
+        assertEquals( levelA, orgUnitLevelStore.get( idA ) );
+        assertEquals( levelB, orgUnitLevelStore.get( idB ) );
     }
 
     @Test
@@ -72,10 +153,10 @@
         OrganisationUnitLevel levelA = new OrganisationUnitLevel( 1, "National" );
         OrganisationUnitLevel levelB = new OrganisationUnitLevel( 2, "District" );
 
-        organisationUnitLevelStore.save( levelA );
-        organisationUnitLevelStore.save( levelB );
+        orgUnitLevelStore.save( levelA );
+        orgUnitLevelStore.save( levelB );
 
-        List<OrganisationUnitLevel> actual = organisationUnitLevelStore.getAll();
+        List<OrganisationUnitLevel> actual = orgUnitLevelStore.getAll();
 
         assertNotNull( actual );
         assertEquals( 2, actual.size() );
@@ -89,20 +170,20 @@
         OrganisationUnitLevel levelA = new OrganisationUnitLevel( 1, "National" );
         OrganisationUnitLevel levelB = new OrganisationUnitLevel( 2, "District" );
 
-        int idA = organisationUnitLevelStore.save( levelA );
-        int idB = organisationUnitLevelStore.save( levelB );
-
-        assertNotNull( organisationUnitLevelStore.get( idA ) );
-        assertNotNull( organisationUnitLevelStore.get( idB ) );
-
-        organisationUnitLevelStore.delete( levelA );
-
-        assertNull( organisationUnitLevelStore.get( idA ) );
-        assertNotNull( organisationUnitLevelStore.get( idB ) );
-
-        organisationUnitLevelStore.delete( levelB );
-
-        assertNull( organisationUnitLevelStore.get( idA ) );
-        assertNull( organisationUnitLevelStore.get( idB ) );
+        int idA = orgUnitLevelStore.save( levelA );
+        int idB = orgUnitLevelStore.save( levelB );
+
+        assertNotNull( orgUnitLevelStore.get( idA ) );
+        assertNotNull( orgUnitLevelStore.get( idB ) );
+
+        orgUnitLevelStore.delete( levelA );
+
+        assertNull( orgUnitLevelStore.get( idA ) );
+        assertNotNull( orgUnitLevelStore.get( idB ) );
+
+        orgUnitLevelStore.delete( levelB );
+
+        assertNull( orgUnitLevelStore.get( idA ) );
+        assertNull( orgUnitLevelStore.get( idB ) );
     }
 }
\ No newline at end of file

=== modified file 'dhis-2/dhis-support/dhis-support-test/src/main/java/org/hisp/dhis/DhisConvenienceTest.java'
--- dhis-2/dhis-support/dhis-support-test/src/main/java/org/hisp/dhis/DhisConvenienceTest.java	2015-09-04 10:49:08 +0000
+++ dhis-2/dhis-support/dhis-support-test/src/main/java/org/hisp/dhis/DhisConvenienceTest.java	2015-09-09 08:41:15 +0000
@@ -132,7 +132,7 @@
 {
     protected static final Log log = LogFactory.getLog( DhisConvenienceTest.class );
 
-    protected static final String BASE_UID = "123456789a";
+    protected static final String BASE_UID = "abcdefghij";
 
     protected static final String BASE_IN_UID = "inabcdefgh";
 

=== modified file 'dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-organisationunit/src/main/java/org/hisp/dhis/oum/action/search/SearchOrganisationUnitsAction.java'
--- dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-organisationunit/src/main/java/org/hisp/dhis/oum/action/search/SearchOrganisationUnitsAction.java	2015-02-19 09:18:17 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-organisationunit/src/main/java/org/hisp/dhis/oum/action/search/SearchOrganisationUnitsAction.java	2015-09-09 08:41:15 +0000
@@ -28,26 +28,27 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.HashSet;
 import java.util.List;
+import java.util.Set;
 
 import org.apache.commons.lang3.StringUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.hisp.dhis.common.Grid;
 import org.hisp.dhis.common.GridHeader;
-import org.hisp.dhis.common.comparator.IdentifiableObjectNameComparator;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.organisationunit.OrganisationUnitGroup;
 import org.hisp.dhis.organisationunit.OrganisationUnitGroupService;
 import org.hisp.dhis.organisationunit.OrganisationUnitGroupSet;
+import org.hisp.dhis.organisationunit.OrganisationUnitQueryParams;
 import org.hisp.dhis.organisationunit.OrganisationUnitService;
 import org.hisp.dhis.ouwt.manager.OrganisationUnitSelectionManager;
 import org.hisp.dhis.system.grid.ListGrid;
 
+import com.google.common.collect.Sets;
 import com.opensymphony.xwork2.Action;
 
 /**
@@ -62,7 +63,7 @@
     private static final String DEFAULT_TYPE = "html";
     
     // -------------------------------------------------------------------------
-    // Depdencies
+    // Dependencies
     // -------------------------------------------------------------------------
 
     private OrganisationUnitGroupService organisationUnitGroupService;
@@ -145,14 +146,7 @@
     {
         return selectedOrganisationUnit;
     }
-    
-    boolean limited = false;
-
-    public boolean isLimited()
-    {
-        return limited;
-    }
-    
+        
     private String type;
     
     public void setType( String type )
@@ -177,33 +171,17 @@
     {
         type = StringUtils.trimToNull( type );
         
-        // ---------------------------------------------------------------------
-        // Get group sets
-        // ---------------------------------------------------------------------
-
-        groupSets = new ArrayList<>( organisationUnitGroupService.getCompulsoryOrganisationUnitGroupSets() );
-        
-        Collections.sort( groupSets, IdentifiableObjectNameComparator.INSTANCE );
-        
-        // ---------------------------------------------------------------------
-        // Assemble groups and get search result
-        // ---------------------------------------------------------------------
-
+        groupSets = organisationUnitGroupService.getCompulsoryOrganisationUnitGroupSets();
+        
+        Collections.sort( groupSets );
+        
         if ( !skipSearch )
         {
             name = StringUtils.trimToNull( name );
             
             selectedOrganisationUnit = selectionManager.getSelectedOrganisationUnit();
 
-            log.debug( "Name: " + name + ", Orgunit: " + selectedOrganisationUnit + ", type: " + type );
-
-            // -----------------------------------------------------------------
-            // Set orgunit to null if root to avoid subquery and improve perf
-            // -----------------------------------------------------------------
-
-            selectedOrganisationUnit = selectedOrganisationUnit != null && selectedOrganisationUnit.getParent() == null ? null : selectedOrganisationUnit;
-            
-            Collection<OrganisationUnitGroup> groups = new HashSet<>();
+            Set<OrganisationUnitGroup> groups = new HashSet<>();
             
             for ( Integer id : groupId )
             {
@@ -214,13 +192,21 @@
                 }
             }
             
-            boolean limit = type == null; // Only limit for HTML view since browser is memory constrained
-            
-            organisationUnits = new ArrayList<>( organisationUnitService.getOrganisationUnitsByNameAndGroups( name, groups, selectedOrganisationUnit, limit ) );
-            
-            limited = organisationUnits != null && organisationUnits.size() == OrganisationUnitService.MAX_LIMIT;
-            
-            Collections.sort( organisationUnits, IdentifiableObjectNameComparator.INSTANCE );
+            OrganisationUnitQueryParams params = new OrganisationUnitQueryParams();
+            params.setQuery( name );
+            params.setGroups( groups );
+            params.setMax( 500 );
+                        
+            if ( selectedOrganisationUnit != null )
+            {
+                params.setParents( Sets.newHashSet( selectedOrganisationUnit ) );
+            }
+            
+            log.debug( "Org unit query params: " + params );
+            
+            organisationUnits = organisationUnitService.getOrganisationUnitsByQuery( params );
+            
+            Collections.sort( organisationUnits );
             
             if ( type != null && !type.equalsIgnoreCase( DEFAULT_TYPE ) )
             {

=== modified file 'dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-organisationunit/src/main/webapp/dhis-web-maintenance-organisationunit/javascript/organisationUnitSearch.js'
--- dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-organisationunit/src/main/webapp/dhis-web-maintenance-organisationunit/javascript/organisationUnitSearch.js	2011-09-11 09:37:07 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-organisationunit/src/main/webapp/dhis-web-maintenance-organisationunit/javascript/organisationUnitSearch.js	2015-09-09 08:41:15 +0000
@@ -41,15 +41,6 @@
 
 function download( type )
 {
-    if ( type != null && type != "" )
-    {
-        setHeaderWaitDelayMessage( i18n_please_wait_while_downloading );
-    }
-    else
-    {
-        setWaitMessage( i18n_please_wait_while_searching );
-    }
-
     $( "#type" ).val( type );
 
     document.getElementById( "searchForm" ).submit();

=== modified file 'dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-organisationunit/src/main/webapp/dhis-web-maintenance-organisationunit/organisationUnitSearch.vm'
--- dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-organisationunit/src/main/webapp/dhis-web-maintenance-organisationunit/organisationUnitSearch.vm	2012-10-31 17:24:33 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-organisationunit/src/main/webapp/dhis-web-maintenance-organisationunit/organisationUnitSearch.vm	2015-09-09 08:41:15 +0000
@@ -75,7 +75,7 @@
 
 #if ( $organisationUnits )
 
-<h4>$i18n.getString( "found" ) $organisationUnits.size() $i18n.getString( "organisation_units" ) #if( $limited )($i18n.getString( "limited" ))#end</h4>
+<h4>$i18n.getString( "found" ) $organisationUnits.size() $i18n.getString( "organisation_units" ) (max 500)</h4>
 
 <table class="listTable" style="width:95%">
 	<thead>