← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 22000: various object bundle create mode fixes

 

------------------------------------------------------------
revno: 22000
committer: Morten Olav Hansen <morten@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2016-02-18 20:44:33 +0700
message:
  various object bundle create mode fixes
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/DefaultObjectBundleService.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-18 11:15:51 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/preheat/DefaultPreheatService.java	2016-02-18 13:44:33 +0000
@@ -288,27 +288,33 @@
                     IdentifiableObject refObject = ReflectionUtils.invokeMethod( object, p.getGetterMethod() );
                     IdentifiableObject ref = preheat.get( identifier, refObject );
 
-                    if ( ref == null && refObject != null )
+                    if ( ref == null && refObject != null && !Preheat.isDefault( refObject ) )
                     {
                         preheatValidation.addInvalidReference( object, identifier, refObject, p );
                     }
                 }
                 else
                 {
-                    // Collection<IdentifiableObject> objects = ReflectionUtils.newCollectionInstance( p.getKlass() );
+                    Collection<IdentifiableObject> objects = ReflectionUtils.newCollectionInstance( p.getKlass() );
                     Collection<IdentifiableObject> refObjects = ReflectionUtils.invokeMethod( object, p.getGetterMethod() );
 
                     for ( IdentifiableObject refObject : refObjects )
                     {
+                        if ( Preheat.isDefault( refObject ) ) continue;
+
                         IdentifiableObject ref = preheat.get( identifier, refObject );
 
                         if ( ref == null && refObject != null )
                         {
                             preheatValidation.addInvalidReference( object, identifier, refObject, p );
                         }
+                        else
+                        {
+                            objects.add( refObject );
+                        }
                     }
 
-                    // ReflectionUtils.invokeMethod( object, p.getSetterMethod(), objects );
+                    ReflectionUtils.invokeMethod( object, p.getSetterMethod(), objects );
                 }
             } );
 
@@ -347,8 +353,6 @@
                     Collection<T> objects = ReflectionUtils.newCollectionInstance( p.getKlass() );
                     Collection<IdentifiableObject> refObjects = ReflectionUtils.invokeMethod( object, p.getGetterMethod() );
 
-                    System.err.println( "refObjects: " + refObjects );
-
                     for ( IdentifiableObject refObject : refObjects )
                     {
                         T ref = preheat.get( identifier, (T) refObject );

=== 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-02-18 11:15:51 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/DefaultObjectBundleService.java	2016-02-18 13:44:33 +0000
@@ -47,10 +47,13 @@
 import org.hisp.dhis.user.CurrentUserService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
+import org.springframework.util.StringUtils;
 
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Map;
 
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
@@ -98,6 +101,52 @@
 
         bundle.setPreheat( preheatService.preheat( preheatParams ) );
 
+        if ( !(bundle.getImportMode().isCreate() || bundle.getImportMode().isCreateAndUpdate()) )
+        {
+            return bundle;
+        }
+
+        // add preheat placeholders for objects that will be created
+        for ( Class<? extends IdentifiableObject> klass : bundle.getObjects().keySet() )
+        {
+            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 ( Preheat.isDefault( identifiableObject ) ) continue;
+
+                if ( !StringUtils.isEmpty( identifiableObject.getUid() ) )
+                {
+                    map.get( PreheatIdentifier.UID ).get( klass ).put( identifiableObject.getUid(), identifiableObject );
+                }
+
+                if ( !StringUtils.isEmpty( identifiableObject.getCode() ) )
+                {
+                    map.get( PreheatIdentifier.CODE ).get( klass ).put( identifiableObject.getCode(), identifiableObject );
+                }
+            }
+        }
+
         return bundle;
     }