=== 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, Collection> fromMetadata( InputStream inputStream, RenderFormat format ); + Map, List> 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, Collection> fromMetadata( InputStream inputStream, RenderFormat format ) + @SuppressWarnings( "unchecked" ) + public Map, List> fromMetadata( InputStream inputStream, RenderFormat format ) throws IOException { - Map, Collection> map = new HashMap<>(); + Map, List> 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 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 collection = new ArrayList<>(); + + for ( JsonNode item : node ) + { + IdentifiableObject value = mapper.treeToValue( item, (Class) schema.getKlass() ); + if ( value != null ) collection.add( value ); + } + + map.put( (Class) 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 + */ +public class RenderServiceTest + extends DhisSpringTest +{ + @Autowired + private RenderService renderService; + + @Test + public void testParseJsonMetadata() throws IOException + { + Map, List> 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, List> 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 @@ + + + + + + + + \ No newline at end of file