← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 22203: Fixes for data set / section updates

 

------------------------------------------------------------
revno: 22203
committer: Morten Olav Hansen <morten@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2016-03-09 14:55:01 +0700
message:
  Fixes for data set / section updates
added:
  dhis-2/dhis-services/dhis-service-dxf2/src/test/resources/dxf2/dataset_with_sections_gf_update.json
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/preheat/Preheat.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/main/resources/org/hisp/dhis/dataelement/hibernate/DataElementCategoryOption.hbm.xml
  dhis-2/dhis-services/dhis-service-core/src/main/resources/org/hisp/dhis/dataset/hibernate/Section.hbm.xml
  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/SectionObjectBundleHook.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/Preheat.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/preheat/Preheat.java	2016-03-08 06:33:11 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/preheat/Preheat.java	2016-03-09 07:55:01 +0000
@@ -28,6 +28,7 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import javassist.util.proxy.ProxyFactory;
 import org.hisp.dhis.common.IdentifiableObject;
 import org.hisp.dhis.dataelement.DataElementCategory;
 import org.hisp.dhis.dataelement.DataElementCategoryCombo;
@@ -139,16 +140,19 @@
         return isEmpty( identifier ) || !map.get( identifier ).containsKey( klass ) || map.get( identifier ).get( klass ).isEmpty();
     }
 
+    @SuppressWarnings( "unchecked" )
     public <T extends IdentifiableObject> Preheat put( PreheatIdentifier identifier, T object )
     {
         if ( object == null ) return this;
 
+        Class<? extends IdentifiableObject> klass = (Class<? extends IdentifiableObject>) getRealClass( object.getClass() );
+
         if ( PreheatIdentifier.UID == identifier || PreheatIdentifier.AUTO == identifier )
         {
             if ( !map.containsKey( PreheatIdentifier.UID ) ) map.put( PreheatIdentifier.UID, new HashMap<>() );
-            if ( !map.get( PreheatIdentifier.UID ).containsKey( object.getClass() ) ) map.get( PreheatIdentifier.UID ).put( object.getClass(), new HashMap<>() );
+            if ( !map.get( PreheatIdentifier.UID ).containsKey( klass ) ) map.get( PreheatIdentifier.UID ).put( klass, new HashMap<>() );
 
-            Map<String, IdentifiableObject> identifierMap = map.get( PreheatIdentifier.UID ).get( object.getClass() );
+            Map<String, IdentifiableObject> identifierMap = map.get( PreheatIdentifier.UID ).get( klass );
             String key = PreheatIdentifier.UID.getIdentifier( object );
             identifierMap.put( key, object );
         }
@@ -156,9 +160,9 @@
         if ( PreheatIdentifier.CODE == identifier || PreheatIdentifier.AUTO == identifier )
         {
             if ( !map.containsKey( PreheatIdentifier.CODE ) ) map.put( PreheatIdentifier.CODE, new HashMap<>() );
-            if ( !map.get( PreheatIdentifier.CODE ).containsKey( object.getClass() ) ) map.get( PreheatIdentifier.CODE ).put( object.getClass(), new HashMap<>() );
+            if ( !map.get( PreheatIdentifier.CODE ).containsKey( klass ) ) map.get( PreheatIdentifier.CODE ).put( klass, new HashMap<>() );
 
-            Map<String, IdentifiableObject> identifierMap = map.get( PreheatIdentifier.CODE ).get( object.getClass() );
+            Map<String, IdentifiableObject> identifierMap = map.get( PreheatIdentifier.CODE ).get( klass );
             String key = PreheatIdentifier.CODE.getIdentifier( object );
             identifierMap.put( key, object );
         }
@@ -263,4 +267,14 @@
     {
         return isDefaultClass( object ) && "default".equals( object.getName() );
     }
+
+    public static Class<?> getRealClass( Class<?> klass )
+    {
+        if ( ProxyFactory.isProxyClass( klass ) )
+        {
+            klass = klass.getSuperclass();
+        }
+
+        return klass;
+    }
 }

=== 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-03-08 08:09:23 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/preheat/DefaultPreheatService.java	2016-03-09 07:55:01 +0000
@@ -33,6 +33,7 @@
 import org.hisp.dhis.common.IdentifiableObject;
 import org.hisp.dhis.common.IdentifiableObjectManager;
 import org.hisp.dhis.common.MergeMode;
+import org.hisp.dhis.dataelement.DataElementCategoryCombo;
 import org.hisp.dhis.dataelement.DataElementOperand;
 import org.hisp.dhis.feedback.ErrorCode;
 import org.hisp.dhis.feedback.ObjectErrorReport;
@@ -259,9 +260,9 @@
             if ( !uidMap.containsKey( objectClass ) ) uidMap.put( objectClass, new HashSet<>() );
             if ( !codeMap.containsKey( objectClass ) ) codeMap.put( objectClass, new HashSet<>() );
 
-            properties.forEach( p -> {
-                for ( IdentifiableObject object : identifiableObjects )
-                {
+            for ( IdentifiableObject object : identifiableObjects )
+            {
+                properties.forEach( p -> {
                     if ( !p.isCollection() )
                     {
                         Class<? extends IdentifiableObject> klass = (Class<? extends IdentifiableObject>) p.getKlass();
@@ -301,42 +302,43 @@
                         }
                     }
 
-                    if ( !uidMap.containsKey( Attribute.class ) ) uidMap.put( Attribute.class, new HashSet<>() );
-                    if ( !codeMap.containsKey( Attribute.class ) ) codeMap.put( Attribute.class, new HashSet<>() );
-
-                    object.getAttributeValues().forEach( av -> {
-                        Attribute attribute = av.getAttribute();
-
-                        if ( attribute != null )
-                        {
-                            if ( !StringUtils.isEmpty( attribute.getUid() ) ) uidMap.get( Attribute.class ).add( attribute.getUid() );
-                            if ( !StringUtils.isEmpty( attribute.getCode() ) ) codeMap.get( Attribute.class ).add( attribute.getCode() );
-                        }
-                    } );
-
-                    if ( !uidMap.containsKey( UserGroup.class ) ) uidMap.put( UserGroup.class, new HashSet<>() );
-                    if ( !codeMap.containsKey( UserGroup.class ) ) codeMap.put( UserGroup.class, new HashSet<>() );
-
-                    object.getUserGroupAccesses().forEach( uga -> {
-                        UserGroup userGroup = uga.getUserGroup();
-
-                        if ( userGroup != null )
-                        {
-                            if ( !StringUtils.isEmpty( userGroup.getUid() ) ) uidMap.get( UserGroup.class ).add( userGroup.getUid() );
-                            if ( !StringUtils.isEmpty( userGroup.getCode() ) ) codeMap.get( UserGroup.class ).add( userGroup.getCode() );
-                        }
-                    } );
-
-                    if ( !StringUtils.isEmpty( object.getUid() ) ) uidMap.get( objectClass ).add( object.getUid() );
-                    if ( !StringUtils.isEmpty( object.getCode() ) ) codeMap.get( objectClass ).add( object.getCode() );
-
-                    if ( uidMap.get( Attribute.class ).isEmpty() ) uidMap.remove( Attribute.class );
-                    if ( codeMap.get( Attribute.class ).isEmpty() ) codeMap.remove( Attribute.class );
-
-                    if ( uidMap.get( UserGroup.class ).isEmpty() ) uidMap.remove( UserGroup.class );
-                    if ( codeMap.get( UserGroup.class ).isEmpty() ) codeMap.remove( UserGroup.class );
-                }
-            } );
+                } );
+
+                if ( !uidMap.containsKey( Attribute.class ) ) uidMap.put( Attribute.class, new HashSet<>() );
+                if ( !codeMap.containsKey( Attribute.class ) ) codeMap.put( Attribute.class, new HashSet<>() );
+
+                object.getAttributeValues().forEach( av -> {
+                    Attribute attribute = av.getAttribute();
+
+                    if ( attribute != null )
+                    {
+                        if ( !StringUtils.isEmpty( attribute.getUid() ) ) uidMap.get( Attribute.class ).add( attribute.getUid() );
+                        if ( !StringUtils.isEmpty( attribute.getCode() ) ) codeMap.get( Attribute.class ).add( attribute.getCode() );
+                    }
+                } );
+
+                if ( !uidMap.containsKey( UserGroup.class ) ) uidMap.put( UserGroup.class, new HashSet<>() );
+                if ( !codeMap.containsKey( UserGroup.class ) ) codeMap.put( UserGroup.class, new HashSet<>() );
+
+                object.getUserGroupAccesses().forEach( uga -> {
+                    UserGroup userGroup = uga.getUserGroup();
+
+                    if ( userGroup != null )
+                    {
+                        if ( !StringUtils.isEmpty( userGroup.getUid() ) ) uidMap.get( UserGroup.class ).add( userGroup.getUid() );
+                        if ( !StringUtils.isEmpty( userGroup.getCode() ) ) codeMap.get( UserGroup.class ).add( userGroup.getCode() );
+                    }
+                } );
+
+                if ( !StringUtils.isEmpty( object.getUid() ) ) uidMap.get( objectClass ).add( object.getUid() );
+                if ( !StringUtils.isEmpty( object.getCode() ) ) codeMap.get( objectClass ).add( object.getCode() );
+
+                if ( uidMap.get( Attribute.class ).isEmpty() ) uidMap.remove( Attribute.class );
+                if ( codeMap.get( Attribute.class ).isEmpty() ) codeMap.remove( Attribute.class );
+
+                if ( uidMap.get( UserGroup.class ).isEmpty() ) uidMap.remove( UserGroup.class );
+                if ( codeMap.get( UserGroup.class ).isEmpty() ) codeMap.remove( UserGroup.class );
+            }
         }
 
         return map;

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/resources/org/hisp/dhis/dataelement/hibernate/DataElementCategoryOption.hbm.xml'
--- dhis-2/dhis-services/dhis-service-core/src/main/resources/org/hisp/dhis/dataelement/hibernate/DataElementCategoryOption.hbm.xml	2015-11-23 06:52:49 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/resources/org/hisp/dhis/dataelement/hibernate/DataElementCategoryOption.hbm.xml	2016-03-09 07:55:01 +0000
@@ -30,14 +30,14 @@
         foreign-key="fk_categoryoption_organisationunits_organisationunitid" />
     </set>
 
-    <set name="categoryOptionCombos" table="categoryoptioncombos_categoryoptions" inverse="true">
+    <set name="categoryOptionCombos" table="categoryoptioncombos_categoryoptions" inverse="true" lazy="false">
       <cache usage="read-write" />
       <key column="categoryoptionid" foreign-key="fk_categoryoptioncombos_categoryoptions_categoryoptionid" />
       <many-to-many class="org.hisp.dhis.dataelement.DataElementCategoryOptionCombo" column="categoryoptioncomboid"
         foreign-key="fk_categoryoption_categoryoptioncomboid" />
     </set>
 
-    <set name="categories" table="categories_categoryoptions" inverse="true">
+    <set name="categories" table="categories_categoryoptions" inverse="true" lazy="false">
       <cache usage="read-write" />
       <key column="categoryoptionid" />
       <many-to-many class="org.hisp.dhis.dataelement.DataElementCategory" column="categoryid" />

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/resources/org/hisp/dhis/dataset/hibernate/Section.hbm.xml'
--- dhis-2/dhis-services/dhis-service-core/src/main/resources/org/hisp/dhis/dataset/hibernate/Section.hbm.xml	2015-04-01 08:55:40 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/resources/org/hisp/dhis/dataset/hibernate/Section.hbm.xml	2016-03-09 07:55:01 +0000
@@ -39,7 +39,7 @@
         foreign-key="fk_section_indicatorid" />
     </list>
 
-    <set name="greyedFields" table="sectiongreyedfields">
+    <set name="greyedFields" table="sectiongreyedfields" cascade="delete-orphan">
       <cache usage="read-write" />
       <key column="sectionid" foreign-key="fk_sectiongreyedfields_sectionid" />
       <many-to-many class="org.hisp.dhis.dataelement.DataElementOperand" column="dataelementoperandid"

=== 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-03-08 08:09:23 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/DefaultObjectBundleService.java	2016-03-09 07:55:01 +0000
@@ -49,7 +49,8 @@
 import org.hisp.dhis.schema.validation.SchemaValidator;
 import org.hisp.dhis.user.CurrentUserService;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.stereotype.Component;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 import org.springframework.util.StringUtils;
 
 import java.util.ArrayList;
@@ -61,7 +62,8 @@
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
  */
-@Component
+@Service
+@Transactional
 public class DefaultObjectBundleService implements ObjectBundleService
 {
     private static final Log log = LogFactory.getLog( DefaultObjectBundleService.class );
@@ -110,6 +112,7 @@
         }
 
         bundle.setPreheat( preheatService.preheat( preheatParams ) );
+        bundle.setObjectReferences( preheatService.collectObjectReferences( params.getObjects() ) );
 
         if ( !(bundle.getImportMode().isCreate() || bundle.getImportMode().isCreateAndUpdate()) )
         {
@@ -162,8 +165,6 @@
             }
         }
 
-        bundle.setObjectReferences( preheatService.collectObjectReferences( params.getObjects() ) );
-
         return bundle;
     }
 
@@ -211,6 +212,8 @@
 
                     if ( object == null )
                     {
+                        if ( Preheat.isDefaultClass( identifiableObject.getClass() ) ) continue;
+
                         ObjectErrorReport objectErrorReport = new ObjectErrorReport( klass, idx );
                         objectErrorReport.addErrorReport( new ErrorReport( klass, ErrorCode.E5001, bundle.getPreheatIdentifier(),
                             bundle.getPreheatIdentifier().getIdentifiersWithName( identifiableObject ) ) );
@@ -339,6 +342,8 @@
                 String msg = "Created object '" + bundle.getPreheatIdentifier().getIdentifiersWithName( object ) + "'";
                 log.debug( msg );
             }
+
+            sessionFactory.getCurrentSession().flush();
         }
     }
 
@@ -355,6 +360,7 @@
             preheatService.connectReferences( object, bundle.getPreheat(), bundle.getPreheatIdentifier() );
 
             IdentifiableObject persistedObject = bundle.getPreheat().get( bundle.getPreheatIdentifier(), object );
+
             persistedObject.mergeWith( object, bundle.getMergeMode() );
             persistedObject.mergeSharingWith( object );
 
@@ -367,6 +373,8 @@
                 String msg = "Updated object '" + bundle.getPreheatIdentifier().getIdentifiersWithName( persistedObject ) + "'";
                 log.debug( msg );
             }
+
+            sessionFactory.getCurrentSession().flush();
         }
     }
 
@@ -388,6 +396,8 @@
                 String msg = "Deleted object '" + bundle.getPreheatIdentifier().getIdentifiersWithName( object ) + "'";
                 log.debug( msg );
             }
+
+            sessionFactory.getCurrentSession().flush();
         }
     }
 

=== 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-26 10:05:22 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/ObjectBundle.java	2016-03-09 07:55:01 +0000
@@ -64,7 +64,7 @@
 
     private Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> objects = new HashMap<>();
 
-    private Map<Class<?>, Map<String, Map<String, Object>>> objectReferences;
+    private Map<Class<?>, Map<String, Map<String, Object>>> objectReferences = new HashMap<>();
 
     public ObjectBundle( ObjectBundleParams params )
     {

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/hooks/SectionObjectBundleHook.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/hooks/SectionObjectBundleHook.java	2016-03-08 10:39:47 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/hooks/SectionObjectBundleHook.java	2016-03-09 07:55:01 +0000
@@ -34,6 +34,9 @@
 import org.hisp.dhis.dxf2.metadata2.objectbundle.ObjectBundle;
 import org.springframework.stereotype.Component;
 
+import java.util.Map;
+import java.util.Set;
+
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
  */
@@ -43,10 +46,7 @@
     @Override
     public void preCreate( IdentifiableObject identifiableObject, ObjectBundle objectBundle )
     {
-        if ( !Section.class.isInstance( identifiableObject ) )
-        {
-            return;
-        }
+        if ( !Section.class.isInstance( identifiableObject ) ) return;
 
         Section section = (Section) identifiableObject;
 
@@ -56,4 +56,33 @@
             sessionFactory.getCurrentSession().save( dataElementOperand );
         }
     }
+
+    @Override
+    public void preUpdate( IdentifiableObject identifiableObject, ObjectBundle objectBundle )
+    {
+        if ( !Section.class.isInstance( identifiableObject ) ) return;
+
+        Section section = (Section) identifiableObject;
+        section.getGreyedFields().clear();
+    }
+
+    @Override
+    @SuppressWarnings( "unchecked" )
+    public void postUpdate( IdentifiableObject identifiableObject, ObjectBundle objectBundle )
+    {
+        if ( !Section.class.isInstance( identifiableObject ) ) return;
+
+        Section section = (Section) identifiableObject;
+        Map<String, Object> references = objectBundle.getObjectReferences( Section.class ).get( section.getUid() );
+        Set<DataElementOperand> dataElementOperands = (Set<DataElementOperand>) references.get( "greyedFields" );
+
+        for ( DataElementOperand dataElementOperand : dataElementOperands )
+        {
+            preheatService.connectReferences( dataElementOperand, objectBundle.getPreheat(), objectBundle.getPreheatIdentifier() );
+            sessionFactory.getCurrentSession().save( dataElementOperand );
+            section.getGreyedFields().add( dataElementOperand );
+        }
+
+        sessionFactory.getCurrentSession().update( section );
+    }
 }

=== 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-03-08 10:39:47 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/hooks/UserObjectBundleHook.java	2016-03-09 07:55:01 +0000
@@ -56,10 +56,7 @@
     @Override
     public void preCreate( IdentifiableObject identifiableObject, ObjectBundle objectBundle )
     {
-        if ( !User.class.isInstance( identifiableObject ) || ((User) identifiableObject).getUserCredentials() == null )
-        {
-            return;
-        }
+        if ( !User.class.isInstance( identifiableObject ) || ((User) identifiableObject).getUserCredentials() == null ) return;
 
         User user = (User) identifiableObject;
         userCredentials = user.getUserCredentials();
@@ -81,10 +78,7 @@
     @Override
     public void postCreate( IdentifiableObject identifiableObject, ObjectBundle objectBundle )
     {
-        if ( !User.class.isInstance( identifiableObject ) || userCredentials == null )
-        {
-            return;
-        }
+        if ( !User.class.isInstance( identifiableObject ) || userCredentials == null ) return;
 
         User user = (User) identifiableObject;
         userCredentials.setUserInfo( user );
@@ -99,10 +93,7 @@
     @SuppressWarnings( "unchecked" )
     public void postImport( ObjectBundle objectBundle )
     {
-        if ( !objectBundle.getObjects().containsKey( User.class ) )
-        {
-            return;
-        }
+        if ( !objectBundle.getObjects().containsKey( User.class ) ) return;
 
         List<IdentifiableObject> objects = objectBundle.getObjects().get( User.class );
         Map<String, Map<String, Object>> references = objectBundle.getObjectReferences( User.class );
@@ -114,6 +105,7 @@
 
         for ( IdentifiableObject identifiableObject : objects )
         {
+            identifiableObject = objectBundle.getPreheat().get( objectBundle.getPreheatIdentifier(), identifiableObject );
             Map<String, Object> referenceMap = references.get( identifiableObject.getUid() );
 
             if ( referenceMap.isEmpty() )

=== 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-03-09 02:59:46 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/metadata2/objectbundle/ObjectBundleServiceTest.java	2016-03-09 07:55:01 +0000
@@ -36,7 +36,9 @@
 import org.hisp.dhis.common.IdentifiableObjectManager;
 import org.hisp.dhis.common.MergeMode;
 import org.hisp.dhis.dataelement.DataElement;
+import org.hisp.dhis.dataelement.DataElementCategory;
 import org.hisp.dhis.dataelement.DataElementCategoryCombo;
+import org.hisp.dhis.dataelement.DataElementCategoryOption;
 import org.hisp.dhis.dataelement.DataElementGroup;
 import org.hisp.dhis.dataelement.DataElementOperand;
 import org.hisp.dhis.dataset.DataSet;
@@ -50,12 +52,14 @@
 import org.hisp.dhis.option.Option;
 import org.hisp.dhis.option.OptionSet;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
+import org.hisp.dhis.organisationunit.OrganisationUnitLevel;
 import org.hisp.dhis.period.PeriodType;
 import org.hisp.dhis.preheat.PreheatErrorReport;
 import org.hisp.dhis.preheat.PreheatIdentifier;
 import org.hisp.dhis.preheat.PreheatMode;
 import org.hisp.dhis.render.RenderFormat;
 import org.hisp.dhis.render.RenderService;
+import org.hisp.dhis.trackedentity.TrackedEntity;
 import org.hisp.dhis.user.User;
 import org.hisp.dhis.user.UserAuthorityGroup;
 import org.hisp.dhis.user.UserGroup;
@@ -82,12 +86,6 @@
     private IdentifiableObjectManager manager;
 
     @Autowired
-    private MetadataExportService metadataExportService;
-
-    @Autowired
-    private NodeService nodeService;
-
-    @Autowired
     private RenderService _renderService;
 
     @Override
@@ -820,8 +818,12 @@
         List<UserAuthorityGroup> userRoles = manager.getAll( UserAuthorityGroup.class );
         List<User> users = manager.getAll( User.class );
         List<DataElementOperand> dataElementOperands = manager.getAll( DataElementOperand.class );
+        List<TrackedEntity> trackedEntities = manager.getAll( TrackedEntity.class );
+        List<OrganisationUnitLevel> organisationUnitLevels = manager.getAll( OrganisationUnitLevel.class );
 
         assertFalse( organisationUnits.isEmpty() );
+        assertEquals( 1, organisationUnitLevels.size() );
+        assertEquals( 1, trackedEntities.size() );
         assertFalse( dataElements.isEmpty() );
         assertFalse( users.isEmpty() );
         assertFalse( userRoles.isEmpty() );
@@ -846,6 +848,83 @@
         assertNotNull( section.getDataSet() );
         assertNotNull( section.getCategoryCombo() );
         assertEquals( 1, section.getGreyedFields().size() );
+
+        DataElementCategoryCombo categoryCombo = manager.get( DataElementCategoryCombo.class, "faV8QvLgIwB" );
+        assertNotNull( categoryCombo );
+
+        DataElementCategory category = manager.get( DataElementCategory.class, "XJGLlMAMCcn" );
+        assertNotNull( category );
+
+        DataElementCategoryOption categoryOption1 = manager.get( DataElementCategoryOption.class, "JYiFOMKa25J" );
+        DataElementCategoryOption categoryOption2 = manager.get( DataElementCategoryOption.class, "tdaMRD34m8o" );
+
+        assertNotNull( categoryOption1 );
+        assertNotNull( categoryOption2 );
+    }
+
+    @Test
+    public void testUpdateDataSetWithSectionsAndGreyedFields() throws IOException
+    {
+        Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> metadata = renderService.fromMetadata(
+            new ClassPathResource( "dxf2/dataset_with_sections_gf.json" ).getInputStream(), RenderFormat.JSON );
+
+        ObjectBundleParams params = new ObjectBundleParams();
+        params.setObjectBundleMode( ObjectBundleMode.COMMIT );
+        params.setImportMode( ImportStrategy.CREATE );
+        params.setObjects( metadata );
+
+        ObjectBundle bundle = objectBundleService.create( params );
+        ObjectBundleValidation validate = objectBundleService.validate( bundle );
+        assertTrue( validate.getObjectErrorReportsMap().isEmpty() );
+
+        objectBundleService.commit( bundle );
+
+        metadata = renderService.fromMetadata( new ClassPathResource( "dxf2/dataset_with_sections_gf_update.json" ).getInputStream(), RenderFormat.JSON );
+
+        params = new ObjectBundleParams();
+        params.setObjectBundleMode( ObjectBundleMode.COMMIT );
+        params.setImportMode( ImportStrategy.UPDATE );
+        params.setObjects( metadata );
+
+        bundle = objectBundleService.create( params );
+        validate = objectBundleService.validate( bundle );
+        assertTrue( validate.getObjectErrorReportsMap().isEmpty() );
+
+        objectBundleService.commit( bundle );
+
+        List<DataSet> dataSets = manager.getAll( DataSet.class );
+        List<Section> sections = manager.getAll( Section.class );
+        List<OrganisationUnit> organisationUnits = manager.getAll( OrganisationUnit.class );
+        List<DataElement> dataElements = manager.getAll( DataElement.class );
+        List<UserAuthorityGroup> userRoles = manager.getAll( UserAuthorityGroup.class );
+        List<User> users = manager.getAll( User.class );
+        List<DataElementOperand> dataElementOperands = manager.getAll( DataElementOperand.class );
+
+        assertFalse( organisationUnits.isEmpty() );
+        assertFalse( dataElements.isEmpty() );
+        assertFalse( users.isEmpty() );
+        assertFalse( userRoles.isEmpty() );
+
+        assertEquals( 1, dataSets.size() );
+        assertEquals( 2, sections.size() );
+        assertEquals( 1, dataElementOperands.size() );
+
+        DataSet dataSet = dataSets.get( 0 );
+        assertEquals( 2, dataSet.getSections().size() );
+
+        Section section1 = sections.get( 0 );
+        Section section2 = sections.get( 1 );
+
+        assertEquals( 1, section1.getDataElements().size() );
+        assertEquals( 1, section2.getDataElements().size() );
+
+        assertNotNull( section1.getDataSet() );
+        assertNotNull( section2.getDataSet() );
+
+        Section section = manager.get( Section.class, "JwcV2ZifEQf" );
+        assertNotNull( section.getDataSet() );
+        assertNotNull( section.getCategoryCombo() );
+        assertEquals( 1, section.getGreyedFields().size() );
     }
 
     private void defaultSetup()

=== added file 'dhis-2/dhis-services/dhis-service-dxf2/src/test/resources/dxf2/dataset_with_sections_gf_update.json'
--- dhis-2/dhis-services/dhis-service-dxf2/src/test/resources/dxf2/dataset_with_sections_gf_update.json	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/test/resources/dxf2/dataset_with_sections_gf_update.json	2016-03-09 07:55:01 +0000
@@ -0,0 +1,417 @@
+{
+  "dataSets": [
+    {
+      "version": 5,
+      "name": "Data Set",
+      "user": {
+        "id": "T12jeH7KPzk"
+      },
+      "expiryDays": 0,
+      "mobile": false,
+      "compulsoryDataElementOperands": [ ],
+      "created": "2016-03-08T07:31:30.424+0000",
+      "publicAccess": "rw------",
+      "periodType": "Monthly",
+      "validCompleteOnly": false,
+      "attributeValues": [ ],
+      "openFuturePeriods": 0,
+      "userGroupAccesses": [ ],
+      "organisationUnits": [
+        {
+          "id": "x3gVvpbVgqy"
+        }
+      ],
+      "timelyDays": 15,
+      "dataElements": [
+        {
+          "id": "nHwIqKAudKN"
+        },
+        {
+          "id": "VolAzjFe5zP"
+        }
+      ],
+      "indicators": [ ],
+      "noValueRequiresComment": false,
+      "notifyCompletingUser": false,
+      "skipOffline": false,
+      "renderAsTabs": false,
+      "id": "em8Bg4LCr5k",
+      "shortName": "Data Set",
+      "fieldCombinationRequired": false,
+      "categoryCombo": {
+        "id": "VfQ8S1gSE9T"
+      },
+      "renderHorizontally": false,
+      "lastUpdated": "2016-03-08T07:34:23.833+0000",
+      "dataElementDecoration": false
+    }
+  ],
+  "organisationUnits": [
+    {
+      "openingDate": "2016-03-08",
+      "name": "Country",
+      "user": {
+        "id": "T12jeH7KPzk"
+      },
+      "description": "",
+      "path": "/x3gVvpbVgqy",
+      "uuid": "d981e7c4-3c72-4e0d-8995-078f16830473",
+      "created": "2016-03-08T07:27:17.757+0000",
+      "id": "x3gVvpbVgqy",
+      "shortName": "Country",
+      "attributeValues": [ ],
+      "featureType": "NONE",
+      "lastUpdated": "2016-03-08T07:27:17.772+0000"
+    }
+  ],
+  "categoryOptions": [
+    {
+      "name": "default",
+      "id": "NS6DGfTR650",
+      "publicAccess": "--------",
+      "created": "2016-03-08T07:26:31.533+0000",
+      "lastUpdated": "2016-03-08T07:26:31.551+0000",
+      "userGroupAccesses": [ ],
+      "organisationUnits": [ ],
+      "attributeValues": [ ],
+      "shortName": "default"
+    },
+    {
+      "created": "2016-03-08T07:28:50.372+0000",
+      "publicAccess": "rw------",
+      "id": "JYiFOMKa25J",
+      "shortName": "Female",
+      "organisationUnits": [ ],
+      "attributeValues": [ ],
+      "userGroupAccesses": [ ],
+      "lastUpdated": "2016-03-08T07:28:50.374+0000",
+      "name": "Female",
+      "user": {
+        "id": "T12jeH7KPzk"
+      }
+    },
+    {
+      "attributeValues": [ ],
+      "organisationUnits": [ ],
+      "userGroupAccesses": [ ],
+      "shortName": "Male",
+      "lastUpdated": "2016-03-08T07:28:44.217+0000",
+      "created": "2016-03-08T07:28:44.214+0000",
+      "id": "tdaMRD34m8o",
+      "publicAccess": "rw------",
+      "name": "Male",
+      "user": {
+        "id": "T12jeH7KPzk"
+      }
+    }
+  ],
+  "dataElements": [
+    {
+      "domainType": "AGGREGATE",
+      "user": {
+        "id": "T12jeH7KPzk"
+      },
+      "valueType": "NUMBER",
+      "name": "Simple Value",
+      "lastUpdated": "2016-03-08T07:30:07.699+0000",
+      "attributeValues": [ ],
+      "shortName": "Simple Value",
+      "userGroupAccesses": [ ],
+      "categoryCombo": {
+        "id": "VfQ8S1gSE9T"
+      },
+      "aggregationLevels": [ ],
+      "aggregationType": "SUM",
+      "publicAccess": "rw------",
+      "zeroIsSignificant": false,
+      "id": "nHwIqKAudKN",
+      "created": "2016-03-08T07:30:07.698+0000"
+    },
+    {
+      "user": {
+        "id": "T12jeH7KPzk"
+      },
+      "valueType": "NUMBER",
+      "name": "Value with CC",
+      "domainType": "AGGREGATE",
+      "publicAccess": "rw------",
+      "zeroIsSignificant": false,
+      "id": "VolAzjFe5zP",
+      "created": "2016-03-08T07:30:58.416+0000",
+      "lastUpdated": "2016-03-08T07:30:58.417+0000",
+      "attributeValues": [ ],
+      "userGroupAccesses": [ ],
+      "shortName": "Value with CC",
+      "aggregationType": "SUM",
+      "aggregationLevels": [ ],
+      "categoryCombo": {
+        "id": "faV8QvLgIwB"
+      }
+    }
+  ],
+  "userRoles": [
+    {
+      "authorities": [
+        "F_TRACKED_ENTITY_INSTANCE_SEARCH_IN_ALL_ORGUNITS",
+        "ALL",
+        "F_USERGROUP_MANAGING_RELATIONSHIPS_ADD",
+        "F_REPORTTABLE_PUBLIC_ADD",
+        "F_TRACKED_ENTITY_INSTANCE_DELETE",
+        "F_USER_GROUPS_READ_ONLY_ADD_MEMBERS",
+        "F_MAP_PUBLIC_ADD",
+        "F_USER_ADD_WITHIN_MANAGED_GROUP",
+        "F_TRACKED_ENTITY_INSTANCE_SEARCH",
+        "F_PROGRAM_ENROLLMENT",
+        "F_REPORTTABLE_EXTERNAL",
+        "F_SQLVIEW_EXTERNAL",
+        "F_GIS_ADMIN",
+        "F_REPLICATE_USER",
+        "F_INSERT_CUSTOM_JS_CSS",
+        "F_DASHBOARD_PUBLIC_ADD",
+        "F_METADATA_IMPORT",
+        "F_CHART_PUBLIC_ADD",
+        "F_VIEW_UNAPPROVED_DATA",
+        "F_CHART_EXTERNAL",
+        "F_USERGROUP_MANAGING_RELATIONSHIPS_VIEW",
+        "F_METADATA_EXPORT",
+        "F_PROGRAM_UNENROLLMENT",
+        "F_APPROVE_DATA",
+        "F_ACCEPT_DATA_LOWER_LEVELS",
+        "F_TRACKED_ENTITY_INSTANCE_ADD",
+        "F_USERGROUP_PUBLIC_ADD",
+        "F_OAUTH2_CLIENT_MANAGE",
+        "F_TRACKED_ENTITY_DATAVALUE_ADD",
+        "F_PROGRAM_DASHBOARD_CONFIG_ADMIN",
+        "F_MAP_EXTERNAL",
+        "F_APPROVE_DATA_LOWER_LEVELS",
+        "F_TRACKED_ENTITY_DATAVALUE_DELETE"
+      ],
+      "name": "Superuser",
+      "dataSets": [ ],
+      "userGroupAccesses": [ ],
+      "lastUpdated": "2016-03-08T07:26:51.925+0000",
+      "programs": [ ],
+      "created": "2016-03-08T07:26:51.925+0000",
+      "publicAccess": "--------",
+      "id": "wSdzlWUHmEu"
+    }
+  ],
+  "trackedEntities": [
+    {
+      "description": "Person",
+      "id": "MDvmqCKmXQM",
+      "name": "Person",
+      "attributeValues": [ ]
+    }
+  ],
+  "categoryCombos": [
+    {
+      "created": "2016-03-08T07:26:31.545+0000",
+      "skipTotal": false,
+      "publicAccess": "--------",
+      "id": "VfQ8S1gSE9T",
+      "userGroupAccesses": [ ],
+      "lastUpdated": "2016-03-08T07:26:31.548+0000",
+      "categories": [
+        {
+          "id": "k3ASmFuqr11"
+        }
+      ],
+      "name": "default",
+      "dataDimensionType": "DISAGGREGATION"
+    },
+    {
+      "created": "2016-03-08T07:29:07.104+0000",
+      "id": "faV8QvLgIwB",
+      "skipTotal": false,
+      "publicAccess": "rw------",
+      "userGroupAccesses": [ ],
+      "lastUpdated": "2016-03-08T07:29:15.719+0000",
+      "name": "Gender",
+      "categories": [
+        {
+          "id": "XJGLlMAMCcn"
+        }
+      ],
+      "user": {
+        "id": "T12jeH7KPzk"
+      }
+    }
+  ],
+  "organisationUnitLevels": [
+    {
+      "id": "JEMRBUHgpqN",
+      "created": "2016-03-08T07:27:22.448+0000",
+      "name": "Level 1",
+      "level": 1,
+      "lastUpdated": "2016-03-08T07:27:22.449+0000"
+    }
+  ],
+  "users": [
+    {
+      "lastUpdated": "2016-03-08T07:26:51.909+0000",
+      "attributeValues": [ ],
+      "organisationUnits": [
+        {
+          "id": "x3gVvpbVgqy"
+        }
+      ],
+      "firstName": "admin",
+      "userCredentials": {
+        "cogsDimensionConstraints": [ ],
+        "lastLogin": "2016-03-08T07:26:51.942+0000",
+        "invitation": false,
+        "username": "admin",
+        "passwordLastUpdated": "2016-03-08T07:26:51.942+0000",
+        "selfRegistered": false,
+        "externalAuth": false,
+        "created": "2016-03-08T07:26:52.033+0000",
+        "disabled": false,
+        "userInfo": {
+          "id": "T12jeH7KPzk"
+        },
+        "catDimensionConstraints": [ ],
+        "userRoles": [
+          {
+            "id": "wSdzlWUHmEu"
+          }
+        ]
+      },
+      "id": "T12jeH7KPzk",
+      "created": "2016-03-08T07:26:51.909+0000",
+      "teiSearchOrganisationUnits": [ ],
+      "dataViewOrganisationUnits": [ ],
+      "surname": "admin"
+    }
+  ],
+  "categories": [
+    {
+      "lastUpdated": "2016-03-08T07:26:31.729+0000",
+      "categoryOptions": [
+        {
+          "id": "NS6DGfTR650"
+        }
+      ],
+      "userGroupAccesses": [ ],
+      "id": "k3ASmFuqr11",
+      "publicAccess": "--------",
+      "created": "2016-03-08T07:26:31.543+0000",
+      "dataDimensionType": "DISAGGREGATION",
+      "dataDimension": true,
+      "name": "default"
+    },
+    {
+      "id": "XJGLlMAMCcn",
+      "publicAccess": "rw------",
+      "created": "2016-03-08T07:28:58.738+0000",
+      "lastUpdated": "2016-03-08T07:28:58.740+0000",
+      "categoryOptions": [
+        {
+          "id": "JYiFOMKa25J"
+        },
+        {
+          "id": "tdaMRD34m8o"
+        }
+      ],
+      "userGroupAccesses": [ ],
+      "dataDimension": true,
+      "user": {
+        "id": "T12jeH7KPzk"
+      },
+      "name": "Gender"
+    }
+  ],
+  "sections": [
+    {
+      "lastUpdated": "2016-03-08T07:32:29.362+0000",
+      "indicators": [ ],
+      "dataElements": [
+        {
+          "id": "nHwIqKAudKN"
+        }
+      ],
+      "dataSet": {
+        "id": "em8Bg4LCr5k"
+      },
+      "id": "JwcV2ZifEQf",
+      "greyedFields": [
+        {
+          "dataElement": {
+            "id": "VolAzjFe5zP"
+          },
+          "categoryOptionCombo": {
+            "id": "p99yaU6mweU"
+          }
+        }
+      ],
+      "created": "2016-03-08T07:32:29.361+0000",
+      "sortOrder": 0,
+      "name": "Section Default"
+    },
+    {
+      "dataElements": [
+        {
+          "id": "VolAzjFe5zP"
+        }
+      ],
+      "indicators": [ ],
+      "greyedFields": [ ],
+      "lastUpdated": "2016-03-08T07:34:23.831+0000",
+      "created": "2016-03-08T07:32:42.140+0000",
+      "dataSet": {
+        "id": "em8Bg4LCr5k"
+      },
+      "id": "C50M0WxaI7y",
+      "name": "Section Gender",
+      "sortOrder": 0
+    }
+  ],
+  "date": "2016-03-08T07:35:11.295+0000",
+  "categoryOptionCombos": [
+    {
+      "name": "default",
+      "created": "2016-03-08T07:26:31.546+0000",
+      "id": "Z3oAZ1xdHIg",
+      "ignoreApproval": false,
+      "categoryCombo": {
+        "id": "VfQ8S1gSE9T"
+      },
+      "lastUpdated": "2016-03-08T07:26:31.546+0000",
+      "categoryOptions": [
+        {
+          "id": "NS6DGfTR650"
+        }
+      ]
+    },
+    {
+      "name": "Female",
+      "created": "2016-03-08T07:29:15.714+0000",
+      "id": "J5uZylXMmbB",
+      "ignoreApproval": false,
+      "categoryCombo": {
+        "id": "faV8QvLgIwB"
+      },
+      "categoryOptions": [
+        {
+          "id": "JYiFOMKa25J"
+        }
+      ],
+      "lastUpdated": "2016-03-08T07:29:15.715+0000"
+    },
+    {
+      "name": "Male",
+      "created": "2016-03-08T07:29:15.717+0000",
+      "id": "p99yaU6mweU",
+      "ignoreApproval": false,
+      "categoryCombo": {
+        "id": "faV8QvLgIwB"
+      },
+      "categoryOptions": [
+        {
+          "id": "tdaMRD34m8o"
+        }
+      ],
+      "lastUpdated": "2016-03-08T07:29:15.717+0000"
+    }
+  ]
+}