=== 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: + * + * + */ + 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 @@ + === 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 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 @@ -