← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 22051: Properly re-attach User OU references when importing User (using postImport hooks)

 

------------------------------------------------------------
revno: 22051
committer: Morten Olav Hansen <morten@xxxxxxxxx>
branch nick: dhis2
timestamp: Fri 2016-02-26 17:05:22 +0700
message:
  Properly re-attach User OU references when importing User (using postImport hooks)
modified:
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/preheat/DefaultPreheatService.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/ObjectBundle.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/hooks/UserObjectBundleHook.java
  dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/metadata2/objectbundle/ObjectBundleServiceTest.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/preheat/DefaultPreheatService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/preheat/DefaultPreheatService.java	2016-02-26 09:07:23 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/preheat/DefaultPreheatService.java	2016-02-26 10:05:22 +0000
@@ -29,7 +29,6 @@
  */
 
 import com.google.common.collect.Lists;
-import org.hisp.dhis.common.BaseIdentifiableObject;
 import org.hisp.dhis.common.IdentifiableObject;
 import org.hisp.dhis.common.IdentifiableObjectManager;
 import org.hisp.dhis.common.MergeMode;
@@ -340,9 +339,15 @@
 
                         if ( reference != null )
                         {
-                            IdentifiableObject identifiableObject = new BaseIdentifiableObject();
-                            identifiableObject.mergeWith( reference, MergeMode.REPLACE );
-                            objectReferenceMap.get( object.getUid() ).put( p.getName(), identifiableObject );
+                            try
+                            {
+                                IdentifiableObject identifiableObject = (IdentifiableObject) p.getKlass().newInstance();
+                                identifiableObject.mergeWith( reference, MergeMode.REPLACE );
+                                objectReferenceMap.get( object.getUid() ).put( p.getName(), identifiableObject );
+                            }
+                            catch ( InstantiationException | IllegalAccessException ignored )
+                            {
+                            }
                         }
                     }
                     else
@@ -354,9 +359,16 @@
                         {
                             for ( IdentifiableObject reference : references )
                             {
-                                IdentifiableObject identifiableObject = new BaseIdentifiableObject();
-                                identifiableObject.mergeWith( reference, MergeMode.REPLACE );
-                                refObjects.add( identifiableObject );
+                                try
+                                {
+                                    IdentifiableObject identifiableObject = (IdentifiableObject) p.getItemKlass().newInstance();
+                                    identifiableObject.mergeWith( reference, MergeMode.REPLACE );
+                                    refObjects.add( identifiableObject );
+                                }
+                                catch ( InstantiationException | IllegalAccessException ignored )
+                                {
+                                }
+
                             }
                         }
 

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/ObjectBundle.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/ObjectBundle.java	2016-02-26 09:07:23 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/ObjectBundle.java	2016-02-26 10:05:22 +0000
@@ -171,6 +171,11 @@
         this.objects = objects;
     }
 
+    public Map<String, Map<String, Object>> getObjectReferences( Class<?> klass )
+    {
+        return objectReferences.get( klass );
+    }
+
     public Map<Class<?>, Map<String, Map<String, Object>>> getObjectReferences()
     {
         return objectReferences;

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/hooks/UserObjectBundleHook.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/hooks/UserObjectBundleHook.java	2016-02-26 09:07:23 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/hooks/UserObjectBundleHook.java	2016-02-26 10:05:22 +0000
@@ -30,6 +30,7 @@
 
 import org.hisp.dhis.common.IdentifiableObject;
 import org.hisp.dhis.dxf2.metadata2.objectbundle.ObjectBundle;
+import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.user.User;
 import org.hisp.dhis.user.UserCredentials;
 import org.hisp.dhis.user.UserService;
@@ -38,6 +39,8 @@
 import org.springframework.util.StringUtils;
 
 import java.util.List;
+import java.util.Map;
+import java.util.Set;
 
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
@@ -76,6 +79,7 @@
     }
 
     @Override
+    @SuppressWarnings( "unchecked" )
     public void postImport( ObjectBundle objectBundle )
     {
         if ( !objectBundle.getObjects().containsKey( User.class ) )
@@ -84,11 +88,28 @@
         }
 
         List<IdentifiableObject> objects = objectBundle.getObjects().get( User.class );
+        Map<String, Map<String, Object>> references = objectBundle.getObjectReferences( User.class );
+
+        if ( references == null || references.isEmpty() )
+        {
+            return;
+        }
 
         for ( IdentifiableObject identifiableObject : objects )
         {
+            Map<String, Object> referenceMap = references.get( identifiableObject.getUid() );
+
+            if ( referenceMap.isEmpty() )
+            {
+                continue;
+            }
+
             User user = (User) identifiableObject;
+            user.setOrganisationUnits( (Set<OrganisationUnit>) referenceMap.get( "organisationUnits" ) );
+            user.setDataViewOrganisationUnits( (Set<OrganisationUnit>) referenceMap.get( "dataViewOrganisationUnits" ) );
+
             preheatService.connectReferences( identifiableObject, objectBundle.getPreheat(), objectBundle.getPreheatIdentifier() );
+
             manager.update( identifiableObject );
         }
     }

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/metadata2/objectbundle/ObjectBundleServiceTest.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/metadata2/objectbundle/ObjectBundleServiceTest.java	2016-02-26 09:07:23 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/metadata2/objectbundle/ObjectBundleServiceTest.java	2016-02-26 10:05:22 +0000
@@ -380,7 +380,7 @@
 
         ObjectBundle bundle = objectBundleService.create( params );
         ObjectBundleValidation validate = objectBundleService.validate( bundle );
-        
+
         assertNotNull( validate );
     }
 
@@ -474,7 +474,8 @@
         assertNotNull( user.getUserCredentials() );
         assertEquals( "admin", user.getUserCredentials().getUsername() );
         assertFalse( user.getUserCredentials().getUserAuthorityGroups().isEmpty() );
-        // assertFalse( user.getOrganisationUnits().isEmpty() );
+        assertFalse( user.getOrganisationUnits().isEmpty() );
+        assertEquals( "PdWlltZnVZe", user.getOrganisationUnit().getUid() );
     }
 
     private void defaultSetup()