← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 21844: make PreheatService.collectReferences collect all references without using PreheatIdentifier, mak...

 

------------------------------------------------------------
revno: 21844
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2016-02-01 16:41:43 +0700
message:
  make PreheatService.collectReferences collect all references without using PreheatIdentifier, make it more useful for AUTO mode attachment, updated tests
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
  dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/metadata2/MetadataImportServiceTest.java
  dhis-2/dhis-support/dhis-support-test/src/main/java/org/hisp/dhis/DhisConvenienceTest.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-02-01 07:10:53 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/preheat/PreheatService.java	2016-02-01 09:41:43 +0000
@@ -55,20 +55,25 @@
     /**
      * Scan object and collect all references (both id object and collections with id objects).
      *
-     * @param object     Object to scan
-     * @param identifier Identifier to collect
+     * @param object Object to scan
      * @return Maps classes to collections of identifiers
      */
-    Map<Class<? extends IdentifiableObject>, Set<String>> collectReferences( Object object, PreheatIdentifier identifier );
+    Map<PreheatIdentifier, Map<Class<? extends IdentifiableObject>, Set<String>>> collectReferences( Object object );
 
     /**
      * Scan objects and collect all references (both id object and collections with id objects).
      *
-     * @param objects    Objects to scan
-     * @param identifier Identifier to collect
+     * @param objects Objects to scan
      * @return Maps classes to collections of identifiers
      */
-    Map<Class<? extends IdentifiableObject>, Set<String>> collectReferences( Set<Object> objects, PreheatIdentifier identifier );
+    Map<PreheatIdentifier, Map<Class<? extends IdentifiableObject>, Set<String>>> collectReferences( Set<Object> objects );
 
+    /**
+     * Connects id object references on a given object using a given identifier + a preheated Preheat cache.
+     *
+     * @param object     Object to connect to
+     * @param preheat    Preheat Cache to use
+     * @param identifier Use this identifier type to attach references
+     */
     <T extends IdentifiableObject> void connectReferences( T object, Preheat preheat, 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-02-01 07:10:53 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/preheat/DefaultPreheatService.java	2016-02-01 09:41:43 +0000
@@ -122,16 +122,22 @@
     }
 
     @Override
-    public Map<Class<? extends IdentifiableObject>, Set<String>> collectReferences( Object object, PreheatIdentifier identifier )
+    public Map<PreheatIdentifier, Map<Class<? extends IdentifiableObject>, Set<String>>> collectReferences( Object object )
     {
-        return collectReferences( Sets.newHashSet( object ), identifier );
+        return collectReferences( Sets.newHashSet( object ) );
     }
 
     @Override
     @SuppressWarnings( "unchecked" )
-    public Map<Class<? extends IdentifiableObject>, Set<String>> collectReferences( Set<Object> objects, PreheatIdentifier identifier )
+    public Map<PreheatIdentifier, Map<Class<? extends IdentifiableObject>, Set<String>>> collectReferences( Set<Object> objects )
     {
-        Map<Class<? extends IdentifiableObject>, Set<String>> map = new HashMap<>();
+        Map<PreheatIdentifier, Map<Class<? extends IdentifiableObject>, Set<String>>> map = new HashMap<>();
+
+        map.put( PreheatIdentifier.UID, new HashMap<>() );
+        map.put( PreheatIdentifier.CODE, new HashMap<>() );
+
+        Map<Class<? extends IdentifiableObject>, Set<String>> uidMap = map.get( PreheatIdentifier.UID );
+        Map<Class<? extends IdentifiableObject>, Set<String>> codeMap = map.get( PreheatIdentifier.CODE );
 
         if ( objects.isEmpty() )
         {
@@ -149,7 +155,10 @@
                 if ( !p.isCollection() )
                 {
                     Class<? extends IdentifiableObject> klass = (Class<? extends IdentifiableObject>) p.getKlass();
-                    if ( !map.containsKey( klass ) ) map.put( klass, new HashSet<>() );
+
+                    if ( !uidMap.containsKey( klass ) ) uidMap.put( klass, new HashSet<>() );
+                    if ( !codeMap.containsKey( klass ) ) codeMap.put( klass, new HashSet<>() );
+
                     Object reference = ReflectionUtils.invokeMethod( object, p.getGetterMethod() );
 
                     if ( reference != null )
@@ -159,20 +168,17 @@
                         String uid = identifiableObject.getUid();
                         String code = identifiableObject.getCode();
 
-                        if ( PreheatIdentifier.UID == identifier )
-                        {
-                            if ( uid != null ) map.get( klass ).add( uid );
-                        }
-                        else if ( PreheatIdentifier.CODE == identifier )
-                        {
-                            if ( code != null ) map.get( klass ).add( code );
-                        }
+                        if ( uid != null ) uidMap.get( klass ).add( uid );
+                        if ( code != null ) codeMap.get( klass ).add( code );
                     }
                 }
                 else
                 {
                     Class<? extends IdentifiableObject> klass = (Class<? extends IdentifiableObject>) p.getItemKlass();
-                    if ( !map.containsKey( klass ) ) map.put( klass, new HashSet<>() );
+
+                    if ( !uidMap.containsKey( klass ) ) uidMap.put( klass, new HashSet<>() );
+                    if ( !codeMap.containsKey( klass ) ) codeMap.put( klass, new HashSet<>() );
+
                     Collection<IdentifiableObject> reference = ReflectionUtils.invokeMethod( object, p.getGetterMethod() );
 
                     for ( IdentifiableObject identifiableObject : reference )
@@ -180,14 +186,8 @@
                         String uid = identifiableObject.getUid();
                         String code = identifiableObject.getCode();
 
-                        if ( PreheatIdentifier.UID == identifier )
-                        {
-                            if ( uid != null ) map.get( klass ).add( uid );
-                        }
-                        else if ( PreheatIdentifier.CODE == identifier )
-                        {
-                            if ( code != null ) map.get( klass ).add( code );
-                        }
+                        if ( uid != null ) uidMap.get( klass ).add( uid );
+                        if ( code != null ) codeMap.get( klass ).add( code );
                     }
                 }
             }

=== 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-02-01 08:40:20 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/preheat/PreheatServiceTest.java	2016-02-01 09:41:43 +0000
@@ -37,6 +37,7 @@
 import org.hisp.dhis.dataelement.DataElementGroup;
 import org.hisp.dhis.legend.LegendSet;
 import org.hisp.dhis.option.OptionSet;
+import org.hisp.dhis.render.RenderService;
 import org.hisp.dhis.user.User;
 import org.junit.Ignore;
 import org.junit.Test;
@@ -62,6 +63,15 @@
     @Autowired
     private IdentifiableObjectManager manager;
 
+    @Autowired
+    private RenderService _renderService;
+
+    @Override
+    protected void setUpTest() throws Exception
+    {
+        renderService = _renderService;
+    }
+
     @Ignore
     @Test( expected = PreheatException.class )
     public void testValidateAllFail()
@@ -99,7 +109,8 @@
     public void testCollectNoObjectsDE()
     {
         DataElement dataElement = createDataElement( 'A' );
-        Map<Class<? extends IdentifiableObject>, Set<String>> references = preheatService.collectReferences( dataElement, PreheatIdentifier.UID );
+        Map<Class<? extends IdentifiableObject>, Set<String>> references = preheatService.collectReferences( dataElement )
+            .get( PreheatIdentifier.UID );
 
         assertTrue( references.containsKey( OptionSet.class ) );
         assertTrue( references.containsKey( LegendSet.class ) );
@@ -111,7 +122,8 @@
     public void testCollectNoObjectsDEG()
     {
         DataElementGroup dataElementGroup = createDataElementGroup( 'A' );
-        Map<Class<? extends IdentifiableObject>, Set<String>> references = preheatService.collectReferences( dataElementGroup, PreheatIdentifier.UID );
+        Map<Class<? extends IdentifiableObject>, Set<String>> references = preheatService.collectReferences( dataElementGroup )
+            .get( PreheatIdentifier.UID );
 
         assertTrue( references.containsKey( DataElement.class ) );
         assertTrue( references.containsKey( User.class ) );
@@ -134,7 +146,8 @@
 
         deg1.setUser( user );
 
-        Map<Class<? extends IdentifiableObject>, Set<String>> references = preheatService.collectReferences( deg1, PreheatIdentifier.UID );
+        Map<Class<? extends IdentifiableObject>, Set<String>> references = preheatService.collectReferences( deg1 )
+            .get( PreheatIdentifier.UID );
 
         assertTrue( references.containsKey( DataElement.class ) );
         assertTrue( references.containsKey( User.class ) );
@@ -164,7 +177,7 @@
         deg2.addDataElement( de3 );
 
         Map<Class<? extends IdentifiableObject>, Set<String>> references = preheatService.collectReferences(
-            Sets.newHashSet( deg1, deg2 ), PreheatIdentifier.UID );
+            Sets.newHashSet( deg1, deg2 ) ).get( PreheatIdentifier.UID );
 
         assertTrue( references.containsKey( DataElement.class ) );
         assertEquals( 3, references.get( DataElement.class ).size() );
@@ -191,7 +204,8 @@
 
         dataElementGroup.setUser( user );
 
-        Map<Class<? extends IdentifiableObject>, Set<String>> references = preheatService.collectReferences( dataElementGroup, PreheatIdentifier.CODE );
+        Map<Class<? extends IdentifiableObject>, Set<String>> references = preheatService.collectReferences( dataElementGroup )
+            .get( PreheatIdentifier.CODE );
 
         assertTrue( references.containsKey( DataElement.class ) );
         assertTrue( references.containsKey( User.class ) );
@@ -221,7 +235,7 @@
         deg2.addDataElement( de3 );
 
         Map<Class<? extends IdentifiableObject>, Set<String>> references = preheatService.collectReferences(
-            Sets.newHashSet( deg1, deg2 ), PreheatIdentifier.CODE );
+            Sets.newHashSet( deg1, deg2 ) ).get( PreheatIdentifier.CODE );
 
         assertTrue( references.containsKey( DataElement.class ) );
         assertEquals( 3, references.get( DataElement.class ).size() );
@@ -426,7 +440,8 @@
         DataElementGroup dataElementGroup = fromJson( "preheat/degAUidRef.json", DataElementGroup.class );
         defaultSetup();
 
-        Map<Class<? extends IdentifiableObject>, Set<String>> references = preheatService.collectReferences( dataElementGroup, PreheatIdentifier.UID );
+        Map<Class<? extends IdentifiableObject>, Set<String>> references = preheatService.collectReferences( dataElementGroup )
+            .get( PreheatIdentifier.UID );
 
         PreheatParams params = new PreheatParams();
         params.setPreheatMode( PreheatMode.REFERENCE );
@@ -454,7 +469,8 @@
         DataElementGroup dataElementGroup = fromJson( "preheat/degACodeRef.json", DataElementGroup.class );
         defaultSetup();
 
-        Map<Class<? extends IdentifiableObject>, Set<String>> references = preheatService.collectReferences( dataElementGroup, PreheatIdentifier.CODE );
+        Map<Class<? extends IdentifiableObject>, Set<String>> references = preheatService.collectReferences( dataElementGroup )
+            .get( PreheatIdentifier.CODE );
 
         PreheatParams params = new PreheatParams();
         params.setPreheatIdentifier( PreheatIdentifier.CODE );
@@ -483,7 +499,8 @@
         DataElementGroup dataElementGroup = fromJson( "preheat/degAUidRef.json", DataElementGroup.class );
         defaultSetup();
 
-        Map<Class<? extends IdentifiableObject>, Set<String>> references = preheatService.collectReferences( dataElementGroup, PreheatIdentifier.UID );
+        Map<Class<? extends IdentifiableObject>, Set<String>> references = preheatService.collectReferences( dataElementGroup )
+            .get( PreheatIdentifier.UID );
 
         PreheatParams params = new PreheatParams();
         params.setPreheatMode( PreheatMode.REFERENCE );
@@ -514,7 +531,8 @@
         DataElementGroup dataElementGroup = fromJson( "preheat/degACodeRef.json", DataElementGroup.class );
         defaultSetup();
 
-        Map<Class<? extends IdentifiableObject>, Set<String>> references = preheatService.collectReferences( dataElementGroup, PreheatIdentifier.CODE );
+        Map<Class<? extends IdentifiableObject>, Set<String>> references = preheatService.collectReferences( dataElementGroup )
+            .get( PreheatIdentifier.CODE );
 
         PreheatParams params = new PreheatParams();
         params.setPreheatIdentifier( PreheatIdentifier.CODE );
@@ -525,8 +543,6 @@
         Preheat preheat = preheatService.preheat( params );
         preheatService.connectReferences( dataElementGroup, preheat, PreheatIdentifier.CODE );
 
-        manager.update( dataElementGroup );
-
         List<DataElement> members = new ArrayList<>( dataElementGroup.getMembers() );
 
         assertEquals( "DataElementA", members.get( 0 ).getName() );
@@ -549,7 +565,8 @@
         DataElementGroup dataElementGroup = fromJson( "preheat/degAAutoRef.json", DataElementGroup.class );
         defaultSetup();
 
-        Map<Class<? extends IdentifiableObject>, Set<String>> references = preheatService.collectReferences( dataElementGroup, PreheatIdentifier.UID );
+        Map<Class<? extends IdentifiableObject>, Set<String>> references = preheatService.collectReferences( dataElementGroup )
+            .get( PreheatIdentifier.UID );
 
         PreheatParams params = new PreheatParams();
         params.setPreheatIdentifier( PreheatIdentifier.CODE );
@@ -560,8 +577,6 @@
         Preheat preheat = preheatService.preheat( params );
         preheatService.connectReferences( dataElementGroup, preheat, PreheatIdentifier.CODE );
 
-        manager.update( dataElementGroup );
-
         List<DataElement> members = new ArrayList<>( dataElementGroup.getMembers() );
 
         assertEquals( "DataElementA", members.get( 0 ).getName() );

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/metadata2/MetadataImportServiceTest.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/metadata2/MetadataImportServiceTest.java	2016-02-01 08:08:59 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/metadata2/MetadataImportServiceTest.java	2016-02-01 09:41:43 +0000
@@ -31,6 +31,7 @@
 import org.hisp.dhis.DhisSpringTest;
 import org.hisp.dhis.common.IdentifiableObjectManager;
 import org.hisp.dhis.constant.Constant;
+import org.hisp.dhis.render.RenderService;
 import org.junit.Test;
 import org.springframework.beans.factory.annotation.Autowired;
 
@@ -48,6 +49,15 @@
     @Autowired
     private IdentifiableObjectManager manager;
 
+    @Autowired
+    private RenderService _renderService;
+
+    @Override
+    protected void setUpTest() throws Exception
+    {
+        renderService = _renderService;
+    }
+
     @Test
     public void testConstantImport()
     {

=== modified file 'dhis-2/dhis-support/dhis-support-test/src/main/java/org/hisp/dhis/DhisConvenienceTest.java'
--- dhis-2/dhis-support/dhis-support-test/src/main/java/org/hisp/dhis/DhisConvenienceTest.java	2016-02-01 08:08:59 +0000
+++ dhis-2/dhis-support/dhis-support-test/src/main/java/org/hisp/dhis/DhisConvenienceTest.java	2016-02-01 09:41:43 +0000
@@ -99,7 +99,6 @@
 import org.joda.time.DateTime;
 import org.springframework.aop.framework.Advised;
 import org.springframework.aop.support.AopUtils;
-import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.core.io.ClassPathResource;
 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
 import org.springframework.security.core.Authentication;
@@ -161,7 +160,6 @@
 
     protected UserService userService;
 
-    @Autowired
     protected RenderService renderService;
 
     static
@@ -1448,6 +1446,8 @@
 
     protected <T extends IdentifiableObject> T fromJson( String path, Class<T> klass )
     {
+        Assert.notNull( renderService, "RenderService must be injected in test" );
+
         try
         {
             return renderService.fromJson( new ClassPathResource( path ).getInputStream(), klass );