← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 12384: Reinstated sdmx data import (in case anyone is using it).

 

------------------------------------------------------------
revno: 12384
committer: Bob Jolliffe <bobjolliffe@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2013-10-02 13:25:58 +0100
message:
  Reinstated sdmx data import (in case anyone is using it).
added:
  dhis-2/dhis-web/dhis-web-api/src/main/resources/templates/cross2dxf2.xsl
modified:
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/DataValueSetController.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-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/DataValueSetController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/DataValueSetController.java	2013-10-01 13:26:59 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/DataValueSetController.java	2013-10-02 12:25:58 +0000
@@ -28,23 +28,29 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import java.io.*;
 import static org.hisp.dhis.api.utils.ContextUtils.CONTENT_TYPE_CSV;
 import static org.hisp.dhis.api.utils.ContextUtils.CONTENT_TYPE_HTML;
 import static org.hisp.dhis.api.utils.ContextUtils.CONTENT_TYPE_JSON;
 import static org.hisp.dhis.api.utils.ContextUtils.CONTENT_TYPE_TEXT;
 import static org.hisp.dhis.api.utils.ContextUtils.CONTENT_TYPE_XML;
 
-import java.io.IOException;
-import java.io.InputStream;
 import java.util.Date;
 import java.util.HashSet;
 import java.util.Set;
 
 import javax.servlet.http.HttpServletResponse;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerConfigurationException;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.stream.StreamResult;
+import javax.xml.transform.stream.StreamSource;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.hisp.dhis.api.utils.ContextUtils;
+import org.hisp.dhis.api.view.ClassPathUriResolver;
 import org.hisp.dhis.api.webdomain.DataValueSets;
 import org.hisp.dhis.common.IdentifiableObjectUtils;
 import org.hisp.dhis.dxf2.datavalueset.DataValueSet;
@@ -54,11 +60,11 @@
 import org.hisp.dhis.dxf2.utils.JacksonUtils;
 import org.hisp.dhis.organisationunit.OrganisationUnitService;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.core.io.ClassPathResource;
 import org.springframework.format.annotation.DateTimeFormat;
 import org.springframework.security.access.prepost.PreAuthorize;
 import org.springframework.stereotype.Controller;
 import org.springframework.ui.Model;
-import org.springframework.web.HttpRequestMethodNotSupportedException;
 import org.springframework.web.bind.annotation.ExceptionHandler;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
@@ -69,7 +75,8 @@
 public class DataValueSetController
 {
     public static final String RESOURCE_PATH = "/dataValueSets";
-
+    public static final String SDMXCROSS2DXF2_TRANSFORM = "/templates/cross2dxf2.xsl";
+    
     private static final Log log = LogFactory.getLog( DataValueSetController.class );
 
     @Autowired
@@ -192,18 +199,18 @@
     @RequestMapping(method = RequestMethod.POST, consumes = "application/sdmx+xml")
     @PreAuthorize("hasRole('ALL') or hasRole('F_DATAVALUE_ADD')")
     public void postSDMXDataValueSet( ImportOptions importOptions,
-        HttpServletResponse response, InputStream in, Model model ) throws IOException, HttpRequestMethodNotSupportedException
+        HttpServletResponse response, InputStream in, Model model ) throws 
+        IOException, TransformerConfigurationException, TransformerException
     {
-        throw new HttpRequestMethodNotSupportedException( RequestMethod.POST.toString() );
-        /* TODO: reimplement without using integration service
-        ImportSummary summary = integrationService.importSDMXDataValueSet( in, importOptions );
-
-        log.info( "Data values set saved " + importOptions );
-
-        response.setContentType( CONTENT_TYPE_XML );
-        JacksonUtils.toXml( response.getOutputStream(), summary );
-        * 
-        */
+        TransformerFactory tf = TransformerFactory.newInstance();
+        tf.setURIResolver( new ClassPathUriResolver() );
+        
+        Transformer transformer = tf.newTransformer( new StreamSource( new ClassPathResource( SDMXCROSS2DXF2_TRANSFORM ).getInputStream() ) );
+
+        StringWriter dxf2 = new StringWriter();
+        transformer.transform( new StreamSource( in ), new StreamResult( dxf2 ) );
+        ImportSummary summary = dataValueSetService.saveDataValueSetJson( 
+            new ByteArrayInputStream(dxf2.toString().getBytes( "UTF-8") ), importOptions );
     }
 
     // -------------------------------------------------------------------------

=== added file 'dhis-2/dhis-web/dhis-web-api/src/main/resources/templates/cross2dxf2.xsl'
--- dhis-2/dhis-web/dhis-web-api/src/main/resources/templates/cross2dxf2.xsl	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/resources/templates/cross2dxf2.xsl	2013-10-02 12:25:58 +0000
@@ -0,0 +1,30 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
+                xmlns:d="http://dhis2.org/schema/dxf/2.0";
+                version="1.0">
+  
+    <xsl:output method="xml" indent="yes" />
+
+    <xsl:template match="/">
+        <xsl:for-each select="//*[local-name()='DataSet']">
+            <d:dataValueSet period='{@TIME_PERIOD}' 
+              orgUnitIdScheme='code' 
+              dataElementIdScheme='code' 
+              dataSet='{@datasetID}' 
+              orgUnit='{@FACILITY}'>
+                <xsl:apply-templates />
+            </d:dataValueSet>
+        </xsl:for-each>
+    </xsl:template>
+
+    <xsl:template match='*[local-name()="OBS_VALUE"]'>
+      <xsl:element name="d:dataValue" >
+      <xsl:attribute name="dataElement"><xsl:value-of select="@DATAELEMENT"/></xsl:attribute>  
+      <xsl:if test="@DISAGG">
+        <xsl:attribute name="categoryOptionCombo"><xsl:value-of select="@DISAGG"/></xsl:attribute>
+      </xsl:if>
+      <xsl:attribute name="value"><xsl:value-of select="@value"/></xsl:attribute>  
+    </xsl:element>
+
+    </xsl:template> 
+</xsl:stylesheet>