← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 20051: WIP, improved org unit search, using new path property in query

 

------------------------------------------------------------
revno: 20051
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2015-09-08 23:20:48 +0200
message:
  WIP, improved org unit search, using new path property in query
added:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitQueryParams.java
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitStore.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/hibernate/HibernateOrganisationUnitStore.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
=== added file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitQueryParams.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitQueryParams.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitQueryParams.java	2015-09-08 21:20:48 +0000
@@ -0,0 +1,133 @@
+package org.hisp.dhis.organisationunit;
+
+/*
+ * Copyright (c) 2004-2015, University of Oslo
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ *
+ * Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * Neither the name of the HISP project nor the names of its contributors may
+ * be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+import java.util.HashSet;
+import java.util.Set;
+
+import com.google.common.base.MoreObjects;
+
+/**
+ * @author Lars Helge Overland
+ */
+public class OrganisationUnitQueryParams
+{
+    private String query;
+    
+    private Set<OrganisationUnit> parents = new HashSet<>();
+    
+    private Set<OrganisationUnitGroup> groups = new HashSet<>();
+
+    private Integer first;
+    
+    private Integer max;
+
+    public OrganisationUnitQueryParams()
+    {
+    }
+
+    // -------------------------------------------------------------------------
+    // Logic
+    // -------------------------------------------------------------------------
+
+    public boolean hasParents()
+    {
+        return parents != null && !parents.isEmpty();
+    }
+    
+    public boolean hasGroups()
+    {
+        return groups != null && !groups.isEmpty();
+    }
+    
+    @Override
+    public String toString()
+    {
+        return MoreObjects.toStringHelper( this ).
+            add( "query", query ).
+            add( "parents", parents ).
+            add( "groups", groups ).
+            add( "first", first ).
+            add( "max", max ).toString();
+    }
+    
+    // -------------------------------------------------------------------------
+    // Getters and setters
+    // -------------------------------------------------------------------------
+
+    public String getQuery()
+    {
+        return query;
+    }
+
+    public void setQuery( String query )
+    {
+        this.query = query;
+    }
+
+    public Set<OrganisationUnit> getParents()
+    {
+        return parents;
+    }
+
+    public void setParents( Set<OrganisationUnit> parents )
+    {
+        this.parents = parents;
+    }
+
+    public Set<OrganisationUnitGroup> getGroups()
+    {
+        return groups;
+    }
+
+    public void setGroups( Set<OrganisationUnitGroup> groups )
+    {
+        this.groups = groups;
+    }
+
+    public Integer getFirst()
+    {
+        return first;
+    }
+
+    public void setFirst( Integer first )
+    {
+        this.first = first;
+    }
+
+    public Integer getMax()
+    {
+        return max;
+    }
+
+    public void setMax( Integer max )
+    {
+        this.max = max;
+    }
+}

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitStore.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitStore.java	2015-09-08 20:58:02 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitStore.java	2015-09-08 21:20:48 +0000
@@ -126,6 +126,8 @@
      * @return all OrganisationUnits which are not a member of any OrganisationUnitGroups.
      */
     List<OrganisationUnit> getOrganisationUnitsWithoutGroups();
+    
+    List<OrganisationUnit> getOrganisationUnits( OrganisationUnitQueryParams params );
 
     /**
      * Returns all OrganisationUnit which names are like the given name, or which

=== 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 20:58:02 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/hibernate/HibernateOrganisationUnitStore.java	2015-09-08 21:20:48 +0000
@@ -28,6 +28,8 @@
  * 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;
@@ -49,10 +51,13 @@
 import org.hisp.dhis.common.AuditLogUtil;
 import org.hisp.dhis.common.SetMap;
 import org.hisp.dhis.common.hibernate.HibernateIdentifiableObjectStore;
+import org.hisp.dhis.commons.util.SqlHelper;
+import org.hisp.dhis.commons.util.TextUtils;
 import org.hisp.dhis.dbms.DbmsManager;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.organisationunit.OrganisationUnitGroup;
 import org.hisp.dhis.organisationunit.OrganisationUnitHierarchy;
+import org.hisp.dhis.organisationunit.OrganisationUnitQueryParams;
 import org.hisp.dhis.organisationunit.OrganisationUnitService;
 import org.hisp.dhis.organisationunit.OrganisationUnitStore;
 import org.hisp.dhis.system.objectmapper.OrganisationUnitRelationshipRowMapper;
@@ -169,6 +174,75 @@
 
     @Override
     @SuppressWarnings( "unchecked" )
+    public List<OrganisationUnit> getOrganisationUnits( OrganisationUnitQueryParams params )
+    {
+        SqlHelper hlp = new SqlHelper();
+        
+        String hql = "select distinct 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)" ;
+        }
+
+        if ( params.hasGroups() )
+        {
+            hql += hlp.whereAnd() + " g.id in (:groupIds) ";
+        }
+        
+        if ( params.hasParents() )
+        {
+            hql += hlp.whereAnd() + " (";
+            
+            for ( OrganisationUnit parent : params.getParents() )
+            {
+                hql += "o.parent like :" + parent.getUid() + " or ";
+            }
+            
+            hql = TextUtils.removeLastOr( hql ) + ")";
+        }
+        
+        Query query = getQuery( hql );
+        
+        if ( params.getQuery() != null )
+        {
+            query.setString( "expression", "%" + params.getQuery().toLowerCase() + "%" );
+            query.setString( "query", params.getQuery() );
+        }
+        
+        if ( params.hasGroups() )
+        {
+            query.setParameterList( "groupIds", getIdentifiers( params.getGroups() ) );
+        }
+        
+        if ( params.hasParents() )
+        {
+            for ( OrganisationUnit parent : params.getParents() )
+            {
+                query.setString( parent.getUid(), "%" + parent.getUid() + "%" );
+            }
+        }
+
+        if ( params.getFirst() != null )
+        {
+            query.setFirstResult( params.getFirst() );
+        }
+        
+        if ( params.getMax() != null )
+        {
+            query.setMaxResults( params.getMax() ).list();
+        }
+        
+        return query.list();
+    }
+
+    @Override
+    @SuppressWarnings( "unchecked" )
     public List<OrganisationUnit> getOrganisationUnitsByNameAndGroups( String query,
         Collection<OrganisationUnitGroup> groups, boolean limit )
     {