← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 17929: support strategy parameter in TEI POSTs, CREATE (default) and CREATE_AND_UPDATE is supported

 

------------------------------------------------------------
revno: 17929
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Sat 2015-01-10 17:45:23 +0700
message:
  support strategy parameter in TEI POSTs, CREATE (default) and CREATE_AND_UPDATE is supported
modified:
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/trackedentity/AbstractTrackedEntityInstanceService.java
  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/TrackedEntityInstanceController.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/AbstractTrackedEntityInstanceService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/trackedentity/AbstractTrackedEntityInstanceService.java	2014-11-06 11:55:56 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/trackedentity/AbstractTrackedEntityInstanceService.java	2015-01-10 10:45:23 +0000
@@ -70,31 +70,31 @@
     // -------------------------------------------------------------------------
 
     @Autowired
-    private org.hisp.dhis.trackedentity.TrackedEntityInstanceService teiService;
-
-    @Autowired
-    private TrackedEntityAttributeValueService attributeValueService;
-
-    @Autowired
-    private TrackedEntityAttributeService trackedEntityAttributeService;
-
-    @Autowired
-    private RelationshipService relationshipService;
-
-    @Autowired
-    private TrackedEntityService trackedEntityService;
-
-    @Autowired
-    private TrackedEntityAttributeValueService trackedEntityAttributeValueService;
-
-    @Autowired
-    private org.hisp.dhis.trackedentity.TrackedEntityInstanceService entityInstanceService;
-
-    @Autowired
-    private IdentifiableObjectManager manager;
-
-    @Autowired
-    private UserService userService;
+    protected org.hisp.dhis.trackedentity.TrackedEntityInstanceService teiService;
+
+    @Autowired
+    protected TrackedEntityAttributeValueService attributeValueService;
+
+    @Autowired
+    protected TrackedEntityAttributeService trackedEntityAttributeService;
+
+    @Autowired
+    protected RelationshipService relationshipService;
+
+    @Autowired
+    protected TrackedEntityService trackedEntityService;
+
+    @Autowired
+    protected TrackedEntityAttributeValueService trackedEntityAttributeValueService;
+
+    @Autowired
+    protected org.hisp.dhis.trackedentity.TrackedEntityInstanceService entityInstanceService;
+
+    @Autowired
+    protected IdentifiableObjectManager manager;
+
+    @Autowired
+    protected UserService userService;
 
     // -------------------------------------------------------------------------
     // READ

=== 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-09 14:12:28 +0000
+++ 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
@@ -33,8 +33,10 @@
 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 org.springframework.util.StringUtils;
 
 import java.io.IOException;
 import java.io.InputStream;
@@ -93,6 +95,168 @@
     // -------------------------------------------------------------------------
 
     @Override
+    public ImportSummaries addTrackedEntityInstanceXml( InputStream inputStream, ImportStrategy strategy ) throws IOException
+    {
+        ImportSummaries importSummaries = new 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 );
+                        }
+                    }
+                }
+            }
+
+        }
+        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 ) );
+        }
+
+        return importSummaries;
+    }
+
+    @Override
+    public ImportSummaries addTrackedEntityInstanceJson( InputStream inputStream, ImportStrategy strategy ) throws IOException
+    {
+        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() ) )
+                    {
+                        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 );
+                    }
+                }
+            }
+        }
+
+        for ( TrackedEntityInstance trackedEntityInstance : create.getTrackedEntityInstances() )
+        {
+            importSummaries.addImportSummary( addTrackedEntityInstance( trackedEntityInstance ) );
+        }
+
+        for ( TrackedEntityInstance trackedEntityInstance : update.getTrackedEntityInstances() )
+        {
+            importSummaries.addImportSummary( updateTrackedEntityInstance( trackedEntityInstance ) );
+        }
+
+        return importSummaries;
+    }
+
+    @Override
     public ImportSummaries addTrackedEntityInstanceXml( InputStream inputStream ) throws IOException
     {
         ImportSummaries importSummaries = new ImportSummaries();

=== 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	2014-03-18 08:10:10 +0000
+++ 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
@@ -30,6 +30,7 @@
 
 import org.hisp.dhis.dxf2.importsummary.ImportSummaries;
 import org.hisp.dhis.dxf2.importsummary.ImportSummary;
+import org.hisp.dhis.importexport.ImportStrategy;
 
 import java.io.IOException;
 import java.io.InputStream;
@@ -51,11 +52,13 @@
     // CREATE
     // -------------------------------------------------------------------------
 
-    ImportSummaries addTrackedEntityInstanceXml( InputStream inputStream )
-        throws IOException;
-
-    ImportSummaries addTrackedEntityInstanceJson( InputStream inputStream )
-        throws IOException;
+    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 );
 
@@ -63,11 +66,9 @@
     // UPDATE
     // -------------------------------------------------------------------------
 
-    ImportSummary updateTrackedEntityInstanceXml( String id, InputStream inputStream )
-        throws IOException;
+    ImportSummary updateTrackedEntityInstanceXml( String id, InputStream inputStream ) throws IOException;
 
-    ImportSummary updateTrackedEntityInstanceJson( String id, InputStream inputStream )
-        throws IOException;
+    ImportSummary updateTrackedEntityInstanceJson( String id, InputStream inputStream ) throws IOException;
 
     ImportSummary updateTrackedEntityInstance( TrackedEntityInstance trackedEntityInstance );
 

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/event/TrackedEntityInstanceController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/event/TrackedEntityInstanceController.java	2014-11-06 13:22:10 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/event/TrackedEntityInstanceController.java	2015-01-10 10:45:23 +0000
@@ -38,6 +38,7 @@
 import org.hisp.dhis.dxf2.importsummary.ImportSummary;
 import org.hisp.dhis.dxf2.utils.JacksonUtils;
 import org.hisp.dhis.event.EventStatus;
+import org.hisp.dhis.importexport.ImportStrategy;
 import org.hisp.dhis.program.ProgramStatus;
 import org.hisp.dhis.schema.descriptors.TrackedEntityInstanceSchemaDescriptor;
 import org.hisp.dhis.system.grid.GridUtils;
@@ -243,10 +244,10 @@
 
     @RequestMapping( value = "", method = RequestMethod.POST, consumes = MediaType.APPLICATION_XML_VALUE )
     @PreAuthorize( "hasRole('ALL') or hasRole('F_TRACKED_ENTITY_INSTANCE_ADD')" )
-    public void postTrackedEntityInstanceXml( HttpServletRequest request, HttpServletResponse response )
+    public void postTrackedEntityInstanceXml( @RequestParam( defaultValue = "CREATE" ) ImportStrategy strategy, HttpServletRequest request, HttpServletResponse response )
         throws IOException
     {
-        ImportSummaries importSummaries = trackedEntityInstanceService.addTrackedEntityInstanceXml( request.getInputStream() );
+        ImportSummaries importSummaries = trackedEntityInstanceService.addTrackedEntityInstanceXml( request.getInputStream(), strategy );
         response.setContentType( MediaType.APPLICATION_XML_VALUE );
 
         if ( importSummaries.getImportSummaries().size() > 1 )
@@ -281,10 +282,10 @@
 
     @RequestMapping( value = "", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE )
     @PreAuthorize( "hasRole('ALL') or hasRole('F_TRACKED_ENTITY_INSTANCE_ADD')" )
-    public void postTrackedEntityInstanceJson( HttpServletRequest request, HttpServletResponse response )
+    public void postTrackedEntityInstanceJson( @RequestParam( defaultValue = "CREATE" ) ImportStrategy strategy,HttpServletRequest request, HttpServletResponse response )
         throws IOException
     {
-        ImportSummaries importSummaries = trackedEntityInstanceService.addTrackedEntityInstanceJson( request.getInputStream() );
+        ImportSummaries importSummaries = trackedEntityInstanceService.addTrackedEntityInstanceJson( request.getInputStream(), strategy );
         response.setContentType( MediaType.APPLICATION_JSON_VALUE );
 
         if ( importSummaries.getImportSummaries().size() > 1 )