← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 21797: support getting references from id objects and collections with id object in PreheatService.scanO...

 

------------------------------------------------------------
revno: 21797
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Fri 2016-01-22 12:01:07 +0700
message:
  support getting references from id objects and collections with id object in PreheatService.scanObjectForReferences
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/preheat/PreheatService.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/preheat/DefaultPreheatService.java
  dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/preheat/PreheatServiceTest.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/PreheatService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/preheat/PreheatService.java	2016-01-22 04:05:17 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/preheat/PreheatService.java	2016-01-22 05:01:07 +0000
@@ -30,7 +30,7 @@
 
 import org.hisp.dhis.common.IdentifiableObject;
 
-import java.util.Collection;
+import java.util.List;
 import java.util.Map;
 
 /**
@@ -59,5 +59,5 @@
      * @param identifier Identifier to collect
      * @return Maps classes to collections of identifiers
      */
-    Map<Class<? extends IdentifiableObject>, Collection<String>> scanObjectForReferences( Object object, PreheatIdentifier identifier );
+    Map<Class<? extends IdentifiableObject>, List<String>> scanObjectForReferences( Object object, PreheatIdentifier identifier );
 }

=== 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-01-22 04:05:17 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/preheat/DefaultPreheatService.java	2016-01-22 05:01:07 +0000
@@ -33,6 +33,7 @@
 import org.hisp.dhis.schema.PropertyType;
 import org.hisp.dhis.schema.Schema;
 import org.hisp.dhis.schema.SchemaService;
+import org.hisp.dhis.system.util.ReflectionUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.transaction.annotation.Transactional;
 
@@ -83,9 +84,9 @@
 
     @Override
     @SuppressWarnings( "unchecked" )
-    public Map<Class<? extends IdentifiableObject>, Collection<String>> scanObjectForReferences( Object object, PreheatIdentifier identifier )
+    public Map<Class<? extends IdentifiableObject>, List<String>> scanObjectForReferences( Object object, PreheatIdentifier identifier )
     {
-        Map<Class<? extends IdentifiableObject>, Collection<String>> map = new HashMap<>();
+        Map<Class<? extends IdentifiableObject>, List<String>> map = new HashMap<>();
 
         if ( object == null )
         {
@@ -102,11 +103,51 @@
             {
                 Class<? extends IdentifiableObject> klass = (Class<? extends IdentifiableObject>) p.getKlass();
                 if ( !map.containsKey( klass ) ) map.put( klass, new ArrayList<>() );
+                Object reference = ReflectionUtils.invokeMethod( object, p.getGetterMethod() );
+
+                if ( reference != null )
+                {
+                    IdentifiableObject identifiableObject = (IdentifiableObject) reference;
+
+                    if ( PreheatIdentifier.UID == identifier )
+                    {
+                        if ( identifiableObject.getUid() != null )
+                        {
+                            map.get( klass ).add( identifiableObject.getUid() );
+                        }
+                    }
+                    else if ( PreheatIdentifier.CODE == identifier )
+                    {
+                        if ( identifiableObject.getCode() != null )
+                        {
+                            map.get( klass ).add( identifiableObject.getCode() );
+                        }
+                    }
+                }
             }
             else
             {
                 Class<? extends IdentifiableObject> klass = (Class<? extends IdentifiableObject>) p.getItemKlass();
                 if ( !map.containsKey( klass ) ) map.put( klass, new ArrayList<>() );
+                Collection<IdentifiableObject> reference = ReflectionUtils.invokeMethod( object, p.getGetterMethod() );
+
+                for ( IdentifiableObject identifiableObject : reference )
+                {
+                    if ( PreheatIdentifier.UID == identifier )
+                    {
+                        if ( identifiableObject.getUid() != null )
+                        {
+                            map.get( klass ).add( identifiableObject.getUid() );
+                        }
+                    }
+                    else if ( PreheatIdentifier.CODE == identifier )
+                    {
+                        if ( identifiableObject.getCode() != null )
+                        {
+                            map.get( klass ).add( identifiableObject.getCode() );
+                        }
+                    }
+                }
             }
         } );
 

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/preheat/PreheatServiceTest.java'
--- dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/preheat/PreheatServiceTest.java	2016-01-22 04:05:17 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/preheat/PreheatServiceTest.java	2016-01-22 05:01:07 +0000
@@ -40,9 +40,10 @@
 import org.junit.Test;
 import org.springframework.beans.factory.annotation.Autowired;
 
-import java.util.Collection;
+import java.util.List;
 import java.util.Map;
 
+import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
 
 /**
@@ -92,7 +93,7 @@
         DataElement dataElement = new DataElement( "DataElementA" );
         dataElement.setAutoFields();
 
-        Map<Class<? extends IdentifiableObject>, Collection<String>> references = preheatService.scanObjectForReferences( dataElement, PreheatIdentifier.UID );
+        Map<Class<? extends IdentifiableObject>, List<String>> references = preheatService.scanObjectForReferences( dataElement, PreheatIdentifier.UID );
 
         assertTrue( references.containsKey( OptionSet.class ) );
         assertTrue( references.containsKey( LegendSet.class ) );
@@ -106,9 +107,46 @@
         DataElementGroup dataElementGroup = new DataElementGroup( "DataElementGroupA" );
         dataElementGroup.setAutoFields();
 
-        Map<Class<? extends IdentifiableObject>, Collection<String>> references = preheatService.scanObjectForReferences( dataElementGroup, PreheatIdentifier.UID );
-
-        assertTrue( references.containsKey( DataElement.class ) );
-        assertTrue( references.containsKey( User.class ) );
+        Map<Class<? extends IdentifiableObject>, List<String>> references = preheatService.scanObjectForReferences( dataElementGroup, PreheatIdentifier.UID );
+
+        assertTrue( references.containsKey( DataElement.class ) );
+        assertTrue( references.containsKey( User.class ) );
+    }
+
+    @Test
+    public void testScanReferenceUidDEG()
+    {
+        DataElementGroup dataElementGroup = new DataElementGroup( "DataElementGroupA" );
+        dataElementGroup.setAutoFields();
+
+        DataElement de1 = new DataElement( "DataElement1" );
+        DataElement de2 = new DataElement( "DataElement1" );
+        DataElement de3 = new DataElement( "DataElement1" );
+
+        de1.setAutoFields();
+        de2.setAutoFields();
+        de3.setAutoFields();
+
+        User user = new User();
+        user.setAutoFields();
+
+        dataElementGroup.addDataElement( de1 );
+        dataElementGroup.addDataElement( de2 );
+        dataElementGroup.addDataElement( de3 );
+
+        dataElementGroup.setUser( user );
+
+        Map<Class<? extends IdentifiableObject>, List<String>> references = preheatService.scanObjectForReferences( dataElementGroup, PreheatIdentifier.UID );
+
+        assertTrue( references.containsKey( DataElement.class ) );
+        assertTrue( references.containsKey( User.class ) );
+
+        assertEquals( 3, references.get( DataElement.class ).size() );
+        assertEquals( 1, references.get( User.class ).size() );
+
+        assertTrue( references.get( DataElement.class ).contains( de1.getUid() ) );
+        assertTrue( references.get( DataElement.class ).contains( de2.getUid() ) );
+        assertTrue( references.get( DataElement.class ).contains( de3.getUid() ) );
+        assertEquals( user.getUid(), references.get( User.class ).get( 0 ) );
     }
 }