← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 6512: Lots of minor changes to importer, import of constants now work with all schemes and strategies. ...

 

------------------------------------------------------------
revno: 6512
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Sat 2012-04-07 10:07:14 +0200
message:
  Lots of minor changes to importer, import of constants now work with all schemes and strategies. Conflict messages might need some work.
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/IdScheme.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-services/dhis-service-core/src/main/java/org/hisp/dhis/common/DefaultIdentifiableObjectManager.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-06 21:31:51 +0000
+++ dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/DefaultImportService.java	2012-04-07 08:07:14 +0000
@@ -85,7 +85,7 @@
 
             if ( importer != null )
             {
-                List<ImportConflict> conflicts = importer.importCollection( objects, importOptions );
+                List<ImportConflict> conflicts = importer.importObjects( objects, importOptions );
                 ImportCount count = importer.getCurrentImportCount();
 
                 importSummary.getConflicts().addAll( conflicts );

=== modified file 'dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/IdScheme.java'
--- dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/IdScheme.java	2012-04-06 14:20:16 +0000
+++ dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/IdScheme.java	2012-04-07 08:07:14 +0000
@@ -67,8 +67,8 @@
         if ( scheme != null )
         {
             uidScheme = scheme.equals( IdScheme.UID_SCHEME );
+            nameScheme = scheme.equals( IdScheme.NAME_SCHEME );
             codeScheme = scheme.equals( IdScheme.CODE_SCHEME );
-            nameScheme = scheme.equals( IdScheme.NAME_SCHEME );
         }
     }
 
@@ -77,13 +77,13 @@
         return uidScheme;
     }
 
+    public boolean isNameScheme()
+    {
+        return nameScheme;
+    }
+
     public boolean isCodeScheme()
     {
         return codeScheme;
     }
-
-    public boolean isNameScheme()
-    {
-        return nameScheme;
-    }
 }

=== 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-06 14:20:16 +0000
+++ dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/Importer.java	2012-04-07 08:07:14 +0000
@@ -38,7 +38,10 @@
 public interface Importer<T>
 {
     /**
-     * Import a single object, return null or a ImportConflict is there is a conflict
+     * Import a single object, return null or a ImportConflict is there is a conflict.
+     *
+     * This is meant to be a one-off import of a single object, if you want to import multiple
+     * objects, then use importObjects..
      *
      * @param object  Object to import
      * @param options Import options
@@ -53,7 +56,7 @@
      * @param options Import options
      * @return List of all the ImportConflicts encountered
      */
-    List<ImportConflict> importCollection( List<T> objects, ImportOptions options );
+    List<ImportConflict> importObjects( 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-06 21:31:51 +0000
+++ dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/importers/AbstractImporter.java	2012-04-07 08:07:14 +0000
@@ -141,7 +141,7 @@
     //-------------------------------------------------------------------------------------------------------
 
     @Override
-    public List<ImportConflict> importCollection( List<T> objects, ImportOptions options )
+    public List<ImportConflict> importObjects( List<T> objects, ImportOptions options )
     {
         List<ImportConflict> conflicts = new ArrayList<ImportConflict>();
 
@@ -383,7 +383,7 @@
             }
             else if ( uidObject != null && uidObject != nameObject )
             {
-                conflict = reportNameConflict( object, options );
+                conflict = reportUidConflict( object, options );
             }
             else if ( codeObject != null && codeObject != nameObject )
             {
@@ -398,11 +398,11 @@
             }
             else if ( uidObject != null && uidObject != codeObject )
             {
-                conflict = reportNameConflict( object, options );
+                conflict = reportUidConflict( object, options );
             }
             else if ( nameObject != null && nameObject != codeObject )
             {
-                conflict = reportCodeConflict( object, options );
+                conflict = reportNameConflict( object, options );
             }
         }
 
@@ -435,32 +435,32 @@
 
     private ImportConflict reportUidLookupConflict( IdentifiableObject object, ImportOptions options )
     {
-        return new ImportConflict( getDisplayName( object, options.getIdScheme() ), "UID LOOKUP CONFLICT" );
+        return new ImportConflict( getDisplayName( object, options.getIdScheme() ), "Object does not exist, lookup done using UID." );
     }
 
     private ImportConflict reportNameLookupConflict( IdentifiableObject object, ImportOptions options )
     {
-        return new ImportConflict( getDisplayName( object, options.getIdScheme() ), "NAME LOOKUP CONFLICT" );
+        return new ImportConflict( getDisplayName( object, options.getIdScheme() ), "Object does not exist, lookup done using NAME." );
     }
 
     private ImportConflict reportCodeLookupConflict( IdentifiableObject object, ImportOptions options )
     {
-        return new ImportConflict( getDisplayName( object, options.getIdScheme() ), "CODE LOOKUP CONFLICT" );
+        return new ImportConflict( getDisplayName( object, options.getIdScheme() ), "Object does not exist, lookup done using CODE." );
     }
 
     private ImportConflict reportUidConflict( IdentifiableObject object, ImportOptions options )
     {
-        return new ImportConflict( getDisplayName( object, options.getIdScheme() ), "UID CONFLICT" );
+        return new ImportConflict( getDisplayName( object, options.getIdScheme() ), "Object already exists, lookup done using UID." );
     }
 
     private ImportConflict reportNameConflict( IdentifiableObject object, ImportOptions options )
     {
-        return new ImportConflict( getDisplayName( object, options.getIdScheme() ), "NAME CONFLICT" );
+        return new ImportConflict( getDisplayName( object, options.getIdScheme() ), "Object already exists, lookup done using NAME." );
     }
 
     private ImportConflict reportCodeConflict( IdentifiableObject object, ImportOptions options )
     {
-        return new ImportConflict( getDisplayName( object, options.getIdScheme() ), "CODE CONFLICT" );
+        return new ImportConflict( getDisplayName( object, options.getIdScheme() ), "Object already exists, lookup done using CODE." );
     }
 
     private T getObject( T object, IdScheme scheme )

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/common/DefaultIdentifiableObjectManager.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/common/DefaultIdentifiableObjectManager.java	2012-03-29 16:45:18 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/common/DefaultIdentifiableObjectManager.java	2012-04-07 08:07:14 +0000
@@ -27,16 +27,15 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.transaction.annotation.Transactional;
+
+import javax.annotation.PostConstruct;
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Set;
 
-import javax.annotation.PostConstruct;
-
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.transaction.annotation.Transactional;
-
 import static org.hisp.dhis.common.IdentifiableObject.IdentifiableProperty;
 
 /**
@@ -55,12 +54,12 @@
     }
 
     private Map<Class<IdentifiableObject>, GenericIdentifiableObjectStore<IdentifiableObject>> objectStoreMap;
-    
+
     @PostConstruct
     public void init()
     {
         objectStoreMap = new HashMap<Class<IdentifiableObject>, GenericIdentifiableObjectStore<IdentifiableObject>>();
-        
+
         for ( GenericIdentifiableObjectStore<IdentifiableObject> store : objectStores )
         {
             objectStoreMap.put( store.getClazz(), store );
@@ -87,63 +86,83 @@
         objectStoreMap.get( object.getClass() ).delete( object );
     }
 
-    @SuppressWarnings("unchecked")
+    @SuppressWarnings( "unchecked" )
     public <T extends IdentifiableObject> Map<String, T> getIdMap( Class<T> clazz, IdentifiableProperty property )
     {
         Map<String, T> map = new HashMap<String, T>();
-        
+
         GenericIdentifiableObjectStore<T> store = (GenericIdentifiableObjectStore<T>) objectStoreMap.get( clazz );
-        
+
         Collection<T> objects = store.getAll();
-        
+
         for ( T object : objects )
         {
             if ( IdentifiableProperty.ID.equals( property ) )
             {
-                map.put( String.valueOf( object.getId() ), object );
+                // since we are using primitives for ID, check that the ID is larger than 0, so that it is a valid
+                // hibernate identifier.
+                if ( object.getId() > 0 )
+                {
+                    map.put( String.valueOf( object.getId() ), object );
+                }
             }
             else if ( IdentifiableProperty.UID.equals( property ) )
             {
-                map.put( object.getUid(), object );
+                if ( object.getUid() != null )
+                {
+                    map.put( object.getUid(), object );
+                }
+            }
+            else if ( IdentifiableProperty.NAME.equals( property ) )
+            {
+                if ( object.getName() != null )
+                {
+                    map.put( object.getName(), object );
+                }
             }
             else if ( IdentifiableProperty.CODE.equals( property ) )
             {
-                map.put( object.getCode(), object );
-            }
-            else if ( IdentifiableProperty.NAME.equals( property ) )
-            {
-                map.put( object.getName(), object );
+                if ( object.getCode() != null )
+                {
+                    map.put( object.getCode(), object );
+                }
             }
         }
-        
+
         return map;
     }
 
-    @SuppressWarnings("unchecked")
+    @SuppressWarnings( "unchecked" )
     public <T extends IdentifiableObject> T getObject( Class<T> clazz, IdentifiableProperty property, String id )
     {
         GenericIdentifiableObjectStore<T> store = (GenericIdentifiableObjectStore<T>) objectStoreMap.get( clazz );
-        
-        if ( IdentifiableProperty.ID.equals( property ) )
-        {
-            return store.get( Integer.valueOf( id ) );
-        }
-        else if ( IdentifiableProperty.UID.equals( property ) )
-        {
-            return store.getByUid( id );
-        }
-        else if ( IdentifiableProperty.CODE.equals( property ) )
-        {
-            return store.getByCode( id );
-        }
-        else if ( IdentifiableProperty.NAME.equals( property ) )
-        {
-            return store.getByName( id );
-        }
-        
+
+        if ( id != null )
+        {
+            if ( IdentifiableProperty.ID.equals( property ) )
+            {
+                if ( Integer.valueOf( id ) > 0 )
+                {
+                    return store.get( Integer.valueOf( id ) );
+                }
+            }
+            else if ( IdentifiableProperty.UID.equals( property ) )
+            {
+                return store.getByUid( id );
+            }
+            else if ( IdentifiableProperty.CODE.equals( property ) )
+            {
+                return store.getByCode( id );
+            }
+            else if ( IdentifiableProperty.NAME.equals( property ) )
+            {
+                return store.getByName( id );
+            }
+        }
+
         throw new IllegalArgumentException( String.valueOf( property ) );
     }
-    
+
     public IdentifiableObject getObject( String uid, String simpleClassName )
     {
         for ( GenericIdentifiableObjectStore<IdentifiableObject> objectStore : objectStores )
@@ -153,10 +172,10 @@
                 return objectStore.getByUid( uid );
             }
         }
-        
+
         return null;
     }
-    
+
     public IdentifiableObject getObject( int id, String simpleClassName )
     {
         for ( GenericIdentifiableObjectStore<IdentifiableObject> objectStore : objectStores )
@@ -166,7 +185,7 @@
                 return objectStore.get( id );
             }
         }
-        
+
         return null;
     }
 }