← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 17931: add import strategy to tracker controllers, wip

 

------------------------------------------------------------
revno: 17931
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Sat 2015-01-10 18:26:52 +0700
message:
  add import strategy to tracker controllers, wip
modified:
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/enrollment/EnrollmentService.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/enrollment/JacksonEnrollmentService.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/trackedentity/JacksonTrackedEntityInstanceService.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/event/EnrollmentController.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/enrollment/EnrollmentService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/enrollment/EnrollmentService.java	2014-10-20 16:24:09 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/enrollment/EnrollmentService.java	2015-01-10 11:26:52 +0000
@@ -31,6 +31,7 @@
 import org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance;
 import org.hisp.dhis.dxf2.importsummary.ImportSummaries;
 import org.hisp.dhis.dxf2.importsummary.ImportSummary;
+import org.hisp.dhis.importexport.ImportStrategy;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.program.Program;
 import org.hisp.dhis.program.ProgramInstance;
@@ -94,22 +95,22 @@
     // CREATE
     // -------------------------------------------------------------------------
 
+    ImportSummaries addEnrollmentsJson( InputStream inputStream, ImportStrategy strategy ) throws IOException;
+
+    ImportSummaries addEnrollmentsXml( InputStream inputStream, ImportStrategy strategy ) throws IOException;
+
     ImportSummary addEnrollment( Enrollment enrollment );
 
-    ImportSummaries addEnrollmentsJson( InputStream inputStream ) throws IOException;
-
-    ImportSummaries addEnrollmentsXml( InputStream inputStream ) throws IOException;
-
     // -------------------------------------------------------------------------
     // UPDATE
     // -------------------------------------------------------------------------
 
-    ImportSummary updateEnrollment( Enrollment enrollment );
-
     ImportSummary updateEnrollmentJson( String id, InputStream inputStream ) throws IOException;
 
     ImportSummary updateEnrollmentXml( String id, InputStream inputStream ) throws IOException;
 
+    ImportSummary updateEnrollment( Enrollment enrollment );
+
     // -------------------------------------------------------------------------
     // DELETE
     // -------------------------------------------------------------------------

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/enrollment/JacksonEnrollmentService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/enrollment/JacksonEnrollmentService.java	2015-01-02 14:37:39 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/enrollment/JacksonEnrollmentService.java	2015-01-10 11:26:52 +0000
@@ -33,12 +33,15 @@
 import com.fasterxml.jackson.dataformat.xml.XmlMapper;
 import org.hisp.dhis.dxf2.importsummary.ImportSummaries;
 import org.hisp.dhis.dxf2.importsummary.ImportSummary;
+import org.hisp.dhis.importexport.ImportStrategy;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.util.StreamUtils;
 
 import java.io.IOException;
 import java.io.InputStream;
 import java.nio.charset.Charset;
+import java.util.ArrayList;
+import java.util.List;
 
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
@@ -93,50 +96,63 @@
     // -------------------------------------------------------------------------
 
     @Override
-    public ImportSummaries addEnrollmentsJson( InputStream inputStream ) throws IOException
+    public ImportSummaries addEnrollmentsJson( InputStream inputStream, ImportStrategy strategy ) throws IOException
     {
-        ImportSummaries importSummaries = new ImportSummaries();
         String input = StreamUtils.copyToString( inputStream, Charset.forName( "UTF-8" ) );
-        Enrollments enrollments = new Enrollments();
+        List<Enrollment> enrollments = new ArrayList<>();
 
         try
         {
             Enrollments fromJson = fromJson( input, Enrollments.class );
-            enrollments.getEnrollments().addAll( fromJson.getEnrollments() );
+            enrollments.addAll( fromJson.getEnrollments() );
         }
         catch ( Exception ex )
         {
-            Enrollment enrollment = fromJson( input, Enrollment.class );
-            enrollments.getEnrollments().add( enrollment );
-        }
-
-        for ( Enrollment enrollment : enrollments.getEnrollments() )
-        {
-            importSummaries.addImportSummary( addEnrollment( enrollment ) );
-        }
-
-        return importSummaries;
+            Enrollment fromJson = fromJson( input, Enrollment.class );
+            enrollments.add( fromJson );
+        }
+
+        return addEnrollments( enrollments, strategy );
     }
 
     @Override
-    public ImportSummaries addEnrollmentsXml( InputStream inputStream ) throws IOException
+    public ImportSummaries addEnrollmentsXml( InputStream inputStream, ImportStrategy strategy ) throws IOException
     {
-        ImportSummaries importSummaries = new ImportSummaries();
         String input = StreamUtils.copyToString( inputStream, Charset.forName( "UTF-8" ) );
-        Enrollments enrollments = new Enrollments();
+        List<Enrollment> enrollments = new ArrayList<>();
 
         try
         {
-            Enrollments fromJson = fromXml( input, Enrollments.class );
-            enrollments.getEnrollments().addAll( fromJson.getEnrollments() );
+            Enrollments fromXml = fromXml( input, Enrollments.class );
+            enrollments.addAll( fromXml.getEnrollments() );
         }
         catch ( Exception ex )
         {
-            Enrollment enrollment = fromXml( input, Enrollment.class );
-            enrollments.getEnrollments().add( enrollment );
-        }
-
-        for ( Enrollment enrollment : enrollments.getEnrollments() )
+            Enrollment fromXml = fromXml( input, Enrollment.class );
+            enrollments.add( fromXml );
+        }
+
+        return addEnrollments( enrollments, strategy );
+    }
+
+    private ImportSummaries addEnrollments( List<Enrollment> enrollments, ImportStrategy strategy )
+    {
+        ImportSummaries importSummaries = new ImportSummaries();
+
+        Enrollments create = new Enrollments();
+        Enrollments update = new Enrollments();
+
+        for ( Enrollment enrollment : enrollments )
+        {
+            importSummaries.addImportSummary( addEnrollment( enrollment ) );
+        }
+
+        for ( Enrollment enrollment : create.getEnrollments() )
+        {
+            importSummaries.addImportSummary( addEnrollment( enrollment ) );
+        }
+
+        for ( Enrollment enrollment : update.getEnrollments() )
         {
             importSummaries.addImportSummary( addEnrollment( enrollment ) );
         }

=== 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 11:02:17 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/trackedentity/JacksonTrackedEntityInstanceService.java	2015-01-10 11:26:52 +0000
@@ -41,7 +41,7 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.nio.charset.Charset;
-import java.util.Arrays;
+import java.util.ArrayList;
 import java.util.List;
 
 /**
@@ -99,41 +99,41 @@
     @Override
     public ImportSummaries addTrackedEntityInstanceXml( InputStream inputStream, ImportStrategy strategy ) throws IOException
     {
-        ImportSummaries importSummaries;
         String input = StreamUtils.copyToString( inputStream, Charset.forName( "UTF-8" ) );
+        List<TrackedEntityInstance> trackedEntityInstances = new ArrayList<>();
 
         try
         {
             TrackedEntityInstances fromXml = fromXml( input, TrackedEntityInstances.class );
-            importSummaries = addTrackedEntityInstance( fromXml.getTrackedEntityInstances(), strategy );
+            trackedEntityInstances.addAll( fromXml.getTrackedEntityInstances() );
         }
         catch ( Exception ex )
         {
             TrackedEntityInstance fromXml = fromXml( input, TrackedEntityInstance.class );
-            importSummaries = addTrackedEntityInstance( Arrays.asList( fromXml ), strategy );
+            trackedEntityInstances.add( fromXml );
         }
 
-        return importSummaries;
+        return addTrackedEntityInstance( trackedEntityInstances, strategy );
     }
 
     @Override
     public ImportSummaries addTrackedEntityInstanceJson( InputStream inputStream, ImportStrategy strategy ) throws IOException
     {
-        ImportSummaries importSummaries;
         String input = StreamUtils.copyToString( inputStream, Charset.forName( "UTF-8" ) );
+        List<TrackedEntityInstance> trackedEntityInstances = new ArrayList<>();
 
         try
         {
             TrackedEntityInstances fromJson = fromJson( input, TrackedEntityInstances.class );
-            importSummaries = addTrackedEntityInstance( fromJson.getTrackedEntityInstances(), strategy );
+            trackedEntityInstances.addAll( fromJson.getTrackedEntityInstances() );
         }
         catch ( Exception ex )
         {
             TrackedEntityInstance fromJson = fromJson( input, TrackedEntityInstance.class );
-            importSummaries = addTrackedEntityInstance( Arrays.asList( fromJson ), strategy );
+            trackedEntityInstances.add( fromJson );
         }
 
-        return importSummaries;
+        return addTrackedEntityInstance( trackedEntityInstances, strategy );
     }
 
     private ImportSummaries addTrackedEntityInstance( List<TrackedEntityInstance> trackedEntityInstances, ImportStrategy strategy )

=== 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	2015-01-10 11:02:17 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/event/EnrollmentController.java	2015-01-10 11:26:52 +0000
@@ -181,7 +181,7 @@
     @PreAuthorize( "hasRole('ALL') or hasRole('F_PROGRAM_ENROLLMENT')" )
     public void postEnrollmentXml( @RequestParam( defaultValue = "CREATE" ) ImportStrategy strategy, HttpServletRequest request, HttpServletResponse response ) throws IOException
     {
-        ImportSummaries importSummaries = enrollmentService.addEnrollmentsXml( request.getInputStream() );
+        ImportSummaries importSummaries = enrollmentService.addEnrollmentsXml( request.getInputStream(), strategy );
         response.setContentType( MediaType.APPLICATION_XML_VALUE );
 
         if ( importSummaries.getImportSummaries().size() > 1 )
@@ -207,7 +207,7 @@
     @PreAuthorize( "hasRole('ALL') or hasRole('F_PROGRAM_ENROLLMENT')" )
     public void postEnrollmentJson( @RequestParam( defaultValue = "CREATE" ) ImportStrategy strategy, HttpServletRequest request, HttpServletResponse response ) throws IOException
     {
-        ImportSummaries importSummaries = enrollmentService.addEnrollmentsJson( request.getInputStream() );
+        ImportSummaries importSummaries = enrollmentService.addEnrollmentsJson( request.getInputStream(), strategy );
         response.setContentType( MediaType.APPLICATION_JSON_VALUE );
 
         if ( importSummaries.getImportSummaries().size() > 1 )