=== 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-06-16 05:11:29 +0000 +++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitService.java 2015-07-07 05:22:43 +0000 @@ -28,6 +28,7 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +import org.hisp.dhis.common.OrganisationUnitSelectionMode; import org.hisp.dhis.hierarchy.HierarchyViolationException; import java.util.Collection; @@ -70,7 +71,7 @@ * Updates the version of the organisation unit hierarchy. */ void updateOrganisationUnitVersion(); - + /** * Updates an OrganisationUnit. * @@ -126,7 +127,7 @@ * Returns all OrganisationUnits. * * @return a list of all OrganisationUnits, or an empty list if - * there are no OrganisationUnits. + * there are no OrganisationUnits. */ List getAllOrganisationUnits(); @@ -135,7 +136,7 @@ * * @param active Get active or inactive * @return a list of all OrganisationUnits, or an empty list if - * there are no OrganisationUnits. + * there are no OrganisationUnits. */ List getAllOrganisationUnitsByStatus( boolean status ); @@ -144,7 +145,7 @@ * * @param lastUpdated OrganisationUnits from this date * @return a list of all OrganisationUnits, or an empty list if - * there are no OrganisationUnits. + * there are no OrganisationUnits. */ List getAllOrganisationUnitsByLastUpdated( Date lastUpdated ); @@ -154,7 +155,7 @@ * @param active Get active or inactive * @param lastUpdated OrganisationUnits from this date * @return a list of all OrganisationUnits, or an empty list if - * there are no OrganisationUnits. + * there are no OrganisationUnits. */ List getAllOrganisationUnitsByStatusLastUpdated( boolean status, Date lastUpdated ); @@ -220,7 +221,7 @@ * OrganisationUnit with no parent/the parent set to null. * * @return a list containing all root OrganisationUnits, or an empty - * list if there are no OrganisationUnits. + * list if there are no OrganisationUnits. */ List getRootOrganisationUnits(); @@ -241,34 +242,36 @@ /** * Returns the intersection of the members of the given OrganisationUnitGroups * and the OrganisationUnits which are children of the given collection of - * parents in the hierarchy. If the given parent collection is null or empty, + * parents in the hierarchy. If the given parent collection is null or empty, * the members of the group are returned. - * - * @param groups the collection of OrganisationUnitGroups. + * + * @param groups the collection of OrganisationUnitGroups. * @param parents the collection of OrganisationUnit parents in the hierarchy. * @return A list of OrganisationUnits. */ List getOrganisationUnits( Collection groups, Collection parents ); - + + Set getOrganisationUnits( Set parents, OrganisationUnitSelectionMode ouMode ); + /** * Returns an OrganisationUnit and all its children. * * @param uid the uid of the parent OrganisationUnit in the subtree. * @return a list containing the OrganisationUnit with the given id - * and all its children, or an empty list if no - * OrganisationUnits match. + * and all its children, or an empty list if no + * OrganisationUnits match. */ List getOrganisationUnitWithChildren( String uid ); /** * Returns an OrganisationUnit and all its children. * - * @param uid the uid of the parent OrganisationUnit in the subtree. - * @param maxLevels the max number of levels to return relative to - * the given root, inclusive. + * @param uid the uid of the parent OrganisationUnit in the subtree. + * @param maxLevels the max number of levels to return relative to + * the given root, inclusive. * @return a list containing the OrganisationUnit with the given id - * and all its children, or an empty list if no - * OrganisationUnits match. + * and all its children, or an empty list if no + * OrganisationUnits match. */ List getOrganisationUnitWithChildren( String uid, Integer maxLevels ); @@ -277,20 +280,20 @@ * * @param id the id of the parent OrganisationUnit in the subtree. * @return a list containing the OrganisationUnit with the given id - * and all its children, or an empty list if no - * OrganisationUnits match. + * and all its children, or an empty list if no + * OrganisationUnits match. */ List getOrganisationUnitWithChildren( int id ); /** * Returns an OrganisationUnit and all its children. * - * @param id the id of the parent OrganisationUnit in the subtree. - * @param maxLevels the max number of levels to return relative to - * the given root, inclusive. + * @param id the id of the parent OrganisationUnit in the subtree. + * @param maxLevels the max number of levels to return relative to + * the given root, inclusive. * @return a list containing the OrganisationUnit with the given id - * and all its children, or an empty list if no - * OrganisationUnits match. + * and all its children, or an empty list if no + * OrganisationUnits match. */ List getOrganisationUnitWithChildren( int id, Integer maxLevels ); @@ -299,23 +302,23 @@ * * @param uids the uids of the parent OrganisationUnits. * @return a list containing the OrganisationUnit with the given id - * and all its children, or an empty list if no - * OrganisationUnits match. + * and all its children, or an empty list if no + * OrganisationUnits match. */ List getOrganisationUnitsWithChildren( Collection uids ); /** * Returns the OrganisationUnits and all their children. * - * @param uids the uids of the parent OrganisationUnits. - * @param maxLevels the max number of levels to return relative to - * the given root, inclusive. + * @param uids the uids of the parent OrganisationUnits. + * @param maxLevels the max number of levels to return relative to + * the given root, inclusive. * @return a list containing the OrganisationUnit with the given id - * and all its children, or an empty list if no - * OrganisationUnits match. + * and all its children, or an empty list if no + * OrganisationUnits match. */ List getOrganisationUnitsWithChildren( Collection uids, Integer maxLevels ); - + /** * Returns the branch of OrganisationUnits from a root to a given * OrganisationUnit. Both root and target OrganisationUnits are included in @@ -323,8 +326,8 @@ * * @param id the id of the OrganisationUnit to trace upwards from. * @return the list of OrganisationUnits from a root to the given - * OrganisationUnit, or an empty list if the given OrganisationUnit - * doesn't exist. + * OrganisationUnit, or an empty list if the given OrganisationUnit + * doesn't exist. */ List getOrganisationUnitBranch( int id ); @@ -334,7 +337,7 @@ * * @param level the hierarchical level. * @return a list of all OrganisationUnits at a given hierarchical - * level, or an empty list if the level is empty. + * level, or an empty list if the level is empty. * @throws IllegalArgumentException if the level is zero or negative. */ List getOrganisationUnitsAtLevel( int level ); @@ -344,10 +347,10 @@ * at the given hierarchical level. The root OrganisationUnits are at level 1. * If parent is null, then all OrganisationUnits at the given level are returned. * - * @param level the hierarchical level. + * @param level the hierarchical level. * @param parent the parent unit. * @return all OrganisationUnits which are children of the given unit and are - * at the given hierarchical level. + * at the given hierarchical level. * @throws IllegalArgumentException if the level is illegal. */ List getOrganisationUnitsAtLevel( int level, OrganisationUnit parent ); @@ -360,11 +363,11 @@ * @param levels the hierarchical levels. * @param parent the parent unit. * @return all OrganisationUnits which are children of the given unit and are - * at the given hierarchical level. + * at the given hierarchical level. * @throws IllegalArgumentException if the level is illegal. */ List getOrganisationUnitsAtLevels( Collection levels, Collection parents ); - + /** * Returns all OrganisationUnits which are children of the given units and are * at the given hierarchical level. The root OrganisationUnits are at level 1. @@ -373,7 +376,7 @@ * @param level the hierarchical level. * @param parent the parent units. * @return all OrganisationUnits which are children of the given units and are - * at the given hierarchical level. + * at the given hierarchical level. * @throws IllegalArgumentException if the level is illegal. */ List getOrganisationUnitsAtLevel( int level, Collection parents ); @@ -429,27 +432,25 @@ List getOrganisationUnitsBetweenByStatusLastUpdated( boolean status, Date lastUpdated, int first, int max ); Map getUuidOrganisationUnitMap(); - + /** * Retrieves all the org units within the distance from center location. * - * @param longitude The longitude of the center location. - * @param latitude The latitude of the center location. - * @param distance The distance from center location. - * + * @param longitude The longitude of the center location. + * @param latitude The latitude of the center location. + * @param distance The distance from center location. * @return a list of objects. */ List getOrganisationUnitWithinDistance( double longitude, double latitude, double distance ); /** * Retrieves the orgunit(s) by coordinate. - * - * @param longitude The longitude of the location. - * @param latitude The latitude of the location. + * + * @param longitude The longitude of the location. + * @param latitude The latitude of the location. * @param topOrgUnitUid Optional. Uid of the search top level org unit (ex. - * Country level orgunit) - * @param targetLevel Optional. The level being searched. - * + * Country level orgunit) + * @param targetLevel Optional. The level being searched. * @return list of objects. */ List getOrganisationUnitByCoordinate( double longitude, double latitude, String topOrgUnitUid, @@ -458,7 +459,7 @@ /** * Indicates whether the given organisation unit is part of the hierarchy * of the organisation units of the current user. - * + * * @param organisationUnit the organisation unit. * @return true if the given organisation unit is part of the hierarchy. */ @@ -467,14 +468,14 @@ /** * Indicates whether the given organisation unit is part of the hierarchy * of the given user organisation units. - * - * @param uid the uid of the organisation unit. - * @param organisationUnits the set of organisation units associated with a - * user. + * + * @param uid the uid of the organisation unit. + * @param organisationUnits the set of organisation units associated with a + * user. * @return true if the organisation unit with the given uid is part of the hierarchy. */ boolean isInUserHierarchy( String uid, Set organisationUnits ); - + // ------------------------------------------------------------------------- // OrganisationUnitHierarchy // ------------------------------------------------------------------------- @@ -529,11 +530,11 @@ 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: - * + *

*

    *
  • Get level of organisation unit of the current user.
  • *
  • Get level from system configuration.
  • === 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-06-16 05:11:29 +0000 +++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/DefaultOrganisationUnitService.java 2015-07-07 05:22:43 +0000 @@ -28,23 +28,12 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -import static org.hisp.dhis.common.IdentifiableObjectUtils.getUids; -import static org.hisp.dhis.i18n.I18nUtils.i18n; - -import java.awt.geom.Point2D; -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.Iterator; -import java.util.List; -import java.util.Map; -import java.util.Set; - +import com.google.common.collect.Maps; +import com.google.common.collect.Sets; import org.hisp.dhis.common.IdentifiableObjectUtils; +import org.hisp.dhis.common.OrganisationUnitSelectionMode; import org.hisp.dhis.commons.filter.Filter; +import org.hisp.dhis.commons.filter.FilterUtils; import org.hisp.dhis.configuration.ConfigurationService; import org.hisp.dhis.hierarchy.HierarchyViolationException; import org.hisp.dhis.i18n.I18nService; @@ -54,12 +43,23 @@ import org.hisp.dhis.system.util.ValidationUtils; import org.hisp.dhis.user.CurrentUserService; import org.hisp.dhis.user.User; -import org.hisp.dhis.commons.filter.FilterUtils; import org.hisp.dhis.version.VersionService; import org.springframework.transaction.annotation.Transactional; -import com.google.common.collect.Maps; -import com.google.common.collect.Sets; +import java.awt.geom.Point2D; +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.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import static org.hisp.dhis.common.IdentifiableObjectUtils.getUids; +import static org.hisp.dhis.i18n.I18nUtils.i18n; /** * @author Torgeir Lorange Ostby @@ -131,16 +131,16 @@ currentUserService.getCurrentUser().getOrganisationUnits().add( organisationUnit ); } - + return id; } @Override public void updateOrganisationUnit( OrganisationUnit organisationUnit ) { - organisationUnitStore.update( organisationUnit ); + organisationUnitStore.update( organisationUnit ); } - + @Override public void updateOrganisationUnitVersion() { @@ -197,7 +197,7 @@ @Override public List getAllOrganisationUnitsByLastUpdated( Date lastUpdated ) { - return i18n( i18nService, organisationUnitStore.getAllOrganisationUnitsByLastUpdated( lastUpdated ) ); + return i18n( i18nService, organisationUnitStore.getAllOrganisationUnitsByLastUpdated( lastUpdated ) ); } @Override @@ -286,7 +286,7 @@ @Override public List getRootOrganisationUnits() { - return i18n( i18nService, organisationUnitStore.getRootOrganisationUnits()); + return i18n( i18nService, organisationUnitStore.getRootOrganisationUnits() ); } @Override @@ -322,6 +322,40 @@ } @Override + public Set getOrganisationUnits( Set parents, OrganisationUnitSelectionMode ouMode ) + { + List ouParents = new ArrayList<>( organisationUnitStore.getByUid( parents ) ); + Set ou = new HashSet<>(); + User user = currentUserService.getCurrentUser(); + + if ( user != null && OrganisationUnitSelectionMode.ACCESSIBLE == ouMode ) + { + ouParents = new ArrayList<>( user.getDataViewOrganisationUnitsWithFallback() ); + ouMode = OrganisationUnitSelectionMode.DESCENDANTS; + } + + for ( OrganisationUnit organisationUnit : ouParents ) + { + if ( OrganisationUnitSelectionMode.DESCENDANTS == ouMode ) + { + ou.add( organisationUnit.getUid() ); + ou.addAll( getUids( getOrganisationUnitWithChildren( organisationUnit.getUid() ) ) ); + } + else if ( OrganisationUnitSelectionMode.CHILDREN == ouMode ) + { + ou.add( organisationUnit.getUid() ); + ou.addAll( getUids( organisationUnit.getChildren() ) ); + } + else // SELECTED + { + ou.add( organisationUnit.getUid() ); + } + } + + return ou; + } + + @Override public List getOrganisationUnitsWithChildren( Collection parentUids ) { return getOrganisationUnitsWithChildren( parentUids, null ); @@ -350,9 +384,9 @@ public List getOrganisationUnitWithChildren( String uid, Integer maxLevels ) { OrganisationUnit unit = getOrganisationUnit( uid ); - + int id = unit != null ? unit.getId() : -1; - + return getOrganisationUnitWithChildren( id, maxLevels ); } @@ -361,7 +395,7 @@ { return getOrganisationUnitWithChildren( id, null ); } - + @Override public List getOrganisationUnitWithChildren( int id, Integer maxLevels ) { @@ -371,7 +405,7 @@ { return new ArrayList<>(); } - + if ( maxLevels != null && maxLevels <= 0 ) { return new ArrayList<>(); @@ -384,8 +418,8 @@ organisationUnit.setLevel( rootLevel ); result.add( organisationUnit ); - final Integer maxLevel = maxLevels != null ? ( rootLevel + maxLevels - 1 ) : null; - + final Integer maxLevel = maxLevels != null ? (rootLevel + maxLevels - 1) : null; + addOrganisationUnitChildren( organisationUnit, result, rootLevel, maxLevel ); return result; @@ -401,7 +435,7 @@ { level++; } - + if ( maxLevel != null && level > maxLevel ) { return; @@ -611,7 +645,7 @@ if ( parent == null ) { - return new ArrayList(result); + return new ArrayList( result ); } final Set subTree = new HashSet<>( getOrganisationUnitWithChildren( parent.getId() ) ); @@ -652,7 +686,7 @@ /** * Retains only the data sets from the map which the current user has access to. - * + * * @param associationMap the associations between organisation unit and data sets. */ private void filterUserDataSets( Map> associationMap ) @@ -672,10 +706,10 @@ /** * Retains only the organisation units in the sub-tree of the current user. - * + * * @param associationMap the associations between organisation unit and data sets. - * @param maxLevels the maximum number of levels to include relative to - * current user, inclusive. + * @param maxLevels the maximum number of levels to include relative to + * current user, inclusive. */ private void filterChildOrganisationUnits( Map> associationMap, Integer maxLevels ) { @@ -686,7 +720,7 @@ Collection parentIds = getUids( currentUser.getOrganisationUnits() ); Collection organisationUnitsWithChildren = getOrganisationUnitsWithChildren( parentIds, maxLevels ); - + Collection children = getUids( organisationUnitsWithChildren ); associationMap.keySet().retainAll( children ); @@ -711,7 +745,7 @@ @Override public List getOrganisationUnitsBetween( int first, int max ) { - return i18n( i18nService, organisationUnitStore.getAllOrderedName( first, max ) ); + return i18n( i18nService, organisationUnitStore.getAllOrderedName( first, max ) ); } @Override @@ -736,21 +770,21 @@ public List getOrganisationUnitsBetweenByStatusLastUpdated( boolean status, Date lastUpdated, int first, int max ) { - return i18n( i18nService, organisationUnitStore.getBetweenByStatusLastUpdated( status, lastUpdated, first, max )); + return i18n( i18nService, organisationUnitStore.getBetweenByStatusLastUpdated( status, lastUpdated, first, max ) ); } @Override public Map getUuidOrganisationUnitMap() { Map map = new HashMap<>(); - + Collection organisationUnits = getAllOrganisationUnits(); - + for ( OrganisationUnit organisationUnit : organisationUnits ) { map.put( organisationUnit.getUuid(), organisationUnit ); } - + return map; } @@ -758,7 +792,7 @@ public boolean isInUserHierarchy( OrganisationUnit organisationUnit ) { User user = currentUserService.getCurrentUser(); - + return user != null ? user.isInUserHierarchy( organisationUnit ) : false; } @@ -766,10 +800,10 @@ public boolean isInUserHierarchy( String uid, Set organisationUnits ) { OrganisationUnit organisationUnit = organisationUnitStore.getByUid( uid ); - + return User.isInUserHierarchy( organisationUnit, organisationUnits ); } - + // ------------------------------------------------------------------------- // OrganisationUnitHierarchy // ------------------------------------------------------------------------- @@ -926,7 +960,7 @@ return levelMap; } - + @Override public int getNumberOfOrganisationUnits() { @@ -947,15 +981,15 @@ // --------------------------------------------------------------------- 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(); @@ -967,7 +1001,7 @@ // --------------------------------------------------------------------- OrganisationUnitLevel level = configurationService.getConfiguration().getOfflineOrganisationUnitLevel(); - + if ( level != null ) { return level.getLevel(); @@ -980,7 +1014,7 @@ int max = getOrganisationUnitLevels().size(); OrganisationUnitLevel maxLevel = getOrganisationUnitLevelByLevel( max ); - + if ( maxLevel != null ) { return maxLevel.getLevel(); @@ -1004,7 +1038,7 @@ longitude, latitude, distance ) ); // Go through the list and remove the ones located outside radius - + if ( objects != null && objects.size() > 0 ) { Iterator iter = objects.iterator(); @@ -1048,7 +1082,7 @@ else { // Get top search point through top level org unit which contains coordinate - + Collection orgUnitsTopLevel = getTopLevelOrgUnitWithPoint( longitude, latitude, 1, getNumberOfOrganisationalLevels() - 1 ); @@ -1059,7 +1093,7 @@ } // Search children org units to get the lowest level org unit that contains coordinate - + if ( topOrgUnit != null ) { Collection orgUnitChildren = new ArrayList<>(); @@ -1074,9 +1108,9 @@ } FilterUtils.filter( orgUnitChildren, new OrganisationUnitPolygonCoveringCoordinateFilter( longitude, latitude ) ); - + // Get org units with lowest level - + int bottomLevel = topOrgUnit.getLevel(); for ( OrganisationUnit ou : orgUnitChildren ) @@ -1117,14 +1151,14 @@ /** * Searches organisation units until finding one with polygon containing point. */ - private List getTopLevelOrgUnitWithPoint( double longitude, double latitude, + private List getTopLevelOrgUnitWithPoint( double longitude, double latitude, int searchLevel, int stopLevel ) { for ( int i = searchLevel; i <= stopLevel; i++ ) { List unitsAtLevel = new ArrayList<>( getOrganisationUnitsAtLevel( i ) ); FilterUtils.filter( unitsAtLevel, new OrganisationUnitPolygonCoveringCoordinateFilter( longitude, latitude ) ); - + if ( unitsAtLevel.size() > 0 ) { return unitsAtLevel;