← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 6564: Bugfixes for importer. Added support for GZip (you can now do /api/metaData.gz for GET and POST).

 

------------------------------------------------------------
revno: 6564
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Fri 2012-04-13 17:11:00 +0300
message:
  Bugfixes for importer. Added support for GZip (you can now do /api/metaData.gz for GET and POST).
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/IdentifiableObjectManager.java
  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/importers/DefaultImporter.java
  dhis-2/dhis-dxf2/src/main/resources/META-INF/dhis/beans.xml
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/common/DefaultIdentifiableObjectManager.java
  dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/ReflectionUtils.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/MetaDataController.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-api/src/main/java/org/hisp/dhis/common/IdentifiableObjectManager.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/IdentifiableObjectManager.java	2012-04-13 10:38:21 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/IdentifiableObjectManager.java	2012-04-13 14:11:00 +0000
@@ -44,6 +44,10 @@
 
     <T extends IdentifiableObject> T get( Class<T> clazz, String uid );
 
+    <T extends IdentifiableObject> T getByCode( Class<T> clazz, String code );
+
+    <T extends IdentifiableObject> T getByName( Class<T> clazz, String name );
+
     <T extends IdentifiableObject> Collection<T> getAll( Class<T> clazz );
 
     void delete( IdentifiableObject object );

=== 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-13 10:38:21 +0000
+++ dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/DefaultImportService.java	2012-04-13 14:11:00 +0000
@@ -78,7 +78,7 @@
         // the correct order
 
         // FIXME this is not currently in the "correct" order
-        doImport( metaData.getConcepts(), importOptions, importSummary );
+/*        doImport( metaData.getConcepts(), importOptions, importSummary );
         doImport( metaData.getConstants(), importOptions, importSummary );
         doImport( metaData.getDocuments(), importOptions, importSummary );
         doImport( metaData.getAttributeTypes(), importOptions, importSummary );
@@ -99,7 +99,11 @@
         doImport( metaData.getMapLegendSets(), importOptions, importSummary );
         doImport( metaData.getMapLayers(), importOptions, importSummary );
         doImport( metaData.getMessageConversations(), importOptions, importSummary );
+*/
+
         doImport( metaData.getOrganisationUnits(), importOptions, importSummary );
+
+/*
         doImport( metaData.getOrganisationUnitGroups(), importOptions, importSummary );
         doImport( metaData.getOrganisationUnitGroupSets(), importOptions, importSummary );
         doImport( metaData.getSqlViews(), importOptions, importSummary );
@@ -113,7 +117,7 @@
         doImport( metaData.getReportTables(), importOptions, importSummary );
         doImport( metaData.getCharts(), importOptions, importSummary );
         doImport( metaData.getDataSets(), importOptions, importSummary );
-
+*/
         return importSummary;
     }
 

=== modified file 'dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/importers/DefaultImporter.java'
--- dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/importers/DefaultImporter.java	2012-04-13 10:38:21 +0000
+++ dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/importers/DefaultImporter.java	2012-04-13 14:11:00 +0000
@@ -118,13 +118,13 @@
             return null;
         }
 
-        // log.info( "Trying to save new object with UID: " + object.getUid() );
+        log.info( "Trying to save new object with UID: " + object.getUid() );
 
         findAndUpdateReferences( object );
-        //manager.save( object );
-        //updateIdMaps( object );
+        manager.save( object );
+        updateIdMaps( object );
 
-        // log.info( "Save successful." );
+        log.info( "Save successful." );
 
         return null;
     }
@@ -144,13 +144,13 @@
             return null;
         }
 
-        // log.info( "Trying to update object with UID: " + oldObject.getUid() );
+        log.info( "Starting update of object " + getDisplayName( oldObject ) + " (" + oldObject.getClass().getSimpleName() + ")" );
 
         findAndUpdateReferences( object );
-        // oldObject.mergeWith( object );
-        // manager.update( oldObject );
+        oldObject.mergeWith( object );
+        manager.update( oldObject );
 
-        // log.info( "Update successful." );
+        log.info( "Update successful." );
 
         return null;
     }
@@ -259,6 +259,19 @@
      */
     protected String getDisplayName( IdentifiableObject object )
     {
+        if ( object.getUid() != null )
+        {
+            return object.getUid();
+        }
+        else if ( object.getCode() != null )
+        {
+            return object.getCode();
+        }
+        else if ( object.getName() != null )
+        {
+            return object.getName();
+        }
+
         return object.getClass().getName();
     }
 
@@ -267,7 +280,7 @@
      *
      * @return Name of object
      */
-    protected String getObjectName()
+    protected String getClassName()
     {
         return importerClass.getSimpleName();
     }
@@ -556,11 +569,31 @@
         return matchedObject;
     }
 
+    private IdentifiableObject findObjectByReference( IdentifiableObject identifiableObject )
+    {
+        IdentifiableObject match = null;
+
+        if ( identifiableObject.getUid() != null )
+        {
+            match = manager.get( identifiableObject.getClass(), identifiableObject.getUid() );
+        }
+        else if ( identifiableObject.getCode() != null )
+        {
+            match = manager.getByCode( identifiableObject.getClass(), identifiableObject.getCode() );
+        }
+        else if ( identifiableObject.getName() != null )
+        {
+            match = manager.getByName( identifiableObject.getClass(), identifiableObject.getName() );
+        }
+
+        return match;
+    }
+
     private void findAndUpdateReferences( T object )
     {
         Field[] fields = object.getClass().getDeclaredFields();
 
-        log.info( "Finding and updating references for " + object.getClass().getSimpleName() );
+        log.info( "-> Finding and updating references." );
 
         for ( Field field : fields )
         {
@@ -570,22 +603,22 @@
 
                 if ( identifiableObject != null )
                 {
-                    log.info( "Verifying field " + field.getName() );
-
                     if ( Period.class.isAssignableFrom( identifiableObject.getClass() ) )
                     {
+                        // FIXME
                         log.info( "Skipping Period.class" );
                     }
                     else
                     {
-                        IdentifiableObject ref = manager.get( identifiableObject.getClass(), identifiableObject.getUid() );
+                        IdentifiableObject ref = findObjectByReference( identifiableObject );
 
                         if ( ref != null )
                         {
+                            ReflectionUtils.invokeSetterMethod( field.getName(), object, ref );
                         }
                         else
                         {
-                            log.info( "Reference " + identifiableObject + " not found." );
+                            log.info( "--> Ignored reference " + getDisplayName( identifiableObject ) + "." );
                         }
                     }
                 }
@@ -598,30 +631,26 @@
                 {
                     Collection<IdentifiableObject> identifiableObjects = ReflectionUtils.invokeGetterMethod( field.getName(), object );
 
-                    if ( identifiableObjects == null )
-                    {
-                        log.info( "identifiableObjects is null for field " + field.getName() );
-                    }
-                    else if ( !identifiableObjects.isEmpty() )
-                    {
-                        log.info( "Verifying field " + field.getName() );
-
+                    if ( identifiableObjects != null && !identifiableObjects.isEmpty() )
+                    {
                         for ( IdentifiableObject identifiableObject : identifiableObjects )
                         {
                             if ( Period.class.isAssignableFrom( identifiableObject.getClass() ) )
                             {
+                                // FIXME
                                 log.info( "Skipping Period.class" );
                                 continue;
                             }
 
-                            IdentifiableObject ref = manager.get( identifiableObject.getClass(), identifiableObject.getUid() );
+                            IdentifiableObject ref = findObjectByReference( identifiableObject );
 
                             if ( ref != null )
                             {
+                                ReflectionUtils.invokeSetterMethod( field.getName(), object, ref );
                             }
                             else
                             {
-                                log.info( "Reference " + identifiableObject + " not found." );
+                                log.info( "--> Ignored reference " + getDisplayName( identifiableObject ) + "." );
                             }
                         }
                     }

=== modified file 'dhis-2/dhis-dxf2/src/main/resources/META-INF/dhis/beans.xml'
--- dhis-2/dhis-dxf2/src/main/resources/META-INF/dhis/beans.xml	2012-04-13 01:16:51 +0000
+++ dhis-2/dhis-dxf2/src/main/resources/META-INF/dhis/beans.xml	2012-04-13 14:11:00 +0000
@@ -49,6 +49,10 @@
     <constructor-arg name="importerClass" type="java.lang.Class" value="org.hisp.dhis.chart.Chart"/>
   </bean>
 
+  <bean id="conceptImporter" class="org.hisp.dhis.dxf2.metadata.importers.DefaultImporter">
+    <constructor-arg name="importerClass" type="java.lang.Class" value="org.hisp.dhis.concept.Concept"/>
+  </bean>
+
   <bean id="constantImporter" class="org.hisp.dhis.dxf2.metadata.importers.DefaultImporter">
     <constructor-arg name="importerClass" type="java.lang.Class" value="org.hisp.dhis.constant.Constant"/>
   </bean>

=== 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-04-13 10:38:21 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/common/DefaultIdentifiableObjectManager.java	2012-04-13 14:11:00 +0000
@@ -99,6 +99,28 @@
         return null;
     }
 
+    @Override
+    public <T extends IdentifiableObject> T getByCode( Class<T> clazz, String code )
+    {
+        if ( objectStoreMap.get( clazz ) != null )
+        {
+            return (T) objectStoreMap.get( clazz ).getByCode( code );
+        }
+
+        return null;
+    }
+
+    @Override
+    public <T extends IdentifiableObject> T getByName( Class<T> clazz, String name )
+    {
+        if ( objectStoreMap.get( clazz ) != null )
+        {
+            return (T) objectStoreMap.get( clazz ).getByName( name );
+        }
+
+        return null;
+    }
+
     @SuppressWarnings( "unchecked" )
     public <T extends IdentifiableObject> Collection<T> getAll( Class<T> clazz )
     {

=== 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-07 12:44:57 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/ReflectionUtils.java	2012-04-13 14:11:00 +0000
@@ -205,6 +205,17 @@
         }
     }
 
+    public static Method findSetterMethod( String fieldName, Object object )
+    {
+        try
+        {
+            return object.getClass().getMethod( "set" + StringUtils.capitalize( fieldName ) );
+        } catch ( NoSuchMethodException e )
+        {
+            return null;
+        }
+    }
+
     public static <T> T invokeGetterMethod( String fieldName, Object object )
     {
         Method method = findGetterMethod( fieldName, object );
@@ -226,6 +237,27 @@
         }
     }
 
+    public static <T> T invokeSetterMethod( String fieldName, Object object, Object... objects )
+    {
+        Method method = findSetterMethod( fieldName, object );
+
+        if ( method == null )
+        {
+            return null;
+        }
+
+        try
+        {
+            return (T) method.invoke( object, objects );
+        } catch ( InvocationTargetException e )
+        {
+            return null;
+        } catch ( IllegalAccessException e )
+        {
+            return null;
+        }
+    }
+
     public static boolean isType( Field field, Class<?> clazz )
     {
         Class<?> type = field.getType();

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/MetaDataController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/MetaDataController.java	2012-04-13 01:16:51 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/MetaDataController.java	2012-04-13 14:11:00 +0000
@@ -27,21 +27,10 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import java.io.BufferedInputStream;
-import java.io.IOException;
-import java.util.zip.ZipEntry;
-import java.util.zip.ZipInputStream;
-import java.util.zip.ZipOutputStream;
-
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-import javax.xml.bind.JAXBException;
-
 import org.hisp.dhis.api.utils.ContextUtils;
 import org.hisp.dhis.common.view.ExportView;
 import org.hisp.dhis.dxf2.importsummary.ImportSummary;
 import org.hisp.dhis.dxf2.metadata.*;
-import org.hisp.dhis.dxf2.metadata.MetaData;
 import org.hisp.dhis.dxf2.utils.JacksonUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.MediaType;
@@ -51,6 +40,12 @@
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
 
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.xml.bind.JAXBException;
+import java.io.IOException;
+import java.util.zip.*;
+
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
  */
@@ -73,9 +68,9 @@
     @PreAuthorize( "hasRole('ALL') or hasRole('F_METADATA_EXPORT')" )
     public String export( ExportOptions exportOptions, Model model )
     {
-        MetaData dxf2 = exportService.getMetaData( exportOptions );
+        MetaData metaData = exportService.getMetaData( exportOptions );
 
-        model.addAttribute( "model", dxf2 );
+        model.addAttribute( "model", metaData );
         model.addAttribute( "view", "export" );
 
         return "export";
@@ -85,7 +80,7 @@
     @PreAuthorize( "hasRole('ALL') or hasRole('F_METADATA_EXPORT')" )
     public void exportZippedXML( ExportOptions exportOptions, HttpServletResponse response ) throws IOException, JAXBException
     {
-        MetaData dxf2 = exportService.getMetaData( exportOptions );
+        MetaData metaData = exportService.getMetaData( exportOptions );
 
         response.setContentType( ContextUtils.CONTENT_TYPE_ZIP );
         response.addHeader( "Content-Disposition", "attachment; filename=\"export.xml.zip\"" );
@@ -94,14 +89,14 @@
         ZipOutputStream zip = new ZipOutputStream( response.getOutputStream() );
         zip.putNextEntry( new ZipEntry( "export.xml" ) );
 
-        JacksonUtils.toXmlWithView( zip, dxf2, ExportView.class );
+        JacksonUtils.toXmlWithView( zip, metaData, ExportView.class );
     }
 
     @RequestMapping( value = MetaDataController.RESOURCE_PATH + ".zip", method = RequestMethod.GET, headers = {"Accept=application/json"} )
     @PreAuthorize( "hasRole('ALL') or hasRole('F_METADATA_EXPORT')" )
     public void exportZippedJSON( ExportOptions exportOptions, HttpServletResponse response ) throws IOException, JAXBException
     {
-        MetaData dxf2 = exportService.getMetaData( exportOptions );
+        MetaData metaData = exportService.getMetaData( exportOptions );
 
         response.setContentType( ContextUtils.CONTENT_TYPE_ZIP );
         response.addHeader( "Content-Disposition", "attachment; filename=\"export.json.zip\"" );
@@ -110,7 +105,31 @@
         ZipOutputStream zip = new ZipOutputStream( response.getOutputStream() );
         zip.putNextEntry( new ZipEntry( "export.json" ) );
 
-        JacksonUtils.toJsonWithView( zip, dxf2, ExportView.class );
+        JacksonUtils.toJsonWithView( zip, metaData, ExportView.class );
+    }
+
+    @RequestMapping( value = MetaDataController.RESOURCE_PATH + ".gz", method = RequestMethod.GET, headers = {"Accept=application/xml, text/*"} )
+    @PreAuthorize( "hasRole('ALL') or hasRole('F_METADATA_EXPORT')" )
+    public void exportGZippedXML( ExportOptions exportOptions, HttpServletResponse response ) throws IOException, JAXBException
+    {
+        MetaData metaData = exportService.getMetaData( exportOptions );
+
+        response.setContentType( ContextUtils.CONTENT_TYPE_GZIP );
+        GZIPOutputStream gzip = new GZIPOutputStream( response.getOutputStream() );
+
+        JacksonUtils.toXmlWithView( gzip, metaData, ExportView.class );
+    }
+
+    @RequestMapping( value = MetaDataController.RESOURCE_PATH + ".gz", method = RequestMethod.GET, headers = {"Accept=application/json"} )
+    @PreAuthorize( "hasRole('ALL') or hasRole('F_METADATA_EXPORT')" )
+    public void exportGZippedJSON( ExportOptions exportOptions, HttpServletResponse response ) throws IOException, JAXBException
+    {
+        MetaData metaData = exportService.getMetaData( exportOptions );
+
+        response.setContentType( ContextUtils.CONTENT_TYPE_GZIP );
+        GZIPOutputStream gzip = new GZIPOutputStream( response.getOutputStream() );
+
+        JacksonUtils.toJsonWithView( gzip, metaData, ExportView.class );
     }
 
     //-------------------------------------------------------------------------------------------------------
@@ -121,10 +140,10 @@
     @PreAuthorize( "hasRole('ALL') or hasRole('F_METADATA_IMPORT')" )
     public void importXml( ImportOptions importOptions, HttpServletResponse response, HttpServletRequest request ) throws JAXBException, IOException
     {
-        MetaData dxf2 = JacksonUtils.fromXml( request.getInputStream(), MetaData.class );
-        System.err.println( dxf2 );
+        MetaData metaData = JacksonUtils.fromXml( request.getInputStream(), MetaData.class );
+        System.err.println( metaData );
 
-        ImportSummary summary = importService.importMetaData( dxf2, importOptions );
+        ImportSummary summary = importService.importMetaData( metaData, importOptions );
 
         response.setContentType( MediaType.APPLICATION_XML.toString() );
         JacksonUtils.toXml( response.getOutputStream(), summary );
@@ -134,10 +153,10 @@
     @PreAuthorize( "hasRole('ALL') or hasRole('F_METADATA_IMPORT')" )
     public void importJson( ImportOptions importOptions, HttpServletResponse response, HttpServletRequest request ) throws IOException
     {
-        MetaData dxf2 = JacksonUtils.fromJson( request.getInputStream(), MetaData.class );
-        System.err.println( dxf2 );
+        MetaData metaData = JacksonUtils.fromJson( request.getInputStream(), MetaData.class );
+        System.err.println( metaData );
 
-        ImportSummary summary = importService.importMetaData( dxf2, importOptions );
+        ImportSummary summary = importService.importMetaData( metaData, importOptions );
 
         response.setContentType( MediaType.APPLICATION_JSON.toString() );
         JacksonUtils.toJson( response.getOutputStream(), summary );
@@ -147,13 +166,13 @@
     @PreAuthorize( "hasRole('ALL') or hasRole('F_METADATA_IMPORT')" )
     public void importZippedXml( ImportOptions importOptions, HttpServletResponse response, HttpServletRequest request ) throws JAXBException, IOException
     {
-        ZipInputStream zip = new ZipInputStream( new BufferedInputStream( request.getInputStream() ) );
+        ZipInputStream zip = new ZipInputStream( request.getInputStream() );
         ZipEntry entry = zip.getNextEntry();
 
-        MetaData dxf2 = JacksonUtils.fromXml( zip, MetaData.class );
-        System.err.println( dxf2 );
+        MetaData metaData = JacksonUtils.fromXml( zip, MetaData.class );
+        System.err.println( metaData );
 
-        ImportSummary summary = importService.importMetaData( dxf2, importOptions );
+        ImportSummary summary = importService.importMetaData( metaData, importOptions );
 
         response.setContentType( MediaType.APPLICATION_XML.toString() );
         JacksonUtils.toXml( response.getOutputStream(), summary );
@@ -166,10 +185,42 @@
         ZipInputStream zip = new ZipInputStream( request.getInputStream() );
         ZipEntry entry = zip.getNextEntry();
 
-        MetaData dxf2 = JacksonUtils.fromJson( zip, MetaData.class );
-        System.err.println( dxf2 );
-
-        ImportSummary summary = importService.importMetaData( dxf2, importOptions );
+        MetaData metaData = JacksonUtils.fromJson( zip, MetaData.class );
+        System.err.println( metaData );
+
+        ImportSummary summary = importService.importMetaData( metaData, importOptions );
+
+        response.setContentType( MediaType.APPLICATION_JSON.toString() );
+        JacksonUtils.toJson( response.getOutputStream(), summary );
+    }
+
+
+    @RequestMapping( value = MetaDataController.RESOURCE_PATH + ".gz", method = RequestMethod.POST, headers = {"Content-Type=application/xml, text/xml"} )
+    @PreAuthorize( "hasRole('ALL') or hasRole('F_METADATA_IMPORT')" )
+    public void importGZippedXml( ImportOptions importOptions, HttpServletResponse response, HttpServletRequest request ) throws JAXBException, IOException
+    {
+        GZIPInputStream gzip = new GZIPInputStream( request.getInputStream() );
+        MetaData metaData = JacksonUtils.fromXml( gzip, MetaData.class );
+
+        System.err.println( metaData );
+
+        ImportSummary summary = importService.importMetaData( metaData, importOptions );
+
+        response.setContentType( MediaType.APPLICATION_XML.toString() );
+        JacksonUtils.toXml( response.getOutputStream(), summary );
+    }
+
+    @RequestMapping( value = MetaDataController.RESOURCE_PATH + ".gz", method = RequestMethod.POST, headers = {"Content-Type=application/json"} )
+    @PreAuthorize( "hasRole('ALL') or hasRole('F_METADATA_IMPORT')" )
+    public void importGZippedJson( ImportOptions importOptions, HttpServletResponse response, HttpServletRequest request ) throws IOException
+    {
+        GZIPInputStream gzip = new GZIPInputStream( request.getInputStream() );
+
+        MetaData metaData = JacksonUtils.fromJson( gzip, MetaData.class );
+
+        System.err.println( metaData );
+
+        ImportSummary summary = importService.importMetaData( metaData, importOptions );
 
         response.setContentType( MediaType.APPLICATION_JSON.toString() );
         JacksonUtils.toJson( response.getOutputStream(), summary );