← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 21996: validate/commit bundle objects classes sorted by schema order

 

------------------------------------------------------------
revno: 21996
committer: Morten Olav Hansen <morten@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2016-02-18 16:31:56 +0700
message:
  validate/commit bundle objects classes sorted by schema order
modified:
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/DefaultObjectBundleService.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/objectbundle/DefaultObjectBundleService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/DefaultObjectBundleService.java	2016-02-18 07:59:49 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/DefaultObjectBundleService.java	2016-02-18 09:31:56 +0000
@@ -39,6 +39,7 @@
 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.hisp.dhis.schema.validation.SchemaValidator;
 import org.hisp.dhis.schema.validation.ValidationViolation;
 import org.hisp.dhis.user.CurrentUserService;
@@ -67,6 +68,9 @@
     private SchemaValidator schemaValidator;
 
     @Autowired
+    private SchemaService schemaService;
+
+    @Autowired
     private SessionFactory sessionFactory;
 
     @Autowired
@@ -100,7 +104,9 @@
     {
         ObjectBundleValidation objectBundleValidation = new ObjectBundleValidation();
 
-        for ( Class<? extends IdentifiableObject> klass : bundle.getObjects().keySet() )
+        List<Class<? extends IdentifiableObject>> klasses = getSortedClasses( bundle );
+
+        for ( Class<? extends IdentifiableObject> klass : klasses )
         {
             if ( bundle.getImportMode().isUpdate() || bundle.getImportMode().isDelete() )
             {
@@ -143,6 +149,7 @@
     }
 
     @Override
+    @SuppressWarnings( "unchecked" )
     public void commit( ObjectBundle bundle )
     {
         if ( ObjectBundleMode.VALIDATE == bundle.getObjectBundleMode() )
@@ -152,8 +159,11 @@
 
         Session session = sessionFactory.getCurrentSession();
 
-        for ( Class<? extends IdentifiableObject> klass : bundle.getObjects().keySet() )
+        List<Class<? extends IdentifiableObject>> klasses = getSortedClasses( bundle );
+
+        for ( Class<? extends IdentifiableObject> klass : klasses )
         {
+            System.err.println( "klass: " + klass );
             List<IdentifiableObject> objects = bundle.getObjects().get( klass );
 
             if ( objects.isEmpty() )
@@ -203,7 +213,7 @@
         for ( IdentifiableObject object : objects )
         {
             preheatService.connectReferences( object, bundle.getPreheat(), bundle.getPreheatIdentifier() );
-            session.save( object );
+            manager.save( object, bundle.getUser() );
 
             if ( log.isDebugEnabled() )
             {
@@ -233,4 +243,21 @@
             }
         }
     }
+
+    @SuppressWarnings( "unchecked" )
+    private List<Class<? extends IdentifiableObject>> getSortedClasses( ObjectBundle bundle )
+    {
+        List<Class<? extends IdentifiableObject>> klasses = new ArrayList<>();
+
+        schemaService.getMetadataSchemas().forEach( schema -> {
+            Class<? extends IdentifiableObject> klass = (Class<? extends IdentifiableObject>) schema.getKlass();
+
+            if ( bundle.getObjects().containsKey( klass ) )
+            {
+                klasses.add( klass );
+            }
+        } );
+
+        return klasses;
+    }
 }