← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 22256: more robust handling of user.userCredentials.user in new dxf2 importer, needs to be updated after...

 

------------------------------------------------------------
revno: 22256
committer: Morten Olav Hansen <morten@xxxxxxxxx>
branch nick: dhis2
timestamp: Sat 2016-03-12 13:43:22 +0700
message:
  more robust handling of user.userCredentials.user in new dxf2 importer, needs to be updated after import ends since we don't know the order of users, and the 'creator' of a user might not itself have been created yet in an create import
added:
  dhis-2/dhis-services/dhis-service-dxf2/src/test/resources/dxf2/users.json
modified:
  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-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-03-10 11:11:14 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/hooks/UserObjectBundleHook.java	2016-03-12 06:43:22 +0000
@@ -96,9 +96,10 @@
         if ( !objectBundle.getObjects().containsKey( User.class ) ) return;
 
         List<IdentifiableObject> objects = objectBundle.getObjects().get( User.class );
-        Map<String, Map<String, Object>> references = objectBundle.getObjectReferences( User.class );
+        Map<String, Map<String, Object>> userReferences = objectBundle.getObjectReferences( User.class );
+        Map<String, Map<String, Object>> userCredentialsReferences = objectBundle.getObjectReferences( UserCredentials.class );
 
-        if ( references == null || references.isEmpty() )
+        if ( userReferences == null || userReferences.isEmpty() || userCredentialsReferences == null || userCredentialsReferences.isEmpty() )
         {
             return;
         }
@@ -106,19 +107,31 @@
         for ( IdentifiableObject identifiableObject : objects )
         {
             identifiableObject = objectBundle.getPreheat().get( objectBundle.getPreheatIdentifier(), identifiableObject );
-            Map<String, Object> referenceMap = references.get( identifiableObject.getUid() );
+            Map<String, Object> userReferenceMap = userReferences.get( identifiableObject.getUid() );
 
-            if ( referenceMap == null || referenceMap.isEmpty() )
+            if ( userReferenceMap == null || userReferenceMap.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() );
-            sessionFactory.getCurrentSession().update( identifiableObject );
+            Map<String, Object> userCredentialsReferenceMap = userCredentialsReferences.get( user.getUserCredentials().getUid() );
+
+            if ( userCredentialsReferenceMap == null || userCredentialsReferenceMap.isEmpty() )
+            {
+                continue;
+            }
+
+            user.setOrganisationUnits( (Set<OrganisationUnit>) userReferenceMap.get( "organisationUnits" ) );
+            user.setDataViewOrganisationUnits( (Set<OrganisationUnit>) userReferenceMap.get( "dataViewOrganisationUnits" ) );
+
+            user.getUserCredentials().setUser( (User) userCredentialsReferenceMap.get( "user" ) );
+
+            preheatService.connectReferences( user, objectBundle.getPreheat(), objectBundle.getPreheatIdentifier() );
+            preheatService.connectReferences( user.getUserCredentials(), objectBundle.getPreheat(), objectBundle.getPreheatIdentifier() );
+
+            sessionFactory.getCurrentSession().update( user.getUserCredentials() );
+            sessionFactory.getCurrentSession().update( user );
         }
     }
 }

=== 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-03-10 13:24:10 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/metadata2/objectbundle/ObjectBundleServiceTest.java	2016-03-12 06:43:22 +0000
@@ -1103,6 +1103,44 @@
         assertEquals( "vAczVs4mxna", validationRule2.getRightSide().getDataElementsInExpression().iterator().next().getUid() );
     }
 
+    @Test
+    public void testCreateUsers() throws IOException
+    {
+        Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> metadata = renderService.fromMetadata(
+            new ClassPathResource( "dxf2/users.json" ).getInputStream(), RenderFormat.JSON );
+
+        ObjectBundleParams params = new ObjectBundleParams();
+        params.setObjectBundleMode( ObjectBundleMode.COMMIT );
+        params.setImportMode( ImportStrategy.CREATE );
+        params.setObjects( metadata );
+
+        ObjectBundle bundle = objectBundleService.create( params );
+        ObjectBundleValidation validate = objectBundleService.validate( bundle );
+        assertTrue( validate.getObjectErrorReports().isEmpty() );
+        objectBundleService.commit( bundle );
+
+        List<User> users = manager.getAll( User.class );
+        assertEquals( 3, users.size() );
+
+        User userA = manager.get( User.class, "sPWjoHSY03y" );
+        User userB = manager.get( User.class, "MwhEJUnTHkn" );
+
+        assertNotNull( userA );
+        assertNotNull( userB );
+
+        assertNotNull( userA.getUserCredentials().getUserInfo() );
+        assertNotNull( userB.getUserCredentials().getUserInfo() );
+        assertNotNull( userA.getUserCredentials().getUserInfo().getUserCredentials() );
+        assertNotNull( userB.getUserCredentials().getUserInfo().getUserCredentials() );
+        assertEquals( "UserA", userA.getUserCredentials().getUserInfo().getUserCredentials().getUsername() );
+        assertEquals( "UserB", userB.getUserCredentials().getUserInfo().getUserCredentials().getUsername() );
+
+        assertNotNull( userA.getUserCredentials().getUser() );
+        assertNotNull( userB.getUserCredentials().getUser() );
+        assertEquals( "admin", userA.getUserCredentials().getUser().getUserCredentials().getUsername() );
+        assertEquals( "admin", userB.getUserCredentials().getUser().getUserCredentials().getUsername() );
+    }
+
     private void defaultSetup()
     {
         DataElement de1 = createDataElement( 'A' );

=== added file 'dhis-2/dhis-services/dhis-service-dxf2/src/test/resources/dxf2/users.json'
--- dhis-2/dhis-services/dhis-service-dxf2/src/test/resources/dxf2/users.json	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/test/resources/dxf2/users.json	2016-03-12 06:43:22 +0000
@@ -0,0 +1,267 @@
+{
+  "categoryCombos": [
+    {
+      "publicAccess": "--------",
+      "name": "default",
+      "lastUpdated": "2016-03-12T06:18:23.544+0000",
+      "created": "2016-03-12T06:18:23.542+0000",
+      "skipTotal": false,
+      "categories": [
+        {
+          "id": "xO3nVNouT8B"
+        }
+      ],
+      "id": "frKSiFeYBD2",
+      "userGroupAccesses": [ ]
+    }
+  ],
+  "userRoles": [
+    {
+      "dataSets": [ ],
+      "publicAccess": "--------",
+      "name": "Superuser",
+      "programs": [ ],
+      "lastUpdated": "2016-03-12T06:18:41.386+0000",
+      "created": "2016-03-12T06:18:41.386+0000",
+      "userGroupAccesses": [ ],
+      "authorities": [
+        "F_TRACKED_ENTITY_INSTANCE_SEARCH_IN_ALL_ORGUNITS",
+        "ALL",
+        "F_USERGROUP_MANAGING_RELATIONSHIPS_ADD",
+        "F_REPORTTABLE_PUBLIC_ADD",
+        "F_TRACKED_ENTITY_INSTANCE_DELETE",
+        "F_USER_GROUPS_READ_ONLY_ADD_MEMBERS",
+        "F_MAP_PUBLIC_ADD",
+        "F_USER_ADD_WITHIN_MANAGED_GROUP",
+        "F_TRACKED_ENTITY_INSTANCE_SEARCH",
+        "F_PROGRAM_ENROLLMENT",
+        "F_REPORTTABLE_EXTERNAL",
+        "F_SQLVIEW_EXTERNAL",
+        "F_GIS_ADMIN",
+        "F_REPLICATE_USER",
+        "F_INSERT_CUSTOM_JS_CSS",
+        "F_DASHBOARD_PUBLIC_ADD",
+        "F_METADATA_IMPORT",
+        "F_CHART_PUBLIC_ADD",
+        "F_VIEW_UNAPPROVED_DATA",
+        "F_CHART_EXTERNAL",
+        "F_USERGROUP_MANAGING_RELATIONSHIPS_VIEW",
+        "F_METADATA_EXPORT",
+        "F_PROGRAM_UNENROLLMENT",
+        "F_APPROVE_DATA",
+        "F_ACCEPT_DATA_LOWER_LEVELS",
+        "F_TRACKED_ENTITY_INSTANCE_ADD",
+        "F_USERGROUP_PUBLIC_ADD",
+        "F_OAUTH2_CLIENT_MANAGE",
+        "F_TRACKED_ENTITY_DATAVALUE_ADD",
+        "F_PROGRAM_DASHBOARD_CONFIG_ADMIN",
+        "F_MAP_EXTERNAL",
+        "F_APPROVE_DATA_LOWER_LEVELS",
+        "F_TRACKED_ENTITY_DATAVALUE_DELETE"
+      ],
+      "id": "tHEWGwIMsJk"
+    }
+  ],
+  "categoryOptions": [
+    {
+      "name": "default",
+      "publicAccess": "--------",
+      "userGroupAccesses": [ ],
+      "attributeValues": [ ],
+      "id": "l9KXrytvyib",
+      "organisationUnits": [ ],
+      "created": "2016-03-12T06:18:23.528+0000",
+      "lastUpdated": "2016-03-12T06:18:23.546+0000"
+    }
+  ],
+  "date": "2016-03-12T06:21:34.667+0000",
+  "categoryOptionCombos": [
+    {
+      "name": "default",
+      "created": "2016-03-12T06:18:23.543+0000",
+      "lastUpdated": "2016-03-12T06:18:23.543+0000",
+      "categoryCombo": {
+        "id": "frKSiFeYBD2"
+      },
+      "categoryOptions": [
+        {
+          "id": "l9KXrytvyib"
+        }
+      ],
+      "ignoreApproval": false,
+      "id": "gf1lAXwR7G5"
+    }
+  ],
+  "organisationUnits": [
+    {
+      "path": "/inVD5SdytkT",
+      "openingDate": "2016-03-11T17:00:00.000+0000",
+      "lastUpdated": "2016-03-12T06:19:49.665+0000",
+      "created": "2016-03-12T06:19:49.649+0000",
+      "shortName": "Country",
+      "user": {
+        "id": "enHApD3I6Ho"
+      },
+      "attributeValues": [ ],
+      "id": "inVD5SdytkT",
+      "name": "Country",
+      "description": "",
+      "featureType": "NONE",
+      "uuid": "0f2d7a85-3b6e-4cae-9d5a-e2903331629b"
+    }
+  ],
+  "trackedEntities": [
+    {
+      "id": "xjZaLLspj2r",
+      "attributeValues": [ ],
+      "description": "Person",
+      "name": "Person"
+    }
+  ],
+  "categories": [
+    {
+      "id": "xO3nVNouT8B",
+      "userGroupAccesses": [ ],
+      "dataDimension": true,
+      "lastUpdated": "2016-03-12T06:18:23.683+0000",
+      "created": "2016-03-12T06:18:23.540+0000",
+      "categoryOptions": [
+        {
+          "id": "l9KXrytvyib"
+        }
+      ],
+      "publicAccess": "--------",
+      "name": "default"
+    }
+  ],
+  "users": [
+    {
+      "email": "user@xxxxx",
+      "surname": "BB",
+      "firstName": "User",
+      "created": "2016-03-12T06:21:02.324+0000",
+      "lastUpdated": "2016-03-12T06:21:02.325+0000",
+      "dataViewOrganisationUnits": [
+        {
+          "id": "inVD5SdytkT"
+        }
+      ],
+      "organisationUnits": [
+        {
+          "id": "inVD5SdytkT"
+        }
+      ],
+      "userCredentials": {
+        "user": {
+          "id": "enHApD3I6Ho"
+        },
+        "cogsDimensionConstraints": [ ],
+        "selfRegistered": false,
+        "catDimensionConstraints": [ ],
+        "lastLogin": "2016-03-12T06:21:02.226+0000",
+        "created": "2016-03-12T06:21:02.329+0000",
+        "passwordLastUpdated": "2016-03-12T06:21:02.226+0000",
+        "userInfo": {
+          "id": "MwhEJUnTHkn"
+        },
+        "userRoles": [
+          {
+            "id": "tHEWGwIMsJk"
+          }
+        ],
+        "username": "UserB",
+        "invitation": false,
+        "disabled": false,
+        "externalAuth": false
+      },
+      "teiSearchOrganisationUnits": [ ],
+      "id": "MwhEJUnTHkn",
+      "attributeValues": [ ]
+    },
+    {
+      "firstName": "User",
+      "email": "user@xxxxx",
+      "surname": "AA",
+      "attributeValues": [ ],
+      "id": "sPWjoHSY03y",
+      "created": "2016-03-12T06:20:43.502+0000",
+      "lastUpdated": "2016-03-12T06:20:43.502+0000",
+      "dataViewOrganisationUnits": [
+        {
+          "id": "inVD5SdytkT"
+        }
+      ],
+      "organisationUnits": [
+        {
+          "id": "inVD5SdytkT"
+        }
+      ],
+      "userCredentials": {
+        "username": "UserA",
+        "userRoles": [
+          {
+            "id": "tHEWGwIMsJk"
+          }
+        ],
+        "invitation": false,
+        "disabled": false,
+        "externalAuth": false,
+        "cogsDimensionConstraints": [ ],
+        "user": {
+          "id": "enHApD3I6Ho"
+        },
+        "selfRegistered": false,
+        "catDimensionConstraints": [ ],
+        "lastLogin": "2016-03-12T06:20:43.407+0000",
+        "created": "2016-03-12T06:20:43.508+0000",
+        "userInfo": {
+          "id": "sPWjoHSY03y"
+        },
+        "passwordLastUpdated": "2016-03-12T06:20:43.407+0000"
+      },
+      "teiSearchOrganisationUnits": [ ]
+    },
+    {
+      "surname": "admin",
+      "firstName": "admin",
+      "organisationUnits": [
+        {
+          "id": "inVD5SdytkT"
+        }
+      ],
+      "dataViewOrganisationUnits": [
+        {
+          "id": "inVD5SdytkT"
+        }
+      ],
+      "userCredentials": {
+        "externalAuth": false,
+        "disabled": false,
+        "invitation": false,
+        "userRoles": [
+          {
+            "id": "tHEWGwIMsJk"
+          }
+        ],
+        "username": "admin",
+        "passwordLastUpdated": "2016-03-12T06:18:41.403+0000",
+        "userInfo": {
+          "id": "enHApD3I6Ho"
+        },
+        "created": "2016-03-12T06:18:41.505+0000",
+        "lastLogin": "2016-03-12T06:18:41.401+0000",
+        "catDimensionConstraints": [ ],
+        "selfRegistered": false,
+        "user": {
+          "id": "enHApD3I6Ho"
+        },
+        "cogsDimensionConstraints": [ ]
+      },
+      "lastUpdated": "2016-03-12T06:20:18.336+0000",
+      "created": "2016-03-12T06:18:41.374+0000",
+      "teiSearchOrganisationUnits": [ ],
+      "id": "enHApD3I6Ho",
+      "attributeValues": [ ]
+    }
+  ]
+}