=== added file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/adapter/JacksonRowDataSerializer.java' --- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/adapter/JacksonRowDataSerializer.java 1970-01-01 00:00:00 +0000 +++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/adapter/JacksonRowDataSerializer.java 2012-04-25 22:37:28 +0000 @@ -0,0 +1,67 @@ +package org.hisp.dhis.common.adapter; + +/* + * Copyright (c) 2004-2012, 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.core.JsonGenerator; +import com.fasterxml.jackson.databind.JsonSerializer; +import com.fasterxml.jackson.databind.SerializerProvider; + +import java.io.IOException; +import java.util.List; + +/** + * @author Morten Olav Hansen + */ +public class JacksonRowDataSerializer + extends JsonSerializer>> +{ + @Override + public void serialize( List> values, JsonGenerator jgen, SerializerProvider provider ) throws IOException + { + boolean b = true; + + for ( List value : values ) + { + if ( !b ) + { + jgen.writeFieldName( "row" ); + } + + b = false; + + jgen.writeStartObject(); + + for ( Object object : value ) + { + jgen.writeStringField( "rowData", "" + object ); + } + + jgen.writeEndObject(); + } + } +} === modified file 'dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/grid/ListGrid.java' --- dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/grid/ListGrid.java 2012-04-25 20:43:17 +0000 +++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/grid/ListGrid.java 2012-04-25 22:37:28 +0000 @@ -27,33 +27,28 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -import static org.hisp.dhis.system.util.MathUtils.getRounded; - -import java.sql.ResultSet; -import java.sql.ResultSetMetaData; -import java.sql.SQLException; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonView; +import com.fasterxml.jackson.databind.annotation.JsonSerialize; +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 net.sf.jasperreports.engine.JRException; import net.sf.jasperreports.engine.JRField; - import org.apache.commons.math.stat.regression.SimpleRegression; import org.hisp.dhis.common.Dxf2Namespace; import org.hisp.dhis.common.Grid; import org.hisp.dhis.common.GridHeader; +import org.hisp.dhis.common.adapter.JacksonRowDataSerializer; import org.hisp.dhis.common.view.DetailedView; import org.hisp.dhis.system.util.MathUtils; -import com.fasterxml.jackson.annotation.JsonProperty; -import com.fasterxml.jackson.annotation.JsonView; -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 java.sql.ResultSet; +import java.sql.ResultSetMetaData; +import java.sql.SQLException; +import java.util.*; + +import static org.hisp.dhis.system.util.MathUtils.getRounded; /** * @author Lars Helge Overland @@ -122,7 +117,7 @@ @JsonProperty @JsonView( {DetailedView.class} ) - @JacksonXmlProperty + @JacksonXmlProperty( namespace = Dxf2Namespace.NAMESPACE ) public String getTitle() { return title; @@ -137,7 +132,7 @@ @JsonProperty @JsonView( {DetailedView.class} ) - @JacksonXmlProperty + @JacksonXmlProperty( namespace = Dxf2Namespace.NAMESPACE ) public String getSubtitle() { return subtitle; @@ -152,7 +147,7 @@ @JsonProperty @JsonView( {DetailedView.class} ) - @JacksonXmlProperty + @JacksonXmlProperty( namespace = Dxf2Namespace.NAMESPACE ) public String getTable() { return table; @@ -176,8 +171,8 @@ @JsonProperty @JsonView( {DetailedView.class} ) - @JacksonXmlElementWrapper( localName = "headers" ) - @JacksonXmlProperty( localName = "header" ) + @JacksonXmlElementWrapper( localName = "headers", namespace = Dxf2Namespace.NAMESPACE ) + @JacksonXmlProperty( localName = "header", namespace = Dxf2Namespace.NAMESPACE ) public List getHeaders() { return headers; @@ -200,7 +195,7 @@ @JsonProperty @JsonView( {DetailedView.class} ) - @JacksonXmlProperty + @JacksonXmlProperty( namespace = Dxf2Namespace.NAMESPACE ) public int getHeight() { return (grid != null && grid.size() > 0) ? grid.size() : 0; @@ -208,7 +203,7 @@ @JsonProperty @JsonView( {DetailedView.class} ) - @JacksonXmlProperty + @JacksonXmlProperty( namespace = Dxf2Namespace.NAMESPACE ) public int getWidth() { verifyGridState(); @@ -245,9 +240,10 @@ } @JsonProperty + @JsonSerialize( using = JacksonRowDataSerializer.class ) @JsonView( {DetailedView.class} ) - @JacksonXmlElementWrapper( localName = "rows" ) - @JacksonXmlProperty( localName = "row" ) + @JacksonXmlElementWrapper( localName = "rows", namespace = Dxf2Namespace.NAMESPACE ) + @JacksonXmlProperty( localName = "row", namespace = Dxf2Namespace.NAMESPACE ) public List> getRows() { return grid; @@ -524,8 +520,7 @@ { addHeader( new GridHeader( rsmd.getColumnLabel( i ), false, false ) ); } - } - catch ( SQLException ex ) + } catch ( SQLException ex ) { throw new RuntimeException( ex ); } @@ -540,19 +535,18 @@ while ( rs.next() ) { addRow(); - + for ( int i = 1; i <= columnNo; i++ ) { addValue( rs.getObject( i ) ); } } - } - catch ( SQLException ex ) + } catch ( SQLException ex ) { throw new RuntimeException( ex ); } } - + // ------------------------------------------------------------------------- // Supportive methods // -------------------------------------------------------------------------