← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 21924: more object bundle validation fixes

 

------------------------------------------------------------
revno: 21924
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2016-02-10 14:45:27 +0700
message:
  more object bundle validation fixes
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/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/PreheatService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/preheat/PreheatService.java	2016-02-10 06:31:33 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/preheat/PreheatService.java	2016-02-10 07:45:27 +0000
@@ -63,12 +63,20 @@
     Map<PreheatIdentifier, Map<Class<? extends IdentifiableObject>, Set<String>>> collectReferences( Object object );
 
     /**
+     * Scan object and collect all references (both id object and collections with id objects).
+     *
+     * @param objects Object to scan
+     * @return Maps classes to collections of identifiers
+     */
+    Map<PreheatIdentifier, Map<Class<? extends IdentifiableObject>, Set<String>>> collectReferences( Collection<IdentifiableObject> objects );
+
+    /**
      * Scan objects and collect all references (both id object and collections with id objects).
      *
      * @param objects Objects to scan
      * @return Maps classes to collections of identifiers
      */
-    Map<PreheatIdentifier, Map<Class<? extends IdentifiableObject>, Set<String>>> collectReferences( Collection<?> objects );
+    Map<PreheatIdentifier, Map<Class<? extends IdentifiableObject>, Set<String>>> collectReferences( Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> objects );
 
     /**
      * Checks but does not connect any references, returns check report

=== 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-10 06:31:33 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/preheat/DefaultPreheatService.java	2016-02-10 07:45:27 +0000
@@ -28,7 +28,7 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import com.google.common.collect.Sets;
+import com.google.common.collect.Lists;
 import org.hisp.dhis.common.IdentifiableObject;
 import org.hisp.dhis.query.Query;
 import org.hisp.dhis.query.QueryService;
@@ -145,14 +145,37 @@
     }
 
     @Override
+    @SuppressWarnings( "unchecked" )
     public Map<PreheatIdentifier, Map<Class<? extends IdentifiableObject>, Set<String>>> collectReferences( Object object )
     {
-        return collectReferences( Sets.newHashSet( object ) );
+        if ( !IdentifiableObject.class.isInstance( object ) )
+        {
+            return new HashMap<>();
+        }
+
+        Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> map = new HashMap<>();
+        map.put( (Class<? extends IdentifiableObject>) object.getClass(), Lists.<IdentifiableObject>newArrayList( (IdentifiableObject) object ) );
+
+        return collectReferences( map );
+    }
+
+    @Override
+    public Map<PreheatIdentifier, Map<Class<? extends IdentifiableObject>, Set<String>>> collectReferences( Collection<IdentifiableObject> objects )
+    {
+        if ( objects == null || objects.isEmpty() )
+        {
+            return new HashMap<>();
+        }
+
+        Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> map = new HashMap<>();
+        map.put( objects.iterator().next().getClass(), Lists.newArrayList( objects ) );
+
+        return collectReferences( map );
     }
 
     @Override
     @SuppressWarnings( "unchecked" )
-    public Map<PreheatIdentifier, Map<Class<? extends IdentifiableObject>, Set<String>>> collectReferences( Collection<?> objects )
+    public Map<PreheatIdentifier, Map<Class<? extends IdentifiableObject>, Set<String>>> collectReferences( Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> objects )
     {
         Map<PreheatIdentifier, Map<Class<? extends IdentifiableObject>, Set<String>>> map = new HashMap<>();
 
@@ -167,54 +190,60 @@
             return map;
         }
 
-        Schema schema = schemaService.getDynamicSchema( objects.iterator().next().getClass() );
-        List<Property> properties = schema.getProperties().stream()
-            .filter( p -> p.isPersisted() && p.isOwner() && (PropertyType.REFERENCE == p.getPropertyType() || PropertyType.REFERENCE == p.getItemPropertyType()) )
-            .collect( Collectors.toList() );
-
-        properties.forEach( p -> {
-            for ( Object object : objects )
-            {
-                if ( !p.isCollection() )
-                {
-                    Class<? extends IdentifiableObject> klass = (Class<? extends IdentifiableObject>) p.getKlass();
-
-                    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 )
-                    {
-                        IdentifiableObject identifiableObject = (IdentifiableObject) reference;
-
-                        String uid = identifiableObject.getUid();
-                        String code = identifiableObject.getCode();
-
-                        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 ( !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 )
-                    {
-                        String uid = identifiableObject.getUid();
-                        String code = identifiableObject.getCode();
-
-                        if ( uid != null ) uidMap.get( klass ).add( uid );
-                        if ( code != null ) codeMap.get( klass ).add( code );
-                    }
-                }
-            }
-        } );
+        for ( Class<? extends IdentifiableObject> objectClass : objects.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 = objects.get( objectClass );
+
+            properties.forEach( p -> {
+                for ( Object object : identifiableObjects )
+                {
+                    if ( !p.isCollection() )
+                    {
+                        Class<? extends IdentifiableObject> klass = (Class<? extends IdentifiableObject>) p.getKlass();
+
+                        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 )
+                        {
+                            IdentifiableObject identifiableObject = (IdentifiableObject) reference;
+
+                            String uid = identifiableObject.getUid();
+                            String code = identifiableObject.getCode();
+
+                            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 ( !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 )
+                        {
+                            String uid = identifiableObject.getUid();
+                            String code = identifiableObject.getCode();
+
+                            if ( uid != null ) uidMap.get( klass ).add( uid );
+                            if ( code != null ) codeMap.get( klass ).add( code );
+                        }
+                    }
+                }
+            } );
+
+        }
 
         return map;
     }

=== 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-10 03:08:09 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/preheat/PreheatServiceTest.java	2016-02-10 07:45:27 +0000
@@ -28,6 +28,7 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import com.google.common.collect.Lists;
 import com.google.common.collect.Sets;
 import org.hisp.dhis.DhisSpringTest;
 import org.hisp.dhis.common.IdentifiableObject;
@@ -178,7 +179,7 @@
         deg2.addDataElement( de3 );
 
         Map<Class<? extends IdentifiableObject>, Set<String>> references = preheatService.collectReferences(
-            Sets.newHashSet( deg1, deg2 ) ).get( PreheatIdentifier.UID );
+            Lists.newArrayList( deg1, deg2 ) ).get( PreheatIdentifier.UID );
 
         assertTrue( references.containsKey( DataElement.class ) );
         assertEquals( 3, references.get( DataElement.class ).size() );
@@ -236,7 +237,7 @@
         deg2.addDataElement( de3 );
 
         Map<Class<? extends IdentifiableObject>, Set<String>> references = preheatService.collectReferences(
-            Sets.newHashSet( deg1, deg2 ) ).get( PreheatIdentifier.CODE );
+            Lists.newArrayList( deg1, deg2 ) ).get( PreheatIdentifier.CODE );
 
         assertTrue( references.containsKey( DataElement.class ) );
         assertEquals( 3, references.get( DataElement.class ).size() );

=== 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-10 06:31:33 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/DefaultObjectBundleService.java	2016-02-10 07:45:27 +0000
@@ -53,7 +53,7 @@
     public ObjectBundle create( ObjectBundleParams params )
     {
         ObjectBundle bundle = new ObjectBundle();
-        bundle.putObjects( params.getObjectMap() );
+        bundle.putObjects( params.getObjects() );
 
         PreheatParams preheatParams = params.getPreheatParams();
 

=== 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-10 06:31:33 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/ObjectBundleParams.java	2016-02-10 07:45:27 +0000
@@ -49,7 +49,7 @@
 
     private PreheatMode preheatMode = PreheatMode.REFERENCE;
 
-    private Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> objectMap = new HashMap<>();
+    private Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> objects = new HashMap<>();
 
     public ObjectBundleParams()
     {
@@ -87,26 +87,14 @@
         this.preheatMode = preheatMode;
     }
 
-    public Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> getObjectMap()
-    {
-        return objectMap;
-    }
-
-    public List<IdentifiableObject> getObjects()
-    {
-        List<IdentifiableObject> objects = new ArrayList<>();
-
-        for ( Class<? extends IdentifiableObject> klass : objectMap.keySet() )
-        {
-            objects.addAll( objectMap.get( klass ) );
-        }
-
+    public Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> getObjects()
+    {
         return objects;
     }
 
-    public void setObjectMap( Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> objectMap )
+    public void setObjects( Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> objects )
     {
-        this.objectMap = objectMap;
+        this.objects = objects;
     }
 
     public void addObject( Class<? extends IdentifiableObject> klass, IdentifiableObject object )
@@ -116,12 +104,12 @@
             return;
         }
 
-        if ( !objectMap.containsKey( klass ) )
+        if ( !objects.containsKey( klass ) )
         {
-            objectMap.put( klass, new ArrayList<>() );
+            objects.put( klass, new ArrayList<>() );
         }
 
-        objectMap.get( klass ).add( object );
+        objects.get( klass ).add( object );
     }
 
     public void addObject( IdentifiableObject object )
@@ -131,12 +119,12 @@
             return;
         }
 
-        if ( !objectMap.containsKey( object.getClass() ) )
+        if ( !objects.containsKey( object.getClass() ) )
         {
-            objectMap.put( object.getClass(), new ArrayList<>() );
+            objects.put( object.getClass(), new ArrayList<>() );
         }
 
-        objectMap.get( object.getClass() ).add( object );
+        objects.get( object.getClass() ).add( object );
     }
 
     public PreheatParams getPreheatParams()

=== 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-10 06:31:33 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/metadata2/objectbundle/ObjectBundleServiceTest.java	2016-02-10 07:45:27 +0000
@@ -38,7 +38,6 @@
 import org.hisp.dhis.render.RenderFormat;
 import org.hisp.dhis.render.RenderService;
 import org.hisp.dhis.user.User;
-import org.junit.Ignore;
 import org.junit.Test;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.core.io.ClassPathResource;
@@ -117,7 +116,6 @@
     }
 
     @Test
-    @Ignore
     public void testPreheatValidations() throws IOException
     {
         Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> metadata = renderService.fromMetadata(
@@ -125,12 +123,11 @@
 
         ObjectBundleParams params = new ObjectBundleParams();
         params.setObjectBundleMode( ObjectBundleMode.VALIDATE );
-        params.setObjectMap( metadata );
+        params.setObjects( metadata );
 
         ObjectBundle bundle = objectBundleService.create( params );
         ObjectBundleValidation validate = objectBundleService.validate( bundle );
-
-        // assertFalse( validate.getPreheatValidations().isEmpty() );
+        assertFalse( validate.getPreheatValidations().isEmpty() );
     }
 
     private void defaultSetup()