How to set image in jasper title programmatically - java

I am creating JR report without using jrxml template. I need to know how can we set how image in the report title with Java code?
...
//Title
band = new JRDesignBand();
band.setHeight(50);
textField = new JRDesignTextField();
textField.setBlankWhenNull(true);
textField.setX(0);
textField.setY(10);
textField.setWidth(515);
textField.setHeight(30);
textField.setHorizontalAlignment(HorizontalAlignEnum.CENTER);
textField.setStyle(normalStyle);
textField.setFontSize(22);
expression = new JRDesignExpression();
expression.setValueClass(java.lang.String.class);
expression.setText("$P{ReportTitle}");
textField.setExpression(expression);
band.addElement(textField);
jasperDesign.setTitle(band);
Here instead of text field I need to add an image. What should I do?

You can use JRDesignImage class for setting image.
The sample:
public JasperDesign getJasperDesign() throws JRException {
JasperDesign jasperDesign = new JasperDesign();
jasperDesign.setName(DESIGN_NAME);
jasperDesign.setPageWidth(595);
jasperDesign.setPageHeight(842);
jasperDesign.setColumnWidth(515);
jasperDesign.setColumnSpacing(0);
jasperDesign.setLeftMargin(40);
jasperDesign.setRightMargin(40);
jasperDesign.setTopMargin(50);
jasperDesign.setBottomMargin(50);
JRDesignParameter parameter = new JRDesignParameter();
parameter.setName("ReportTitle");
parameter.setValueClass(java.lang.String.class);
jasperDesign.addParameter(parameter);
//Title band
JRDesignBand band = new JRDesignBand();
band.setHeight(250);
JRDesignTextField textField = new JRDesignTextField();
textField.setBlankWhenNull(true);
textField.setX(0);
textField.setY(10);
textField.setWidth(515);
textField.setHeight(30);
textField.setHorizontalAlignment(HorizontalAlignEnum.CENTER);
textField.setFontSize(22);
JRDesignExpression expression = new JRDesignExpression();
expression.setValueClass(java.lang.String.class);
expression.setText("$P{ReportTitle}");
textField.setExpression(expression);
band.addElement(textField);
// Image
String imgPath = "\"dukesign.jpg\"";
expression = new JRDesignExpression();
expression.setText(imgPath);
JRDesignImage image = new JRDesignImage(jasperDesign);
image.setX(45);
image.setY(55);
image.setWidth(130);
image.setHeight(104);
image.setScaleImage(ScaleImageEnum.FILL_FRAME);
image.setExpression(expression);
// Add image to Detail band
band.addElement(image);
jasperDesign.setTitle(band);
return jasperDesign;
}
The result will be (the generated pdf file):

I something like this with a List Component and a dataset
<title>
<band height="70" splitType="Stretch">
<staticText>
<reportElement uuid="30b7421d-058a-478d-9179-901273499bc4" x="74" y="0" width="420" height="55"/>
<textElement textAlignment="Center" markup="html">
<font size="18"/>
</textElement>
<text><![CDATA[Report Header ]]></text>
</staticText>
<componentElement>
<reportElement uuid="f1f3b8ab-5dbf-4139-8033-d048a7e9896d" x="0" y="0" width="74" height="70"/>
<jr:list xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd" printOrder="Vertical">
<datasetRun subDataset="Agency_Image" uuid="6c1c4672-d9c2-4968-9265-0918ce1b8e91">
<datasetParameter name="USER_AGENCY_CODE">
<datasetParameterExpression><![CDATA[$P{USER_AGENCY_CODE}]]></datasetParameterExpression>
</datasetParameter>
<connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
</datasetRun>
<jr:listContents height="70" width="74">
<image onErrorType="Blank">
<reportElement uuid="1c56b92b-ea0c-4f16-8402-df13d3f01317" x="0" y="0" width="46" height="46"/>
<imageExpression><![CDATA[$F{AGENCY_IMAGE}]]></imageExpression>
</image>
</jr:listContents>
</jr:list>
</componentElement>
<textField>
<reportElement uuid="9d151874-7f50-4987-a89f-a183dfe744cd" x="74" y="55" width="420" height="15"/>
<textElement textAlignment="Center">
<font isBold="true"/>
</textElement>
<textFieldExpression><![CDATA[$P{AGENCY_NAME}]]></textFieldExpression>
</textField>
</band>
</title>

Related

How to create nested table like jasper report?

I have been trying to create a jasper report which should look like the following image:
For my sample report, Out-1 and Out-2 are rows for the outer table and asd, asds, adasd are rows for the inner table.
I am trying to achieve it using nested tables. But the table takes the field and I can not assign field to the inner-table.
I am creating jasper reports in Java code rather than using SQL connection to the table for some reason.
JXML
<?xml version="1.0" encoding="UTF-8"?>
<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="report1" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="ec55a262-fec4-45f4-9b48-5be98088aafa">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<subDataset name="New Dataset 1" uuid="cdef25ec-abc4-45ba-b70e-28d82716626b">
<field name="sachNr" class="java.lang.String"/>
<field name="akundenNr" class="java.lang.String"/>
<field name="nestedTable" class="java.lang.String"/>
</subDataset>
<subDataset name="New Dataset 2" uuid="eec983aa-a227-4a28-9c44-73cbe31fa024">
<field name="packStNr" class="java.lang.String"/>
<field name="prodDatum" class="java.lang.String"/>
<field name="stueck" class="java.lang.String"/>
</subDataset>
<parameter name=" artikeldatenTable" class="java.lang.String"/>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="79" splitType="Stretch"/>
</title>
<pageHeader>
<band height="35" splitType="Stretch"/>
</pageHeader>
<columnHeader>
<band height="61" splitType="Stretch"/>
</columnHeader>
<detail>
<band height="125" splitType="Stretch">
<componentElement>
<reportElement key="table" x="0" y="28" width="360" height="50" uuid="fae14075-18a8-4fcc-b6f9-c14e3623e75d"/>
<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="New Dataset 1" uuid="17a10558-3a43-47c2-809d-6362924e5015">
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.JREmptyDataSource(1)]]></dataSourceExpression>
</datasetRun>
<jr:column width="137" uuid="7a8ca2ff-7a1a-41a0-8c30-b02f7be6e2b9">
<jr:tableHeader height="30" rowSpan="1">
<textField>
<reportElement x="0" y="0" width="137" height="30" uuid="0fe0e502-dea8-45f6-9f96-633f5db90cd8"/>
<textFieldExpression><![CDATA[$F{sachNr}]]></textFieldExpression>
</textField>
</jr:tableHeader>
<jr:detailCell height="42" rowSpan="1">
<componentElement>
<reportElement key="table" x="0" y="0" width="137" height="42" uuid="9e72ca8c-182d-41c0-a021-98493162c7da"/>
<jr:table>
<datasetRun subDataset="New Dataset 2" uuid="7475e256-4bc5-4e88-be92-b6d2eb2fd4c5">
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.JREmptyDataSource()]]></dataSourceExpression>
</datasetRun>
<jr:column width="90" uuid="cd851138-c894-459f-91c9-636c3602b247">
<jr:detailCell height="20" rowSpan="1">
<textField>
<reportElement x="0" y="0" width="90" height="20" uuid="ce21338e-c593-4bae-b7c4-08741127575c"/>
<textFieldExpression><![CDATA[$F{packStNr}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column width="90" uuid="5f2b25a9-e34a-4698-b1ab-e8843dafec3c">
<jr:detailCell height="20" rowSpan="1">
<textField>
<reportElement x="0" y="0" width="90" height="20" uuid="5487305d-d22f-46fa-97bc-88e671f7085c"/>
<textFieldExpression><![CDATA[$F{prodDatum}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column width="90" uuid="93c28f55-9bda-4be0-989a-03ce573715ba">
<jr:detailCell height="20" rowSpan="1">
<textField>
<reportElement x="0" y="0" width="90" height="20" uuid="53c4e2b3-7591-49b3-9b13-50cc1f16641a"/>
<textFieldExpression><![CDATA[$F{stueck}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
</jr:table>
</componentElement>
</jr:detailCell>
</jr:column>
<jr:column width="90" uuid="f898d2e7-1c2f-48fe-9357-61ee5d767a51">
<jr:tableHeader height="30" rowSpan="1">
<textField>
<reportElement x="0" y="0" width="90" height="30" uuid="16b70095-2900-46f0-96ec-d638edb784f3"/>
<textFieldExpression><![CDATA[$F{akundenNr}]]></textFieldExpression>
</textField>
</jr:tableHeader>
<jr:detailCell height="42" rowSpan="1"/>
</jr:column>
<jr:column width="111" uuid="0121d9c8-d1f0-447f-84ed-15385693a072">
<jr:tableHeader height="30" rowSpan="1"/>
<jr:detailCell height="42" rowSpan="1"/>
</jr:column>
<jr:column width="90" uuid="ca27e48c-a1d6-4c32-a1de-2580b6786b3d">
<jr:tableHeader height="30" rowSpan="1"/>
<jr:detailCell height="42" rowSpan="1"/>
</jr:column>
</jr:table>
</componentElement>
</band>
</detail>
<columnFooter>
<band height="45" splitType="Stretch"/>
</columnFooter>
<pageFooter>
<band height="54" splitType="Stretch"/>
</pageFooter>
<summary>
<band height="42" splitType="Stretch"/>
</summary>
</jasperReport>
Outer Table POJO
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
public class OuterTable {
private String sachNr;
private String akundenNr;
private String SUBREPORT_DIR;
private JRBeanCollectionDataSource nestedTable;
public String getSachNr() {
return sachNr;
}
public void setSachNr(String sachNr) {
this.sachNr = sachNr;
}
public String getAkundenNr() {
return akundenNr;
}
public void setAkundenNr(String akundenNr) {
this.akundenNr = akundenNr;
}
public String getSUBREPORT_DIR() {
return SUBREPORT_DIR;
}
public void setSUBREPORT_DIR(String sUBREPORT_DIR) {
SUBREPORT_DIR = sUBREPORT_DIR;
}
public JRBeanCollectionDataSource getNestedTable() {
return nestedTable;
}
public void setNestedTable(JRBeanCollectionDataSource nestedTable) {
this.nestedTable = nestedTable;
}
}
Inner Table POJO
public class InnerTable {
private String packStNr;
private String prodDatum;
private Integer stueck;
public String getPackStNr() {
return packStNr;
}
public void setPackStNr(String packStNr) {
this.packStNr = packStNr;
}
public String getProdDatum() {
return prodDatum;
}
public void setProdDatum(String prodDatum) {
this.prodDatum = prodDatum;
}
public Integer getStueck() {
return stueck;
}
public void setStueck(Integer stueck) {
this.stueck = stueck;
}
}
My Controller
private void generateLagerbestand(List<Artikeldaten> artikelList) throws JRException, FileNotFoundException {
JasperReport jasperReport;
Map<String, Object> parameters = new HashMap<String, Object>();
List <OuterTable> outerTableList = new ArrayList<OuterTable>();
for(int i = 0; i < artikelList.size(); i++) {
List <InnerTable> innerTableList = new ArrayList<InnerTable>();
List<Lager> lagersList = artikelList.get(i).getLagers();
for(int j = 0; j < lagersList.size(); j++) {
InnerTable innerTable = new InnerTable();
innerTable.setPackStNr(lagersList.get(j).getPackstnr());
innerTable.setProdDatum(lagersList.get(j).getProddatum().toString());
innerTable.setStueck(lagersList.get(j).getStueck());
innerTableList.add(innerTable);
}
OuterTable outerTable = new OuterTable();
outerTable.setAkundenNr(artikelList.get(i).getAkundennr());
outerTable.setSachNr(artikelList.get(i).getSachnr());
JRBeanCollectionDataSource nestedTable = new JRBeanCollectionDataSource(innerTableList);
outerTable.setNestedTable(nestedTable);
outerTableList.add(OuterTable);
}
try {
jasperReport = JasperCompileManager.compileReport("PathToMyReport\\MyReport.jrxml");
JRBeanCollectionDataSource outerTableItems = new JRBeanCollectionDataSource(outerTableList);
// artikeldatenTable is outerTable
parameters.put("artikeldatenTable", outerTableItems);
JREmptyDataSource jrEmptyDataSource = new JREmptyDataSource();
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, jrEmptyDataSource);
OutputStream outputStream = new FileOutputStream(new File("PathToMyPDF:\\MyPDF.pdf"));
JasperExportManager.exportReportToPdfStream(jasperPrint, outputStream);
} catch(Exception ex) {
ex.printStackTrace();
}
}
You are missing the concept of the detail band, the detail will iterate on the datasource you pass to report
JasperFillManager.fillReport(jasperReport, parameters, jrEmptyDataSource)
That's an empty datasource!, it will not iterate at all.
Instead, pass
JRBeanCollectionDataSource outerTableItems = new JRBeanCollectionDataSource(outerTableList);
To the report, define the fields of OuterTable in your main report (not a subDatasource) including nestedTable
<field name="nestedTable" class="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource"/>
Then
In detail band you put textFields related to first level "Out-1" and "Out-2" in your example.
Bellow you add the jr:table component taking the datasource from the bean
<datasetRun subDataset="New Dataset 1" uuid="17a10558-3a43-47c2-809d-6362924e5015">
<dataSourceExpression><![CDATA[$F{nestedTable}]]></dataSourceExpression>
</datasetRun>
In general within the bean I would not have JRBeanCollectionDatasource but instead store the data in a normal List<>, the reason is that a JRDatasource is consumable you can only use it once, see this to understand better How to use same JRBeanCollectionDataSource on multiple sub reports?

How to populate Jasper table using collection in Java [duplicate]

I need to show JRBeanCollectionDataSource data in Table component (JasperReports).
Here is my template, ShowPerson.jrxml file:
<?xml version="1.0" encoding="UTF-8"?>
<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="ShowPerson" pageWidth="612" pageHeight="792" whenNoDataType="NoDataSection" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="304c4c4e-c99a-4399-8081-748d3b7c0b8c">
<style name="table">
<box>
<pen lineWidth="1.0" lineColor="#000000"/>
</box>
</style>
<style name="table_TH" mode="Opaque" backcolor="#F0F8FF">
<box>
<pen lineWidth="0.5" lineColor="#000000"/>
</box>
</style>
<style name="table_CH" mode="Opaque" backcolor="#BFE1FF">
<box>
<pen lineWidth="0.5" lineColor="#000000"/>
</box>
</style>
<style name="table_TD" mode="Opaque" backcolor="#FFFFFF">
<box>
<pen lineWidth="0.5" lineColor="#000000"/>
</box>
</style>
<subDataset name="Table Dataset 1" whenResourceMissingType="Empty" uuid="63b01547-bce2-47c9-ba15-666f94d11387">
<queryString language="SQL">
<![CDATA[]]>
</queryString>
<field name="name" class="java.lang.String"/>
<field name="age" class="java.lang.Integer"/>
</subDataset>
<parameter name="INFO" class="java.lang.String"/>
<title>
<band height="40" splitType="Stretch">
<staticText>
<reportElement uuid="e96450a8-8358-4cae-a094-3add06d57de2" x="0" y="20" width="56" height="20"/>
<textElement/>
<text><![CDATA[Info]]></text>
</staticText>
<textField>
<reportElement uuid="e21e9932-ebfe-4bb5-904d-ea99e141866b" x="56" y="20" width="100" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[$P{INFO}]]></textFieldExpression>
</textField>
</band>
</title>
<detail>
<band height="40" splitType="Stretch">
<componentElement>
<reportElement uuid="dea5d821-81b6-434b-ae27-4f3a0268e805" key="table 1" x="0" y="0" width="572" height="40"/>
<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="Table Dataset 1" uuid="39f8f0bf-8a2b-44f4-9a4c-0c873da79fba">
<datasetParameter name="REPORT_DATA_SOURCE">
<datasetParameterExpression><![CDATA[]]></datasetParameterExpression>
</datasetParameter>
<dataSourceExpression><![CDATA[$P{REPORT_DATA_SOURCE}]]></dataSourceExpression>
</datasetRun>
<jr:column width="169" uuid="aae649c4-6a69-485f-8a0d-87022df793ee">
<jr:tableHeader height="20" rowSpan="1">
<staticText>
<reportElement uuid="795dac43-0ff0-482c-89a0-7dac3b27d513" x="0" y="0" width="169" height="20"/>
<textElement/>
<text><![CDATA[Name]]></text>
</staticText>
</jr:tableHeader>
<jr:detailCell height="20" rowSpan="1">
<textField>
<reportElement uuid="4f1ab13a-d776-4cd5-a2a7-a5cf23360816" x="0" y="0" width="169" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column width="173" uuid="a6997eea-131e-4555-80e6-a20d4decb18f">
<jr:tableHeader height="20" rowSpan="1">
<staticText>
<reportElement uuid="5f6e2413-8025-47ca-9638-9609ea13f93f" x="0" y="0" width="173" height="20"/>
<textElement/>
<text><![CDATA[Age]]></text>
</staticText>
</jr:tableHeader>
<jr:detailCell height="20" rowSpan="1">
<textField>
<reportElement uuid="195d51a0-9e45-4201-ad67-d3026ce2e72c" x="0" y="0" width="173" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[$F{age}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
</jr:table>
</componentElement>
</band>
</detail>
</jasperReport>
My POJO:
public class Person {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Main class for building report:
public class OpenReport {
public static void main(String[] args) throws JRException {
Map<String, Object> peopleMap = new HashMap<>();
peopleMap.put("Sisco", 17);
peopleMap.put("Eve", 19);
peopleMap.put("John", 20);
peopleMap.put("George", 21);
peopleMap.put("Steve", 18);
ArrayList<Person> dataList = new ArrayList<Person>();
for(Map.Entry<String, Object> personMap : peopleMap.entrySet()) {
Person person = new Person();
person.setName(personMap.getKey());
person.setAge(Integer.valueOf(personMap.getValue().toString()));
dataList.add(person);
}
JRBeanCollectionDataSource beanColDataSource = new JRBeanCollectionDataSource(dataList);
Map parameters = new HashMap();
parameters.put("INFO", "Hello");
JasperReport report = (JasperReport) JRLoader.loadObject("src/test/ireport/ShowPerson.jasper");
JasperPrint jasperPrint = JasperFillManager.fillReport(report, parameters, beanColDataSource);
JFrame frame = new JFrame("Report");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new JRViewer(jasperPrint));
frame.pack();
frame.setVisible(true);
}
}
1.Send your datasource from the server as a parameter, and fill the report with a different one (can be empty).
JRBeanCollectionDataSource beanColDataSource = new JRBeanCollectionDataSource(dataList);
Map parameters = new HashMap();
parameters.put("INFO", "Hello");
parameters.put("DS1", beanColDataSource);
JasperReport report = (JasperReport) JRLoader.loadObject("src/test/ireport/ShowPerson.jasper");
JasperPrint jasperPrint = JasperFillManager.fillReport(report, parameters, new JREmptyDataSource());
2.Decalre a new parameter in the report of type JRBeanCollectionDataSource
<parameter name="DS1" class="net.sf.jasperreports.engine.JRBeanCollectionDataSource"/>
3.Set TableDatasource to use the $P{DS1} parameter.
<jr:table ...>
<datasetRun subDataset="Table Dataset 1">
<datasetParameter name="REPORT_DATA_SOURCE">
<datasetParameterExpression><![CDATA[$P{DS1}]]></datasetParameterExpression>
</datasetParameter>
</datasetRun>
.....
4.Declare the fields (name, age) in Table Dataset 1.
5.In the table, in the DetailBand add a TextField in each column with the corresponding field ($F{name} and $F{age} respectively).
#laura: I get new error when trying set the TableDatasource to use the $P{DS1} parameter.
net.sf.jasperreports.engine.JRBeanCollectionDataSource cannot be resolved to a type
Now, I have found the solution, maybe not the best.
Map parameters = new HashMap();
parameters.put("INFO", "Hello");
parameters.put("DS1", dataList);
JasperReport report = (JasperReport) JRLoader.loadObject("src/test/ireport/ShowPerson.jasper");
JasperPrint jasperPrint = JasperFillManager.fillReport(report, parameters, new JREmptyDataSource());
Modify the class of $P{DS1} to java.util.Collection or java.util.List
And set the table datasource to
new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{DS1})

Filling reports from json using JasperReports [duplicate]

This question already has answers here:
How to fill report using JSON datasource without getting null values?
(3 answers)
Closed 6 years ago.
I'm beginner with JasperReports ( first try ) so,
I set up my netbeans, and my first try using data from database was successful. But now I just want to create my report from json data using Jasper Reports in Netbeans And I think it's not like getting data from Database.
By the way I tried to edit my xml file and add this :
<textField>
<reportElement x="0" y="31" width="555" height="20" uuid="9678a129-61e8-4034-ab8e-739ee5723c87"/>
<textElement textAlignment="Right">
<font size="12" isBold="true"/>
</textElement>
<textFieldExpression><![CDATA[$F{nom}]]></textFieldExpression>
</textField>
But it gives me null in report.
Here is what I did in java side :
Map parametersMap = new HashMap();
parametersMap.put("name", json.getString("Name"));
parametersMap.put("start", json.getString("Start"));
parametersMap.put("end", json.getString("end"));
String report = "C:\\My\\Path\\toReport\\report1.jrxml";
JasperReport Jasp = JasperCompileManager.compileReport(report);
JasperPrint JASP_PRINT = JasperFillManager.fillReport(Jasp,parametersMap);
JasperViewer.viewReport(JASP_PRINT);
UPDATED:
Here is my Java code
public void start(Future<Void> startFuture){
String fileJSON = "C:\\Users\\PathToMyFile\\data.txt";
String file = "/Test1/:name";
Router router = Router.router(vertx);
router.route(file).handler(routingContext -> {
HttpServerResponse response = routingContext.response();
response.setChunked(true);
routingContext.vertx().setTimer(1000, tid -> routingContext.response().end());
});
vertx.createHttpServer()
.requestHandler(router::accept)
.listen(8089, "localhost", res -> {
if (res.succeeded())
startFuture.complete();
else
startFuture.fail(res.cause());
});
vertx.fileSystem().readFile(fileJSON, (AsyncResult<Buffer> result) -> {
if (result.succeeded()) {
JsonObject json = result.result().toJsonObject();
try{
Map parametersMap = new HashMap();
parametersMap.put("name",json.getString("name"));
parametersMap.put("start",json.getString("start"));
parametersMap.put("end",json.getString("end"));
String report = "C:\\Users\\pathToMyPackage\\report1.jrxml";
JasperReport Jasp = JasperCompileManager.compileReport(report);
JasperPrint JASP_PRINT = JasperFillManager.fillReport(Jasp,parametersMap);
JasperViewer.viewReport(JASP_PRINT);
And for my JRXML file :
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<queryString language="json">
<![CDATA[]]>
</queryString>
<field name="nom" class="java.lang.String">
<fieldDescription><![CDATA[name]]></fieldDescription>
</field>
<field name="dateDebut" class="java.lang.String">
<fieldDescription><![CDATA[start]]></fieldDescription>
</field>
<background>
<band splitType="Stretch"/>
</background>
<detail>
<band height="125" splitType="Stretch">
<textField>
<reportElement x="100" y="0" width="100" height="30" uuid="02b279da-3795-4655-8571-5a36a3ef378c"/>
<textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
</textField>
<staticText>
<reportElement x="0" y="0" width="100" height="30" uuid="671e61ad-8d8f-48cb-969f-78c05a516398"/>
<text><![CDATA[name]]></text>
</staticText>
<textField>
<reportElement x="100" y="30" width="100" height="30" uuid="9d53f46f-a252-48b3-9213-8c3092c29f49"/>
<textFieldExpression><![CDATA[$F{start}]]></textFieldExpression>
</textField>
<staticText>
<reportElement x="0" y="30" width="100" height="30" uuid="3b49affb-685a-4df2-a872-c0e6fdcab94b"/>
<text><![CDATA[start]]></text>
</staticText>
</band>
</detail>
And my JSON file :
{
"Name": "Test",
"Start": "16-06-2015",
"End":"16-06-2019"
}
You need to pass the data as a datasource, in json this is done by passing the stream through the parameter map (since you are using vertex, get the json in String format and then pass it as a stream).
Example
InputStream iostream = new ByteArrayInputStream(jsonString.getBytes(StandardCharsets.UTF_8));
parametersMap.put(JsonQueryExecuterFactory.JSON_INPUT_STREAM, iostream);
Then you can define the fields related to your json and json (xpath) query

Getting null values in report when using JavaBean Data Source

When I tried to generate a report by using JavaBean Data Source and displaying the bean's field values in the table, the PDF report is showing null values.
I have a simple bean (PersonBean) as follows:
public class PersonBean {
private String Field1;
private String Field2;
public String getField1() {
return Field1;
}
public void setField1(String field1) {
Field1 = field1;
}
public String getField2() {
return Field2;
}
public void setField2(String field2) {
Field2 = field2;
}
}
The Person class which populate and return the collection of bean is as follows:
import java.util.ArrayList;
import java.util.List;
public class Person {
public static java.util.List<PersonBean> getReportData() throws Exception {
List<PersonBean> personBeanList = null;
try {
personBeanList = new ArrayList<PersonBean>();
PersonBean personBean1 = new PersonBean();
personBean1.setField1("Hina");
personBean1.setField2("Sachdev");
personBeanList.add(personBean1);
PersonBean personBean2 = new PersonBean();
personBean2.setField1("Swathi");
personBean2.setField2("Singh");
personBeanList.add(personBean2);
} catch (Exception e) {
e.printStackTrace();
}
return personBeanList;
}
}
The PersonReport class which creates report is as follows:
import java.io.FileNotFoundException;
import java.util.*;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.xml.JRXmlLoader;
import net.sf.jasperreports.view.JasperViewer;
public class PersonReport implements JRDataSource {
public static void main(String[] args) throws JRException, FileNotFoundException {
try {
Map<String, Object> params = new HashMap<String, Object>();
JasperDesign jasperDesign = JRXmlLoader.load("C:/Users/sachdevh/Desktop/reports/personReport.jrxml");
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params,
new JRBeanCollectionDataSource(Person.getReportData()));
JasperExportManager.exportReportToPdfFile(jasperPrint, "./Util/bean.pdf");
JasperViewer.viewReport(jasperPrint);
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
public Object getFieldValue(JRField arg0) throws JRException {
return null;
}
#Override
public boolean next() throws JRException {
return false;
}
}
By using iReport 4.6.0, I designed a report by inserting table component.
The jrxml file is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport ..>
<subDataset name="New Dataset 1" uuid="50bff6d9-9b98-444b-9851-bdd37ea570e4">
<queryString><![CDATA[]]></queryString>
<field name="field1" class="java.lang.String">
<fieldDescription><![CDATA[field1]]></fieldDescription>
</field>
<field name="field2" class="java.lang.String">
<fieldDescription><![CDATA[field2]]></fieldDescription>
</field>
</subDataset>
<parameter name="SUBREPORT_DIR" class="java.lang.String" isForPrompting="false">
<defaultValueExpression><![CDATA["C:\\Users\\sachdevh\\Desktop\\reports\\"]]></defaultValueExpression>
</parameter>
<parameter name="parameter1" class="java.lang.String">
<defaultValueExpression><![CDATA[$F{field1}]]></defaultValueExpression>
</parameter>
<queryString>
<![CDATA[]]>
</queryString>
<field name="field1" class="java.lang.String">
<fieldDescription><![CDATA[field1]]></fieldDescription>
</field>
<field name="field2" class="java.lang.String">
<fieldDescription><![CDATA[field2]]></fieldDescription>
</field>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="79" splitType="Stretch"/>
</title>
<pageHeader>
<band height="35" splitType="Stretch"/>
</pageHeader>
<columnHeader>
<band height="61" splitType="Stretch">
<componentElement>
<reportElement uuid="b3e3e08d-91d9-4e01-ae74-abdfc270551a" key="table" x="0" y="0" width="555"
height="61"/>
<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="New Dataset 1" uuid="f5f39e4f-4349-4e5d-9736-03f84a7a7617">
<dataSourceExpression>
<![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource(Person.getReportData())]]></dataSourceExpression>
</datasetRun>
<jr:column uuid="9e2a6076-7f96-40c8-86ae-318b521a4f81" width="90">
<jr:tableHeader height="30"/>
<jr:tableFooter height="30"/>
<jr:columnHeader height="30">
<staticText>
<reportElement uuid="99411482-c1d5-44fa-aef7-9d9f4bf392ec" x="0" y="0" width="90"
height="30"/>
<textElement/>
<text><![CDATA[field1]]></text>
</staticText>
</jr:columnHeader>
<jr:columnFooter height="30"/>
<jr:detailCell height="20">
<textField>
<reportElement uuid="74861d9e-48a8-47ce-a477-81d4f497d483" x="0" y="0" width="90"
height="20"/>
<textElement/>
<textFieldExpression><![CDATA[$F{field1}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column uuid="84c3b078-0114-452e-b3a3-f1c7dcd1b8cb" width="90">
<jr:tableHeader height="30"/>
<jr:tableFooter height="30"/>
<jr:columnHeader height="30">
<staticText>
<reportElement uuid="cb87f1a0-bbbf-40b2-bb19-292ecf8d9a18" x="0" y="0" width="90"
height="30"/>
<textElement/>
<text><![CDATA[field2]]></text>
</staticText>
</jr:columnHeader>
<jr:columnFooter height="30"/>
<jr:detailCell height="20">
<textField>
<reportElement uuid="84fd788f-24e8-4dfa-8c83-ce388c663f45" x="0" y="0" width="90"
height="20"/>
<textElement/>
<textFieldExpression><![CDATA[$F{field2}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
</jr:table>
</componentElement>
</band>
</columnHeader>
<detail>
<band height="125" splitType="Stretch"/>
</detail>
<columnFooter>
<band height="45" splitType="Stretch"/>
</columnFooter>
<pageFooter>
<band height="54" splitType="Stretch"/>
</pageFooter>
<summary>
<band height="42" splitType="Stretch"/>
</summary>
</jasperReport>
I think the datasource expression below
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource(Person.getReportData())]]></dataSourceExpression>
You can solve this issue in two steps:
Move table component to the Title band (now it lies in Column Header band) - to prevent occurring of net.sf.jasperreports.engine.JRRuntimeException: Infinite loop creating new page due to column header overflow exception in cases when datasource has a lot of elements;
Change the table's subDataset dataSourceExpression to value below, that is relevant to your Java code:
<datasetRun subDataset="New Dataset 1" uuid="f5f39e4f-4349-4e5d-9736-03f84a7a7617">
<dataSourceExpression><![CDATA[$P{REPORT_DATA_SOURCE}]]></dataSourceExpression>
</datasetRun>

JRBeanCollectionDataSource: How to show data from the java.util.List from JavaBean?

My JavaBean contains the java.util.List.
Userinfo {
private String username;
private String password;
List<Address> listAddress;
}
How to show the data of this List in the Detail band?
Here is the working sample.
The key points of this sample:
using of the _THIS expression;
using List (jr:list) component in Detail band
The snippet of code for generating report:
public static void testBuildPdf() {
try {
Map<String, Object> params = new HashMap<String, Object>();
JasperReport jasperReport = JasperCompileManager.compileReport(reportSource);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, getDataSource());
JasperExportManager.exportReportToPdfFile(jasperPrint, outputFileName);
} catch (Exception e) {
e.printStackTrace();
}
}
private static JRDataSource getDataSource() {
Collection<BeanWithList> coll = new ArrayList<BeanWithList>();
coll.add(new BeanWithList(Arrays.asList("London", "Paris"), 1));
coll.add(new BeanWithList(Arrays.asList("London", "Madrid", "Moscow"), 2));
coll.add(new BeanWithList(Arrays.asList("Rome"), 3));
return new JRBeanCollectionDataSource(coll);
}
The JavaBean code:
public class BeanWithList {
// The member's name can be any. The JR engine is using public getter for extracting field's value
private List<String> cities;
private Integer id;
public BeanWithList(List<String> cities, Integer id) {
this.cities = cities;
this.id = id;
}
// getter should be public
public List<String> getCities() {
return this.cities;
}
public Integer getId() {
return this.id;
}
}
The jrxml file:
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport ...>
<subDataset name="dataset1">
<field name="city" class="java.lang.String">
<fieldDescription><![CDATA[_THIS]]></fieldDescription>
</field>
</subDataset>
<field name="id" class="java.lang.Integer"/>
<field name="cities" class="java.util.Collection"/>
<title>
<band height="103" splitType="Stretch">
<staticText>
<reportElement x="138" y="28" width="258" height="20"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font isBold="true" isItalic="true"/>
</textElement>
<text><![CDATA[Bean with List sample]]></text>
</staticText>
</band>
</title>
<columnHeader>
<band height="20">
<staticText>
<reportElement x="0" y="0" width="100" height="20"/>
<box>
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font isBold="true" isItalic="true" isUnderline="false"/>
</textElement>
<text><![CDATA[Id]]></text>
</staticText>
<staticText>
<reportElement x="100" y="0" width="100" height="20"/>
<box>
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font isBold="true" isItalic="true" isUnderline="false"/>
</textElement>
<text><![CDATA[City name]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="20" splitType="Stretch">
<textField>
<reportElement stretchType="RelativeToTallestObject" x="0" y="0" width="100" height="20"/>
<box leftPadding="10">
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement/>
<textFieldExpression><![CDATA[$F{id}]]></textFieldExpression>
</textField>
<componentElement>
<reportElement x="100" y="0" width="400" height="20"/>
<jr:list xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd" printOrder="Vertical">
<datasetRun subDataset="dataset1">
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{cities})]]></dataSourceExpression>
</datasetRun>
<jr:listContents height="20" width="400">
<textField>
<reportElement x="0" y="0" width="100" height="20"/>
<box leftPadding="10">
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement/>
<textFieldExpression><![CDATA[$F{city}]]></textFieldExpression>
</textField>
</jr:listContents>
</jr:list>
</componentElement>
</band>
</detail>
</jasperReport>
The result will be:
Other related questions are the How do I print a list of strings contained within another list in iReport? question and Passing the List of primitive type objects as datasource for subreport question.
public void generisiIzvestaj(HttpServletRequest request, HttpServletResponse response) throws Exception {
List<Predstava> predstave = (List<Predstava>)request.getSession().getAttribute("predstaveR");
JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(predstave);
InputStream inputStream = this.getClass().getResourceAsStream("/reports/Predstave.jrxml");
JasperReport jasperReport = JasperCompileManager.compileReport(inputStream);
Map<String, Object> params = new HashMap<String, Object>();
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, dataSource);
inputStream.close();
response.setContentType("application/x-download");
response.addHeader("Content-disposition", "attachment; filename=PredstaveRezisera.pdf");
ServletOutputStream out = response.getOutputStream();
JasperExportManager.exportReportToPdfStream(jasperPrint,out);

Categories