dhis2-devs team mailing list archive
-
dhis2-devs team
-
Mailing list archive
-
Message #39883
[Branch ~dhis2-devs-core/dhis2/trunk] Rev 20212: Re-implemented getOrganisationUnitsAtLevel using org unit query
------------------------------------------------------------
revno: 20212
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2015-09-17 15:26:47 +0200
message:
Re-implemented getOrganisationUnitsAtLevel using org unit query
modified:
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/hibernate/HibernateOrganisationUnitStore.java
dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/organisationunit/OrganisationUnitServiceTest.java
--
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-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-09-17 12:29:02 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/DefaultOrganisationUnitService.java 2015-09-17 13:26:47 +0000
@@ -398,88 +398,37 @@
@Override
public List<OrganisationUnit> getOrganisationUnitsAtLevel( int level )
{
- List<OrganisationUnit> roots = getRootOrganisationUnits();
-
- if ( level == 1 )
- {
- return roots;
- }
-
- return getOrganisationUnitsAtLevels( Sets.newHashSet( level ), roots );
+ OrganisationUnitQueryParams params = new OrganisationUnitQueryParams();
+ params.setLevels( Sets.newHashSet( level ) );
+
+ return organisationUnitStore.getOrganisationUnits( params );
}
@Override
public List<OrganisationUnit> getOrganisationUnitsAtLevel( int level, OrganisationUnit parent )
{
- List<OrganisationUnit> parents = new ArrayList<>();
- parents.add( parent );
-
- return getOrganisationUnitsAtLevels( Sets.newHashSet( level ), parent != null ? parents : null );
+ OrganisationUnitQueryParams params = new OrganisationUnitQueryParams();
+ params.setLevels( Sets.newHashSet( level ) );
+
+ if ( parent != null )
+ {
+ params.setParents( Sets.newHashSet( parent ) );
+ }
+
+ return organisationUnitStore.getOrganisationUnits( params );
}
- //TODO rewrite using path
@Override
public List<OrganisationUnit> getOrganisationUnitsAtLevels( Collection<Integer> levels, Collection<OrganisationUnit> parents )
{
- if ( parents == null || parents.isEmpty() )
- {
- parents = new HashSet<>( getRootOrganisationUnits() );
- }
-
- List<OrganisationUnit> result = new ArrayList<>();
-
- for ( Integer level : levels )
- {
- if ( level < 1 )
- {
- throw new IllegalArgumentException( "Level must be greater than zero" );
- }
-
- for ( OrganisationUnit parent : parents )
- {
- int parentLevel = parent.getLevel();
-
- if ( level < parentLevel )
- {
- throw new IllegalArgumentException(
- "Level must be greater than or equal to level of parent organisation unit" );
- }
-
- if ( level == parentLevel )
- {
- result.add( parent );
- }
- else
- {
- addOrganisationUnitChildrenAtLevel( parent, parentLevel + 1, level, result );
- }
- }
- }
-
- return result;
- }
-
- /**
- * Support method for getOrganisationUnitsAtLevel(). Adds all children at a
- * given targetLevel to a result collection. The parent's children are at
- * the current level.
- */
- private void addOrganisationUnitChildrenAtLevel( OrganisationUnit parent, int currentLevel, int targetLevel,
- List<OrganisationUnit> result )
- {
- if ( currentLevel == targetLevel )
- {
- result.addAll( parent.getChildren() );
- }
- else
- {
- for ( OrganisationUnit child : parent.getChildren() )
- {
- addOrganisationUnitChildrenAtLevel( child, currentLevel + 1, targetLevel, result );
- }
- }
- }
-
+ OrganisationUnitQueryParams params = new OrganisationUnitQueryParams();
+ params.setLevels( Sets.newHashSet( levels ) );
+ params.setParents( Sets.newHashSet( parents ) );
+
+ return organisationUnitStore.getOrganisationUnits( params );
+ }
+
+ //Rewrite using path
@Override
public int getNumberOfOrganisationalLevels()
{
=== 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-17 12:59:41 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/hibernate/HibernateOrganisationUnitStore.java 2015-09-17 13:26:47 +0000
@@ -345,7 +345,7 @@
counter++;
}
}
-
+
@Override
@SuppressWarnings( "unchecked" )
public void forceUpdatePaths()
=== modified file 'dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/organisationunit/OrganisationUnitServiceTest.java'
--- dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/organisationunit/OrganisationUnitServiceTest.java 2015-09-17 12:29:02 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/organisationunit/OrganisationUnitServiceTest.java 2015-09-17 13:26:47 +0000
@@ -418,21 +418,6 @@
OrganisationUnit unitN = createOrganisationUnit( 'N', unitG );
OrganisationUnit unitO = createOrganisationUnit( 'O', unitG );
- unitA.getChildren().add( unitB );
- unitA.getChildren().add( unitC );
- unitB.getChildren().add( unitD );
- unitB.getChildren().add( unitE );
- unitC.getChildren().add( unitF );
- unitC.getChildren().add( unitG );
- unitD.getChildren().add( unitH );
- unitD.getChildren().add( unitI );
- unitE.getChildren().add( unitJ );
- unitE.getChildren().add( unitK );
- unitF.getChildren().add( unitL );
- unitF.getChildren().add( unitM );
- unitG.getChildren().add( unitN );
- unitG.getChildren().add( unitO );
-
organisationUnitService.addOrganisationUnit( unitA );
organisationUnitService.addOrganisationUnit( unitB );
organisationUnitService.addOrganisationUnit( unitC );
@@ -449,9 +434,9 @@
organisationUnitService.addOrganisationUnit( unitN );
organisationUnitService.addOrganisationUnit( unitO );
- assertTrue( equals( organisationUnitService.getOrganisationUnitsAtLevel( 2, unitB ), unitB ) );
- assertTrue( equals( organisationUnitService.getOrganisationUnitsAtLevel( 3, unitB ), unitD, unitE ) );
- assertTrue( equals( organisationUnitService.getOrganisationUnitsAtLevel( 4, unitB ), unitH, unitI, unitJ, unitK ) );
+ assertEquals( Sets.newHashSet( unitB ), Sets.newHashSet( organisationUnitService.getOrganisationUnitsAtLevel( 2, unitB ) ) );
+ assertEquals( Sets.newHashSet( unitD, unitE ), Sets.newHashSet( organisationUnitService.getOrganisationUnitsAtLevel( 3, unitB ) ) );
+ assertEquals( Sets.newHashSet( unitH, unitI, unitJ, unitK ), Sets.newHashSet( organisationUnitService.getOrganisationUnitsAtLevel( 4, unitB ) ) );
assertEquals( 2, unitB.getLevel() );
assertEquals( 3, unitD.getLevel() );
@@ -515,9 +500,9 @@
List<OrganisationUnit> unitsA = new ArrayList<>( Arrays.asList( unitB, unitC ) );
List<OrganisationUnit> unitsB = new ArrayList<>( Arrays.asList( unitD, unitE ) );
- assertTrue( equals( organisationUnitService.getOrganisationUnitsAtLevels( Sets.newHashSet( 3 ), unitsA ), unitD, unitE, unitF, unitG ) );
- assertTrue( equals( organisationUnitService.getOrganisationUnitsAtLevels( Sets.newHashSet( 4 ), unitsA ), unitH, unitI, unitJ, unitK, unitL, unitM, unitN, unitO ) );
- assertTrue( equals( organisationUnitService.getOrganisationUnitsAtLevels( Sets.newHashSet( 4 ), unitsB ), unitH, unitI, unitJ, unitK ) );
+ assertEquals( Sets.newHashSet( unitD, unitE, unitF, unitG ), Sets.newHashSet( organisationUnitService.getOrganisationUnitsAtLevels( Sets.newHashSet( 3 ), unitsA ) ) );
+ assertEquals( Sets.newHashSet( unitH, unitI, unitJ, unitK, unitL, unitM, unitN, unitO ), Sets.newHashSet( organisationUnitService.getOrganisationUnitsAtLevels( Sets.newHashSet( 4 ), unitsA ) ) );
+ assertEquals( Sets.newHashSet( unitH, unitI, unitJ, unitK ), Sets.newHashSet( organisationUnitService.getOrganisationUnitsAtLevels( Sets.newHashSet( 4 ), unitsB ) ) );
assertEquals( 2, unitB.getLevel() );
assertEquals( 3, unitD.getLevel() );