← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 595: (GIS) Improved performance for loading of MapOrganisationUnitRelations.

 

------------------------------------------------------------
revno: 595
committer: Lars Helge Oeverland larshelge@xxxxxxxxx
branch nick: trunk
timestamp: Sat 2009-09-05 18:19:02 +0200
message:
  (GIS) Improved performance for loading of MapOrganisationUnitRelations.
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/mapping/MappingService.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/mapping/MappingStore.java
  dhis-2/dhis-services/dhis-service-mapping/src/main/java/org/hisp/dhis/mapping/DefaultMappingService.java
  dhis-2/dhis-services/dhis-service-mapping/src/main/java/org/hisp/dhis/mapping/hibernate/HibernateMappingStore.java
  dhis-2/dhis-web/dhis-web-mapping/src/main/java/org/hisp/dhis/mapping/action/DeleteMapOrganisationUnitRelationAction.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-api/src/main/java/org/hisp/dhis/mapping/MappingService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/mapping/MappingService.java	2009-09-01 09:29:11 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/mapping/MappingService.java	2009-09-05 16:19:02 +0000
@@ -238,7 +238,7 @@
      * @return a Collection<MapOrganisationUnitRelation> which contains the given Map and
      *         OrganisationUnit.
      */
-    Collection<MapOrganisationUnitRelation> getMapOrganisationUnitRelation( Map map, OrganisationUnit organisationUnit );
+    MapOrganisationUnitRelation getMapOrganisationUnitRelation( Map map, OrganisationUnit organisationUnit );
 
     /**
      * Returns a Collection of MapOrganisationUnitRelations.

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/mapping/MappingStore.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/mapping/MappingStore.java	2009-09-01 09:29:11 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/mapping/MappingStore.java	2009-09-05 16:19:02 +0000
@@ -152,7 +152,7 @@
      * @return a Collection<MapOrganisationUnitRelation> which contains the given Map and
      *         OrganisationUnit.
      */
-    Collection<MapOrganisationUnitRelation> getMapOrganisationUnitRelation( Map map, OrganisationUnit organisationUnit );
+    MapOrganisationUnitRelation getMapOrganisationUnitRelation( Map map, OrganisationUnit organisationUnit );
     
     /**
      * Returns a Collection of MapOrganisationUnitRelations.

=== modified file 'dhis-2/dhis-services/dhis-service-mapping/src/main/java/org/hisp/dhis/mapping/DefaultMappingService.java'
--- dhis-2/dhis-services/dhis-service-mapping/src/main/java/org/hisp/dhis/mapping/DefaultMappingService.java	2009-09-01 09:29:11 +0000
+++ dhis-2/dhis-services/dhis-service-mapping/src/main/java/org/hisp/dhis/mapping/DefaultMappingService.java	2009-09-05 16:19:02 +0000
@@ -29,6 +29,7 @@
 
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
@@ -261,12 +262,10 @@
 
         OrganisationUnit organisationUnit = organisationUnitService.getOrganisationUnit( organisationUnitId );
 
-        MapOrganisationUnitRelation mapOrganisationUnitRelation;
+        MapOrganisationUnitRelation mapOrganisationUnitRelation = getMapOrganisationUnitRelation( map, organisationUnit );
         
-        if ( getMapOrganisationUnitRelation( map, organisationUnit ).iterator().hasNext() )
-        {
-            mapOrganisationUnitRelation = getMapOrganisationUnitRelation( map, organisationUnit ).iterator().next();
-            
+        if ( mapOrganisationUnitRelation != null )
+        {            
             mapOrganisationUnitRelation.setFeatureId( featureId );
 
             updateMapOrganisationUnitRelation( mapOrganisationUnitRelation );
@@ -294,7 +293,7 @@
         return mappingStore.getMapOrganisationUnitRelation( id );
     }
 
-    public Collection<MapOrganisationUnitRelation> getMapOrganisationUnitRelation( Map map, OrganisationUnit organisationUnit )
+    public MapOrganisationUnitRelation getMapOrganisationUnitRelation( Map map, OrganisationUnit organisationUnit )
     {
         return mappingStore.getMapOrganisationUnitRelation( map, organisationUnit );
     }
@@ -310,27 +309,39 @@
     }
 
     public Collection<MapOrganisationUnitRelation> getAvailableMapOrganisationUnitRelations( Map map )
-    {
+    {        
         Collection<OrganisationUnit> organisationUnits = organisationUnitService.getOrganisationUnitsAtLevel( map
             .getOrganisationUnitLevel().getLevel() );
 
-        Collection<MapOrganisationUnitRelation> relations = new ArrayList<MapOrganisationUnitRelation>();
-
+        java.util.Map<Integer, MapOrganisationUnitRelation> relationMap = getRelationshipMap( mappingStore.getMapOrganisationUnitRelationByMap( map ) );
+        
+        Collection<MapOrganisationUnitRelation> availableRelations = new ArrayList<MapOrganisationUnitRelation>();
+        
         for ( OrganisationUnit unit : organisationUnits )
         {
-            Collection<MapOrganisationUnitRelation> relation = getMapOrganisationUnitRelation( map, unit );
+            MapOrganisationUnitRelation relation = relationMap.get( unit.getId() );
             
-            if ( relation.size() == 0 )
-            {
-                relations.add( new MapOrganisationUnitRelation( map, unit, null ) );
-            }
-            else
-            {
-                relations.addAll( relation );
-            }
-        }
-        
-        return relations;
+            availableRelations.add( relation != null ? relation : new MapOrganisationUnitRelation( map, unit, null ) );
+        }
+        
+        return availableRelations;
+    }
+    
+    /**
+     * Returns a Map<Integer, MapOrganisationUnitRelation> where the key is the 
+     * OrganisationUnit identifier and the value the MapOrganisationUnitRelation
+     * itself.
+     */
+    private java.util.Map<Integer, MapOrganisationUnitRelation> getRelationshipMap( Collection<MapOrganisationUnitRelation> relations )
+    {
+        java.util.Map<Integer, MapOrganisationUnitRelation> map = new HashMap<Integer, MapOrganisationUnitRelation>();
+        
+        for ( MapOrganisationUnitRelation relation : relations )
+        {
+            map.put( relation.getOrganisationUnit().getId(), relation );
+        }
+        
+        return map;
     }
 
     public Collection<MapOrganisationUnitRelation> getAvailableMapOrganisationUnitRelations( String mapLayerPath )

=== modified file 'dhis-2/dhis-services/dhis-service-mapping/src/main/java/org/hisp/dhis/mapping/hibernate/HibernateMappingStore.java'
--- dhis-2/dhis-services/dhis-service-mapping/src/main/java/org/hisp/dhis/mapping/hibernate/HibernateMappingStore.java	2009-09-01 09:29:11 +0000
+++ dhis-2/dhis-services/dhis-service-mapping/src/main/java/org/hisp/dhis/mapping/hibernate/HibernateMappingStore.java	2009-09-05 16:19:02 +0000
@@ -171,7 +171,7 @@
     }
 
     @SuppressWarnings("unchecked")
-    public Collection<MapOrganisationUnitRelation> getMapOrganisationUnitRelation( Map map, OrganisationUnit organisationUnit )
+    public MapOrganisationUnitRelation getMapOrganisationUnitRelation( Map map, OrganisationUnit organisationUnit )
     {
         Session session = sessionFactory.getCurrentSession();
 
@@ -180,7 +180,7 @@
         criteria.add( Restrictions.eq( "map", map ) );
         criteria.add( Restrictions.eq( "organisationUnit", organisationUnit ) );
 
-        return (Collection<MapOrganisationUnitRelation>) criteria.list();
+        return (MapOrganisationUnitRelation) criteria.uniqueResult();
     }
 
     @SuppressWarnings( "unchecked" )

=== modified file 'dhis-2/dhis-web/dhis-web-mapping/src/main/java/org/hisp/dhis/mapping/action/DeleteMapOrganisationUnitRelationAction.java'
--- dhis-2/dhis-web/dhis-web-mapping/src/main/java/org/hisp/dhis/mapping/action/DeleteMapOrganisationUnitRelationAction.java	2009-09-01 09:29:11 +0000
+++ dhis-2/dhis-web/dhis-web-mapping/src/main/java/org/hisp/dhis/mapping/action/DeleteMapOrganisationUnitRelationAction.java	2009-09-05 16:19:02 +0000
@@ -88,12 +88,10 @@
         
         Map map = mappingService.getMapByMapLayerPath( mapLayerPath );
         
-        MapOrganisationUnitRelation relation;
+        MapOrganisationUnitRelation relation = mappingService.getMapOrganisationUnitRelation( map, unit );
         
-        if ( mappingService.getMapOrganisationUnitRelation( map, unit ).iterator().hasNext() )
-        {
-            relation = mappingService.getMapOrganisationUnitRelation( map, unit ).iterator().next();
-            
+        if ( relation != null )
+        {            
             mappingService.deleteMapOrganisationUnitRelation( relation );
         }