← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 6707: give better conflict information on import

 

------------------------------------------------------------
revno: 6707
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2012-04-23 17:12:48 +0300
message:
  give better conflict information on import
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/DefaultObjectBridge.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/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-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-23 08:02:51 +0000
+++ dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/DefaultImportService.java	2012-04-23 14:12:48 +0000
@@ -82,6 +82,7 @@
         objectBridge.init();
 
         Date startDate = new Date();
+        log.info( "Started import at " + startDate );
 
         doImport( metaData.getOrganisationUnits(), importOptions, importSummary );
         doImport( metaData.getOrganisationUnitLevels(), importOptions, importSummary );
@@ -127,17 +128,15 @@
         doImport( metaData.getReports(), importOptions, importSummary );
         doImport( metaData.getReportTables(), importOptions, importSummary );
         doImport( metaData.getCharts(), importOptions, importSummary );
-
+*/
         doImport( metaData.getDataSets(), importOptions, importSummary );
-*/
+
         cacheManager.clearCache();
         objectBridge.destroy();
 
         Date endDate = new Date();
-        log.info( "Started import at " + startDate );
         log.info( "Finished import at " + endDate );
 
-
         return importSummary;
     }
 
@@ -177,10 +176,10 @@
 
             if ( importer != null )
             {
-                List<ImportConflict> conflicts = importer.importObjects( objects, importOptions );
+                List<ImportConflict> importConflicts = importer.importObjects( objects, importOptions );
                 ImportCount count = importer.getCurrentImportCount();
 
-                importSummary.getConflicts().addAll( conflicts );
+                importSummary.getConflicts().addAll( importConflicts );
                 // importSummary.getCounts().add( count ); //FIXME
             }
             else
@@ -188,5 +187,7 @@
                 log.info( "Importer for object of type " + objects.get( 0 ).getClass().getSimpleName() + " not found." );
             }
         }
+
+        log.info( "importConflicts = " + importSummary.getConflicts() );
     }
 }

=== modified file 'dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/DefaultObjectBridge.java'
--- dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/DefaultObjectBridge.java	2012-04-23 08:02:51 +0000
+++ dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/DefaultObjectBridge.java	2012-04-23 14:12:48 +0000
@@ -32,6 +32,7 @@
 import org.hisp.dhis.common.IdentifiableObject;
 import org.hisp.dhis.common.IdentifiableObjectManager;
 import org.hisp.dhis.common.NameableObject;
+import org.hisp.dhis.dataset.DataSet;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.organisationunit.OrganisationUnitGroup;
 import org.hisp.dhis.organisationunit.OrganisationUnitGroupSet;
@@ -95,6 +96,7 @@
         registeredTypes.add( OrganisationUnit.class );
         registeredTypes.add( OrganisationUnitGroup.class );
         registeredTypes.add( OrganisationUnitGroupSet.class );
+        registeredTypes.add( DataSet.class );
     }
 
     @Override
@@ -339,7 +341,7 @@
 
             if ( identifiableObject.getUid() != null )
             {
-                IdentifiableObject match = uidMap.get( identifiableObject.getClass() ).get( identifiableObject.getUid() );
+                IdentifiableObject match = getUidMatch( identifiableObject );
 
                 if ( match != null )
                 {
@@ -349,7 +351,7 @@
 
             if ( identifiableObject.getCode() != null )
             {
-                IdentifiableObject match = codeMap.get( identifiableObject.getClass() ).get( identifiableObject.getCode() );
+                IdentifiableObject match = getCodeMatch( identifiableObject );
 
                 if ( match != null )
                 {
@@ -359,7 +361,7 @@
 
             if ( identifiableObject.getName() != null )
             {
-                IdentifiableObject match = nameMap.get( identifiableObject.getClass() ).get( identifiableObject.getName() );
+                IdentifiableObject match = getNameMatch( identifiableObject );
 
                 if ( match != null )
                 {
@@ -374,7 +376,7 @@
 
             if ( nameableObject.getShortName() != null )
             {
-                IdentifiableObject match = shortNameMap.get( nameableObject.getClass() ).get( nameableObject.getShortName() );
+                IdentifiableObject match = getShortNameMatch( nameableObject );
 
                 if ( match != null )
                 {
@@ -450,4 +452,52 @@
             }
         }
     }
+
+    private IdentifiableObject getUidMatch( IdentifiableObject identifiableObject )
+    {
+        Map<String, IdentifiableObject> map = uidMap.get( identifiableObject.getClass() );
+
+        if ( map != null )
+        {
+            return map.get( identifiableObject.getUid() );
+        }
+
+        return null;
+    }
+
+    private IdentifiableObject getCodeMatch( IdentifiableObject identifiableObject )
+    {
+        Map<String, IdentifiableObject> map = codeMap.get( identifiableObject.getClass() );
+
+        if ( map != null )
+        {
+            return map.get( identifiableObject.getCode() );
+        }
+
+        return null;
+    }
+
+    private IdentifiableObject getNameMatch( IdentifiableObject identifiableObject )
+    {
+        Map<String, IdentifiableObject> map = nameMap.get( identifiableObject.getClass() );
+
+        if ( map != null )
+        {
+            return map.get( identifiableObject.getName() );
+        }
+
+        return null;
+    }
+
+    private NameableObject getShortNameMatch( NameableObject nameableObject )
+    {
+        Map<String, NameableObject> map = shortNameMap.get( nameableObject.getClass() );
+
+        if ( map != null )
+        {
+            return map.get( nameableObject.getShortName() );
+        }
+
+        return null;
+    }
 }

=== 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-07 08:07:14 +0000
+++ dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/Importer.java	2012-04-23 14:12:48 +0000
@@ -39,7 +39,7 @@
 {
     /**
      * Import a single object, return null or a ImportConflict is there is a conflict.
-     *
+     * <p/>
      * This is meant to be a one-off import of a single object, if you want to import multiple
      * objects, then use importObjects..
      *
@@ -47,7 +47,7 @@
      * @param options Import options
      * @return ImportConflict instance if a conflict occurred, if not null
      */
-    ImportConflict importObject( T object, ImportOptions options );
+    List<ImportConflict> importObject( T object, ImportOptions options );
 
     /**
      * Import a collection of objects.

=== 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-04-23 13:01:34 +0000
+++ dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/importers/DefaultIdentifiableObjectImporter.java	2012-04-23 14:12:48 +0000
@@ -103,8 +103,10 @@
      * @param object Object to import
      * @return An ImportConflict instance if there was a conflict, otherwise null
      */
-    protected ImportConflict newObject( T object )
+    protected List<ImportConflict> newObject( T object )
     {
+        List<ImportConflict> importConflicts = new ArrayList<ImportConflict>();
+
         // make sure that the internalId is 0, so that the system will generate a ID
         object.setId( 0 );
 
@@ -116,18 +118,18 @@
         Map<Field, Set<? extends IdentifiableObject>> identifiableObjectCollections =
             scanIdentifiableObjectCollections( object );
 
-        updateIdentifiableObjects( object, scanIdentifiableObjects( object ) );
+        importConflicts.addAll( updateIdentifiableObjects( object, scanIdentifiableObjects( object ) ) );
 
         objectBridge.saveObject( object );
 
-        updateIdentifiableObjectCollections( object, identifiableObjectCollections );
+        importConflicts.addAll( updateIdentifiableObjectCollections( object, identifiableObjectCollections ) );
 
         updatePeriodTypes( object );
         objectBridge.updateObject( object );
 
         log.debug( "Save successful." );
 
-        return null;
+        return importConflicts;
     }
 
     /**
@@ -137,13 +139,15 @@
      * @param oldObject The current version of the object
      * @return An ImportConflict instance if there was a conflict, otherwise null
      */
-    protected ImportConflict updatedObject( T object, T oldObject )
+    protected List<ImportConflict> updatedObject( T object, T oldObject )
     {
+        List<ImportConflict> importConflicts = new ArrayList<ImportConflict>();
+
         log.debug( "Starting update of object " + getDisplayName( oldObject ) + " (" + oldObject.getClass()
             .getSimpleName() + ")" );
 
-        updateIdentifiableObjects( object, scanIdentifiableObjects( object ) );
-        updateIdentifiableObjectCollections( object, scanIdentifiableObjectCollections( object ) );
+        importConflicts.addAll( updateIdentifiableObjects( object, scanIdentifiableObjects( object ) ) );
+        importConflicts.addAll( updateIdentifiableObjectCollections( object, scanIdentifiableObjectCollections( object ) ) );
 
         oldObject.mergeWith( object );
         updatePeriodTypes( oldObject );
@@ -152,7 +156,7 @@
 
         log.debug( "Update successful." );
 
-        return null;
+        return importConflicts;
     }
 
     // FIXME to static ATM, should be refactor out.. "type handler", not idObject
@@ -177,11 +181,11 @@
     @SuppressWarnings( "unchecked" )
     public List<ImportConflict> importObjects( List<T> objects, ImportOptions options )
     {
-        List<ImportConflict> conflicts = new ArrayList<ImportConflict>();
+        List<ImportConflict> importConflicts = new ArrayList<ImportConflict>();
 
         if ( objects.isEmpty() )
         {
-            return conflicts;
+            return importConflicts;
         }
 
         init( options );
@@ -195,19 +199,19 @@
 
         for ( T object : objects )
         {
-            ImportConflict importConflict = importObjectLocal( object, options );
+            List<ImportConflict> conflicts = importObjectLocal( object, options );
 
-            if ( importConflict != null )
+            if ( !conflicts.isEmpty() )
             {
-                conflicts.add( importConflict );
+                importConflicts.addAll( conflicts );
             }
         }
 
-        return conflicts;
+        return importConflicts;
     }
 
     @Override
-    public ImportConflict importObject( T object, ImportOptions options )
+    public List<ImportConflict> importObject( T object, ImportOptions options )
     {
         init( options );
 
@@ -273,77 +277,74 @@
         }
     }
 
-    private ImportConflict importObjectLocal( T object, ImportOptions options )
+    private List<ImportConflict> importObjectLocal( T object, ImportOptions options )
     {
-        ImportConflict conflict = validateIdentifiableObject( object, options );
-
-        if ( conflict == null )
-        {
-            conflict = startImport( object, options );
-        }
-
-        if ( conflict != null )
+        List<ImportConflict> importConflicts = new ArrayList<ImportConflict>();
+        ImportConflict importConflict = validateIdentifiableObject( object, options );
+
+        if ( importConflict == null )
+        {
+            importConflicts.addAll( startImport( object, options ) );
+        }
+        else
+        {
+            importConflicts.add( importConflict );
+        }
+
+        if ( importConflicts.isEmpty() )
         {
             totalIgnored++;
         }
 
-        return conflict;
+        return importConflicts;
     }
 
-    private ImportConflict startImport( T object, ImportOptions options )
+    private List<ImportConflict> startImport( T object, ImportOptions options )
     {
         T oldObject = objectBridge.getObject( object );
-        ImportConflict conflict;
+        List<ImportConflict> importConflicts = new ArrayList<ImportConflict>();
 
         if ( ImportStrategy.NEW.equals( options.getImportStrategy() ) )
         {
-            conflict = newObject( object );
+            importConflicts.addAll( newObject( object ) );
 
-            if ( conflict != null )
+            if ( importConflicts.isEmpty() )
             {
-                return conflict;
+                totalImported++;
             }
-
-            totalImported++;
         }
         else if ( ImportStrategy.UPDATES.equals( options.getImportStrategy() ) )
         {
-            conflict = updatedObject( object, oldObject );
+            importConflicts.addAll( updatedObject( object, oldObject ) );
 
-            if ( conflict != null )
+            if ( importConflicts.isEmpty() )
             {
-                return conflict;
+                totalUpdated++;
             }
-
-            totalUpdated++;
         }
         else if ( ImportStrategy.NEW_AND_UPDATES.equals( options.getImportStrategy() ) )
         {
             if ( oldObject != null )
             {
-                conflict = updatedObject( object, oldObject );
+                importConflicts.addAll( updatedObject( object, oldObject ) );
 
-                if ( conflict != null )
+                if ( importConflicts.isEmpty() )
                 {
-                    return conflict;
+                    totalUpdated++;
                 }
-
-                totalUpdated++;
             }
             else
             {
-                conflict = newObject( object );
+                importConflicts.addAll( newObject( object ) );
 
-                if ( conflict != null )
+                if ( importConflicts.isEmpty() )
                 {
-                    return conflict;
+                    totalImported++;
                 }
-
-                totalImported++;
             }
         }
 
-        return null;
+        return importConflicts;
     }
 
     private ImportConflict validateIdentifiableObject( T object, ImportOptions options )
@@ -353,7 +354,7 @@
         // FIXME add bean validation for this
         if ( object.getName() == null || object.getName().length() == 0 )
         {
-            return new ImportConflict( getDisplayName( object ), "Empty name." );
+            return new ImportConflict( getDisplayName( object ), "Empty name for object " + object );
         }
 
         if ( NameableObject.class.isInstance( object ) )
@@ -362,7 +363,7 @@
 
             if ( nameableObject.getShortName() == null || nameableObject.getShortName().length() == 0 )
             {
-                return new ImportConflict( getDisplayName( object ), "Empty shortName." );
+                return new ImportConflict( getDisplayName( object ), "Empty shortName for object " + object );
             }
         }
         // end
@@ -471,9 +472,11 @@
         return identifiableObjects;
     }
 
-    private void updateIdentifiableObjects( IdentifiableObject identifiableObject, Map<Field,
+    private List<ImportConflict> updateIdentifiableObjects( IdentifiableObject identifiableObject, Map<Field,
         IdentifiableObject> identifiableObjects )
     {
+        List<ImportConflict> importConflicts = new ArrayList<ImportConflict>();
+
         for ( Field field : identifiableObjects.keySet() )
         {
             IdentifiableObject idObject = identifiableObjects.get( field );
@@ -489,9 +492,16 @@
                 if ( !OrganisationUnitGroupSet.class.isInstance( idObject ) )
                 {
                     log.warn( "Ignored reference " + idObject + " on object " + identifiableObject + "." );
+
+                    ImportConflict importConflict = new ImportConflict( getDisplayName( identifiableObject ),
+                        "Unknown reference to " + idObject + " on field " + field.getName() + ", reference has been discarded." );
+
+                    importConflicts.add( importConflict );
                 }
             }
         }
+
+        return importConflicts;
     }
 
     private Map<Field, Set<? extends IdentifiableObject>> scanIdentifiableObjectCollections( IdentifiableObject
@@ -522,9 +532,11 @@
         return collected;
     }
 
-    private void updateIdentifiableObjectCollections( IdentifiableObject identifiableObject,
+    private List<ImportConflict> updateIdentifiableObjectCollections( IdentifiableObject identifiableObject,
         Map<Field, Set<? extends IdentifiableObject>> identifiableObjectCollections )
     {
+        List<ImportConflict> importConflicts = new ArrayList<ImportConflict>();
+
         for ( Field field : identifiableObjectCollections.keySet() )
         {
             Collection<? extends IdentifiableObject> identifiableObjects = identifiableObjectCollections.get( field );
@@ -555,10 +567,17 @@
                 else
                 {
                     log.warn( "Ignored reference " + idObject + " on object " + identifiableObject + "." );
+
+                    ImportConflict importConflict = new ImportConflict( getDisplayName( identifiableObject ),
+                        "Unknown reference to " + idObject + " on field " + field.getName() + ", reference has been discarded." );
+
+                    importConflicts.add( importConflict );
                 }
             }
 
             ReflectionUtils.invokeSetterMethod( field.getName(), identifiableObject, objects );
         }
+
+        return importConflicts;
     }
 }