← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 21182: Updated handling of attribute value import in dxf2 importer, wip

 

------------------------------------------------------------
revno: 21182
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2015-11-24 13:16:34 +0700
message:
  Updated handling of attribute value import in dxf2 importer, wip
modified:
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/importers/DefaultIdentifiableObjectImporter.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/metadata/importers/DefaultIdentifiableObjectImporter.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/importers/DefaultIdentifiableObjectImporter.java	2015-11-23 07:21:18 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/importers/DefaultIdentifiableObjectImporter.java	2015-11-24 06:16:34 +0000
@@ -37,7 +37,6 @@
 import org.hisp.dhis.attribute.Attribute;
 import org.hisp.dhis.attribute.AttributeService;
 import org.hisp.dhis.attribute.AttributeValue;
-import org.hisp.dhis.attribute.exception.NonUniqueAttributeValueException;
 import org.hisp.dhis.common.BaseAnalyticalObject;
 import org.hisp.dhis.common.BaseIdentifiableObject;
 import org.hisp.dhis.common.DataDimensionItem;
@@ -73,6 +72,7 @@
 import org.hisp.dhis.program.ProgramStageDataElement;
 import org.hisp.dhis.program.ProgramTrackedEntityAttribute;
 import org.hisp.dhis.program.ProgramValidation;
+import org.hisp.dhis.schema.Schema;
 import org.hisp.dhis.schema.SchemaService;
 import org.hisp.dhis.security.acl.AclService;
 import org.hisp.dhis.system.util.ReflectionUtils;
@@ -938,6 +938,8 @@
 
         private User user;
 
+        private Schema schema;
+
         private NonIdentifiableObjects( User user )
         {
             this.user = user;
@@ -945,6 +947,7 @@
 
         public void extract( T object )
         {
+            schema = schemaService.getDynamicSchema( object.getClass() );
             attributeValues = extractAttributeValues( object );
             leftSide = extractExpression( object, "leftSide" );
             rightSide = extractExpression( object, "rightSide" );
@@ -958,9 +961,10 @@
 
         public void delete( T object )
         {
+            schema = schemaService.getDynamicSchema( object.getClass() );
+
             if ( !options.isDryRun() )
             {
-                deleteAttributeValues( object );
                 deleteExpression( object, "leftSide" );
                 deleteExpression( object, "rightSide" );
                 deleteDataEntryForm( object, "dataEntryForm" );
@@ -976,6 +980,8 @@
 
         public void save( T object )
         {
+            schema = schemaService.getDynamicSchema( object.getClass() );
+
             saveAttributeValues( object, attributeValues );
             saveExpression( object, "leftSide", leftSide );
             saveExpression( object, "rightSide", rightSide );
@@ -1112,23 +1118,6 @@
             }
         }
 
-        private Set<AttributeValue> extractAttributeValues( T object )
-        {
-            Set<AttributeValue> attributeValues = Sets.newHashSet();
-
-            if ( ReflectionUtils.findGetterMethod( "attributeValues", object ) != null )
-            {
-                attributeValues = ReflectionUtils.invokeGetterMethod( "attributeValues", object );
-
-                if ( attributeValues != null && attributeValues.size() > 0 )
-                {
-                    ReflectionUtils.invokeSetterMethod( "attributeValues", object, Sets.newHashSet() );
-                }
-            }
-
-            return attributeValues;
-        }
-
         private void saveExpression( T object, String fieldName, Expression expression )
         {
             if ( expression != null )
@@ -1166,46 +1155,41 @@
             sessionFactory.getCurrentSession().flush();
         }
 
-        private void deleteAttributeValues( T object )
+        private Set<AttributeValue> extractAttributeValues( T object )
         {
-            if ( !Attribute.class.isAssignableFrom( object.getClass() ) )
+            Set<AttributeValue> attributeValues = new HashSet<>();
+
+            if ( schema.havePersistedProperty( "attributeValues" ) )
             {
-                Set<AttributeValue> attributeValues = extractAttributeValues( object );
-                attributeValues.forEach( attributeService::deleteAttributeValue );
+                attributeValues = ReflectionUtils.invokeGetterMethod( "attributeValues", object );
+                ReflectionUtils.invokeSetterMethod( "attributeValues", object, new HashSet<>() );
             }
+
+            return attributeValues;
         }
 
         private void saveAttributeValues( T object, Collection<AttributeValue> attributeValues )
         {
-            if ( attributeValues != null && attributeValues.size() > 0 )
-            {
-                for ( AttributeValue attributeValue : attributeValues )
-                {
-                    Attribute attribute = objectBridge.getObject( attributeValue.getAttribute() );
-
-                    if ( attribute == null )
-                    {
-                        log.debug( "Unknown reference to " + attributeValue.getAttribute() + " on object " + attributeValue );
-                        return;
-                    }
-
-                    attributeValue.setId( 0 );
-                    attributeValue.setAttribute( attribute );
-                }
-
-                for ( AttributeValue attributeValue : attributeValues )
-                {
-                    try
-                    {
-                        attributeService.addAttributeValue( object, attributeValue );
-                    }
-                    catch ( NonUniqueAttributeValueException ex )
-                    {
-                        log.info( ex.getMessage() );
-                    }
-                }
-
-                ReflectionUtils.invokeSetterMethod( "attributeValues", object, attributeValues );
+            for ( AttributeValue attributeValue : attributeValues )
+            {
+                Attribute attribute = objectBridge.getObject( attributeValue.getAttribute() );
+
+                if ( attribute == null )
+                {
+                    log.debug( "Unknown reference to " + attributeValue.getAttribute() + " on object " + attributeValue );
+                    return;
+                }
+
+                attributeValue.setAttribute( attribute );
+            }
+
+            try
+            {
+                attributeService.updateAttributeValues( object, new HashSet<>( attributeValues ) );
+            }
+            catch ( Exception ex )
+            {
+                log.info( ex.getMessage() );
             }
         }