← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 21998: wip, proper handling of default category, categoryOption, etc for new dxf2 importer

 

------------------------------------------------------------
revno: 21998
committer: Morten Olav Hansen <morten@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2016-02-18 18:15:51 +0700
message:
  wip, proper handling of default category, categoryOption, etc for new dxf2 importer
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/preheat/Preheat.java
  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
  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-api/src/main/java/org/hisp/dhis/preheat/Preheat.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/preheat/Preheat.java	2016-02-17 09:17:36 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/preheat/Preheat.java	2016-02-18 11:15:51 +0000
@@ -166,6 +166,11 @@
         return this;
     }
 
+    public Map<PreheatIdentifier, Map<Class<? extends IdentifiableObject>, Map<String, IdentifiableObject>>> getMap()
+    {
+        return map;
+    }
+
     public Map<Class<? extends IdentifiableObject>, IdentifiableObject> getDefaults()
     {
         return defaults;
@@ -176,9 +181,14 @@
         this.defaults = defaults;
     }
 
+    public static boolean isDefaultClass( IdentifiableObject object )
+    {
+        return (DataElementCategory.class.isInstance( object ) || DataElementCategoryOption.class.isInstance( object )
+            || DataElementCategoryCombo.class.isInstance( object ));
+    }
+
     public static boolean isDefault( IdentifiableObject object )
     {
-        return (DataElementCategory.class.isInstance( object ) || DataElementCategoryOption.class.isInstance( object )
-            || DataElementCategoryCombo.class.isInstance( object )) && "default".equals( object.getName() );
+        return isDefaultClass( object ) && "default".equals( object.getName() );
     }
 }

=== 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-17 09:17:36 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/preheat/DefaultPreheatService.java	2016-02-18 11:15:51 +0000
@@ -295,7 +295,7 @@
                 }
                 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 )
@@ -308,7 +308,7 @@
                         }
                     }
 
-                    ReflectionUtils.invokeMethod( object, p.getSetterMethod(), objects );
+                    // ReflectionUtils.invokeMethod( object, p.getSetterMethod(), objects );
                 }
             } );
 
@@ -324,6 +324,8 @@
             return;
         }
 
+        Map<Class<? extends IdentifiableObject>, IdentifiableObject> defaults = preheat.getDefaults();
+
         Schema schema = schemaService.getDynamicSchema( object.getClass() );
         schema.getProperties().stream()
             .filter( p -> p.isPersisted() && p.isOwner() && (PropertyType.REFERENCE == p.getPropertyType() || PropertyType.REFERENCE == p.getItemPropertyType()) )
@@ -332,6 +334,12 @@
                 {
                     T refObject = ReflectionUtils.invokeMethod( object, p.getGetterMethod() );
                     T ref = preheat.get( identifier, refObject );
+
+                    if ( Preheat.isDefaultClass( refObject ) && (ref == null || "default".equals( refObject.getName() )) )
+                    {
+                        ref = (T) defaults.get( refObject.getClass() );
+                    }
+
                     ReflectionUtils.invokeMethod( object, p.getSetterMethod(), ref );
                 }
                 else
@@ -339,9 +347,17 @@
                     Collection<T> objects = ReflectionUtils.newCollectionInstance( p.getKlass() );
                     Collection<IdentifiableObject> refObjects = ReflectionUtils.invokeMethod( object, p.getGetterMethod() );
 
-                    for ( IdentifiableObject reference : refObjects )
+                    System.err.println( "refObjects: " + refObjects );
+
+                    for ( IdentifiableObject refObject : refObjects )
                     {
-                        T ref = preheat.get( identifier, (T) reference );
+                        T ref = preheat.get( identifier, (T) refObject );
+
+                        if ( Preheat.isDefaultClass( refObject ) && (ref == null || "default".equals( refObject.getName() )) )
+                        {
+                            ref = (T) defaults.get( refObject.getClass() );
+                        }
+
                         if ( ref != null ) objects.add( ref );
                     }
 

=== 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 09:38:06 +0000
+++ 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
@@ -36,6 +36,8 @@
 import org.hisp.dhis.common.IdentifiableObjectManager;
 import org.hisp.dhis.common.IdentifiableObjectUtils;
 import org.hisp.dhis.preheat.InvalidObject;
+import org.hisp.dhis.preheat.Preheat;
+import org.hisp.dhis.preheat.PreheatIdentifier;
 import org.hisp.dhis.preheat.PreheatMode;
 import org.hisp.dhis.preheat.PreheatParams;
 import org.hisp.dhis.preheat.PreheatService;
@@ -211,9 +213,13 @@
     {
         for ( IdentifiableObject object : objects )
         {
+            if ( Preheat.isDefault( object ) ) continue;
+
             preheatService.connectReferences( object, bundle.getPreheat(), bundle.getPreheatIdentifier() );
             manager.save( object, bundle.getUser() );
 
+            bundle.getPreheat().put( PreheatIdentifier.UID, object );
+
             if ( log.isDebugEnabled() )
             {
                 String msg = "Created object '" + IdentifiableObjectUtils.getDisplayName( object ) + "'";

=== 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-17 09:17:36 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/metadata2/objectbundle/ObjectBundleServiceTest.java	2016-02-18 11:15:51 +0000
@@ -46,7 +46,6 @@
 import org.hisp.dhis.render.RenderFormat;
 import org.hisp.dhis.render.RenderService;
 import org.hisp.dhis.user.User;
-import org.junit.Ignore;
 import org.junit.Test;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.core.io.ClassPathResource;
@@ -425,7 +424,6 @@
     }
 
     @Test
-    @Ignore
     public void testCreateSimpleMetadataUID() throws IOException
     {
         Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> metadata = renderService.fromMetadata(
@@ -443,10 +441,22 @@
         List<OrganisationUnit> organisationUnits = manager.getAll( OrganisationUnit.class );
         List<DataElement> dataElements = manager.getAll( DataElement.class );
         List<DataSet> dataSets = manager.getAll( DataSet.class );
+        Map<Class<? extends IdentifiableObject>, IdentifiableObject> defaults = manager.getDefaults();
 
         assertFalse( organisationUnits.isEmpty() );
         assertFalse( dataElements.isEmpty() );
         assertFalse( dataSets.isEmpty() );
+
+        for ( DataElement dataElement : dataElements )
+        {
+            assertNotNull( dataElement.getCategoryCombo() );
+            assertEquals( defaults.get( DataElementCategoryCombo.class ), dataElement.getCategoryCombo() );
+        }
+
+        assertFalse( dataSets.get( 0 ).getSources().isEmpty() );
+        assertFalse( dataSets.get( 0 ).getDataElements().isEmpty() );
+        assertEquals( 1, dataSets.get( 0 ).getSources().size() );
+        assertEquals( 2, dataSets.get( 0 ).getDataElements().size() );
     }
 
     private void defaultSetup()