← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 10881: implemented support for bulk sending of events (xml/json)

 

------------------------------------------------------------
revno: 10881
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Sat 2013-05-18 17:19:44 +0700
message:
  implemented support for bulk sending of events (xml/json)
added:
  dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/event/Events.java
  dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/importsummary/ImportSummaries.java
modified:
  dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/event/EventService.java
  dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/event/JacksonEventService.java
  dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/utils/JacksonUtils.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/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-dxf2/src/main/java/org/hisp/dhis/dxf2/event/EventService.java'
--- dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/event/EventService.java	2013-05-17 03:29:07 +0000
+++ dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/event/EventService.java	2013-05-18 10:19:44 +0000
@@ -27,6 +27,7 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import org.hisp.dhis.dxf2.importsummary.ImportSummaries;
 import org.hisp.dhis.dxf2.importsummary.ImportSummary;
 
 import java.io.IOException;
@@ -39,5 +40,9 @@
 {
     ImportSummary saveEventXml( InputStream inputStream ) throws IOException;
 
+    ImportSummaries saveEventsXml( InputStream inputStream ) throws IOException;
+
     ImportSummary saveEventJson( InputStream inputStream ) throws IOException;
+
+    ImportSummaries saveEventsJson( InputStream inputStream ) throws IOException;
 }

=== added file 'dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/event/Events.java'
--- dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/event/Events.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/event/Events.java	2013-05-18 10:19:44 +0000
@@ -0,0 +1,86 @@
+package org.hisp.dhis.dxf2.event;
+
+/*
+ * Copyright (c) 2004-2013, University of Oslo
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * * Redistributions of source code must retain the above copyright notice, this
+ *   list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright notice,
+ *   this list of conditions and the following disclaimer in the documentation
+ *   and/or other materials provided with the distribution.
+ * * Neither the name of the HISP project nor the names of its contributors may
+ *   be used to endorse or promote products derived from this software without
+ *   specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
+import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
+import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
+import org.hisp.dhis.common.DxfNamespaces;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
+ */
+@JacksonXmlRootElement( localName = "events", namespace = DxfNamespaces.DXF_2_0 )
+public class Events
+{
+    private String id;
+
+    private List<Event> events = new ArrayList<Event>();
+
+    public Events()
+    {
+    }
+
+    @JsonProperty
+    @JacksonXmlProperty( namespace = DxfNamespaces.DXF_2_0, isAttribute = true )
+    public String getId()
+    {
+        return id;
+    }
+
+    public void setId( String id )
+    {
+        this.id = id;
+    }
+
+    @JsonProperty( "eventList" )
+    @JacksonXmlElementWrapper( localName = "eventList", namespace = DxfNamespaces.DXF_2_0 )
+    @JacksonXmlProperty( localName = "event", namespace = DxfNamespaces.DXF_2_0 )
+    public List<Event> getEvents()
+    {
+        return events;
+    }
+
+    public void setEvents( List<Event> events )
+    {
+        this.events = events;
+    }
+
+    @Override
+    public String toString()
+    {
+        return "Events{" +
+            "id='" + id + '\'' +
+            ", events=" + events +
+            '}';
+    }
+}

=== modified file 'dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/event/JacksonEventService.java'
--- dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/event/JacksonEventService.java	2013-05-17 03:29:07 +0000
+++ dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/event/JacksonEventService.java	2013-05-18 10:19:44 +0000
@@ -27,11 +27,14 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import org.hisp.dhis.dxf2.importsummary.ImportSummaries;
 import org.hisp.dhis.dxf2.importsummary.ImportSummary;
 import org.hisp.dhis.dxf2.utils.JacksonUtils;
+import org.springframework.util.StreamUtils;
 
 import java.io.IOException;
 import java.io.InputStream;
+import java.nio.charset.Charset;
 
 /**
  * Implementation of EventService that uses Jackson for serialization and deserialization.
@@ -45,20 +48,66 @@
     // -------------------------------------------------------------------------
 
     @Override
+    public ImportSummaries saveEventsXml( InputStream inputStream ) throws IOException
+    {
+        ImportSummaries importSummaries = new ImportSummaries();
+
+        String input = StreamUtils.copyToString( inputStream, Charset.defaultCharset() );
+
+        try
+        {
+            Events events = JacksonUtils.fromXml( input, Events.class );
+
+            for ( Event event : events.getEvents() )
+            {
+                importSummaries.getImportSummaries().add( saveEvent( event ) );
+            }
+        }
+        catch ( Exception ex )
+        {
+            Event event = JacksonUtils.fromXml( input, Event.class );
+            importSummaries.getImportSummaries().add( saveEvent( event ) );
+        }
+
+        return importSummaries;
+    }
+
+    @Override
     public ImportSummary saveEventXml( InputStream inputStream ) throws IOException
     {
         Event event = JacksonUtils.fromXml( inputStream, Event.class );
-        ImportSummary importSummary = saveEvent( event );
-
-        return importSummary;
+        return saveEvent( event );
+    }
+
+    @Override
+    public ImportSummaries saveEventsJson( InputStream inputStream ) throws IOException
+    {
+        ImportSummaries importSummaries = new ImportSummaries();
+
+        String input = StreamUtils.copyToString( inputStream, Charset.defaultCharset() );
+
+        try
+        {
+            Events events = JacksonUtils.fromJson( input, Events.class );
+
+            for ( Event event : events.getEvents() )
+            {
+                importSummaries.getImportSummaries().add( saveEvent( event ) );
+            }
+        }
+        catch ( Exception ex )
+        {
+            Event event = JacksonUtils.fromJson( input, Event.class );
+            importSummaries.getImportSummaries().add( saveEvent( event ) );
+        }
+
+        return importSummaries;
     }
 
     @Override
     public ImportSummary saveEventJson( InputStream inputStream ) throws IOException
     {
         Event event = JacksonUtils.fromJson( inputStream, Event.class );
-        ImportSummary importSummary = saveEvent( event );
-
-        return importSummary;
+        return saveEvent( event );
     }
 }

=== added file 'dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/importsummary/ImportSummaries.java'
--- dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/importsummary/ImportSummaries.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/importsummary/ImportSummaries.java	2013-05-18 10:19:44 +0000
@@ -0,0 +1,63 @@
+package org.hisp.dhis.dxf2.importsummary;
+
+/*
+ * Copyright (c) 2004-2013, University of Oslo
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * * Redistributions of source code must retain the above copyright notice, this
+ *   list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright notice,
+ *   this list of conditions and the following disclaimer in the documentation
+ *   and/or other materials provided with the distribution.
+ * * Neither the name of the HISP project nor the names of its contributors may
+ *   be used to endorse or promote products derived from this software without
+ *   specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
+import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
+import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
+import org.hisp.dhis.common.DxfNamespaces;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
+ */
+@JacksonXmlRootElement( localName = "importSummaries", namespace = DxfNamespaces.DXF_2_0 )
+public class ImportSummaries
+{
+    private List<ImportSummary> importSummaries = new ArrayList<ImportSummary>();
+
+    public ImportSummaries()
+    {
+    }
+
+    @JsonProperty
+    @JacksonXmlElementWrapper( localName = "importSummaryList", namespace = DxfNamespaces.DXF_2_0 )
+    @JacksonXmlProperty( localName = "importSummary", namespace = DxfNamespaces.DXF_2_0 )
+    public List<ImportSummary> getImportSummaries()
+    {
+        return importSummaries;
+    }
+
+    public void setImportSummaries( List<ImportSummary> importSummaries )
+    {
+        this.importSummaries = importSummaries;
+    }
+}

=== modified file 'dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/utils/JacksonUtils.java'
--- dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/utils/JacksonUtils.java	2013-04-23 12:02:26 +0000
+++ dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/utils/JacksonUtils.java	2013-05-18 10:19:44 +0000
@@ -27,18 +27,6 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.util.HashMap;
-import java.util.Map;
-
-import org.hisp.dhis.common.view.BasicView;
-import org.hisp.dhis.common.view.DetailedView;
-import org.hisp.dhis.common.view.DimensionalView;
-import org.hisp.dhis.common.view.ExportView;
-import org.hisp.dhis.common.view.ShortNameView;
-
 import com.fasterxml.jackson.annotation.JsonInclude;
 import com.fasterxml.jackson.core.JsonGenerator;
 import com.fasterxml.jackson.databind.DeserializationFeature;
@@ -47,6 +35,17 @@
 import com.fasterxml.jackson.databind.SerializationFeature;
 import com.fasterxml.jackson.dataformat.xml.XmlMapper;
 import com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator;
+import org.hisp.dhis.common.view.BasicView;
+import org.hisp.dhis.common.view.DetailedView;
+import org.hisp.dhis.common.view.DimensionalView;
+import org.hisp.dhis.common.view.ExportView;
+import org.hisp.dhis.common.view.ShortNameView;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.HashMap;
+import java.util.Map;
 
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
@@ -73,7 +72,7 @@
             objectMapper.configure( SerializationFeature.FAIL_ON_EMPTY_BEANS, false );
             objectMapper.configure( SerializationFeature.WRAP_EXCEPTIONS, true );
 
-            objectMapper.configure( DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false );
+            objectMapper.configure( DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true );
             objectMapper.configure( DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES, true );
             objectMapper.configure( DeserializationFeature.WRAP_EXCEPTIONS, true );
 
@@ -88,7 +87,7 @@
         xmlMapper.configure( ToXmlGenerator.Feature.WRITE_XML_DECLARATION, true );
 
         // Register view classes
-        
+
         viewClasses.put( "default", BasicView.class );
         viewClasses.put( "basic", BasicView.class );
         viewClasses.put( "shortName", ShortNameView.class );
@@ -151,6 +150,12 @@
         return (T) jsonMapper.readValue( input, clazz );
     }
 
+    @SuppressWarnings("unchecked")
+    public static <T> T fromJson( String input, Class<?> clazz ) throws IOException
+    {
+        return (T) jsonMapper.readValue( input, clazz );
+    }
+
     //--------------------------------------------------------------------------
     // XML
     //--------------------------------------------------------------------------
@@ -180,4 +185,10 @@
     {
         return (T) xmlMapper.readValue( input, clazz );
     }
+
+    @SuppressWarnings("unchecked")
+    public static <T> T fromXml( String input, Class<?> clazz ) throws IOException
+    {
+        return (T) xmlMapper.readValue( input, clazz );
+    }
 }

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/event/EventController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/event/EventController.java	2013-05-17 03:29:07 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/event/EventController.java	2013-05-18 10:19:44 +0000
@@ -29,7 +29,7 @@
 
 import org.hisp.dhis.api.utils.ContextUtils;
 import org.hisp.dhis.dxf2.event.EventService;
-import org.hisp.dhis.dxf2.importsummary.ImportSummary;
+import org.hisp.dhis.dxf2.importsummary.ImportSummaries;
 import org.hisp.dhis.dxf2.metadata.ImportOptions;
 import org.hisp.dhis.dxf2.utils.JacksonUtils;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -48,7 +48,7 @@
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
  */
 @Controller
-@RequestMapping( value = EventController.RESOURCE_PATH )
+@RequestMapping(value = EventController.RESOURCE_PATH)
 public class EventController
 {
     public static final String RESOURCE_PATH = "/events";
@@ -60,13 +60,21 @@
     // Controller
     // -------------------------------------------------------------------------
 
-    @RequestMapping( method = RequestMethod.POST, consumes = "application/xml" )
-    @PreAuthorize( "hasRole('ALL') or hasRole('F_PATIENT_DATAVALUE_ADD')" )
+    @RequestMapping(method = RequestMethod.POST, consumes = "application/xml")
+    @PreAuthorize("hasRole('ALL') or hasRole('F_PATIENT_DATAVALUE_ADD')")
     public void postDxf2ProgramInstance( ImportOptions importOptions,
         HttpServletResponse response, InputStream inputStream, Model model ) throws IOException
     {
-        ImportSummary importSummary = eventService.saveEventXml( inputStream );
-        JacksonUtils.toXml( response.getOutputStream(), importSummary );
+        ImportSummaries importSummaries = eventService.saveEventsXml( inputStream );
+
+        if ( importSummaries.getImportSummaries().size() == 1 )
+        {
+            JacksonUtils.toXml( response.getOutputStream(), importSummaries.getImportSummaries().get( 0 ) );
+        }
+        else
+        {
+            JacksonUtils.toXml( response.getOutputStream(), importSummaries );
+        }
     }
 
     @RequestMapping( method = RequestMethod.POST, consumes = "application/json" )
@@ -74,8 +82,17 @@
     public void postJsonProgramInstance( ImportOptions importOptions,
         HttpServletResponse response, InputStream inputStream, Model model ) throws IOException
     {
-        ImportSummary importSummary = eventService.saveEventJson( inputStream );
-        JacksonUtils.toJson( response.getOutputStream(), importSummary );
+        ImportSummaries importSummaries = eventService.saveEventsJson( inputStream );
+
+        if ( importSummaries.getImportSummaries().size() == 1 )
+        {
+            JacksonUtils.toJson( response.getOutputStream(), importSummaries.getImportSummaries().get( 0 ) );
+        }
+        else
+        {
+            JacksonUtils.toJson( response.getOutputStream(), importSummaries );
+        }
+
     }
 
     @ExceptionHandler( IllegalArgumentException.class )