← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 20327: Organisation unit, introduced property hierarchyLevel with access='property'. Useful for database...

 

------------------------------------------------------------
revno: 20327
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2015-09-23 20:19:16 +0200
message:
  Organisation unit, introduced property hierarchyLevel with access='property'. Useful for database queries in order to easily get level of org unit.
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnit.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/main/resources/org/hisp/dhis/organisationunit/hibernate/OrganisationUnit.hbm.xml


--
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/organisationunit/OrganisationUnit.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnit.java	2015-09-17 04:29:29 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnit.java	2015-09-23 18:19:16 +0000
@@ -28,14 +28,21 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import com.fasterxml.jackson.annotation.JsonProperty;
-import com.fasterxml.jackson.annotation.JsonView;
-import com.fasterxml.jackson.databind.annotation.JsonSerialize;
-import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
-import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
-import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
-import com.google.common.base.Joiner;
-import com.google.common.collect.Sets;
+import static org.hisp.dhis.common.NameableObjectUtils.getDisplayProperty;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.UUID;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
 import org.apache.commons.lang3.StringUtils;
 import org.hisp.dhis.attribute.AttributeValue;
 import org.hisp.dhis.common.BaseIdentifiableObject;
@@ -57,20 +64,14 @@
 import org.hisp.dhis.schema.annotation.PropertyRange;
 import org.hisp.dhis.user.User;
 
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.Date;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-import java.util.UUID;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-
-import static org.hisp.dhis.common.NameableObjectUtils.getDisplayProperty;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.fasterxml.jackson.annotation.JsonView;
+import com.fasterxml.jackson.databind.annotation.JsonSerialize;
+import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
+import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
+import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
+import com.google.common.base.Joiner;
+import com.google.common.collect.Sets;
 
 /**
  * @author Kristian Nordal
@@ -102,6 +103,8 @@
 
     private String path;
 
+    private Integer hierarchyLevel;
+    
     private Date openingDate;
 
     private Date closedDate;
@@ -859,11 +862,42 @@
         return path;
     }
 
+    /**
+     * Do not set directly.
+     */
     public void setPath( String path )
     {
         this.path = path;
     }
 
+    /**
+     * Used by persistence layer. Purpose is to have a column for use in database 
+     * queries. For application use see {@link getLevel()} which has better performance.
+     */
+    public Integer getHierarchyLevel()
+    {
+        int count = 1;
+        
+        OrganisationUnit current = this;
+        
+        while ( ( current = current.getParent() ) != null )
+        {
+            count++;
+        }
+        
+        hierarchyLevel = count;
+        
+        return hierarchyLevel;
+    }
+
+    /**
+     * Do not set directly.
+     */
+    public void setHierarchyLevel( Integer hierarchyLevel )
+    {
+        this.hierarchyLevel = hierarchyLevel;
+    }
+
     @JsonProperty
     @JsonSerialize( contentUsing = JacksonOrganisationUnitChildrenSerializer.class )
     @JsonView( { DetailedView.class } )

=== 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-23 17:42:53 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/hibernate/HibernateOrganisationUnitStore.java	2015-09-23 18:19:16 +0000
@@ -327,7 +327,7 @@
     @SuppressWarnings( "unchecked" )
     public void updatePaths()
     {
-        List<OrganisationUnit> organisationUnits = new ArrayList<>( getQuery( "from OrganisationUnit ou where ou.path IS NULL" ).list() );
+        List<OrganisationUnit> organisationUnits = new ArrayList<>( getQuery( "from OrganisationUnit ou where ou.path is null or ou.hierarchyLevel is null" ).list() );
         updatePaths( organisationUnits );
     }
 

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/resources/org/hisp/dhis/organisationunit/hibernate/OrganisationUnit.hbm.xml'
--- dhis-2/dhis-services/dhis-service-core/src/main/resources/org/hisp/dhis/organisationunit/hibernate/OrganisationUnit.hbm.xml	2015-09-17 04:30:35 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/resources/org/hisp/dhis/organisationunit/hibernate/OrganisationUnit.hbm.xml	2015-09-23 18:19:16 +0000
@@ -29,6 +29,8 @@
       foreign-key="fk_parentid" index="in_parentid" />
 
     <property name="path" column="path" access="property" unique="true" index="in_organisationunit_path" />
+    
+    <property name="hierarchyLevel" column="hierarchylevel" access="property" index="in_organisationunit_hierarchylevel" />
 
     <property name="uuid" unique="true" length="36" />