← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 22050: Add collectObjectReferences to PreheatService, collects references with objectUid -> propertyName...

 

------------------------------------------------------------
revno: 22050
committer: Morten Olav Hansen <morten@xxxxxxxxx>
branch nick: dhis2
timestamp: Fri 2016-02-26 16:07:23 +0700
message:
  Add collectObjectReferences to PreheatService, collects references with objectUid -> propertyName -> Object order, will be used in importer for cases where the object being imported is referencing other objects that are not saved yet (i..e user.organisationUnits)
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-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/ObjectBundle.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/hooks/AbstractObjectBundleHook.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/hooks/ObjectBundleHook.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/hooks/UserObjectBundleHook.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/PreheatService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/preheat/PreheatService.java	2016-02-10 08:22:47 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/preheat/PreheatService.java	2016-02-26 09:07:23 +0000
@@ -78,9 +78,12 @@
      */
     Map<PreheatIdentifier, Map<Class<? extends IdentifiableObject>, Set<String>>> collectReferences( Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> objects );
 
+    Map<Class<?>, Map<String, Map<String, Object>>> collectObjectReferences( Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> objects );
+
     /**
      * Checks but does not connect any references, returns check report
-     *  @param objects    Object to check
+     *
+     * @param objects    Object to check
      * @param preheat    Preheat Cache to use
      * @param identifier Use this identifier type to check references
      */

=== 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-25 20:16:54 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/preheat/DefaultPreheatService.java	2016-02-26 09:07:23 +0000
@@ -29,8 +29,10 @@
  */
 
 import com.google.common.collect.Lists;
+import org.hisp.dhis.common.BaseIdentifiableObject;
 import org.hisp.dhis.common.IdentifiableObject;
 import org.hisp.dhis.common.IdentifiableObjectManager;
+import org.hisp.dhis.common.MergeMode;
 import org.hisp.dhis.dataelement.DataElementOperand;
 import org.hisp.dhis.query.Query;
 import org.hisp.dhis.query.QueryService;
@@ -285,6 +287,89 @@
     }
 
     @Override
+    @SuppressWarnings( "unchecked" )
+    public Map<Class<?>, Map<String, Map<String, Object>>> collectObjectReferences( Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> objects )
+    {
+        Map<Class<?>, Map<String, Map<String, Object>>> refs = new HashMap<>();
+
+        if ( objects.isEmpty() )
+        {
+            return refs;
+        }
+
+        Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> scanObjects = new HashMap<>();
+        scanObjects.putAll( objects ); // clone objects list, we don't want to modify it
+
+        if ( scanObjects.containsKey( User.class ) )
+        {
+            List<IdentifiableObject> users = scanObjects.get( User.class );
+            List<IdentifiableObject> userCredentials = new ArrayList<>();
+
+            for ( IdentifiableObject identifiableObject : users )
+            {
+                User user = (User) identifiableObject;
+
+                if ( user.getUserCredentials() != null )
+                {
+                    userCredentials.add( user.getUserCredentials() );
+                }
+            }
+
+            scanObjects.put( UserCredentials.class, userCredentials );
+        }
+
+        for ( Class<? extends IdentifiableObject> objectClass : scanObjects.keySet() )
+        {
+            Schema schema = schemaService.getDynamicSchema( objectClass );
+            List<Property> properties = schema.getProperties().stream()
+                .filter( p -> p.isPersisted() && p.isOwner() && (PropertyType.REFERENCE == p.getPropertyType() || PropertyType.REFERENCE == p.getItemPropertyType()) )
+                .collect( Collectors.toList() );
+
+            List<IdentifiableObject> identifiableObjects = scanObjects.get( objectClass );
+            Map<String, Map<String, Object>> objectReferenceMap = new HashMap<>();
+            refs.put( objectClass, objectReferenceMap );
+
+            for ( IdentifiableObject object : identifiableObjects )
+            {
+                objectReferenceMap.put( object.getUid(), new HashMap<>() );
+
+                properties.forEach( p -> {
+                    if ( !p.isCollection() )
+                    {
+                        IdentifiableObject reference = ReflectionUtils.invokeMethod( object, p.getGetterMethod() );
+
+                        if ( reference != null )
+                        {
+                            IdentifiableObject identifiableObject = new BaseIdentifiableObject();
+                            identifiableObject.mergeWith( reference, MergeMode.REPLACE );
+                            objectReferenceMap.get( object.getUid() ).put( p.getName(), identifiableObject );
+                        }
+                    }
+                    else
+                    {
+                        Collection<IdentifiableObject> refObjects = ReflectionUtils.newCollectionInstance( p.getKlass() );
+                        Collection<IdentifiableObject> references = ReflectionUtils.invokeMethod( object, p.getGetterMethod() );
+
+                        if ( references != null )
+                        {
+                            for ( IdentifiableObject reference : references )
+                            {
+                                IdentifiableObject identifiableObject = new BaseIdentifiableObject();
+                                identifiableObject.mergeWith( reference, MergeMode.REPLACE );
+                                refObjects.add( identifiableObject );
+                            }
+                        }
+
+                        objectReferenceMap.get( object.getUid() ).put( p.getCollectionName(), refObjects );
+                    }
+                } );
+            }
+        }
+
+        return refs;
+    }
+
+    @Override
     public List<PreheatValidation> checkReferences( List<IdentifiableObject> objects, Preheat preheat, PreheatIdentifier identifier )
     {
         List<PreheatValidation> preheatValidations = new ArrayList<>();

=== 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-24 08:58:29 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/DefaultObjectBundleService.java	2016-02-26 09:07:23 +0000
@@ -31,6 +31,8 @@
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.hibernate.SessionFactory;
+import org.hisp.dhis.common.BaseIdentifiableObject;
+import org.hisp.dhis.common.CodeGenerator;
 import org.hisp.dhis.common.IdentifiableObject;
 import org.hisp.dhis.common.IdentifiableObjectManager;
 import org.hisp.dhis.common.IdentifiableObjectUtils;
@@ -138,6 +140,11 @@
             {
                 if ( Preheat.isDefault( identifiableObject ) ) continue;
 
+                if ( StringUtils.isEmpty( identifiableObject.getUid() ) )
+                {
+                    ((BaseIdentifiableObject) identifiableObject).setUid( CodeGenerator.generateCode() );
+                }
+
                 if ( !StringUtils.isEmpty( identifiableObject.getUid() ) )
                 {
                     map.get( PreheatIdentifier.UID ).get( klass ).put( identifiableObject.getUid(), identifiableObject );
@@ -150,6 +157,8 @@
             }
         }
 
+        bundle.setObjectReferences( preheatService.collectObjectReferences( params.getObjects() ) );
+
         return bundle;
     }
 
@@ -213,6 +222,8 @@
 
         List<Class<? extends IdentifiableObject>> klasses = getSortedClasses( bundle );
 
+        objectBundleHooks.forEach( hook -> hook.preImport( bundle ) );
+
         for ( Class<? extends IdentifiableObject> klass : klasses )
         {
             List<IdentifiableObject> objects = bundle.getObjects().get( klass );
@@ -253,6 +264,8 @@
             sessionFactory.getCurrentSession().flush();
         }
 
+        objectBundleHooks.forEach( hook -> hook.postImport( bundle ) );
+
         bundle.setObjectBundleStatus( ObjectBundleStatus.COMMITTED );
     }
 

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/ObjectBundle.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/ObjectBundle.java	2016-02-17 05:33:53 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/ObjectBundle.java	2016-02-26 09:07:23 +0000
@@ -64,6 +64,8 @@
 
     private Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> objects = new HashMap<>();
 
+    private Map<Class<?>, Map<String, Map<String, Object>>> objectReferences;
+
     public ObjectBundle( ObjectBundleParams params )
     {
         this.user = params.getUser();
@@ -168,4 +170,14 @@
     {
         this.objects = objects;
     }
+
+    public Map<Class<?>, Map<String, Map<String, Object>>> getObjectReferences()
+    {
+        return objectReferences;
+    }
+
+    public void setObjectReferences( Map<Class<?>, Map<String, Map<String, Object>>> objectReferences )
+    {
+        this.objectReferences = objectReferences;
+    }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/hooks/AbstractObjectBundleHook.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/hooks/AbstractObjectBundleHook.java	2016-02-24 08:58:29 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/hooks/AbstractObjectBundleHook.java	2016-02-26 09:07:23 +0000
@@ -54,6 +54,16 @@
     protected SessionFactory sessionFactory;
 
     @Override
+    public void preImport( ObjectBundle objectBundle )
+    {
+    }
+
+    @Override
+    public void postImport( ObjectBundle objectBundle )
+    {
+    }
+
+    @Override
     public void preCreate( IdentifiableObject identifiableObject, ObjectBundle objectBundle )
     {
     }

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/hooks/ObjectBundleHook.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/hooks/ObjectBundleHook.java	2016-02-24 08:58:29 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/hooks/ObjectBundleHook.java	2016-02-26 09:07:23 +0000
@@ -36,6 +36,10 @@
  */
 public interface ObjectBundleHook
 {
+    void preImport( ObjectBundle objectBundle );
+
+    void postImport( ObjectBundle objectBundle );
+
     void preCreate( IdentifiableObject identifiableObject, ObjectBundle objectBundle );
 
     void postCreate( IdentifiableObject identifiableObject, ObjectBundle objectBundle );

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/hooks/UserObjectBundleHook.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/hooks/UserObjectBundleHook.java	2016-02-24 08:58:29 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/hooks/UserObjectBundleHook.java	2016-02-26 09:07:23 +0000
@@ -37,6 +37,8 @@
 import org.springframework.stereotype.Component;
 import org.springframework.util.StringUtils;
 
+import java.util.List;
+
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
  */
@@ -72,4 +74,22 @@
 
         user.setUserCredentials( userCredentials );
     }
+
+    @Override
+    public void postImport( ObjectBundle objectBundle )
+    {
+        if ( !objectBundle.getObjects().containsKey( User.class ) )
+        {
+            return;
+        }
+
+        List<IdentifiableObject> objects = objectBundle.getObjects().get( User.class );
+
+        for ( IdentifiableObject identifiableObject : objects )
+        {
+            User user = (User) identifiableObject;
+            preheatService.connectReferences( identifiableObject, objectBundle.getPreheat(), objectBundle.getPreheatIdentifier() );
+            manager.update( identifiableObject );
+        }
+    }
 }

=== 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-25 20:15:42 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/metadata2/objectbundle/ObjectBundleServiceTest.java	2016-02-26 09:07:23 +0000
@@ -474,6 +474,7 @@
         assertNotNull( user.getUserCredentials() );
         assertEquals( "admin", user.getUserCredentials().getUsername() );
         assertFalse( user.getUserCredentials().getUserAuthorityGroups().isEmpty() );
+        // assertFalse( user.getOrganisationUnits().isEmpty() );
     }
 
     private void defaultSetup()