← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 21901: automatic preheating when creating object bundles

 

------------------------------------------------------------
revno: 21901
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2016-02-08 15:21:50 +0700
message:
  automatic preheating when creating object bundles
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/preheat/Preheat.java
  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-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/ObjectBundleParams.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-02 22:45:48 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/preheat/Preheat.java	2016-02-08 08:21:50 +0000
@@ -106,12 +106,6 @@
 
         Map<String, IdentifiableObject> identifierMap = map.get( identifier ).get( object.getClass() );
         String key = identifier.getIdentifier( object );
-
-        if ( identifierMap.containsKey( key ) )
-        {
-            throw new PreheatException( "Duplicate key " + key + " for class " + object.getClass().getName() + "." );
-        }
-
         identifierMap.put( key, object );
 
         return this;

=== 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 09:41:43 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/preheat/PreheatService.java	2016-02-08 08:21:50 +0000
@@ -30,6 +30,7 @@
 
 import org.hisp.dhis.common.IdentifiableObject;
 
+import java.util.Collection;
 import java.util.Map;
 import java.util.Set;
 
@@ -66,7 +67,7 @@
      * @param objects Objects to scan
      * @return Maps classes to collections of identifiers
      */
-    Map<PreheatIdentifier, Map<Class<? extends IdentifiableObject>, Set<String>>> collectReferences( Set<Object> objects );
+    Map<PreheatIdentifier, Map<Class<? extends IdentifiableObject>, Set<String>>> collectReferences( Collection<Object> objects );
 
     /**
      * Connects id object references on a given object using a given identifier + a preheated Preheat cache.

=== 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-02 09:16:43 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/preheat/DefaultPreheatService.java	2016-02-08 08:21:50 +0000
@@ -152,7 +152,7 @@
 
     @Override
     @SuppressWarnings( "unchecked" )
-    public Map<PreheatIdentifier, Map<Class<? extends IdentifiableObject>, Set<String>>> collectReferences( Set<Object> objects )
+    public Map<PreheatIdentifier, Map<Class<? extends IdentifiableObject>, Set<String>>> collectReferences( Collection<Object> objects )
     {
         Map<PreheatIdentifier, Map<Class<? extends IdentifiableObject>, Set<String>>> map = new HashMap<>();
 

=== 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-08 07:21:53 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/DefaultObjectBundleService.java	2016-02-08 08:21:50 +0000
@@ -28,18 +28,42 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import org.hisp.dhis.preheat.PreheatMode;
+import org.hisp.dhis.preheat.PreheatParams;
+import org.hisp.dhis.preheat.PreheatService;
+import org.hisp.dhis.schema.SchemaService;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
+import java.util.ArrayList;
+
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
  */
 @Component
 public class DefaultObjectBundleService implements ObjectBundleService
 {
+    @Autowired
+    private SchemaService schemaService;
+
+    @Autowired
+    private PreheatService preheatService;
+
     @Override
     public ObjectBundle create( ObjectBundleParams params )
     {
         ObjectBundle bundle = new ObjectBundle();
+        bundle.addObjects( params.getObjects() );
+
+        PreheatParams preheatParams = params.getPreheatParams();
+
+        if ( PreheatMode.REFERENCE == preheatParams.getPreheatMode() )
+        {
+            preheatParams.setReferences( preheatService.collectReferences( new ArrayList<>( params.getObjects() ) ) );
+        }
+
+        bundle.setPreheat( preheatService.preheat( preheatParams ) );
+
         return bundle;
     }
 

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/ObjectBundleParams.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/ObjectBundleParams.java	2016-02-08 07:21:53 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/ObjectBundleParams.java	2016-02-08 08:21:50 +0000
@@ -28,16 +28,26 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import org.hisp.dhis.common.IdentifiableObject;
 import org.hisp.dhis.preheat.PreheatIdentifier;
+import org.hisp.dhis.preheat.PreheatMode;
+import org.hisp.dhis.preheat.PreheatParams;
+
+import java.util.ArrayList;
+import java.util.List;
 
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
  */
 public class ObjectBundleParams
 {
-    private ObjectBundleMode objectBundleMode;
-
-    private PreheatIdentifier preheatIdentifier;
+    private ObjectBundleMode objectBundleMode = ObjectBundleMode.VALIDATE;
+
+    private PreheatIdentifier preheatIdentifier = PreheatIdentifier.UID;
+
+    private PreheatMode preheatMode = PreheatMode.REFERENCE;
+
+    private List<? extends IdentifiableObject> objects = new ArrayList<>();
 
     public ObjectBundleParams()
     {
@@ -64,4 +74,33 @@
         this.preheatIdentifier = preheatIdentifier;
         return this;
     }
+
+    public PreheatMode getPreheatMode()
+    {
+        return preheatMode;
+    }
+
+    public void setPreheatMode( PreheatMode preheatMode )
+    {
+        this.preheatMode = preheatMode;
+    }
+
+    public List<? extends IdentifiableObject> getObjects()
+    {
+        return objects;
+    }
+
+    public void setObjects( List<? extends IdentifiableObject> objects )
+    {
+        this.objects = objects;
+    }
+
+    public PreheatParams getPreheatParams()
+    {
+        PreheatParams params = new PreheatParams();
+        params.setPreheatIdentifier( preheatIdentifier );
+        params.setPreheatMode( preheatMode );
+
+        return params;
+    }
 }

=== 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-08 07:21:53 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/metadata2/objectbundle/ObjectBundleServiceTest.java	2016-02-08 08:21:50 +0000
@@ -28,15 +28,18 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import com.google.common.collect.Lists;
 import org.hisp.dhis.DhisSpringTest;
+import org.hisp.dhis.common.IdentifiableObjectManager;
+import org.hisp.dhis.dataelement.DataElement;
 import org.hisp.dhis.dataelement.DataElementGroup;
 import org.hisp.dhis.preheat.PreheatIdentifier;
 import org.hisp.dhis.render.RenderService;
+import org.hisp.dhis.user.User;
 import org.junit.Test;
 import org.springframework.beans.factory.annotation.Autowired;
 
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.*;
 
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
@@ -48,6 +51,9 @@
     private ObjectBundleService objectBundleService;
 
     @Autowired
+    private IdentifiableObjectManager manager;
+
+    @Autowired
     private RenderService _renderService;
 
     @Override
@@ -66,9 +72,32 @@
     }
 
     @Test
+    public void testCreateDoesPreheating()
+    {
+        DataElementGroup dataElementGroup = fromJson( "dxf2/degAUidRef.json", DataElementGroup.class );
+        defaultSetup();
+
+        ObjectBundleParams params = new ObjectBundleParams();
+        params.setObjects( Lists.newArrayList( dataElementGroup ) );
+
+        ObjectBundle bundle = objectBundleService.create( params );
+
+        assertNotNull( bundle );
+        assertFalse( bundle.getPreheat().isEmpty() );
+        assertFalse( bundle.getPreheat().isEmpty( PreheatIdentifier.UID ) );
+        assertFalse( bundle.getPreheat().isEmpty( PreheatIdentifier.UID, DataElement.class ) );
+        assertTrue( bundle.getPreheat().containsKey( PreheatIdentifier.UID, DataElement.class, "deabcdefghA" ) );
+        assertTrue( bundle.getPreheat().containsKey( PreheatIdentifier.UID, DataElement.class, "deabcdefghB" ) );
+        assertTrue( bundle.getPreheat().containsKey( PreheatIdentifier.UID, DataElement.class, "deabcdefghC" ) );
+        assertFalse( bundle.getPreheat().containsKey( PreheatIdentifier.UID, DataElement.class, "deabcdefghD" ) );
+    }
+
+    @Test
     public void testObjectBundleShouldAddToObjectAndPreheat()
     {
         ObjectBundleParams params = new ObjectBundleParams();
+        params.setObjectBundleMode( ObjectBundleMode.VALIDATE );
+
         ObjectBundle bundle = objectBundleService.create( params );
 
         DataElementGroup dataElementGroup = fromJson( "dxf2/degAUidRef.json", DataElementGroup.class );
@@ -77,4 +106,20 @@
         assertTrue( bundle.getObjects().get( DataElementGroup.class ).contains( dataElementGroup ) );
         assertTrue( bundle.getPreheat().containsKey( PreheatIdentifier.UID, DataElementGroup.class, dataElementGroup.getUid() ) );
     }
+
+    private void defaultSetup()
+    {
+        DataElement de1 = createDataElement( 'A' );
+        DataElement de2 = createDataElement( 'B' );
+        DataElement de3 = createDataElement( 'C' );
+        DataElement de4 = createDataElement( 'D' );
+
+        manager.save( de1 );
+        manager.save( de2 );
+        manager.save( de3 );
+        manager.save( de4 );
+
+        User user = createUser( 'A' );
+        manager.save( user );
+    }
 }
\ No newline at end of file