← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 17930: minor refactor in Jackson TEI service

 

------------------------------------------------------------
revno: 17930
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Sat 2015-01-10 18:02:17 +0700
message:
  minor refactor in Jackson TEI service
modified:
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/trackedentity/JacksonTrackedEntityInstanceService.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/trackedentity/TrackedEntityInstanceService.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/event/EnrollmentController.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/event/EventController.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-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/trackedentity/JacksonTrackedEntityInstanceService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/trackedentity/JacksonTrackedEntityInstanceService.java	2015-01-10 10:45:23 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/trackedentity/JacksonTrackedEntityInstanceService.java	2015-01-10 11:02:17 +0000
@@ -41,6 +41,8 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.nio.charset.Charset;
+import java.util.Arrays;
+import java.util.List;
 
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
@@ -97,79 +99,18 @@
     @Override
     public ImportSummaries addTrackedEntityInstanceXml( InputStream inputStream, ImportStrategy strategy ) throws IOException
     {
-        ImportSummaries importSummaries = new ImportSummaries();
+        ImportSummaries importSummaries;
         String input = StreamUtils.copyToString( inputStream, Charset.forName( "UTF-8" ) );
 
-        TrackedEntityInstances create = new TrackedEntityInstances();
-        TrackedEntityInstances update = new TrackedEntityInstances();
-
         try
         {
             TrackedEntityInstances fromXml = fromXml( input, TrackedEntityInstances.class );
-
-            if ( strategy.isCreate() )
-            {
-                create.getTrackedEntityInstances().addAll( fromXml.getTrackedEntityInstances() );
-            }
-            else if ( strategy.isCreateAndUpdate() )
-            {
-                for ( TrackedEntityInstance trackedEntityInstance : fromXml.getTrackedEntityInstances() )
-                {
-                    if ( StringUtils.isEmpty( trackedEntityInstance.getTrackedEntityInstance() ) )
-                    {
-                        create.getTrackedEntityInstances().add( trackedEntityInstance );
-                    }
-                    else
-                    {
-                        if ( teiService.getTrackedEntityInstance( trackedEntityInstance.getTrackedEntityInstance() ) == null )
-                        {
-                            create.getTrackedEntityInstances().add( trackedEntityInstance );
-                        }
-                        else
-                        {
-                            update.getTrackedEntityInstances().add( trackedEntityInstance );
-                        }
-                    }
-                }
-            }
-
+            importSummaries = addTrackedEntityInstance( fromXml.getTrackedEntityInstances(), strategy );
         }
         catch ( Exception ex )
         {
             TrackedEntityInstance fromXml = fromXml( input, TrackedEntityInstance.class );
-
-            if ( strategy.isCreate() )
-            {
-                create.getTrackedEntityInstances().add( fromXml );
-            }
-            else if ( strategy.isCreateAndUpdate() )
-            {
-                if ( StringUtils.isEmpty( fromXml.getTrackedEntityInstance() ) )
-                {
-                    create.getTrackedEntityInstances().add( fromXml );
-                }
-                else
-                {
-                    if ( teiService.getTrackedEntityInstance( fromXml.getTrackedEntityInstance() ) == null )
-                    {
-                        create.getTrackedEntityInstances().add( fromXml );
-                    }
-                    else
-                    {
-                        update.getTrackedEntityInstances().add( fromXml );
-                    }
-                }
-            }
-        }
-
-        for ( TrackedEntityInstance trackedEntityInstance : create.getTrackedEntityInstances() )
-        {
-            importSummaries.addImportSummary( addTrackedEntityInstance( trackedEntityInstance ) );
-        }
-
-        for ( TrackedEntityInstance trackedEntityInstance : update.getTrackedEntityInstances() )
-        {
-            importSummaries.addImportSummary( updateTrackedEntityInstance( trackedEntityInstance ) );
+            importSummaries = addTrackedEntityInstance( Arrays.asList( fromXml ), strategy );
         }
 
         return importSummaries;
@@ -178,66 +119,51 @@
     @Override
     public ImportSummaries addTrackedEntityInstanceJson( InputStream inputStream, ImportStrategy strategy ) throws IOException
     {
+        ImportSummaries importSummaries;
+        String input = StreamUtils.copyToString( inputStream, Charset.forName( "UTF-8" ) );
+
+        try
+        {
+            TrackedEntityInstances fromJson = fromJson( input, TrackedEntityInstances.class );
+            importSummaries = addTrackedEntityInstance( fromJson.getTrackedEntityInstances(), strategy );
+        }
+        catch ( Exception ex )
+        {
+            TrackedEntityInstance fromJson = fromJson( input, TrackedEntityInstance.class );
+            importSummaries = addTrackedEntityInstance( Arrays.asList( fromJson ), strategy );
+        }
+
+        return importSummaries;
+    }
+
+    private ImportSummaries addTrackedEntityInstance( List<TrackedEntityInstance> trackedEntityInstances, ImportStrategy strategy )
+    {
         ImportSummaries importSummaries = new ImportSummaries();
-        String input = StreamUtils.copyToString( inputStream, Charset.forName( "UTF-8" ) );
 
         TrackedEntityInstances create = new TrackedEntityInstances();
         TrackedEntityInstances update = new TrackedEntityInstances();
 
-        try
-        {
-            TrackedEntityInstances fromJson = fromJson( input, TrackedEntityInstances.class );
-
-            if ( strategy.isCreate() )
-            {
-                create.getTrackedEntityInstances().addAll( fromJson.getTrackedEntityInstances() );
-            }
-            else if ( strategy.isCreateAndUpdate() )
-            {
-                for ( TrackedEntityInstance trackedEntityInstance : fromJson.getTrackedEntityInstances() )
-                {
-                    if ( StringUtils.isEmpty( trackedEntityInstance.getTrackedEntityInstance() ) )
+        if ( strategy.isCreate() )
+        {
+            create.getTrackedEntityInstances().addAll( trackedEntityInstances );
+        }
+        else if ( strategy.isCreateAndUpdate() )
+        {
+            for ( TrackedEntityInstance trackedEntityInstance : trackedEntityInstances )
+            {
+                if ( StringUtils.isEmpty( trackedEntityInstance.getTrackedEntityInstance() ) )
+                {
+                    create.getTrackedEntityInstances().add( trackedEntityInstance );
+                }
+                else
+                {
+                    if ( teiService.getTrackedEntityInstance( trackedEntityInstance.getTrackedEntityInstance() ) == null )
                     {
                         create.getTrackedEntityInstances().add( trackedEntityInstance );
                     }
                     else
                     {
-                        if ( teiService.getTrackedEntityInstance( trackedEntityInstance.getTrackedEntityInstance() ) == null )
-                        {
-                            create.getTrackedEntityInstances().add( trackedEntityInstance );
-                        }
-                        else
-                        {
-                            update.getTrackedEntityInstances().add( trackedEntityInstance );
-                        }
-                    }
-                }
-            }
-
-        }
-        catch ( Exception ex )
-        {
-            TrackedEntityInstance fromJson = fromJson( input, TrackedEntityInstance.class );
-
-            if ( strategy.isCreate() )
-            {
-                create.getTrackedEntityInstances().add( fromJson );
-            }
-            else if ( strategy.isCreateAndUpdate() )
-            {
-                if ( StringUtils.isEmpty( fromJson.getTrackedEntityInstance() ) )
-                {
-                    create.getTrackedEntityInstances().add( fromJson );
-                }
-                else
-                {
-                    if ( teiService.getTrackedEntityInstance( fromJson.getTrackedEntityInstance() ) == null )
-                    {
-                        create.getTrackedEntityInstances().add( fromJson );
-                    }
-                    else
-                    {
-                        update.getTrackedEntityInstances().add( fromJson );
+                        update.getTrackedEntityInstances().add( trackedEntityInstance );
                     }
                 }
             }
@@ -256,60 +182,6 @@
         return importSummaries;
     }
 
-    @Override
-    public ImportSummaries addTrackedEntityInstanceXml( InputStream inputStream ) throws IOException
-    {
-        ImportSummaries importSummaries = new ImportSummaries();
-        String input = StreamUtils.copyToString( inputStream, Charset.forName( "UTF-8" ) );
-
-        TrackedEntityInstances trackedEntityInstances = new TrackedEntityInstances();
-
-        try
-        {
-            TrackedEntityInstances fromXml = fromXml( input, TrackedEntityInstances.class );
-            trackedEntityInstances.getTrackedEntityInstances().addAll( fromXml.getTrackedEntityInstances() );
-        }
-        catch ( Exception ex )
-        {
-            TrackedEntityInstance fromXml = fromXml( input, TrackedEntityInstance.class );
-            trackedEntityInstances.getTrackedEntityInstances().add( fromXml );
-        }
-
-        for ( TrackedEntityInstance trackedEntityInstance : trackedEntityInstances.getTrackedEntityInstances() )
-        {
-            importSummaries.addImportSummary( addTrackedEntityInstance( trackedEntityInstance ) );
-        }
-
-        return importSummaries;
-    }
-
-    @Override
-    public ImportSummaries addTrackedEntityInstanceJson( InputStream inputStream ) throws IOException
-    {
-        ImportSummaries importSummaries = new ImportSummaries();
-        String input = StreamUtils.copyToString( inputStream, Charset.forName( "UTF-8" ) );
-
-        TrackedEntityInstances trackedEntityInstances = new TrackedEntityInstances();
-
-        try
-        {
-            TrackedEntityInstances fromJson = fromJson( input, TrackedEntityInstances.class );
-            trackedEntityInstances.getTrackedEntityInstances().addAll( fromJson.getTrackedEntityInstances() );
-        }
-        catch ( Exception ex )
-        {
-            TrackedEntityInstance fromJson = fromJson( input, TrackedEntityInstance.class );
-            trackedEntityInstances.getTrackedEntityInstances().add( fromJson );
-        }
-
-        for ( TrackedEntityInstance trackedEntityInstance : trackedEntityInstances.getTrackedEntityInstances() )
-        {
-            importSummaries.addImportSummary( addTrackedEntityInstance( trackedEntityInstance ) );
-        }
-
-        return importSummaries;
-    }
-
     // -------------------------------------------------------------------------
     // UPDATE
     // -------------------------------------------------------------------------

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/trackedentity/TrackedEntityInstanceService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/trackedentity/TrackedEntityInstanceService.java	2015-01-10 10:45:23 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/trackedentity/TrackedEntityInstanceService.java	2015-01-10 11:02:17 +0000
@@ -54,10 +54,6 @@
 
     ImportSummaries addTrackedEntityInstanceXml( InputStream inputStream, ImportStrategy strategy ) throws IOException;
 
-    ImportSummaries addTrackedEntityInstanceXml( InputStream inputStream ) throws IOException;
-
-    ImportSummaries addTrackedEntityInstanceJson( InputStream inputStream ) throws IOException;
-
     ImportSummaries addTrackedEntityInstanceJson( InputStream inputStream, ImportStrategy strategy ) throws IOException;
 
     ImportSummary addTrackedEntityInstance( TrackedEntityInstance trackedEntityInstance );

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/event/EnrollmentController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/event/EnrollmentController.java	2014-10-23 08:07:12 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/event/EnrollmentController.java	2015-01-10 11:02:17 +0000
@@ -40,6 +40,7 @@
 import org.hisp.dhis.dxf2.importsummary.ImportSummaries;
 import org.hisp.dhis.dxf2.importsummary.ImportSummary;
 import org.hisp.dhis.dxf2.utils.JacksonUtils;
+import org.hisp.dhis.importexport.ImportStrategy;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.organisationunit.OrganisationUnitService;
 import org.hisp.dhis.program.Program;
@@ -178,7 +179,7 @@
 
     @RequestMapping( value = "", method = RequestMethod.POST, consumes = MediaType.APPLICATION_XML_VALUE, produces = MediaType.APPLICATION_XML_VALUE )
     @PreAuthorize( "hasRole('ALL') or hasRole('F_PROGRAM_ENROLLMENT')" )
-    public void postEnrollmentXml( HttpServletRequest request, HttpServletResponse response ) throws IOException
+    public void postEnrollmentXml( @RequestParam( defaultValue = "CREATE" ) ImportStrategy strategy, HttpServletRequest request, HttpServletResponse response ) throws IOException
     {
         ImportSummaries importSummaries = enrollmentService.addEnrollmentsXml( request.getInputStream() );
         response.setContentType( MediaType.APPLICATION_XML_VALUE );
@@ -204,7 +205,7 @@
 
     @RequestMapping( value = "", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE )
     @PreAuthorize( "hasRole('ALL') or hasRole('F_PROGRAM_ENROLLMENT')" )
-    public void postEnrollmentJson( HttpServletRequest request, HttpServletResponse response ) throws IOException
+    public void postEnrollmentJson( @RequestParam( defaultValue = "CREATE" ) ImportStrategy strategy, HttpServletRequest request, HttpServletResponse response ) throws IOException
     {
         ImportSummaries importSummaries = enrollmentService.addEnrollmentsJson( request.getInputStream() );
         response.setContentType( MediaType.APPLICATION_JSON_VALUE );

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/event/EventController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/event/EventController.java	2014-11-24 11:08:48 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/event/EventController.java	2015-01-10 11:02:17 +0000
@@ -50,6 +50,7 @@
 import org.hisp.dhis.dxf2.metadata.ImportOptions;
 import org.hisp.dhis.dxf2.utils.JacksonUtils;
 import org.hisp.dhis.event.EventStatus;
+import org.hisp.dhis.importexport.ImportStrategy;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.organisationunit.OrganisationUnitService;
 import org.hisp.dhis.program.Program;
@@ -427,7 +428,7 @@
 
     @RequestMapping( method = RequestMethod.POST, consumes = "application/xml" )
     @PreAuthorize( "hasRole('ALL') or hasRole('F_TRACKED_ENTITY_DATAVALUE_ADD')" )
-    public void postXmlEvent( HttpServletResponse response, HttpServletRequest request, ImportOptions importOptions ) throws Exception
+    public void postXmlEvent( @RequestParam( defaultValue = "CREATE" ) ImportStrategy strategy, HttpServletResponse response, HttpServletRequest request, ImportOptions importOptions ) throws Exception
     {
         InputStream inputStream = StreamUtils.wrapAndCheckCompressionFormat( request.getInputStream() );
 
@@ -472,7 +473,7 @@
 
     @RequestMapping( method = RequestMethod.POST, consumes = "application/json" )
     @PreAuthorize( "hasRole('ALL') or hasRole('F_TRACKED_ENTITY_DATAVALUE_ADD')" )
-    public void postJsonEvent( HttpServletResponse response, HttpServletRequest request, ImportOptions importOptions ) throws Exception
+    public void postJsonEvent( @RequestParam( defaultValue = "CREATE" ) ImportStrategy strategy, HttpServletResponse response, HttpServletRequest request, ImportOptions importOptions ) throws Exception
     {
         InputStream inputStream = StreamUtils.wrapAndCheckCompressionFormat( request.getInputStream() );