← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 21838: wip, PreheatService.connectReferences, method for connecting back references based on a preheat c...

 

------------------------------------------------------------
revno: 21838
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2016-02-01 14:10:53 +0700
message:
  wip, PreheatService.connectReferences, method for connecting back references based on a preheat cache
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/PreheatIdentifier.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-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/Preheat.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/preheat/Preheat.java	2016-01-21 06:00:57 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/preheat/Preheat.java	2016-02-01 07:10:53 +0000
@@ -56,6 +56,29 @@
         return (T) map.get( identifier ).get( klass ).get( key );
     }
 
+    @SuppressWarnings( "unchecked" )
+    public <T extends IdentifiableObject> T get( PreheatIdentifier identifier, T object )
+    {
+        if ( object == null )
+        {
+            return null;
+        }
+
+        T reference = null;
+
+        if ( PreheatIdentifier.UID == identifier || PreheatIdentifier.AUTO == identifier )
+        {
+            reference = get( identifier, object.getClass(), object.getUid() );
+        }
+
+        if ( PreheatIdentifier.CODE == identifier || (reference != null && PreheatIdentifier.AUTO == identifier) )
+        {
+            reference = get( identifier, object.getClass(), object.getCode() );
+        }
+
+        return reference;
+    }
+
     public boolean containsKey( PreheatIdentifier identifier, Class<? extends IdentifiableObject> klass, String key )
     {
         return !(isEmpty() || isEmpty( identifier ) || isEmpty( identifier, klass )) && map.get( identifier ).get( klass ).containsKey( key );

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/preheat/PreheatIdentifier.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/preheat/PreheatIdentifier.java	2016-01-21 05:06:57 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/preheat/PreheatIdentifier.java	2016-02-01 07:10:53 +0000
@@ -43,7 +43,12 @@
     /**
      * Preheat using CODE identifiers.
      */
-    CODE;
+    CODE,
+
+    /**
+     * Find first non-null identifier in order: UID, CODE
+     */
+    AUTO;
 
     <T extends IdentifiableObject> String getIdentifier( T object )
     {

=== 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-29 05:22:32 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/preheat/PreheatService.java	2016-02-01 07:10:53 +0000
@@ -69,4 +69,6 @@
      * @return Maps classes to collections of identifiers
      */
     Map<Class<? extends IdentifiableObject>, Set<String>> collectReferences( Set<Object> objects, PreheatIdentifier identifier );
+
+    <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-01-29 05:29:36 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/preheat/DefaultPreheatService.java	2016-02-01 07:10:53 +0000
@@ -156,19 +156,16 @@
                     {
                         IdentifiableObject identifiableObject = (IdentifiableObject) reference;
 
+                        String uid = identifiableObject.getUid();
+                        String code = identifiableObject.getCode();
+
                         if ( PreheatIdentifier.UID == identifier )
                         {
-                            if ( identifiableObject.getUid() != null )
-                            {
-                                map.get( klass ).add( identifiableObject.getUid() );
-                            }
+                            if ( uid != null ) map.get( klass ).add( uid );
                         }
                         else if ( PreheatIdentifier.CODE == identifier )
                         {
-                            if ( identifiableObject.getCode() != null )
-                            {
-                                map.get( klass ).add( identifiableObject.getCode() );
-                            }
+                            if ( code != null ) map.get( klass ).add( code );
                         }
                     }
                 }
@@ -180,19 +177,16 @@
 
                     for ( IdentifiableObject identifiableObject : reference )
                     {
+                        String uid = identifiableObject.getUid();
+                        String code = identifiableObject.getCode();
+
                         if ( PreheatIdentifier.UID == identifier )
                         {
-                            if ( identifiableObject.getUid() != null )
-                            {
-                                map.get( klass ).add( identifiableObject.getUid() );
-                            }
+                            if ( uid != null ) map.get( klass ).add( uid );
                         }
                         else if ( PreheatIdentifier.CODE == identifier )
                         {
-                            if ( identifiableObject.getCode() != null )
-                            {
-                                map.get( klass ).add( identifiableObject.getCode() );
-                            }
+                            if ( code != null ) map.get( klass ).add( code );
                         }
                     }
                 }
@@ -201,4 +195,39 @@
 
         return map;
     }
+
+    @Override
+    @SuppressWarnings( "unchecked" )
+    public <T extends IdentifiableObject> void connectReferences( T object, Preheat preheat, PreheatIdentifier identifier )
+    {
+        if ( object == null )
+        {
+            return;
+        }
+
+        Schema schema = schemaService.getDynamicSchema( object.getClass() );
+        schema.getProperties().stream()
+            .filter( p -> p.isPersisted() && p.isOwner() && (PropertyType.REFERENCE == p.getPropertyType() || PropertyType.REFERENCE == p.getItemPropertyType()) )
+            .forEach( p -> {
+                if ( !p.isCollection() )
+                {
+                    T refObject = ReflectionUtils.invokeMethod( object, p.getGetterMethod() );
+                    T ref = preheat.get( identifier, refObject );
+                    ReflectionUtils.invokeMethod( object, p.getSetterMethod(), ref );
+                }
+                else
+                {
+                    Collection<T> objects = ReflectionUtils.newCollectionInstance( p.getKlass() );
+                    Collection<IdentifiableObject> refObjects = ReflectionUtils.invokeMethod( object, p.getGetterMethod() );
+
+                    for ( IdentifiableObject reference : refObjects )
+                    {
+                        T ref = preheat.get( identifier, (T) reference );
+                        if ( ref != null ) objects.add( ref );
+                    }
+
+                    ReflectionUtils.invokeMethod( object, p.getSetterMethod(), objects );
+                }
+            } );
+    }
 }

=== 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-29 05:22:32 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/preheat/PreheatServiceTest.java	2016-02-01 07:10:53 +0000
@@ -42,7 +42,10 @@
 import org.junit.Test;
 import org.springframework.beans.factory.annotation.Autowired;
 
+import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
@@ -442,6 +445,12 @@
         dataElementGroup.setUser( user );
         manager.save( dataElementGroup );
 
+        List<DataElement> members = new ArrayList<>( dataElementGroup.getMembers() );
+        members.set( 0, createDataElementIdentifier( de1.getUid(), PreheatIdentifier.UID ) );
+        members.set( 1, createDataElementIdentifier( de2.getUid(), PreheatIdentifier.UID ) );
+        members.set( 2, createDataElementIdentifier( de3.getUid(), PreheatIdentifier.UID ) );
+        dataElementGroup.setMembers( new HashSet<>( members ) );
+
         Map<Class<? extends IdentifiableObject>, Set<String>> references = preheatService.collectReferences( dataElementGroup, PreheatIdentifier.UID );
 
         PreheatParams params = new PreheatParams();
@@ -463,4 +472,159 @@
         assertFalse( preheat.containsKey( PreheatIdentifier.UID, DataElementGroup.class, dataElementGroup.getUid() ) );
         assertTrue( preheat.containsKey( PreheatIdentifier.UID, User.class, user.getUid() ) );
     }
+
+    @Test
+    @SuppressWarnings( "unchecked" )
+    public void testPreheatReferenceConnectUID()
+    {
+        DataElementGroup dataElementGroup = new DataElementGroup( "DataElementGroupA" );
+        dataElementGroup.setAutoFields();
+
+        DataElement de1 = createDataElement( 'A' );
+        DataElement de2 = createDataElement( 'B' );
+        DataElement de3 = createDataElement( 'C' );
+
+        manager.save( de1 );
+        manager.save( de2 );
+        manager.save( de3 );
+
+        User user = createUser( 'A' );
+        manager.save( user );
+
+        dataElementGroup.addDataElement( de1 );
+        dataElementGroup.addDataElement( de2 );
+        dataElementGroup.addDataElement( de3 );
+
+        dataElementGroup.setUser( user );
+
+        manager.save( dataElementGroup );
+        manager.evict( dataElementGroup );
+
+        List<DataElement> members = new ArrayList<>( dataElementGroup.getMembers() );
+        members.set( 0, createDataElementIdentifier( de1.getUid(), PreheatIdentifier.UID ) );
+        members.set( 1, createDataElementIdentifier( de2.getUid(), PreheatIdentifier.UID ) );
+        members.set( 2, createDataElementIdentifier( de3.getUid(), PreheatIdentifier.UID ) );
+
+        dataElementGroup.setMembers( new HashSet<>( members ) );
+        dataElementGroup.setUser( createUserIdentifier( user.getUid(), PreheatIdentifier.UID ) );
+
+        Map<Class<? extends IdentifiableObject>, Set<String>> references = preheatService.collectReferences( dataElementGroup, PreheatIdentifier.UID );
+
+        PreheatParams params = new PreheatParams();
+        params.setPreheatMode( PreheatMode.REFERENCE );
+        params.setReferences( references );
+
+        preheatService.validate( params );
+        Preheat preheat = preheatService.preheat( params );
+        preheatService.connectReferences( dataElementGroup, preheat, PreheatIdentifier.UID );
+
+        manager.update( dataElementGroup );
+
+        members = new ArrayList<>( dataElementGroup.getMembers() );
+
+        assertEquals( "DataElementA", members.get( 0 ).getName() );
+        assertEquals( "DataElementCodeA", members.get( 0 ).getCode() );
+        assertEquals( "DataElementB", members.get( 1 ).getName() );
+        assertEquals( "DataElementCodeB", members.get( 1 ).getCode() );
+        assertEquals( "DataElementC", members.get( 2 ).getName() );
+        assertEquals( "DataElementCodeC", members.get( 2 ).getCode() );
+
+        assertEquals( "FirstNameA", dataElementGroup.getUser().getFirstName() );
+        assertEquals( "SurnameA", dataElementGroup.getUser().getSurname() );
+        assertEquals( "UserCodeA", dataElementGroup.getUser().getCode() );
+    }
+
+    @Test
+    @SuppressWarnings( "unchecked" )
+    public void testPreheatReferenceConnectCODE()
+    {
+        DataElementGroup dataElementGroup = new DataElementGroup( "DataElementGroupA" );
+        dataElementGroup.setAutoFields();
+
+        DataElement de1 = createDataElement( 'A' );
+        DataElement de2 = createDataElement( 'B' );
+        DataElement de3 = createDataElement( 'C' );
+
+        manager.save( de1 );
+        manager.save( de2 );
+        manager.save( de3 );
+
+        User user = createUser( 'A' );
+        manager.save( user );
+
+        dataElementGroup.addDataElement( de1 );
+        dataElementGroup.addDataElement( de2 );
+        dataElementGroup.addDataElement( de3 );
+
+        dataElementGroup.setUser( user );
+
+        manager.save( dataElementGroup );
+        manager.evict( dataElementGroup );
+
+        List<DataElement> members = new ArrayList<>( dataElementGroup.getMembers() );
+        members.set( 0, createDataElementIdentifier( de1.getCode(), PreheatIdentifier.CODE ) );
+        members.set( 1, createDataElementIdentifier( de2.getCode(), PreheatIdentifier.CODE ) );
+        members.set( 2, createDataElementIdentifier( de3.getCode(), PreheatIdentifier.CODE ) );
+
+        dataElementGroup.setMembers( new HashSet<>( members ) );
+        dataElementGroup.setUser( createUserIdentifier( user.getCode(), PreheatIdentifier.CODE ) );
+
+        Map<Class<? extends IdentifiableObject>, Set<String>> references = preheatService.collectReferences( dataElementGroup, PreheatIdentifier.CODE );
+
+        PreheatParams params = new PreheatParams();
+        params.setPreheatIdentifier( PreheatIdentifier.CODE );
+        params.setPreheatMode( PreheatMode.REFERENCE );
+        params.setReferences( references );
+
+        preheatService.validate( params );
+        Preheat preheat = preheatService.preheat( params );
+        preheatService.connectReferences( dataElementGroup, preheat, PreheatIdentifier.CODE );
+
+        manager.update( dataElementGroup );
+
+        members = new ArrayList<>( dataElementGroup.getMembers() );
+
+        assertEquals( "DataElementA", members.get( 0 ).getName() );
+        assertEquals( "DataElementCodeA", members.get( 0 ).getCode() );
+        assertEquals( "DataElementB", members.get( 1 ).getName() );
+        assertEquals( "DataElementCodeB", members.get( 1 ).getCode() );
+        assertEquals( "DataElementC", members.get( 2 ).getName() );
+        assertEquals( "DataElementCodeC", members.get( 2 ).getCode() );
+
+        assertEquals( "FirstNameA", dataElementGroup.getUser().getFirstName() );
+        assertEquals( "SurnameA", dataElementGroup.getUser().getSurname() );
+        assertEquals( "UserCodeA", dataElementGroup.getUser().getCode() );
+    }
+
+    private DataElement createDataElementIdentifier( String identifier, PreheatIdentifier preheatIdentifier )
+    {
+        DataElement dataElement = new DataElement();
+
+        if ( PreheatIdentifier.UID == preheatIdentifier )
+        {
+            dataElement.setUid( identifier );
+        }
+        else if ( PreheatIdentifier.CODE == preheatIdentifier )
+        {
+            dataElement.setCode( identifier );
+        }
+
+        return dataElement;
+    }
+
+    private User createUserIdentifier( String identifier, PreheatIdentifier preheatIdentifier )
+    {
+        User user = new User();
+
+        if ( PreheatIdentifier.UID == preheatIdentifier )
+        {
+            user.setUid( identifier );
+        }
+        else if ( PreheatIdentifier.CODE == preheatIdentifier )
+        {
+            user.setCode( identifier );
+        }
+
+        return user;
+    }
 }