← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 6982: minor refactor in importer

 

------------------------------------------------------------
revno: 6982
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2012-05-17 22:26:32 +0200
message:
  minor refactor in importer
modified:
  dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/importers/DefaultIdentifiableObjectImporter.java
  dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/ReflectionUtils.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-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/importers/DefaultIdentifiableObjectImporter.java'
--- dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/importers/DefaultIdentifiableObjectImporter.java	2012-05-17 19:54:21 +0000
+++ dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/importers/DefaultIdentifiableObjectImporter.java	2012-05-17 20:26:32 +0000
@@ -37,7 +37,6 @@
 import org.hisp.dhis.common.IdentifiableObject;
 import org.hisp.dhis.common.NameableObject;
 import org.hisp.dhis.common.annotation.Scanned;
-import org.hisp.dhis.dataelement.DataElement;
 import org.hisp.dhis.dxf2.importsummary.ImportConflict;
 import org.hisp.dhis.dxf2.importsummary.ImportCount;
 import org.hisp.dhis.dxf2.metadata.ImportOptions;
@@ -45,14 +44,12 @@
 import org.hisp.dhis.dxf2.metadata.ObjectBridge;
 import org.hisp.dhis.dxf2.utils.OrganisationUnitUtils;
 import org.hisp.dhis.importexport.ImportStrategy;
-import org.hisp.dhis.indicator.Indicator;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.organisationunit.comparator.OrganisationUnitComparator;
 import org.hisp.dhis.period.Period;
 import org.hisp.dhis.period.PeriodStore;
 import org.hisp.dhis.period.PeriodType;
 import org.hisp.dhis.system.util.ReflectionUtils;
-import org.hisp.dhis.user.User;
 import org.springframework.beans.factory.annotation.Autowired;
 
 import java.lang.reflect.Field;
@@ -211,13 +208,7 @@
 
         for ( T object : objects )
         {
-            Set<AttributeValue> attributeValues = getAttributeValues( object );
-
-            if ( attributeValues.size() > 0 )
-            {
-                setAttributeValues( object, new HashSet<AttributeValue>() );
-            }
-
+            Set<AttributeValue> attributeValues = getAndClearAttributeValues( object );
             List<ImportConflict> conflicts = importObjectLocal( object, options );
 
             if ( !options.isDryRun() )
@@ -225,28 +216,7 @@
                 sessionFactory.getCurrentSession().flush();
             }
 
-            if ( attributeValues.size() > 0 )
-            {
-                updateAttributeValues( attributeValues );
-
-                for ( AttributeValue attributeValue : attributeValues )
-                {
-                    attributeService.addAttributeValue( attributeValue );
-                }
-
-                if ( !options.isDryRun() )
-                {
-                    sessionFactory.getCurrentSession().flush();
-                }
-
-                setAttributeValues( object, attributeValues );
-
-                if ( !options.isDryRun() )
-                {
-                    sessionFactory.getCurrentSession().flush();
-                }
-            }
-
+            updateAttributeValues( object, attributeValues, options.isDryRun() );
 
             if ( !conflicts.isEmpty() )
             {
@@ -262,44 +232,57 @@
         ReflectionUtils.invokeSetterMethod( "attributeValues", object, attributeValues );
     }
 
-    private Set<AttributeValue> getAttributeValues( T object )
+    private Set<AttributeValue> getAndClearAttributeValues( T object )
     {
-        if ( DataElement.class.isInstance( object ) )
-        {
-            return ((DataElement) object).getAttributeValues();
-        }
-        else if ( Indicator.class.isInstance( object ) )
-        {
-            return ((Indicator) object).getAttributeValues();
-        }
-        else if ( OrganisationUnit.class.isInstance( object ) )
-        {
-            return ((OrganisationUnit) object).getAttributeValues();
-        }
-        else if ( User.class.isInstance( object ) )
-        {
-            return ((User) object).getAttributeValues();
-        }
-
-        return new HashSet<AttributeValue>();
+        Set<AttributeValue> attributeValues = new HashSet<AttributeValue>();
+
+        if ( ReflectionUtils.findGetterMethod( "attributeValues", object ) != null )
+        {
+            attributeValues = ReflectionUtils.invokeGetterMethod( "attributeValues", object );
+
+            if ( attributeValues.size() > 0 )
+            {
+                setAttributeValues( object, new HashSet<AttributeValue>() );
+            }
+        }
+
+        return attributeValues;
     }
 
-    private void updateAttributeValues( Set<AttributeValue> attributeValues )
+    private void updateAttributeValues( T object, Set<AttributeValue> attributeValues, boolean dryRun )
     {
-        for ( AttributeValue attributeValue : attributeValues )
+        if ( attributeValues.size() > 0 )
         {
-            Attribute attribute = objectBridge.getObject( attributeValue.getAttribute() );
-
-            log.info( "Attribute: " + attribute );
-
-            if ( attribute == null )
-            {
-                log.warn( "Unknown reference to " + attributeValue.getAttribute() + " on object " + attributeValue );
-                continue;
-            }
-
-            attributeValue.setId( 0 );
-            attributeValue.setAttribute( attribute );
+            for ( AttributeValue attributeValue : attributeValues )
+            {
+                Attribute attribute = objectBridge.getObject( attributeValue.getAttribute() );
+
+                if ( attribute == null )
+                {
+                    log.warn( "Unknown reference to " + attributeValue.getAttribute() + " on object " + attributeValue );
+                    continue;
+                }
+
+                attributeValue.setId( 0 );
+                attributeValue.setAttribute( attribute );
+            }
+
+            for ( AttributeValue attributeValue : attributeValues )
+            {
+                attributeService.addAttributeValue( attributeValue );
+            }
+
+            if ( !dryRun )
+            {
+                sessionFactory.getCurrentSession().flush();
+            }
+
+            setAttributeValues( object, attributeValues );
+
+            if ( !dryRun )
+            {
+                sessionFactory.getCurrentSession().flush();
+            }
         }
     }
 

=== modified file 'dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/ReflectionUtils.java'
--- dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/ReflectionUtils.java	2012-04-18 20:24:12 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/ReflectionUtils.java	2012-05-17 20:26:32 +0000
@@ -217,9 +217,10 @@
             return object.getClass().getMethod( "get" + StringUtils.capitalize( fieldName ), classes );
         } catch ( NoSuchMethodException e )
         {
-            log.info( "Getter method was not found for fieldName: " + fieldName );
-            return null;
+            // log.warn( "Getter method was not found for fieldName: " + fieldName );
         }
+
+        return null;
     }
 
     public static Method findSetterMethod( String fieldName, Object object, Class<?>... classes )
@@ -248,7 +249,7 @@
 
         if ( method == null )
         {
-            log.info( "Setter method was not found for fieldName: " + fieldName );
+            // log.warn( "Setter method was not found for fieldName: " + fieldName );
         }
 
         return method;
@@ -269,11 +270,11 @@
             return (T) method.invoke( object );
         } catch ( InvocationTargetException e )
         {
-            log.info( "InvocationTargetException for fieldName: " + fieldName );
+            log.warn( "InvocationTargetException for fieldName: " + fieldName );
             return null;
         } catch ( IllegalAccessException e )
         {
-            log.info( "IllegalAccessException for fieldName: " + fieldName );
+            log.warn( "IllegalAccessException for fieldName: " + fieldName );
             return null;
         }
     }
@@ -293,11 +294,11 @@
             return (T) method.invoke( object, objects );
         } catch ( InvocationTargetException e )
         {
-            log.info( "InvocationTargetException for fieldName: " + fieldName );
+            log.warn( "InvocationTargetException for fieldName: " + fieldName );
             return null;
         } catch ( IllegalAccessException e )
         {
-            log.info( "IllegalAccessException for fieldName: " + fieldName );
+            log.warn( "IllegalAccessException for fieldName: " + fieldName );
             return null;
         }
     }