← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 21816: test for export of a given set of classes in metadataexportparams

 

------------------------------------------------------------
revno: 21816
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2016-01-27 10:17:41 +0700
message:
  test for export of a given set of classes in metadataexportparams
modified:
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/DefaultMetadataExportService.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/MetadataExportParams.java
  dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/metadata2/MetadataExportServiceTest.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-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/DefaultMetadataExportService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/DefaultMetadataExportService.java	2016-01-25 08:31:37 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/DefaultMetadataExportService.java	2016-01-27 03:17:41 +0000
@@ -66,8 +66,17 @@
 
         for ( Class<? extends IdentifiableObject> klass : params.getClasses() )
         {
-            Schema schema = schemaService.getDynamicSchema( klass );
-            Query query = Query.from( schema );
+            Query query;
+
+            if ( params.hasQuery( klass ) )
+            {
+                query = params.getQuery( klass );
+            }
+            else
+            {
+                Schema schema = schemaService.getDynamicSchema( klass );
+                query = Query.from( schema );
+            }
 
             List<? extends IdentifiableObject> objects = queryService.query( query );
             metadata.put( klass, objects );

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/MetadataExportParams.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/MetadataExportParams.java	2016-01-25 08:31:37 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/MetadataExportParams.java	2016-01-27 03:17:41 +0000
@@ -29,8 +29,11 @@
  */
 
 import org.hisp.dhis.common.IdentifiableObject;
+import org.hisp.dhis.query.Query;
 
+import java.util.HashMap;
 import java.util.HashSet;
+import java.util.Map;
 import java.util.Set;
 
 /**
@@ -40,6 +43,8 @@
 {
     private Set<Class<? extends IdentifiableObject>> classes = new HashSet<>();
 
+    private Map<Class<? extends IdentifiableObject>, Query> queries = new HashMap<>();
+
     public MetadataExportParams()
     {
     }
@@ -53,4 +58,35 @@
     {
         this.classes = classes;
     }
+
+    public MetadataExportParams addClass( Class<? extends IdentifiableObject> klass )
+    {
+        classes.add( klass );
+        return this;
+    }
+
+    @SuppressWarnings( "unchecked" )
+    public MetadataExportParams addQuery( Query query )
+    {
+        if ( !query.getSchema().isIdentifiableObject() )
+        {
+            return this;
+        }
+
+        Class<? extends IdentifiableObject> klass = (Class<? extends IdentifiableObject>) query.getSchema().getKlass();
+        classes.add( klass );
+        queries.put( klass, query );
+
+        return this;
+    }
+
+    public boolean hasQuery( Class<? extends IdentifiableObject> klass )
+    {
+        return queries.containsKey( klass );
+    }
+
+    public Query getQuery( Class<? extends IdentifiableObject> klass )
+    {
+        return queries.containsKey( klass ) ? queries.get( klass ) : null;
+    }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/metadata2/MetadataExportServiceTest.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/metadata2/MetadataExportServiceTest.java	2016-01-25 08:31:37 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/metadata2/MetadataExportServiceTest.java	2016-01-27 03:17:41 +0000
@@ -40,7 +40,7 @@
 import java.util.List;
 import java.util.Map;
 
-import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.*;
 
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
@@ -91,4 +91,39 @@
         assertEquals( 1, metadata.get( DataElementGroup.class ).size() );
         assertEquals( 3, metadata.get( DataElement.class ).size() );
     }
+
+    @Test
+    @SuppressWarnings( "unchecked" )
+    public void testMetadataExportWithCustomClasses()
+    {
+        DataElementGroup deg1 = createDataElementGroup( 'A' );
+        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 );
+
+        deg1.addDataElement( de1 );
+        deg1.addDataElement( de2 );
+        deg1.addDataElement( de3 );
+
+        deg1.setUser( user );
+        manager.save( deg1 );
+
+        MetadataExportParams params = new MetadataExportParams();
+        params.addClass( DataElement.class );
+
+        Map<Class<? extends IdentifiableObject>, List<? extends IdentifiableObject>> metadata = metadataExportService.getMetadata( params );
+
+        assertFalse( metadata.containsKey( User.class ) );
+        assertFalse( metadata.containsKey( DataElementGroup.class ) );
+        assertTrue( metadata.containsKey( DataElement.class ) );
+
+        assertEquals( 3, metadata.get( DataElement.class ).size() );
+    }
 }