=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitHierarchy.java' --- dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitHierarchy.java 2010-12-02 21:24:43 +0000 +++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitHierarchy.java 2011-07-26 07:39:20 +0000 @@ -36,25 +36,35 @@ import java.util.Set; /** - * The purpose of the OrganisationUnitHierarchy object is to store the parent-child relationship of the - * registered organisation units together with a timestamp. The parent-child relationships are - * stored in a Map, where the key column stores the organisation unit id and the value column - * stores the id of the parent organisation unit. - * * @author Lars Helge Overland - * @version $Id: OrganisationUnitHierarchy.java 2869 2007-02-20 14:26:09Z andegje $ */ public class OrganisationUnitHierarchy { private Map> preparedRelationships = new HashMap>(); - private Collection relationships; + private Map> relationships = new HashMap>(); - public OrganisationUnitHierarchy( Collection relationships ) + public OrganisationUnitHierarchy( Map> relationships ) { this.relationships = relationships; } + public OrganisationUnitHierarchy( Collection relations ) + { + for ( OrganisationUnitRelationship relation : relations ) + { + Set children = relationships.get( relation.getParentId() ); + + if ( children == null ) + { + children = new HashSet(); + relationships.put( relation.getParentId(), children ); + } + + children.add( relation.getChildId() ); + } + } + public OrganisationUnitHierarchy prepareChildren( Collection parents ) { for ( OrganisationUnit unit : parents ) @@ -87,12 +97,13 @@ for ( int i = 0; i < childCounter; i++ ) { - for ( OrganisationUnitRelationship entry : relationships ) + Set currentChildren = relationships.get( children.get( i ) ); + + if ( currentChildren != null ) { - if ( entry.getParentId() == children.get( i ) ) - { - children.add( childCounter++, entry.getChildId() ); - } + children.addAll( currentChildren ); + + childCounter += currentChildren.size(); } } === modified file 'dhis-2/dhis-api/src/test/java/org/hisp/dhis/organisationunit/OrganisationUnitHierarchyTest.java' --- dhis-2/dhis-api/src/test/java/org/hisp/dhis/organisationunit/OrganisationUnitHierarchyTest.java 2010-06-08 19:47:40 +0000 +++ dhis-2/dhis-api/src/test/java/org/hisp/dhis/organisationunit/OrganisationUnitHierarchyTest.java 2011-07-26 07:39:20 +0000 @@ -1,17 +1,52 @@ package org.hisp.dhis.organisationunit; +/* + * Copyright (c) 2004-2010, 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 static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertFalse; import static junit.framework.Assert.assertTrue; import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; import java.util.List; +import java.util.Map; +import java.util.Set; import org.junit.Test; +/** + * @author Lars Helge Overland + */ public class OrganisationUnitHierarchyTest { @Test - public void testGetChildren() + public void testGetChildrenA() { List relationships = new ArrayList(); @@ -26,9 +61,29 @@ relationships.add( new OrganisationUnitRelationship( 4, 10 ) ); relationships.add( new OrganisationUnitRelationship( 4, 11 ) ); relationships.add( new OrganisationUnitRelationship( 4, 12 ) ); - - OrganisationUnitHierarchy hierarchy = new OrganisationUnitHierarchy( relationships ); - + + OrganisationUnitHierarchy hierarchy = new OrganisationUnitHierarchy( relationships ); + + testHierarchy( hierarchy ); + } + + @Test + public void testGetChildrenB() + { + Map> relationships = new HashMap>(); + + relationships.put( 1, getSet( 2, 3 ) ); + relationships.put( 2, getSet( 4, 5, 6 ) ); + relationships.put( 3, getSet( 7, 8, 9 ) ); + relationships.put( 4, getSet( 10, 11, 12 ) ); + + OrganisationUnitHierarchy hierarchy = new OrganisationUnitHierarchy( relationships ); + + testHierarchy( hierarchy ); + } + + private void testHierarchy( OrganisationUnitHierarchy hierarchy ) + { assertEquals( 12, hierarchy.getChildren( 1 ).size() ); assertEquals( 7, hierarchy.getChildren( 2 ).size() ); @@ -41,12 +96,33 @@ assertTrue( hierarchy.getChildren( 2 ).contains( 12 ) ); assertEquals( 4, hierarchy.getChildren( 3 ).size() ); - assertTrue( hierarchy.getChildren( 2 ).contains( 4 ) ); - assertTrue( hierarchy.getChildren( 2 ).contains( 10 ) ); - assertTrue( hierarchy.getChildren( 2 ).contains( 11 ) ); - assertTrue( hierarchy.getChildren( 2 ).contains( 12 ) ); + assertTrue( hierarchy.getChildren( 3 ).contains( 3 ) ); + assertTrue( hierarchy.getChildren( 3 ).contains( 7 ) ); + assertTrue( hierarchy.getChildren( 3 ).contains( 8 ) ); + assertTrue( hierarchy.getChildren( 3 ).contains( 9 ) ); + + assertEquals( 4, hierarchy.getChildren( 4 ).size() ); + assertTrue( hierarchy.getChildren( 4 ).contains( 4 ) ); + assertTrue( hierarchy.getChildren( 4 ).contains( 10 ) ); + assertTrue( hierarchy.getChildren( 4 ).contains( 11 ) ); + assertTrue( hierarchy.getChildren( 4 ).contains( 12 ) ); assertEquals( 1, hierarchy.getChildren( 11 ).size() ); assertTrue( hierarchy.getChildren( 11 ).contains( 11 ) ); + + assertFalse( hierarchy.getChildren( 2 ).contains( 3 ) ); + assertFalse( hierarchy.getChildren( 2 ).contains( 8 ) ); + } + + private Set getSet( Integer... ints ) + { + Set set = new HashSet(); + + for ( Integer i : ints ) + { + set.add( i ); + } + + return set; } } === 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 2011-07-25 20:12:49 +0000 +++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/DefaultOrganisationUnitService.java 2011-07-26 07:39:20 +0000 @@ -423,11 +423,10 @@ public OrganisationUnitDataSetAssociationSet getOrganisationUnitDataSetAssociationSet() { - //TODO hierarchy - Map> associationSet = organisationUnitStore.getOrganisationUnitDataSetAssocationMap(); filterUserDataSets( associationSet ); + filterChildOrganisationUnits( associationSet ); OrganisationUnitDataSetAssociationSet set = new OrganisationUnitDataSetAssociationSet(); @@ -462,6 +461,20 @@ } } + private void filterChildOrganisationUnits( Map> associatonMap ) + { + User currentUser = currentUserService.getCurrentUser(); + + if ( currentUser != null ) + { + Collection parentIds = ConversionUtils.getIdentifiers( OrganisationUnit.class, currentUser.getOrganisationUnits() ); + + Collection children = getOrganisationUnitHierarchy().getChildren( parentIds ); + + associatonMap.keySet().retainAll( children ); + } + } + // ------------------------------------------------------------------------- // OrganisationUnitHierarchy // -------------------------------------------------------------------------