← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 6482: minor changes to importer

 

------------------------------------------------------------
revno: 6482
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2012-04-04 17:27:10 +0200
message:
  minor changes to importer
modified:
  dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/Importer.java
  dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/importers/AbstractImporter.java
  dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/importers/ConstantImporter.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/Importer.java'
--- dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/Importer.java	2012-04-03 13:35:15 +0000
+++ dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/Importer.java	2012-04-04 15:27:10 +0000
@@ -64,7 +64,7 @@
     ImportCount getCurrentImportCount();
 
     /**
-     * Can this importer handle a certain Class type
+     * Can this importer handle a certain Class type?
      *
      * @param clazz Class to check for
      * @return true or false depending on if class is supported

=== modified file 'dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/importers/AbstractImporter.java'
--- dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/importers/AbstractImporter.java	2012-04-03 12:59:54 +0000
+++ dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/importers/AbstractImporter.java	2012-04-04 15:27:10 +0000
@@ -73,7 +73,7 @@
      *
      * @param object Object to import
      */
-    protected abstract void newObject( T object );
+    protected abstract ImportConflict newObject( T object );
 
     /**
      * Update object from old => new.
@@ -81,7 +81,7 @@
      * @param object    Object to import
      * @param oldObject The current version of the object
      */
-    protected abstract void updatedObject( T object, T oldObject );
+    protected abstract ImportConflict updatedObject( T object, T oldObject );
 
     /**
      * Current object name, used to fill name part of a ImportConflict
@@ -119,6 +119,7 @@
     @Override
     public ImportConflict importObject( T object, ImportOptions options )
     {
+        // move this to importCollection
         Map<String, T> map = getIdMap( (Class) object.getClass(), options.getIdScheme() );
         String identifier = getIdentifier( object, options.getIdScheme() );
         T oldObject = map.get( identifier );
@@ -128,34 +129,58 @@
             if ( oldObject != null )
             {
                 ignores++;
-                return new ImportConflict( object.getClass().getName(), "Strategy is new, but identifier '" + identifier + "' already exists." );
+                return new ImportConflict( getDisplayName( object, options.getIdScheme() ), "Strategy is new, but identifier '" + identifier + "' already exists." );
+            }
+
+            ImportConflict conflict = newObject( object );
+
+            if ( conflict != null )
+            {
+                return conflict;
             }
 
             imports++;
-            newObject( object );
         }
         else if ( options.getImportStrategy().isUpdatesStrategy() )
         {
             if ( oldObject == null )
             {
                 ignores++;
-                return new ImportConflict( object.getClass().getName(), "Strategy is updates, but identifier '" + identifier + "' does not exist." );
+                return new ImportConflict( getDisplayName( object, options.getIdScheme() ), "Strategy is updates, but identifier '" + identifier + "' does not exist." );
+            }
+
+            ImportConflict conflict = updatedObject( object, oldObject );
+
+            if ( conflict != null )
+            {
+                return conflict;
             }
 
             updates++;
-            updatedObject( object, oldObject );
         }
         else if ( options.getImportStrategy().isNewAndUpdatesStrategy() )
         {
             if ( oldObject != null )
             {
+                ImportConflict conflict = updatedObject( object, oldObject );
+
+                if ( conflict != null )
+                {
+                    return conflict;
+                }
+
                 updates++;
-                updatedObject( object, oldObject );
             }
             else
             {
+                ImportConflict conflict = newObject( object );
+
+                if ( conflict != null )
+                {
+                    return conflict;
+                }
+
                 imports++;
-                newObject( object );
             }
         }
 
@@ -213,4 +238,31 @@
 
         return null;
     }
+
+    protected String getDisplayName( IdentifiableObject object, IdScheme scheme )
+    {
+        if ( scheme.isUidScheme() )
+        {
+            if ( object.getUid() != null )
+            {
+                return object.getUid();
+            }
+        }
+        else if ( scheme.isNameScheme() )
+        {
+            if ( object.getName() != null )
+            {
+                return object.getName();
+            }
+        }
+        else if ( scheme.isCodeScheme() )
+        {
+            if ( object.getCode() != null )
+            {
+                return object.getCode();
+            }
+        }
+
+        return object.getClass().getName();
+    }
 }

=== modified file 'dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/importers/ConstantImporter.java'
--- dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/importers/ConstantImporter.java	2012-04-03 10:24:37 +0000
+++ dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/importers/ConstantImporter.java	2012-04-04 15:27:10 +0000
@@ -30,6 +30,7 @@
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.hisp.dhis.constant.Constant;
+import org.hisp.dhis.dxf2.importsummary.ImportConflict;
 import org.springframework.stereotype.Component;
 
 /**
@@ -42,15 +43,19 @@
     protected static final Log LOG = LogFactory.getLog( ConstantImporter.class );
 
     @Override
-    protected void newObject( Constant constant )
+    protected ImportConflict newObject( Constant constant )
     {
         LOG.info( "NEW OBJECT: " + constant );
+
+        return null;
     }
 
     @Override
-    protected void updatedObject( Constant constant, Constant oldConstant )
+    protected ImportConflict updatedObject( Constant constant, Constant oldConstant )
     {
         LOG.info( "UPDATED OBJECT: " + constant + ", OLD OBJECT: " + oldConstant );
+
+        return null;
     }
 
     @Override