dhis2-devs team mailing list archive
-
dhis2-devs team
-
Mailing list archive
-
Message #42885
[Branch ~dhis2-devs-core/dhis2/trunk] Rev 21848: support parsing of JSON metadata without main Metadata class (XML can't be supported..)
------------------------------------------------------------
revno: 21848
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2016-02-02 12:58:04 +0700
message:
support parsing of JSON metadata without main Metadata class (XML can't be supported..)
added:
dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/render/
dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/render/RenderServiceTest.java
dhis-2/dhis-services/dhis-service-core/src/test/resources/render/
dhis-2/dhis-services/dhis-service-core/src/test/resources/render/metadata.json
dhis-2/dhis-services/dhis-service-core/src/test/resources/render/metadata.xml
modified:
dhis-2/dhis-api/src/main/java/org/hisp/dhis/render/RenderService.java
dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/render/DefaultRenderService.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-api/src/main/java/org/hisp/dhis/render/RenderService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/render/RenderService.java 2016-02-02 04:06:07 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/render/RenderService.java 2016-02-02 05:58:04 +0000
@@ -33,7 +33,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
-import java.util.Collection;
+import java.util.List;
import java.util.Map;
/**
@@ -69,8 +69,8 @@
* i.e. A property called "dataElements" would be tried to parsed as a collection of data elements.
*
* @param inputStream Stream to read from
- * @param format Payload format (JSON and XML is supported)
+ * @param format Payload format (only JSON is supported)
* @return Map of all id object types that were found
*/
- Map<Class<? extends IdentifiableObject>, Collection<? extends IdentifiableObject>> fromMetadata( InputStream inputStream, RenderFormat format );
+ Map<Class<? extends IdentifiableObject>, List<? extends IdentifiableObject>> fromMetadata( InputStream inputStream, RenderFormat format ) throws IOException;
}
=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/render/DefaultRenderService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/render/DefaultRenderService.java 2016-02-02 04:06:07 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/render/DefaultRenderService.java 2016-02-02 05:58:04 +0000
@@ -33,13 +33,17 @@
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
+import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.util.JSONPObject;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
import org.hisp.dhis.common.IdentifiableObject;
+import org.hisp.dhis.schema.Schema;
import org.hisp.dhis.schema.SchemaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
@@ -48,8 +52,10 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
-import java.util.Collection;
+import java.util.ArrayList;
import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
import java.util.Map;
/**
@@ -60,6 +66,8 @@
public class DefaultRenderService
implements RenderService
{
+ private static final Log log = LogFactory.getLog( RenderService.class );
+
@Autowired
private SchemaService schemaService;
@@ -168,9 +176,53 @@
}
@Override
- public Map<Class<? extends IdentifiableObject>, Collection<? extends IdentifiableObject>> fromMetadata( InputStream inputStream, RenderFormat format )
+ @SuppressWarnings( "unchecked" )
+ public Map<Class<? extends IdentifiableObject>, List<? extends IdentifiableObject>> fromMetadata( InputStream inputStream, RenderFormat format ) throws IOException
{
- Map<Class<? extends IdentifiableObject>, Collection<? extends IdentifiableObject>> map = new HashMap<>();
+ Map<Class<? extends IdentifiableObject>, List<? extends IdentifiableObject>> map = new HashMap<>();
+
+ ObjectMapper mapper;
+
+ if ( RenderFormat.JSON == format )
+ {
+ mapper = jsonMapper;
+ }
+ else if ( RenderFormat.XML == format )
+ {
+ // mapper = xmlMapper;
+ throw new IllegalArgumentException( "XML format is not supported." );
+ }
+ else
+ {
+ return map;
+ }
+
+ JsonNode rootNode = mapper.readTree( inputStream );
+ Iterator<String> fieldNames = rootNode.fieldNames();
+
+ while ( fieldNames.hasNext() )
+ {
+ String fieldName = fieldNames.next();
+ JsonNode node = rootNode.get( fieldName );
+ Schema schema = schemaService.getSchemaByPluralName( fieldName );
+
+ if ( schema == null || !schema.isIdentifiableObject() )
+ {
+ log.info( "Skipping unknown property '" + fieldName + "'." );
+ continue;
+ }
+
+ List<IdentifiableObject> collection = new ArrayList<>();
+
+ for ( JsonNode item : node )
+ {
+ IdentifiableObject value = mapper.treeToValue( item, (Class<? extends IdentifiableObject>) schema.getKlass() );
+ if ( value != null ) collection.add( value );
+ }
+
+ map.put( (Class<? extends IdentifiableObject>) schema.getKlass(), collection );
+ }
+
return map;
}
=== added directory 'dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/render'
=== added file 'dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/render/RenderServiceTest.java'
--- dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/render/RenderServiceTest.java 1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/render/RenderServiceTest.java 2016-02-02 05:58:04 +0000
@@ -0,0 +1,87 @@
+package org.hisp.dhis.render;
+
+/*
+ * Copyright (c) 2004-2016, 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 org.hisp.dhis.DhisSpringTest;
+import org.hisp.dhis.common.IdentifiableObject;
+import org.hisp.dhis.dataelement.DataElement;
+import org.junit.Ignore;
+import org.junit.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.core.io.ClassPathResource;
+
+import java.io.IOException;
+import java.util.List;
+import java.util.Map;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
+ */
+public class RenderServiceTest
+ extends DhisSpringTest
+{
+ @Autowired
+ private RenderService renderService;
+
+ @Test
+ public void testParseJsonMetadata() throws IOException
+ {
+ Map<Class<? extends IdentifiableObject>, List<? extends IdentifiableObject>> map = renderService.fromMetadata(
+ new ClassPathResource( "render/metadata.json" ).getInputStream(), RenderFormat.JSON );
+
+ assertTrue( map.containsKey( DataElement.class ) );
+ assertEquals( 3, map.get( DataElement.class ).size() );
+ assertEquals( "DataElementA", map.get( DataElement.class ).get( 0 ).getName() );
+ assertEquals( "DataElementB", map.get( DataElement.class ).get( 1 ).getName() );
+ assertEquals( "DataElementC", map.get( DataElement.class ).get( 2 ).getName() );
+ assertEquals( "deabcdefghA", map.get( DataElement.class ).get( 0 ).getUid() );
+ assertEquals( "deabcdefghB", map.get( DataElement.class ).get( 1 ).getUid() );
+ assertEquals( "deabcdefghC", map.get( DataElement.class ).get( 2 ).getUid() );
+ }
+
+ @Test
+ @Ignore // ignoring since Jackson can't handle parsing of XML as trees
+ public void testParseXmlMetadata() throws IOException
+ {
+ Map<Class<? extends IdentifiableObject>, List<? extends IdentifiableObject>> map = renderService.fromMetadata(
+ new ClassPathResource( "render/metadata.xml" ).getInputStream(), RenderFormat.XML );
+
+ assertTrue( map.containsKey( DataElement.class ) );
+ assertEquals( 3, map.get( DataElement.class ).size() );
+ assertEquals( "DataElementA", map.get( DataElement.class ).get( 0 ).getName() );
+ assertEquals( "DataElementB", map.get( DataElement.class ).get( 1 ).getName() );
+ assertEquals( "DataElementC", map.get( DataElement.class ).get( 2 ).getName() );
+ assertEquals( "deabcdefghA", map.get( DataElement.class ).get( 0 ).getUid() );
+ assertEquals( "deabcdefghB", map.get( DataElement.class ).get( 1 ).getUid() );
+ assertEquals( "deabcdefghC", map.get( DataElement.class ).get( 2 ).getUid() );
+ }
+}
=== added directory 'dhis-2/dhis-services/dhis-service-core/src/test/resources/render'
=== added file 'dhis-2/dhis-services/dhis-service-core/src/test/resources/render/metadata.json'
--- dhis-2/dhis-services/dhis-service-core/src/test/resources/render/metadata.json 1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/test/resources/render/metadata.json 2016-02-02 05:58:04 +0000
@@ -0,0 +1,16 @@
+{
+ "dataElements": [
+ {
+ "id": "deabcdefghA",
+ "name": "DataElementA"
+ },
+ {
+ "id": "deabcdefghB",
+ "name": "DataElementB"
+ },
+ {
+ "id": "deabcdefghC",
+ "name": "DataElementC"
+ }
+ ]
+}
\ No newline at end of file
=== added file 'dhis-2/dhis-services/dhis-service-core/src/test/resources/render/metadata.xml'
--- dhis-2/dhis-services/dhis-service-core/src/test/resources/render/metadata.xml 1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/test/resources/render/metadata.xml 2016-02-02 05:58:04 +0000
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<metadata xmlns="http://dhis2.org/schema/dxf/2.0">
+ <dataElements>
+ <dataElement id="deabcdefghA" name="DataElementA" />
+ <dataElement id="deabcdefghB" name="DataElementB" />
+ <dataElement id="deabcdefghC" name="DataElementC" />
+ </dataElements>
+</metadata>
\ No newline at end of file