← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 18605: Centralized function for getting offline org unit levels to cache.

 

------------------------------------------------------------
revno: 18605
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Sun 2015-03-15 21:49:24 +0100
message:
  Centralized function for getting offline org unit levels to cache.
modified:
  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/resources/META-INF/dhis/beans.xml
  dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/commons/action/GetOrganisationUnitTreeAction.java
  dhis-2/dhis-web/dhis-web-commons/src/main/resources/META-INF/dhis/beans.xml
  dhis-2/dhis-web/dhis-web-dataentry/src/main/java/org/hisp/dhis/de/action/GetDataSetAssociationsAction.java
  dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-settings/src/main/webapp/dhis-web-maintenance-settings/systemGeneralSettings.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/organisationunit/OrganisationUnitService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitService.java	2015-02-19 16:52:45 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitService.java	2015-03-15 20:49:24 +0000
@@ -503,6 +503,19 @@
     int getNumberOfOrganisationUnits();
 
     int getMaxOfOrganisationUnitLevels();
+    
+    /**
+     * Return the number of organisation unit levels to cache offline, e.g. for
+     * organisation unit tree. Looks for level to return in the following order:
+     * 
+     * <ul>
+     * <li>Get level of organisation unit of the current user.</li>
+     * <li>Get level from system configuration.</li>
+     * <li>Get max level.</li>
+     * <li>Return 1 as fall back.</li>
+     * </ul>
+     */
+    int getOfflineOrganisationUnitLevels();
 
     // -------------------------------------------------------------------------
     // Version

=== 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-03-13 18:39:40 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/DefaultOrganisationUnitService.java	2015-03-15 20:49:24 +0000
@@ -45,6 +45,7 @@
 
 import org.apache.commons.collections.CollectionUtils;
 import org.hisp.dhis.common.IdentifiableObjectUtils;
+import org.hisp.dhis.configuration.ConfigurationService;
 import org.hisp.dhis.hierarchy.HierarchyViolationException;
 import org.hisp.dhis.i18n.I18nService;
 import org.hisp.dhis.organisationunit.comparator.OrganisationUnitLevelComparator;
@@ -101,6 +102,13 @@
         this.versionService = versionService;
     }
 
+    private ConfigurationService configurationService;
+
+    public void setConfigurationService( ConfigurationService configurationService )
+    {
+        this.configurationService = configurationService;
+    }
+
     private I18nService i18nService;
 
     public void setI18nService( I18nService service )
@@ -939,6 +947,60 @@
         return organisationUnitLevelStore.getMaxLevels();
     }
 
+    @Override
+    public int getOfflineOrganisationUnitLevels()
+    {
+        // ---------------------------------------------------------------------
+        // Get level from organisation unit of current user
+        // ---------------------------------------------------------------------
+
+        User user = currentUserService.getCurrentUser();
+        
+        if ( user != null && user.hasOrganisationUnit() )
+        {
+            OrganisationUnit organisationUnit = user.getOrganisationUnit();
+            
+            int level = getLevelOfOrganisationUnit( organisationUnit.getId() );
+            
+            OrganisationUnitLevel orgUnitLevel = getOrganisationUnitLevelByLevel( level );
+            
+            if ( orgUnitLevel != null && orgUnitLevel.getOfflineLevels() != null )
+            {
+                return orgUnitLevel.getOfflineLevels();
+            }
+        }
+
+        // ---------------------------------------------------------------------
+        // Get level from system configuration
+        // ---------------------------------------------------------------------
+
+        OrganisationUnitLevel level = configurationService.getConfiguration().getOfflineOrganisationUnitLevel();
+        
+        if ( level != null )
+        {
+            return level.getLevel();
+        }
+
+        // ---------------------------------------------------------------------
+        // Get max level
+        // ---------------------------------------------------------------------
+
+        int max = getOrganisationUnitLevels().size();
+
+        OrganisationUnitLevel maxLevel = getOrganisationUnitLevelByLevel( max );
+        
+        if ( maxLevel != null )
+        {
+            return maxLevel.getLevel();
+        }
+
+        // ---------------------------------------------------------------------
+        // Return 1 level as fall back
+        // ---------------------------------------------------------------------
+
+        return 1;
+    }
+
     /**
      * Get all the Organisation Units within the distance of a coordinate.
      */

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/resources/META-INF/dhis/beans.xml'
--- dhis-2/dhis-services/dhis-service-core/src/main/resources/META-INF/dhis/beans.xml	2015-03-08 14:57:52 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/resources/META-INF/dhis/beans.xml	2015-03-15 20:49:24 +0000
@@ -535,6 +535,7 @@
     <property name="organisationUnitLevelStore" ref="org.hisp.dhis.organisationunit.OrganisationUnitLevelStore" />
     <property name="currentUserService" ref="org.hisp.dhis.user.CurrentUserService" />
     <property name="versionService" ref="org.hisp.dhis.version.VersionService" />
+    <property name="configurationService" ref="org.hisp.dhis.configuration.ConfigurationService" />
     <property name="i18nService" ref="org.hisp.dhis.i18n.I18nService" />
   </bean>
 

=== modified file 'dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/commons/action/GetOrganisationUnitTreeAction.java'
--- dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/commons/action/GetOrganisationUnitTreeAction.java	2015-03-13 18:39:40 +0000
+++ dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/commons/action/GetOrganisationUnitTreeAction.java	2015-03-15 20:49:24 +0000
@@ -28,9 +28,13 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import com.opensymphony.xwork2.Action;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.UUID;
+
 import org.hisp.dhis.common.comparator.IdentifiableObjectNameComparator;
-import org.hisp.dhis.configuration.ConfigurationService;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.organisationunit.OrganisationUnitLevel;
 import org.hisp.dhis.organisationunit.OrganisationUnitService;
@@ -39,11 +43,7 @@
 import org.hisp.dhis.version.Version;
 import org.hisp.dhis.version.VersionService;
 
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.List;
-import java.util.UUID;
+import com.opensymphony.xwork2.Action;
 
 /**
  * @author mortenoh
@@ -76,13 +76,6 @@
         this.versionService = versionService;
     }
 
-    private ConfigurationService configurationService;
-
-    public void setConfigurationService( ConfigurationService configurationService )
-    {
-        this.configurationService = configurationService;
-    }
-
     // -------------------------------------------------------------------------
     // Input & Output
     // -------------------------------------------------------------------------
@@ -242,11 +235,11 @@
 
         if ( !versionOnly && !rootOrganisationUnits.isEmpty() )
         {
-            final Integer maxLevels = getMaxLevels();
+            Integer offlineLevels = getOfflineOrganisationUnitLevels();
             
             for ( OrganisationUnit unit : rootOrganisationUnits )
             {
-                organisationUnits.addAll( organisationUnitService.getOrganisationUnitWithChildren( unit.getId(), maxLevels ) );
+                organisationUnits.addAll( organisationUnitService.getOrganisationUnitWithChildren( unit.getId(), offlineLevels ) );
             }
         }
 
@@ -284,12 +277,10 @@
      * org unit level argument, next the org unit level from the user org unit,
      * next the level from the configuration.
      */
-    private Integer getMaxLevels()
+    private Integer getOfflineOrganisationUnitLevels()
     {
         List<OrganisationUnitLevel> orgUnitLevels = organisationUnitService.getOrganisationUnitLevels();
 
-        Integer levelFromUserOrgUnit = null;
-        
         if ( orgUnitLevels == null || orgUnitLevels.isEmpty() )
         {
             return null;
@@ -299,35 +290,7 @@
         {
             return offlineLevel;
         }
-        else if ( ( levelFromUserOrgUnit = getMaxLevelsFromUserOrgUnits() ) != null )
-        {
-            return levelFromUserOrgUnit;
-        }
-        else
-        {
-            OrganisationUnitLevel level = configurationService.getConfiguration().getOfflineOrganisationUnitLevel();
-            
-            return level != null ? level.getLevel() : null;
-        }
-    }
-
-    /**
-     * Returns the number of org unit levels to cache offline based on the org unit
-     * level of the first user org unit. Returns null if not defined.
-     */
-    private Integer getMaxLevelsFromUserOrgUnits()
-    {
-        if ( !rootOrganisationUnits.isEmpty() )
-        {
-            OrganisationUnit orgUnit = rootOrganisationUnits.get( 0 );
-            
-            int level = organisationUnitService.getLevelOfOrganisationUnit( orgUnit.getId() );
-            
-            OrganisationUnitLevel orgUnitLevel = organisationUnitService.getOrganisationUnitLevelByLevel( level );
-            
-            return orgUnitLevel != null && orgUnitLevel.getOfflineLevels() != null ? orgUnitLevel.getOfflineLevels() : null;
-        }
         
-        return null;
+        return organisationUnitService.getOfflineOrganisationUnitLevels();
     }
 }

=== modified file 'dhis-2/dhis-web/dhis-web-commons/src/main/resources/META-INF/dhis/beans.xml'
--- dhis-2/dhis-web/dhis-web-commons/src/main/resources/META-INF/dhis/beans.xml	2015-02-23 11:57:42 +0000
+++ dhis-2/dhis-web/dhis-web-commons/src/main/resources/META-INF/dhis/beans.xml	2015-03-15 20:49:24 +0000
@@ -504,7 +504,6 @@
     <property name="currentUserService" ref="org.hisp.dhis.user.CurrentUserService" />
     <property name="organisationUnitService" ref="org.hisp.dhis.organisationunit.OrganisationUnitService" />
     <property name="versionService" ref="org.hisp.dhis.version.VersionService" />
-    <property name="configurationService" ref="org.hisp.dhis.configuration.ConfigurationService" />
   </bean>
 
   <bean id="org.hisp.dhis.commons.action.GetPeriodAction" class="org.hisp.dhis.commons.action.GetPeriodAction"

=== modified file 'dhis-2/dhis-web/dhis-web-dataentry/src/main/java/org/hisp/dhis/de/action/GetDataSetAssociationsAction.java'
--- dhis-2/dhis-web/dhis-web-dataentry/src/main/java/org/hisp/dhis/de/action/GetDataSetAssociationsAction.java	2015-01-17 07:41:26 +0000
+++ dhis-2/dhis-web/dhis-web-dataentry/src/main/java/org/hisp/dhis/de/action/GetDataSetAssociationsAction.java	2015-03-15 20:49:24 +0000
@@ -28,6 +28,8 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import static org.hisp.dhis.system.util.TextUtils.SEP;
+
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.HashMap;
@@ -37,11 +39,9 @@
 
 import org.apache.struts2.ServletActionContext;
 import org.hisp.dhis.common.IdentifiableObjectManager;
-import org.hisp.dhis.configuration.ConfigurationService;
 import org.hisp.dhis.dataset.DataSet;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.organisationunit.OrganisationUnitDataSetAssociationSet;
-import org.hisp.dhis.organisationunit.OrganisationUnitLevel;
 import org.hisp.dhis.organisationunit.OrganisationUnitService;
 import org.hisp.dhis.system.util.DateUtils;
 import org.hisp.dhis.user.CurrentUserService;
@@ -50,8 +50,6 @@
 import org.springframework.beans.factory.annotation.Autowired;
 
 import com.opensymphony.xwork2.Action;
-
-import static org.hisp.dhis.system.util.TextUtils.SEP;
 /**
  * @author Lars Helge Overland
  */
@@ -62,9 +60,6 @@
     private OrganisationUnitService organisationUnitService;
     
     @Autowired
-    private ConfigurationService configurationService;
-    
-    @Autowired
     private IdentifiableObjectManager identifiableObjectManager;
 
     @Autowired
@@ -96,21 +91,19 @@
     public String execute()
     {
         User user = currentUserService.getCurrentUser();
-        
+
+        Integer level = organisationUnitService.getOfflineOrganisationUnitLevels();
+
         Date lastUpdated = DateUtils.max( 
             identifiableObjectManager.getLastUpdated( DataSet.class ), 
             identifiableObjectManager.getLastUpdated( OrganisationUnit.class ) );
-        String tag = lastUpdated != null && user != null ? ( DateUtils.LONG_DATE_FORMAT.format( lastUpdated ) + SEP + user.getUid() ): null;
+        String tag = lastUpdated != null && user != null ? ( DateUtils.LONG_DATE_FORMAT.format( lastUpdated ) + SEP + level + SEP + user.getUid() ): null;
         
         if ( ContextUtils.isNotModified( ServletActionContext.getRequest(), ServletActionContext.getResponse(), tag ) )
         {
             return SUCCESS;
         }
         
-        OrganisationUnitLevel offlineOrgUnitLevel = configurationService.getConfiguration().getOfflineOrganisationUnitLevel();
-
-        Integer level = offlineOrgUnitLevel != null ? offlineOrgUnitLevel.getLevel() : null;
-
         OrganisationUnitDataSetAssociationSet organisationUnitSet = organisationUnitService.getOrganisationUnitDataSetAssociationSet( level );
 
         dataSetAssociationSets = organisationUnitSet.getDataSetAssociationSets();

=== modified file 'dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-settings/src/main/webapp/dhis-web-maintenance-settings/systemGeneralSettings.vm'
--- dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-settings/src/main/webapp/dhis-web-maintenance-settings/systemGeneralSettings.vm	2014-11-26 12:22:29 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-settings/src/main/webapp/dhis-web-maintenance-settings/systemGeneralSettings.vm	2015-03-15 20:49:24 +0000
@@ -125,9 +125,8 @@
 <div class="setting">
 <select id="offlineOrganisationUnitLevel" name="offlineOrganisationUnitLevel">
     #set( $level = 1)
-
 	#foreach ( $organisationUnitLevel in $organisationUnitLevels )
-    <option value="$organisationUnitLevel.id" #if( $organisationUnitLevel.id == $!offlineOrganisationUnitLevel.id)selected="selected"#end>Level $level</option>
+    <option value="$organisationUnitLevel.id" #if( $organisationUnitLevel.id == $!offlineOrganisationUnitLevel.id)selected="selected"#end>${level}</option>
     #set( $level = $level + 1)
 	#end
 </select>