← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 18384: wip, uuid matching on idObjectBridge (for dxf2 imports)

 

------------------------------------------------------------
revno: 18384
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2015-02-24 17:40:21 +0700
message:
  wip, uuid matching on idObjectBridge (for dxf2 imports)
modified:
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/common/DefaultIdentifiableObjectManager.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/DefaultObjectBridge.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/common/DefaultIdentifiableObjectManager.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/common/DefaultIdentifiableObjectManager.java	2015-02-20 09:31:37 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/common/DefaultIdentifiableObjectManager.java	2015-02-24 10:40:21 +0000
@@ -33,10 +33,12 @@
 import org.hibernate.SessionFactory;
 import org.hisp.dhis.common.NameableObject.NameableProperty;
 import org.hisp.dhis.common.comparator.IdentifiableObjectNameComparator;
-import org.hisp.dhis.query.QueryService;
+import org.hisp.dhis.organisationunit.OrganisationUnit;
+import org.hisp.dhis.organisationunit.OrganisationUnitService;
 import org.hisp.dhis.user.UserCredentials;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.transaction.annotation.Transactional;
+import org.springframework.util.StringUtils;
 
 import java.util.ArrayList;
 import java.util.Collection;
@@ -67,7 +69,7 @@
     private SessionFactory sessionFactory;
 
     @Autowired
-    private QueryService queryService;
+    private OrganisationUnitService organisationUnitService;
 
     private Map<Class<? extends IdentifiableObject>, GenericIdentifiableObjectStore<? extends IdentifiableObject>> identifiableObjectStoreMap;
 
@@ -698,6 +700,15 @@
                     map.put( object.getName(), object );
                 }
             }
+            else if ( IdentifiableProperty.UUID.equals( property ) && OrganisationUnit.class.isAssignableFrom( clazz ) )
+            {
+                OrganisationUnit organisationUnit = (OrganisationUnit) object;
+
+                if ( !StringUtils.isEmpty( organisationUnit.getUuid() ) )
+                {
+                    map.put( organisationUnit.getUuid(), (T) organisationUnit );
+                }
+            }
         }
 
         return map;
@@ -770,6 +781,13 @@
             {
                 return store.getByUid( id );
             }
+            else if ( IdentifiableProperty.UUID.equals( property ) )
+            {
+                if ( OrganisationUnit.class.isAssignableFrom( clazz ) )
+                {
+                    return (T) organisationUnitService.getOrganisationUnitByUuid( id );
+                }
+            }
             else if ( IdentifiableProperty.CODE.equals( property ) )
             {
                 return store.getByCode( id );

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/DefaultObjectBridge.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/DefaultObjectBridge.java	2015-02-03 06:04:09 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/DefaultObjectBridge.java	2015-02-24 10:40:21 +0000
@@ -29,7 +29,6 @@
  */
 
 import com.google.common.collect.Lists;
-import com.google.common.collect.Maps;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.hibernate.SessionFactory;
@@ -38,6 +37,8 @@
 import org.hisp.dhis.common.IdentifiableObject;
 import org.hisp.dhis.common.IdentifiableObjectManager;
 import org.hisp.dhis.common.IdentifiableProperty;
+import org.hisp.dhis.organisationunit.OrganisationUnit;
+import org.hisp.dhis.organisationunit.OrganisationUnitService;
 import org.hisp.dhis.period.PeriodStore;
 import org.hisp.dhis.period.PeriodType;
 import org.hisp.dhis.schema.Schema;
@@ -50,6 +51,7 @@
 import org.hisp.dhis.user.UserCredentials;
 import org.hisp.dhis.user.UserService;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.util.StringUtils;
 
 import javax.annotation.PostConstruct;
 import java.util.ArrayList;
@@ -91,6 +93,9 @@
     private SessionFactory sessionFactory;
 
     @Autowired
+    private OrganisationUnitService organisationUnitService;
+
+    @Autowired
     private CurrentUserService currentUserService;
 
     //-------------------------------------------------------------------------------------------------------
@@ -105,6 +110,8 @@
 
     private Map<Class<? extends IdentifiableObject>, Map<String, IdentifiableObject>> uidMap;
 
+    private Map<Class<? extends IdentifiableObject>, Map<String, IdentifiableObject>> uuidMap;
+
     private Map<Class<? extends IdentifiableObject>, Map<String, IdentifiableObject>> codeMap;
 
     private Map<Class<? extends IdentifiableObject>, Map<String, IdentifiableObject>> nameMap;
@@ -137,12 +144,13 @@
         log.info( "Building object-bridge maps (preheatCache: " + preheatCache + ")." );
         Timer timer = new SystemTimer().start();
 
-        masterMap = Maps.newHashMap();
-        periodTypeMap = Maps.newHashMap();
-        uidMap = Maps.newHashMap();
-        codeMap = Maps.newHashMap();
-        nameMap = Maps.newHashMap();
-        usernameMap = Maps.newHashMap();
+        masterMap = new HashMap<>();
+        periodTypeMap = new HashMap<>();
+        uidMap = new HashMap<>();
+        uuidMap = new HashMap<>();
+        codeMap = new HashMap<>();
+        nameMap = new HashMap<>();
+        usernameMap = new HashMap<>();
 
         populatePeriodTypeMap( PeriodType.class );
         populateUsernameMap( UserCredentials.class );
@@ -151,6 +159,7 @@
         {
             populateIdentifiableObjectMap( type );
             populateIdentifiableObjectMap( type, IdentifiableProperty.UID );
+            populateIdentifiableObjectMap( type, IdentifiableProperty.UUID );
             populateIdentifiableObjectMap( type, IdentifiableProperty.CODE );
             populateIdentifiableObjectMap( type, IdentifiableProperty.NAME );
         }
@@ -215,6 +224,10 @@
             {
                 uidMap.put( (Class<? extends IdentifiableObject>) clazz, map );
             }
+            else if ( property == IdentifiableProperty.UUID && OrganisationUnit.class.isAssignableFrom( clazz ) )
+            {
+                uuidMap.put( (Class<? extends IdentifiableObject>) clazz, map );
+            }
             else if ( property == IdentifiableProperty.CODE && identifiableObject.haveUniqueCode() )
             {
                 codeMap.put( (Class<? extends IdentifiableObject>) clazz, map );
@@ -461,6 +474,16 @@
             }
         }
 
+        if ( OrganisationUnit.class.isInstance( object ) )
+        {
+            OrganisationUnit organisationUnit = (OrganisationUnit) getUuidMatch( (OrganisationUnit) object );
+
+            if ( organisationUnit != null )
+            {
+                objects.add( (T) organisationUnit );
+            }
+        }
+
         if ( IdentifiableObject.class.isInstance( object ) )
         {
             IdentifiableObject identifiableObject = (IdentifiableObject) object;
@@ -501,6 +524,37 @@
 
     private <T> void _updateInternalMaps( T object, boolean delete )
     {
+        if ( OrganisationUnit.class.isInstance( object ) )
+        {
+            OrganisationUnit organisationUnit = (OrganisationUnit) object;
+
+            if ( !StringUtils.isEmpty( organisationUnit.getUuid() ) )
+            {
+                Map<String, IdentifiableObject> map = uuidMap.get( OrganisationUnit.class );
+
+                if ( map == null )
+                {
+                    // might be dynamically sub-classed by javassist or cglib, fetch superclass and try again
+                    map = uuidMap.get( OrganisationUnit.class.getSuperclass() );
+                }
+
+                if ( !delete )
+                {
+                    map.put( organisationUnit.getUuid(), organisationUnit );
+                }
+                else
+                {
+                    try
+                    {
+                        map.remove( organisationUnit.getUuid() );
+                    }
+                    catch ( NullPointerException ignored )
+                    {
+                    }
+                }
+            }
+        }
+
         if ( IdentifiableObject.class.isInstance( object ) )
         {
             IdentifiableObject identifiableObject = (IdentifiableObject) object;
@@ -586,6 +640,35 @@
         }
     }
 
+    private IdentifiableObject getUuidMatch( OrganisationUnit organisationUnit )
+    {
+        Class<? extends OrganisationUnit> klass = organisationUnit.getClass();
+        Map<String, IdentifiableObject> map = uuidMap.get( klass );
+        OrganisationUnit entity = null;
+
+        if ( map != null )
+        {
+            entity = (OrganisationUnit) map.get( organisationUnit.getUuid() );
+        }
+        else
+        {
+            uuidMap.put( klass, new HashMap<String, IdentifiableObject>() );
+            map = uuidMap.get( klass );
+        }
+
+        if ( !preheatCache && entity == null )
+        {
+            entity = organisationUnitService.getOrganisationUnitByUuid( organisationUnit.getUuid() );
+
+            if ( entity != null )
+            {
+                map.put( entity.getUuid(), entity );
+            }
+        }
+
+        return entity;
+    }
+
     private IdentifiableObject getUidMatch( IdentifiableObject identifiableObject )
     {
         Map<String, IdentifiableObject> map = uidMap.get( identifiableObject.getClass() );