← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 14205: support xml output in schemas

 

------------------------------------------------------------
revno: 14205
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2014-03-13 14:23:00 +0100
message:
  support xml output in schemas
modified:
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/SchemaController.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/SchemaController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/SchemaController.java	2014-02-13 07:03:09 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/SchemaController.java	2014-03-13 13:23:00 +0000
@@ -28,20 +28,22 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator;
 import com.google.common.collect.Maps;
+import org.hisp.dhis.common.DxfNamespaces;
 import org.hisp.dhis.common.IdentifiableObject;
 import org.hisp.dhis.dxf2.metadata.ExchangeClasses;
 import org.hisp.dhis.dxf2.utils.JacksonUtils;
 import org.hisp.dhis.system.util.ReflectionUtils;
-import org.springframework.http.HttpStatus;
+import org.springframework.http.MediaType;
 import org.springframework.stereotype.Controller;
-import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
-import org.springframework.web.client.HttpClientErrorException;
 
+import javax.servlet.http.HttpServletResponse;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamWriter;
 import java.io.IOException;
-import java.io.OutputStream;
 import java.util.Map;
 
 /**
@@ -52,32 +54,102 @@
 public class SchemaController
 {
     @RequestMapping( value = { "/schemas", "/schemas.json" }, method = RequestMethod.GET )
-    public void getTypesJson( OutputStream outputStream ) throws IOException
-    {
-        Map<String, Map<String, ReflectionUtils.PropertyDescriptor>> output = Maps.newHashMap();
-
-        for ( Class<? extends IdentifiableObject> key : ExchangeClasses.getAllExportMap().keySet() )
-        {
-            Map<String, ReflectionUtils.PropertyDescriptor> classMap = ReflectionUtils.getJacksonClassMap( key );
-            output.put( ExchangeClasses.getAllExportMap().get( key ), classMap );
-        }
-
-        JacksonUtils.toJson( outputStream, output );
-    }
-
-    @RequestMapping( value = { "/schemas/{type}", "/schemas/{type}.json" }, method = RequestMethod.GET )
-    public void getTypeJson( @PathVariable String type, OutputStream outputStream ) throws IOException
-    {
-        for ( Class<? extends IdentifiableObject> key : ExchangeClasses.getAllExportMap().keySet() )
-        {
-            if ( ExchangeClasses.getAllExportMap().get( key ).equals( type ) )
-            {
-                Map<String, ReflectionUtils.PropertyDescriptor> classMap = ReflectionUtils.getJacksonClassMap( key );
-                JacksonUtils.toJson( outputStream, classMap );
-                return;
-            }
-        }
-
-        throw new HttpClientErrorException( HttpStatus.NOT_FOUND );
+    public void getTypesJson( HttpServletResponse response ) throws IOException
+    {
+        response.setContentType( MediaType.APPLICATION_JSON_VALUE );
+        Map<String, Map<String, ReflectionUtils.PropertyDescriptor>> output = Maps.newHashMap();
+
+        for ( Class<? extends IdentifiableObject> key : ExchangeClasses.getAllExportMap().keySet() )
+        {
+            Map<String, ReflectionUtils.PropertyDescriptor> classMap = ReflectionUtils.getJacksonClassMap( key );
+            output.put( ExchangeClasses.getAllExportMap().get( key ), classMap );
+        }
+
+        JacksonUtils.toJson( response.getOutputStream(), output );
+    }
+
+    @RequestMapping( value = { "/schemas.xml" }, method = RequestMethod.GET )
+    public void getTypesXml( HttpServletResponse response ) throws IOException
+    {
+        response.setContentType( MediaType.APPLICATION_XML_VALUE );
+        Map<String, Map<String, ReflectionUtils.PropertyDescriptor>> output = Maps.newHashMap();
+
+        for ( Class<? extends IdentifiableObject> key : ExchangeClasses.getAllExportMap().keySet() )
+        {
+            Map<String, ReflectionUtils.PropertyDescriptor> classMap = ReflectionUtils.getJacksonClassMap( key );
+            output.put( ExchangeClasses.getAllExportMap().get( key ), classMap );
+        }
+
+        try
+        {
+            ToXmlGenerator generator = (ToXmlGenerator) JacksonUtils.getXmlMapper().getJsonFactory().createJsonGenerator( response.getOutputStream() );
+            XMLStreamWriter staxWriter = generator.getStaxWriter();
+
+            staxWriter.writeStartElement( "", "schemas", DxfNamespaces.DXF_2_0 );
+
+            for ( String key : output.keySet() )
+            {
+                Map<String, ReflectionUtils.PropertyDescriptor> map = output.get( key );
+                writeClassMap( staxWriter, key, map );
+            }
+
+            staxWriter.writeEndElement();
+            staxWriter.close();
+        }
+        catch ( XMLStreamException ex )
+        {
+            ex.printStackTrace();
+        }
+    }
+
+    private void writeClassMap( XMLStreamWriter staxWriter, String type, Map<String, ReflectionUtils.PropertyDescriptor> classMap )
+    {
+        try
+        {
+            staxWriter.writeStartElement( "", "schema", DxfNamespaces.DXF_2_0 );
+            staxWriter.writeAttribute( "type", type );
+
+            for ( String field : classMap.keySet() )
+            {
+                staxWriter.writeStartElement( "", field, DxfNamespaces.DXF_2_0 );
+                ReflectionUtils.PropertyDescriptor descriptor = classMap.get( field );
+                writeDescriptor( staxWriter, descriptor );
+                staxWriter.writeEndElement();
+            }
+
+            staxWriter.writeEndElement();
+        }
+        catch ( XMLStreamException ignored )
+        {
+        }
+    }
+
+    private void writeDescriptor( XMLStreamWriter staxWriter, ReflectionUtils.PropertyDescriptor descriptor )
+    {
+        writeSimpleElement( staxWriter, "name", descriptor.getName() );
+        writeSimpleElement( staxWriter, "xmlName", descriptor.getXmlName() );
+        writeSimpleElement( staxWriter, "xmlAttribute", descriptor.isXmlAttribute() );
+        writeSimpleElement( staxWriter, "clazz", descriptor.getClazz() );
+        writeSimpleElement( staxWriter, "collection", descriptor.isCollection() );
+        writeSimpleElement( staxWriter, "identifiableObject", descriptor.isIdentifiableObject() );
+        writeSimpleElement( staxWriter, "description", descriptor.getDescription() );
+    }
+
+    private void writeSimpleElement( XMLStreamWriter staxWriter, String fieldName, Object text )
+    {
+        if ( text == null )
+        {
+            return;
+        }
+
+        try
+        {
+            staxWriter.writeStartElement( "", fieldName, DxfNamespaces.DXF_2_0 );
+            staxWriter.writeCharacters( text.toString() );
+            staxWriter.writeEndElement();
+        }
+        catch ( XMLStreamException ignored )
+        {
+        }
     }
 }