Jump to content
Changes to the Jaspersoft community edition download ×

JSON Data in subreport


davids182009

Recommended Posts

I am trying to generate a report with a header, a table with a detail of products and footer, the header and footer fills with parameters, but i don't know how to fill the table because it is a subreport, i have the input data from json with a number of products.

My code works but the table shows empty,  i know it is because the subreport doesn't have a datasource, but i don't know how to do that. I need to fill the data in the table according to the number of products on the JSON and that the tables repeat over the document two times in this case. Can anyone helps me to do that?. Thanks.

This is my code:

JSON Data:

{"productList": [{        "productNumber": 1,        "modelo": "Bolsa Deportiva Plana",        "tela": "Tafeta",        "dimensiones": "40x35",        "destino": "Bog, D.C",        "observaciones": "Observacion de Prueba",        "cantidad": 500,        "vrUnitario": 1200,        "subTotal": 600000,        "iva": 50000,        "total": 650000    },    {        "productNumber": 2,        "modelo": "Bolsa Deportiva",        "tela": "Otra",        "dimensiones": "50x20",        "destino": "Bog, D.C",        "observaciones": "Observacion de Prueba",        "cantidad": 400,        "vrUnitario": 900,        "subTotal": 360000,        "iva": 40000,        "total": 400000    }]}[/code]

Java:

JasperReport jr = null;    JSONObject jsonRequest = new JSONObject(clientData);    JSONArray jsonProducts = jsonRequest.getJSONArray("productList");    String headerDoc = "HEADER TEXT TEST...";    String footerDoc = "FOOTER TEXT TEST...";    Map<String, Object> parametros = new HashMap<String, Object>();    parametros.put("Header", headerDoc);    parametros.put("footer", footerDoc);    Map<String, String> product = new HashMap<>();    for (int i = 0; i < jsonProducts.length(); i++) {                    int numberProduct = jsonProducts.getJSONObject(i).getInt("productNumber");        String modelo = jsonProducts.getJSONObject(i).getString("modelo");        String tela = jsonProducts.getJSONObject(i).getString("tela");        String dimensiones = jsonProducts.getJSONObject(i).getString("dimensiones");        String destino = jsonProducts.getJSONObject(i).getString("destino");        String observaciones = jsonProducts.getJSONObject(i).getString("observaciones");        int cantidad = jsonProducts.getJSONObject(i).getInt("cantidad");        int vrUnitario = jsonProducts.getJSONObject(i).getInt("vrUnitario");        int subTotal = jsonProducts.getJSONObject(i).getInt("subTotal");        int iva = jsonProducts.getJSONObject(i).getInt("iva");        int total = jsonProducts.getJSONObject(i).getInt("total");        product.put("Product Number", String.valueOf(numberProduct));        product.put("Model", modelo);        product.put("Tela", tela);        product.put("Dimensions", dimensiones);        product.put("Send to", destino);        product.put("Observations", observaciones);        product.put("Ammount", String.valueOf(cantidad));        product.put("Unit Value", String.valueOf(vrUnitario));        product.put("SubTotal", String.valueOf(subTotal));        product.put("Tax", String.valueOf(iva));        product.put("Total", String.valueOf(total));    }    try {        jr = JasperCompileManager.compileReport("PATHTOJASPERFILEMain.jrxml");        JasperPrint jp = JasperFillManager.fillReport(jr, parametros, new JREmptyDataSource());        JasperExportManager.exportReportToPdfFile(jp, "PATHTOEXPORTPDFResult.pdf");        return Response                .status(Response.Status.OK)                .entity("OK!!")                .build();    } catch (JRException e) {        // TODO Auto-generated catch block        e.printStackTrace();        return Response                .status(Response.Status.INTERNAL_SERVER_ERROR)                .entity("ERROR .......!!")                .build();    }[/code]

Main Report

<?xml version="1.0" encoding="UTF-8"?><!-- Created with Jaspersoft Studio version 6.10.0.final using JasperReports Library version 6.10.0-unknown  --><jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="Main" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="8e9538d5-11fe-48f9-b166-d3594b7f64ef"><property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/><parameter name="Header" class="java.lang.String"/><parameter name="footer" class="java.lang.String"/><queryString>    <![CDATA[]]></queryString><background>    <band splitType="Stretch"/></background><pageHeader>    <band height="40" splitType="Stretch">        <textField>            <reportElement x="60" y="0" width="461" height="35" uuid="1562b730-1f0a-4448-bddc-152a0a48723e"/>            <textFieldExpression><![CDATA[$P{Header}]]></textFieldExpression>        </textField>    </band></pageHeader><detail>    <band height="125" splitType="Stretch">        <subreport>            <reportElement x="-1" y="10" width="549" height="100" uuid="086ba30e-6cda-4b8c-b6d9-9d304c6643c6"/>            <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.JREmptyDataSource()]]></dataSourceExpression>            <subreportExpression><![CDATA["C:/Users/David/Documents/Emprendimiento/BIOBOLSA/proyectos/BioBolsaWS/src/com/biobolsa/reporte/subReport.jasper"]]></subreportExpression>        </subreport>    </band></detail><pageFooter>    <band height="54" splitType="Stretch">        <textField>            <reportElement x="60" y="10" width="461" height="35" uuid="8c89af56-0386-4fca-a74e-b35e45e0d321"/>            <textFieldExpression><![CDATA[$P{footer}]]></textFieldExpression>        </textField>    </band></pageFooter>[/code]

 

SubReport

 

<?xml version="1.0" encoding="UTF-8"?><!-- Created with Jaspersoft Studio version 6.10.0.final using JasperReports Library version 6.10.0-unknown  --><jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="subReport" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="dfb0d57a-5f97-459b-a6cb-671424bf4c8f"><property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/><style name="Table_TH" mode="Opaque" backcolor="#F0F8FF">    <box>        <pen lineWidth="0.5" lineColor="#000000"/>        <topPen lineWidth="0.5" lineColor="#000000"/>        <leftPen lineWidth="0.5" lineColor="#000000"/>        <bottomPen lineWidth="0.5" lineColor="#000000"/>        <rightPen lineWidth="0.5" lineColor="#000000"/>    </box></style><style name="Table_CH" mode="Opaque" backcolor="#BFE1FF">    <box>        <pen lineWidth="0.5" lineColor="#000000"/>        <topPen lineWidth="0.5" lineColor="#000000"/>        <leftPen lineWidth="0.5" lineColor="#000000"/>        <bottomPen lineWidth="0.5" lineColor="#000000"/>        <rightPen lineWidth="0.5" lineColor="#000000"/>    </box></style><style name="Table_TD" mode="Opaque" backcolor="#FFFFFF">    <box>        <pen lineWidth="0.5" lineColor="#000000"/>        <topPen lineWidth="0.5" lineColor="#000000"/>        <leftPen lineWidth="0.5" lineColor="#000000"/>        <bottomPen lineWidth="0.5" lineColor="#000000"/>        <rightPen lineWidth="0.5" lineColor="#000000"/>    </box></style><subDataset name="DataSetTable" uuid="42b73ca4-7ae3-4f0d-b530-7acf925f0655">    <queryString>        <![CDATA[]]>    </queryString></subDataset><queryString>    <![CDATA[]]></queryString><background>    <band splitType="Stretch"/></background><detail>    <band height="125" splitType="Stretch">        <componentElement>            <reportElement x="0" y="10" width="561" height="111" uuid="431d8ee9-eb17-4db5-a881-3f4fa4fed78d">                <property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.VerticalRowLayout"/>                <property name="com.jaspersoft.studio.table.style.table_header" value="Table_TH"/>                <property name="com.jaspersoft.studio.table.style.column_header" value="Table_CH"/>                <property name="com.jaspersoft.studio.table.style.detail" value="Table_TD"/>            </reportElement>            <jr:table xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd">                <datasetRun subDataset="DataSetTable" uuid="8e1d9cdd-2f3d-4982-8431-fd4dd89c5eda">                    <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.JREmptyDataSource()]]></dataSourceExpression>                </datasetRun>                <jr:column width="110" uuid="aa543910-08e1-49f0-ba34-61c9acd07598">                    <property name="com.jaspersoft.studio.components.table.model.column.name" value="Column1"/>                    <jr:tableHeader style="Table_TH" height="30" rowSpan="1"/>                    <jr:tableFooter style="Table_TH" height="30" rowSpan="1"/>                    <jr:columnHeader style="Table_CH" height="30" rowSpan="1"/>                    <jr:columnFooter style="Table_CH" height="30" rowSpan="1"/>                    <jr:detailCell style="Table_TD" height="30"/>                </jr:column>                <jr:column width="120" uuid="70570781-1ebd-47ac-aff3-29d6f607e126">                    <property name="com.jaspersoft.studio.components.table.model.column.name" value="Column2"/>                    <jr:tableHeader style="Table_TH" height="30" rowSpan="1"/>                    <jr:tableFooter style="Table_TH" height="30" rowSpan="1"/>                    <jr:columnHeader style="Table_CH" height="30" rowSpan="1"/>                    <jr:columnFooter style="Table_CH" height="30" rowSpan="1"/>                    <jr:detailCell style="Table_TD" height="30"/>                </jr:column>                <jr:column width="120" uuid="2b466fd4-0a02-4018-a35f-3b7feff9cc03">                    <property name="com.jaspersoft.studio.components.table.model.column.name" value="Column3"/>                    <jr:tableHeader style="Table_TH" height="30" rowSpan="1"/>                    <jr:tableFooter style="Table_TH" height="30" rowSpan="1"/>                    <jr:columnHeader style="Table_CH" height="30" rowSpan="1"/>                    <jr:columnFooter style="Table_CH" height="30" rowSpan="1"/>                    <jr:detailCell style="Table_TD" height="30"/>                </jr:column>            </jr:table>        </componentElement>    </band></detail>[/code]

 

Link to comment
Share on other sites

  • Replies 1
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

Pass a parameter to the subreport.

Create a parameter in the subreport and pass it when you define subreport in a master report.

Example:

											"4"		new net.sf.jasperreports.engine.JREmptyDataSource()	"templates/pdf/subreports/detail-layout-2.jasper"[/code]

And then create a parameter in subreport:

	[/code]
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...