← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 22263: Support CREATE_AND_UPDATE mode for dxf2 importer

 

------------------------------------------------------------
revno: 22263
committer: Morten Olav Hansen <morten@xxxxxxxxx>
branch nick: dhis2
timestamp: Sat 2016-03-12 20:39:37 +0700
message:
  Support CREATE_AND_UPDATE mode for dxf2 importer
modified:
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/DefaultObjectBundleService.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/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/DefaultObjectBundleService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/DefaultObjectBundleService.java	2016-03-12 12:20:28 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/DefaultObjectBundleService.java	2016-03-12 13:39:37 +0000
@@ -97,14 +97,14 @@
     @Override
     public ObjectBundle create( ObjectBundleParams params )
     {
+        if ( params.getUser() == null )
+        {
+            params.setUser( currentUserService.getCurrentUser() );
+        }
+
         ObjectBundle bundle = new ObjectBundle( params );
         bundle.addObjects( params.getObjects() );
 
-        if ( params.getUser() == null )
-        {
-            params.setUser( currentUserService.getCurrentUser() );
-        }
-
         PreheatParams preheatParams = params.getPreheatParams();
         preheatParams.setUser( params.getUser() );
 
@@ -116,7 +116,7 @@
         bundle.setPreheat( preheatService.preheat( preheatParams ) );
         bundle.setObjectReferences( preheatService.collectObjectReferences( params.getObjects() ) );
 
-        if ( !(bundle.getImportMode().isCreate() || bundle.getImportMode().isCreateAndUpdate()) )
+        if ( !bundle.getImportMode().isCreate() )
         {
             return bundle;
         }
@@ -126,27 +126,12 @@
         {
             Map<PreheatIdentifier, Map<Class<? extends IdentifiableObject>, Map<String, IdentifiableObject>>> map = bundle.getPreheat().getMap();
 
-            if ( !map.containsKey( PreheatIdentifier.UID ) )
-            {
-                map.put( PreheatIdentifier.UID, new HashMap<>() );
-            }
-
-            if ( !map.get( PreheatIdentifier.UID ).containsKey( klass ) )
-            {
-                map.get( PreheatIdentifier.UID ).put( klass, new HashMap<>() );
-            }
-
-            if ( !map.containsKey( PreheatIdentifier.CODE ) )
-            {
-                map.put( PreheatIdentifier.CODE, new HashMap<>() );
-            }
-
-            if ( !map.get( PreheatIdentifier.CODE ).containsKey( klass ) )
-            {
-                map.get( PreheatIdentifier.CODE ).put( klass, new HashMap<>() );
-            }
-
-            for ( IdentifiableObject identifiableObject : bundle.getObjects().get( klass ) )
+            if ( !map.containsKey( PreheatIdentifier.UID ) ) map.put( PreheatIdentifier.UID, new HashMap<>() );
+            if ( !map.get( PreheatIdentifier.UID ).containsKey( klass ) ) map.get( PreheatIdentifier.UID ).put( klass, new HashMap<>() );
+            if ( !map.containsKey( PreheatIdentifier.CODE ) ) map.put( PreheatIdentifier.CODE, new HashMap<>() );
+            if ( !map.get( PreheatIdentifier.CODE ).containsKey( klass ) ) map.get( PreheatIdentifier.CODE ).put( klass, new HashMap<>() );
+
+            for ( IdentifiableObject identifiableObject : bundle.getObjects( klass, false ) )
             {
                 if ( Preheat.isDefault( identifiableObject ) ) continue;
 
@@ -274,43 +259,25 @@
 
         for ( Class<? extends IdentifiableObject> klass : klasses )
         {
-            List<IdentifiableObject> objects = bundle.getObjects().get( klass );
-
-            if ( objects.isEmpty() )
-            {
-                continue;
-            }
-
-            switch ( bundle.getImportMode() )
-            {
-                case CREATE_AND_UPDATE:
-                case ATOMIC_CREATE_AND_UPDATE:
-                case NEW_AND_UPDATES:
-                {
-                    handleCreatesAndUpdates( session, objects, bundle );
-                    break;
-                }
-                case CREATE:
-                case ATOMIC_CREATE:
-                case NEW:
-                {
-                    handleCreates( session, objects, bundle );
-                    break;
-                }
-                case UPDATE:
-                case ATOMIC_UPDATE:
-                case UPDATES:
-                {
-                    handleUpdates( session, objects, bundle );
-                    break;
-                }
-                case DELETE:
-                case ATOMIC_DELETE:
-                case DELETES:
-                {
-                    handleDeletes( session, objects, bundle );
-                    break;
-                }
+            List<IdentifiableObject> persistedObjects = bundle.getObjects( klass, true );
+            List<IdentifiableObject> nonPersistedObjects = bundle.getObjects( klass, false );
+
+            if ( bundle.getImportMode().isCreateAndUpdate() )
+            {
+                handleCreates( session, nonPersistedObjects, bundle );
+                handleUpdates( session, persistedObjects, bundle );
+            }
+            else if ( bundle.getImportMode().isCreate() )
+            {
+                handleCreates( session, nonPersistedObjects, bundle );
+            }
+            else if ( bundle.getImportMode().isUpdate() )
+            {
+                handleUpdates( session, persistedObjects, bundle );
+            }
+            else if ( bundle.getImportMode().isDelete() )
+            {
+                handleDeletes( session, persistedObjects, bundle );
             }
 
             if ( FlushMode.AUTO == bundle.getFlushMode() ) session.flush();
@@ -323,13 +290,9 @@
         bundle.setObjectBundleStatus( ObjectBundleStatus.COMMITTED );
     }
 
-    private void handleCreatesAndUpdates( Session session, List<IdentifiableObject> objects, ObjectBundle bundle )
-    {
-
-    }
-
     private void handleCreates( Session session, List<IdentifiableObject> objects, ObjectBundle bundle )
     {
+        if ( objects.isEmpty() ) return;
         log.info( "Creating " + objects.size() + " object(s) of type " + objects.get( 0 ).getClass().getSimpleName() );
 
         for ( IdentifiableObject object : objects )
@@ -357,6 +320,7 @@
 
     private void handleUpdates( Session session, List<IdentifiableObject> objects, ObjectBundle bundle )
     {
+        if ( objects.isEmpty() ) return;
         log.info( "Updating " + objects.size() + " object(s) of type " + objects.get( 0 ).getClass().getSimpleName() );
 
         for ( IdentifiableObject object : objects )
@@ -390,6 +354,7 @@
 
     private void handleDeletes( Session session, List<IdentifiableObject> objects, ObjectBundle bundle )
     {
+        if ( objects.isEmpty() ) return;
         log.info( "Deleting " + objects.size() + " object(s) of type " + objects.get( 0 ).getClass().getSimpleName() );
 
         List<IdentifiableObject> persistedObjects = bundle.getPreheat().getAll( bundle.getPreheatIdentifier(), objects );

=== 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-03-12 12:20:28 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/ObjectBundle.java	2016-03-12 13:39:37 +0000
@@ -171,6 +171,26 @@
         return objects;
     }
 
+    public List<IdentifiableObject> getObjects( Class<? extends IdentifiableObject> klass, boolean persisted )
+    {
+        List<IdentifiableObject> objectMap = new ArrayList<>();
+
+        if ( !objects.containsKey( klass ) )
+        {
+            return objectMap;
+        }
+
+        objects.get( klass ).forEach( object -> {
+            IdentifiableObject cachedObject = preheat.get( preheatIdentifier, object );
+            boolean isPersisted = !(cachedObject == null || cachedObject.getId() == 0);
+
+            if ( persisted && isPersisted ) objectMap.add( object );
+            if ( !persisted && !isPersisted ) objectMap.add( object );
+        } );
+
+        return objectMap;
+    }
+
     public void setObjects( Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> objects )
     {
         this.objects = objects;

=== 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-12 08:52:42 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/metadata2/objectbundle/ObjectBundleServiceTest.java	2016-03-12 13:39:37 +0000
@@ -1195,6 +1195,64 @@
         assertEquals( "admin", userB.getUserCredentials().getUser().getUserCredentials().getUsername() );
     }
 
+    @Test
+    public void testCreateAndUpdateMetadata1() throws IOException
+    {
+        defaultSetup();
+
+        Map<String, DataElement> dataElementMap = manager.getIdMap( DataElement.class, IdScheme.UID );
+        UserGroup userGroup = manager.get( UserGroup.class, "ugabcdefghA" );
+        assertEquals( 4, dataElementMap.size() );
+        assertNotNull( userGroup );
+
+        Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> metadata = renderService.fromMetadata(
+            new ClassPathResource( "dxf2/de_update1.json" ).getInputStream(), RenderFormat.JSON );
+
+        ObjectBundleParams params = new ObjectBundleParams();
+        params.setObjectBundleMode( ObjectBundleMode.COMMIT );
+        params.setImportMode( ImportStrategy.CREATE_AND_UPDATE );
+        params.setObjects( metadata );
+
+        ObjectBundle bundle = objectBundleService.create( params );
+        objectBundleService.validate( bundle );
+        objectBundleService.commit( bundle );
+
+        DataElement dataElementA = dataElementMap.get( "deabcdefghA" );
+        DataElement dataElementB = dataElementMap.get( "deabcdefghB" );
+        DataElement dataElementC = dataElementMap.get( "deabcdefghC" );
+        DataElement dataElementD = dataElementMap.get( "deabcdefghD" );
+
+        assertNotNull( dataElementA );
+        assertNotNull( dataElementB );
+        assertNotNull( dataElementC );
+        assertNotNull( dataElementD );
+
+        assertEquals( "DEA", dataElementA.getName() );
+        assertEquals( "DEB", dataElementB.getName() );
+        assertEquals( "DEC", dataElementC.getName() );
+        assertEquals( "DED", dataElementD.getName() );
+
+        assertEquals( "DECA", dataElementA.getCode() );
+        assertEquals( "DECB", dataElementB.getCode() );
+        assertEquals( "DECC", dataElementC.getCode() );
+        assertEquals( "DECD", dataElementD.getCode() );
+
+        assertEquals( "DESA", dataElementA.getShortName() );
+        assertEquals( "DESB", dataElementB.getShortName() );
+        assertEquals( "DESC", dataElementC.getShortName() );
+        assertEquals( "DESD", dataElementD.getShortName() );
+
+        assertEquals( "DEDA", dataElementA.getDescription() );
+        assertEquals( "DEDB", dataElementB.getDescription() );
+        assertEquals( "DEDC", dataElementC.getDescription() );
+        assertEquals( "DEDD", dataElementD.getDescription() );
+
+        assertEquals( 1, dataElementA.getUserGroupAccesses().size() );
+        assertEquals( 0, dataElementB.getUserGroupAccesses().size() );
+        assertEquals( 1, dataElementC.getUserGroupAccesses().size() );
+        assertEquals( 0, dataElementD.getUserGroupAccesses().size() );
+    }
+
     private void defaultSetup()
     {
         DataElement de1 = createDataElement( 'A' );