Jump to content
Changes to the Jaspersoft community edition download ×

How to send a collection of data from a JasperReports template to a scriptlet?


f.e.evans

Recommended Posts

Mostly we've been accepting json data into a multitude of JasperReports templates, picking out individual fields and displaying them.

We now have a new requirement that involves accessing a collection within the json data rather than using the individual fields. It’s proving difficult to do.

Our aim is to have each a heading for each different method and then add a bulleted list of types associated with a particular method underneath.

I have set up a scriptlet that would work through the array and assemble a string that could be displayed in the JasperReports template.

However, I’m struggling to get the collection of data into the scriptlet.

I’ve proven the connection works as I can send through a string (from the template to the scriptlet), tweak it and send it back for display.

Unfortunately I can't seem to access a collection as a variable/field etc in the template and then send it through to the java scriptlet. I've got to the point where the template at least compiles with a field/variable defined with class ‘java.util.Arraylist’, but it that field/variable never contains any of the data from the json.

So I either need a clue as to how to define all this properly, assuming it is possible and I've just done it wrong. Or a way to get access to the whole datasource within the java scriptlet and then pick through for the bits I want in there.

The template I've used for testing:

<?xml version="1.0" encoding="UTF-8"?><!-- Created with Jaspersoft Studio version 6.1.1.final using JasperReports Library version 6.1.1  --><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="reportNewClaim_subreport_evidenceList_uploads" pageWidth="595" pageHeight="842" columnWidth="595" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="43ef5573-1a4b-4c0d-a713-ecc72a8b64ee">   <property name="ireport.zoom" value="3.1384283767210035"/>   <property name="ireport.x" value="304"/>   <property name="ireport.y" value="0"/>   <property name="net.sf.jasperreports.awt.ignore.missing.font" value="true"/>   <property name="net.sf.jasperreports.text.save.line.breaks" value="true"/>   <template><![CDATA[$P{TEMPLATE_DIR} + "StyleTemplateClaim.jrtx"]]></template>   <scriptlet name="uploadsScriptlet" class="xxxxxx.xxx.xxxx.scriptlets.UploadsScriptlet" />   <parameter name="SUBREPORT_DIR" class="java.lang.String">      <defaultValueExpression><![CDATA["./"]]></defaultValueExpression>   </parameter>   <parameter name="TEMPLATE_DIR" class="java.lang.String">      <defaultValueExpression><![CDATA["./"]]></defaultValueExpression>   </parameter>   <queryString language="JSON">      <![CDATA[]]>   </queryString>   <field name="firstEvidence" class="java.lang.String">      <fieldDescription><![CDATA[axxx.upload.evidence[0].type]]>      </fieldDescription>   </field>   <field name="firstEvidence1" class="java.lang.String">      <fieldDescription><![CDATA[axxx.upload.evidence[0]]]>      </fieldDescription>   </field>   <field name="teststring" class="java.util.ArrayList">      <fieldDescription><![CDATA[axxx.body.upload.evidence]]>      </fieldDescription>   </field>   <variable name="variable1" class="java.util.ArrayList" calculation="System" resetType="Report">      <variableExpression><![CDATA[$V{variable1}.add($F{teststring})]]>      </variableExpression>      <initialValueExpression><![CDATA[new ArrayList()]]>      </initialValueExpression>   </variable>   <variable name="evidencefromscriptlet" class="java.lang.String" calculation="System" />   <background>      <band splitType="Stretch"/>   </background>   <detail>      <band height="18" splitType="Stretch">         <property name="com.jaspersoft.studio.unit.height" value="pixel"/>         <textField isStretchWithOverflow="true" isBlankWhenNull="true">            <reportElement positionType="Float" x="0" y="0" width="595" height="18" isRemoveLineWhenBlank="true" uuid="d1f681c7-12c0-4696-afcf-2d7be4833b58">            </reportElement>            <box>               <bottomPen lineWidth="0.0"/>            </box>            <textElement>               <font isBold="true"/>               <paragraph lineSpacing="1_1_2"/>            </textElement>            <textFieldExpression><![CDATA["**** "+ ($V{variable1}).toString() + "**" + ($F{firstEvidence1}).toString() + "**"+ $F{firstEvidence} + "***" +$V{evidencefromscriptlet}]]></textFieldExpression>         </textField>      </band>   </detail></jasperReport>[/code]
The scriptlet:package xxxxxx.xxx.xxxx.scriptlets;import com.fasterxml.jackson.databind.ObjectMapper;import net.sf.jasperreports.engine.JRDefaultScriptlet;import net.sf.jasperreports.engine.JRScriptletException;import java.util.ArrayList;import java.util.List;public class UploadsScriptlet extends JRDefaultScriptlet {    @Override    public void afterReportInit() throws JRScriptletException {        List<Evidence> testList = (List<Evidence>) getVariableValue("variable1");        String test = "temp";        for (Evidence evidence : testList) {            test += evidence.getEvidenceType();            test += " ";        }        setVariableValue("evidencefromscriptlet", test);    }}[/code]

The JSON:

{    "axxx": {        "head": {            "dateTimeSubmitted": "01-Jul-2019 15:08:50",            "language": "en",            "origin": "gb"        },        "body": {            "data": {                "aboutYou": {                    "Title": {                        "question": "Title",                        "answer": "Mr"                    },                    "FirstName": {                        "question": "First name",                        "answer": "David"                    },                    "MiddleName": {                        "question": "Middle name(s)",                        "answer": "John"                    },                    "LastName": {                        "question": "Last name",                        "answer": "Smith"                    }                },                {                    …                }            }        },        "upload": {            "evidence": [                {                    "type": "type1",                    "method": "method1",                    "files": [                        {                            "name": "a.jpg",                            "id": "id1"                        }                    ]                },                {                    "type": "type2",                    "method": "method2",                    "files": []                },                {                    "type": "type3",                    "method": "method3",                    "files": []                }            ],            "groupId": "grpId1"        }    }}[/code]

 

 

Link to comment
Share on other sites

  • Replies 0
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

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...