← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 6483: refactored importer a bit

 

------------------------------------------------------------
revno: 6483
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2012-04-04 19:50:32 +0200
message:
  refactored importer a bit
modified:
  dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/DefaultImportService.java
  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/DefaultImportService.java'
--- dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/DefaultImportService.java	2012-04-03 12:59:54 +0000
+++ dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/DefaultImportService.java	2012-04-04 17:50:32 +0000
@@ -35,7 +35,6 @@
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
-import java.util.Collection;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
@@ -113,11 +112,11 @@
     @Autowired
     private Set<Importer> importerClasses = new HashSet<Importer>();
 
-    private Importer findImporterClass( Collection<? extends IdentifiableObject> clazzes )
+    private Importer findImporterClass( List<? extends IdentifiableObject> clazzes )
     {
-        if ( clazzes.size() > 0 )
+        if ( !clazzes.isEmpty() )
         {
-            IdentifiableObject identifiableObject = clazzes.iterator().next();
+            IdentifiableObject identifiableObject = clazzes.get( 0 );
 
             return findImporterClass( identifiableObject.getClass() );
         }
@@ -138,7 +137,7 @@
         return null;
     }
 
-    private void doImport( Collection<? extends IdentifiableObject> objects, ImportOptions importOptions, ImportSummary importSummary )
+    private void doImport( List<? extends IdentifiableObject> objects, ImportOptions importOptions, ImportSummary importSummary )
     {
         Importer importer = findImporterClass( objects );
 

=== 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-04 15:27:10 +0000
+++ dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/Importer.java	2012-04-04 17:50:32 +0000
@@ -54,7 +54,7 @@
      * @param options Import options
      * @return List of all the ImportConflicts encountered
      */
-    List<ImportConflict> importCollection( Collection<T> objects, ImportOptions options );
+    List<ImportConflict> importCollection( List<T> objects, ImportOptions options );
 
     /**
      * Get an ImportCount instance for the current import numbers.

=== 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-04 15:27:10 +0000
+++ dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/importers/AbstractImporter.java	2012-04-04 17:50:32 +0000
@@ -27,8 +27,9 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import org.hisp.dhis.common.IdentifiableObject;
-import org.hisp.dhis.common.IdentifiableObjectManager;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.hisp.dhis.common.*;
 import org.hisp.dhis.dxf2.importsummary.ImportConflict;
 import org.hisp.dhis.dxf2.importsummary.ImportCount;
 import org.hisp.dhis.dxf2.metadata.IdScheme;
@@ -37,22 +38,23 @@
 import org.springframework.beans.factory.annotation.Autowired;
 
 import java.util.ArrayList;
-import java.util.Collection;
 import java.util.List;
 import java.util.Map;
 
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
  */
-public abstract class AbstractImporter<T extends IdentifiableObject>
+public abstract class AbstractImporter<T extends BaseIdentifiableObject>
     implements Importer<T>
 {
+    private static final Log LOG = LogFactory.getLog( AbstractImporter.class );
+
     //-------------------------------------------------------------------------------------------------------
     // Dependencies
     //-------------------------------------------------------------------------------------------------------
 
     @Autowired
-    private IdentifiableObjectManager manager;
+    protected IdentifiableObjectManager manager;
 
     //-------------------------------------------------------------------------------------------------------
     // Current import counts
@@ -64,6 +66,12 @@
 
     protected int ignores;
 
+    protected Map<String, T> uidMap;
+
+    protected Map<String, T> nameMap;
+
+    protected Map<String, T> codeMap;
+
     //-------------------------------------------------------------------------------------------------------
     // Abstract methods that sub-classes needs to implement
     //-------------------------------------------------------------------------------------------------------
@@ -71,17 +79,21 @@
     /**
      * Called every time a new object is to be imported.
      *
-     * @param object Object to import
+     * @param object  Object to import
+     * @param options Current import options
+     * @return An ImportConflict instance if there was a conflict, otherwise null
      */
-    protected abstract ImportConflict newObject( T object );
+    protected abstract ImportConflict newObject( T object, ImportOptions options );
 
     /**
      * Update object from old => new.
      *
      * @param object    Object to import
      * @param oldObject The current version of the object
+     * @param options   Current import options
+     * @return An ImportConflict instance if there was a conflict, otherwise null
      */
-    protected abstract ImportConflict updatedObject( T object, T oldObject );
+    protected abstract ImportConflict updatedObject( T object, T oldObject, ImportOptions options );
 
     /**
      * Current object name, used to fill name part of a ImportConflict
@@ -94,13 +106,26 @@
     // Importer<T> Implementation
     //-------------------------------------------------------------------------------------------------------
 
-    @Override
-    public List<ImportConflict> importCollection( Collection<T> objects, ImportOptions options )
+    private void reset( Object type )
     {
         imports = 0;
         updates = 0;
         ignores = 0;
 
+        uidMap = manager.getIdMap( (Class) type.getClass(), IdentifiableObject.IdentifiableProperty.UID );
+        nameMap = manager.getIdMap( (Class) type.getClass(), IdentifiableObject.IdentifiableProperty.NAME );
+        codeMap = manager.getIdMap( (Class) type.getClass(), IdentifiableObject.IdentifiableProperty.CODE );
+    }
+
+    @Override
+    public List<ImportConflict> importCollection( List<T> objects, ImportOptions options )
+    {
+        if ( !objects.isEmpty() )
+        {
+            Object object = objects.get( 0 );
+            reset( object );
+        }
+
         List<ImportConflict> conflicts = new ArrayList<ImportConflict>();
 
         for ( T object : objects )
@@ -119,72 +144,14 @@
     @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 );
-
-        if ( options.getImportStrategy().isNewStrategy() )
-        {
-            if ( oldObject != null )
-            {
-                ignores++;
-                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++;
-        }
-        else if ( options.getImportStrategy().isUpdatesStrategy() )
-        {
-            if ( oldObject == null )
-            {
-                ignores++;
-                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++;
-        }
-        else if ( options.getImportStrategy().isNewAndUpdatesStrategy() )
-        {
-            if ( oldObject != null )
-            {
-                ImportConflict conflict = updatedObject( object, oldObject );
-
-                if ( conflict != null )
-                {
-                    return conflict;
-                }
-
-                updates++;
-            }
-            else
-            {
-                ImportConflict conflict = newObject( object );
-
-                if ( conflict != null )
-                {
-                    return conflict;
-                }
-
-                imports++;
-            }
-        }
-
-        return null;
+        ImportConflict conflict = validateIdentifiableObject( object, options );
+
+        if ( conflict == null )
+        {
+            conflict = startImport( object, options );
+        }
+
+        return conflict;
     }
 
     @Override
@@ -203,42 +170,148 @@
     // Helpers
     //-------------------------------------------------------------------------------------------------------
 
-    protected Map<String, T> getIdMap( Class<T> clazz, IdScheme scheme )
-    {
-        if ( scheme.isUidScheme() )
-        {
-            return manager.getIdMap( clazz, IdentifiableObject.IdentifiableProperty.UID );
-        }
-        else if ( scheme.isNameScheme() )
-        {
-            return manager.getIdMap( clazz, IdentifiableObject.IdentifiableProperty.NAME );
-        }
-        else if ( scheme.isCodeScheme() )
-        {
-            return manager.getIdMap( clazz, IdentifiableObject.IdentifiableProperty.CODE );
-        }
-
-        return null;
-    }
-
-    protected String getIdentifier( T object, IdScheme scheme )
-    {
-        if ( scheme.isUidScheme() )
-        {
-            return object.getUid();
-        }
-        else if ( scheme.isNameScheme() )
-        {
-            return object.getName();
-        }
-        else if ( scheme.isCodeScheme() )
-        {
-            return object.getCode();
-        }
-
-        return null;
-    }
-
+    private ImportConflict startImport( T object, ImportOptions options )
+    {
+        T oldObject = getObject( object, options.getIdScheme() );
+        ImportConflict conflict = null;
+
+        if ( options.getImportStrategy().isNewStrategy() )
+        {
+            prepareIdentifiableObject( object );
+            conflict = newObject( object, options );
+
+            if ( conflict != null )
+            {
+                return conflict;
+            }
+
+            imports++;
+        }
+        else if ( options.getImportStrategy().isUpdatesStrategy() )
+        {
+            conflict = updatedObject( object, oldObject, options );
+
+            if ( conflict != null )
+            {
+                return conflict;
+            }
+
+            updates++;
+        }
+        else if ( options.getImportStrategy().isNewAndUpdatesStrategy() )
+        {
+            if ( oldObject != null )
+            {
+                conflict = updatedObject( object, oldObject, options );
+
+                if ( conflict != null )
+                {
+                    return conflict;
+                }
+
+                updates++;
+            }
+            else
+            {
+                prepareIdentifiableObject( object );
+                conflict = newObject( object, options );
+
+                if ( conflict != null )
+                {
+                    return conflict;
+                }
+
+                imports++;
+            }
+        }
+
+        return null;
+    }
+
+    private ImportConflict validateIdentifiableObject( T object, ImportOptions options )
+    {
+        T uidObject = uidMap.get( object );
+        T nameObject = nameMap.get( object );
+        T codeObject = codeMap.get( object );
+
+        ImportConflict conflict = null;
+
+        if ( options.getImportStrategy().isNewStrategy() )
+        {
+            if ( uidObject != null )
+            {
+                conflict = new ImportConflict( getDisplayName( object, options.getIdScheme() ), "Object fails uniqueness constraint on identifier uid." );
+            }
+
+            if ( nameObject != null )
+            {
+                conflict = new ImportConflict( getDisplayName( object, options.getIdScheme() ), "Object fails uniqueness constraint on identifier name." );
+            }
+
+            if ( codeObject != null )
+            {
+                conflict = new ImportConflict( getDisplayName( object, options.getIdScheme() ), "Object fails uniqueness constraint on identifier code." );
+            }
+        }
+        else if ( options.getImportStrategy().isUpdatesStrategy() )
+        {
+            if ( options.getIdScheme().isUidScheme() && uidObject == null )
+            {
+                conflict = new ImportConflict( getDisplayName( object, options.getIdScheme() ), "Object does not exist, did lookup with identifier uid." );
+            }
+
+            if ( options.getIdScheme().isNameScheme() && nameObject == null )
+            {
+                conflict = new ImportConflict( getDisplayName( object, options.getIdScheme() ), "Object does not exist, did lookup with identifier name." );
+            }
+
+            if ( options.getIdScheme().isCodeScheme() && codeObject == null )
+            {
+                conflict = new ImportConflict( getDisplayName( object, options.getIdScheme() ), "Object does not exist, did lookup with identifier code." );
+            }
+        }
+        else if ( options.getImportStrategy().isNewAndUpdatesStrategy() )
+        {
+        }
+
+        return conflict;
+    }
+
+    private T getObject( T object, IdScheme scheme )
+    {
+        if ( scheme.isUidScheme() )
+        {
+            return uidMap.get( object.getUid() );
+        }
+        else if ( scheme.isNameScheme() )
+        {
+            return nameMap.get( object.getName() );
+        }
+        else if ( scheme.isCodeScheme() )
+        {
+            return codeMap.get( object.getCode() );
+        }
+
+        return null;
+    }
+
+    protected void prepareIdentifiableObject( BaseIdentifiableObject object )
+    {
+        if ( object.getUid() == null )
+        {
+            object.setUid( generateUid() );
+        }
+    }
+
+    /**
+     * Try to get a usable display based on current idScheme, mainly used for error-reporting
+     * but can also be use elsewhere. Falls back to the name of the class, if no other alternative
+     * is available.
+     *
+     * @param object Object to get display name for
+     * @param scheme Current idScheme
+     * @return A usable display name
+     */
     protected String getDisplayName( IdentifiableObject object, IdScheme scheme )
     {
         if ( scheme.isUidScheme() )
@@ -265,4 +338,25 @@
 
         return object.getClass().getName();
     }
+
+    protected String generateUid()
+    {
+        return CodeGenerator.generateCode();
+    }
+
+    protected void mergeIdentifiableObject( BaseIdentifiableObject target, BaseIdentifiableObject source )
+    {
+        target.setId( source.getId() );
+        target.setUid( source.getUid() );
+        target.setName( source.getName() );
+        target.setCode( source.getCode() );
+    }
+
+    protected void mergeNameableObject( BaseNameableObject target, BaseNameableObject source )
+    {
+        mergeIdentifiableObject( target, source );
+
+        target.setShortName( source.getShortName() );
+        target.setDescription( source.getDescription() );
+    }
 }

=== 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-04 15:27:10 +0000
+++ dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/importers/ConstantImporter.java	2012-04-04 17:50:32 +0000
@@ -31,6 +31,7 @@
 import org.apache.commons.logging.LogFactory;
 import org.hisp.dhis.constant.Constant;
 import org.hisp.dhis.dxf2.importsummary.ImportConflict;
+import org.hisp.dhis.dxf2.metadata.ImportOptions;
 import org.springframework.stereotype.Component;
 
 /**
@@ -40,21 +41,37 @@
 public class ConstantImporter
     extends AbstractImporter<Constant>
 {
-    protected static final Log LOG = LogFactory.getLog( ConstantImporter.class );
+    private static final Log LOG = LogFactory.getLog( ConstantImporter.class );
 
     @Override
-    protected ImportConflict newObject( Constant constant )
+    protected ImportConflict newObject( Constant constant, ImportOptions options )
     {
         LOG.info( "NEW OBJECT: " + constant );
 
+        if ( !options.isDryRun() )
+        {
+            LOG.info( "Trying to save new object with UID: " + constant.getUid() );
+            manager.save( constant );
+            LOG.info( "Save successful." );
+        }
+
         return null;
     }
 
     @Override
-    protected ImportConflict updatedObject( Constant constant, Constant oldConstant )
+    protected ImportConflict updatedObject( Constant constant, Constant oldConstant, ImportOptions options )
     {
         LOG.info( "UPDATED OBJECT: " + constant + ", OLD OBJECT: " + oldConstant );
 
+        mergeIdentifiableObject( constant, oldConstant );
+
+        if ( !options.isDryRun() )
+        {
+            LOG.info( "Trying to update object with UID: " + constant.getUid() );
+            manager.update( constant );
+            LOG.info( "Update successful." );
+        }
+
         return null;
     }