Problem with charting using JasperReport - java

I have a problem when I generate a report,the problem is that chart repeats
exp if I have in the X axis 5 elements ,the chart will be repeated 5 times
My report:
<?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="BarChartproject" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
<parameter name="SQL" class="java.lang.String">
<defaultValueExpression><![CDATA[]]></defaultValueExpression>
</parameter>
<queryString>
<![CDATA[$P!{SQL}]]>
</queryString>
<field name="nb" class="java.lang.Long"/>
<field name="priority" class="java.lang.String"/>
<field name="project" 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="18" splitType="Stretch"/>
</columnHeader>
<detail>
<band height="178" splitType="Stretch">
<stackedBar3DChart>
<chart>
<reportElement x="70" y="21" width="363" height="132"/>
<chartTitle/>
<chartSubtitle/>
<chartLegend/>
</chart>
<categoryDataset>
<categorySeries>
<seriesExpression><![CDATA[$F{project}]]></seriesExpression>
<categoryExpression><![CDATA[$F{project}]]></categoryExpression>
<valueExpression><![CDATA[$F{nb}]]></valueExpression>
</categorySeries>
</categoryDataset>
<bar3DPlot>
<plot/>
<itemLabel color="#000000" backgroundColor="#FFFFFF"/>
<categoryAxisFormat>
<axisFormat>
<labelFont/>
<tickLabelFont/>
</axisFormat>
</categoryAxisFormat>
<valueAxisFormat>
<axisFormat>
<labelFont/>
<tickLabelFont/>
</axisFormat>
</valueAxisFormat>
</bar3DPlot>
</stackedBar3DChart>
</band>
</detail>
<columnFooter>
<band height="45" splitType="Stretch"/>
</columnFooter>
<pageFooter>
<band height="54" splitType="Stretch"/>
</pageFooter>
<summary>
<band height="42" splitType="Stretch"/>
</summary>
My code:
public String ConstructSQL()
{
System.out.println("status------"+this.getMyChoiceStatus());
System.out.println("chartssssssssss------"+this.getChartType());
for(int i=0;i<checkbox.length;i++)
{
if (checkbox[i].equals("1"))
{
System.out.println("priority checked");
select ="select count(jiraissue.id) as nb ,jiraissue.priority,jiraissue.project,jiraissue.issuestatus";
from =" from jiraissue,issuestatus";
where =" where issuestatus.id=jiraissue.issuestatus";
and =" and issuestatus.pname ="+"'"+this.getMyChoiceStatus()+"'";
groupBy=" group by jiraissue.priority";
sql =select+from+where+and+groupBy+" ;";
System.out.println("SQL report------"+this.sql);
return sql;
}
else
{
if (checkbox[i].equals("2"))
{
System.out.println("project checked");
select ="select count(jiraissue.id) as nb ,jiraissue.project,jiraissue.priority,jiraissue.issuestatus";
from =" from jiraissue,issuestatus";
where =" where issuestatus.id=jiraissue.issuestatus";
and =" and issuestatus.pname ="+"'"+this.getMyChoiceStatus()+"'";
groupBy=" group by jiraissue.project";
sql =select+from+where+and+groupBy+" ;";
System.out.println("SQL report------"+this.sql);
return sql;
}
}
}
return sql;
}
public void fillReport()
{
try {
// - Connexion à la base
Driver monDriver = new com.mysql.jdbc.Driver();
DriverManager.registerDriver(monDriver);
connection = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/jiradb", "","");
// - Chargement et compilation du rapport
JasperDesign jasperDesign = JRXmlLoader.load("C:/Documents and Settings/My Documents/NetBeansProjects/JiraMap/src/java/Reports/"+chartType+chartGrouping()+".jrxml");
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
Map parameterMap = new HashMap();
parameterMap.put("SQL",ConstructSQL());
// // - Execution du rapport
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport,parameterMap, connection);
// - Création du rapport au format PDF
JasperExportManager.exportReportToPdfFile(jasperPrint, "C:/Documents and Settings/My Documents/NetBeansProjects/JiraMap/src/java/Reports/"+chartType+chartGrouping()+".pdf");
// JasperViewer.viewReport(jasperPrint);
}

I think the issue here (which you have most probably discovered already as this question was aksed 7 months ago) is that you have placed the chart into the 'Detail' area. You will have the detail area repeated for each element returned (i.e. row of data in a query).
You should place the chart into the 'Title' area, or one of the other 'non-repeating' areas on a page.

Related

Jasper iReport: Error retrieving field value from bean

I am trying to pass list of days from bean to jasper plugin in Netbeans but i get an error.
I have followed this guide http://javabeansinjasper.blogspot.cz/2011/01/javabeans-as-datasource-in.html#comment-form
Error:
Error filling print... Error retrieving field value from bean : den
net.sf.jasperreports.engine.JRException: Error retrieving field value from bean : den      
at net.sf.jasperreports.engine.data.JRAbstractBeanDataSource.getBeanProperty(JRAbstractBeanDataSource.java:123)      
at net.sf.jasperreports.engine.data.JRAbstractBeanDataSource.getFieldValue(JRAbstractBeanDataSource.java:96)      
at net.sf.jasperreports.engine.data.JRBeanCollectionDataSource.getFieldValue(JRBeanCollectionDataSource.java:109)      
at net.sf.jasperreports.engine.fill.JRFillDataset.setOldValues(JRFillDataset.java:1358)      
at net.sf.jasperreports.engine.fill.JRFillDataset.next(JRFillDataset.java:1259)      
at net.sf.jasperreports.engine.fill.JRFillDataset.next(JRFillDataset.java:1235)      
at net.sf.jasperreports.engine.fill.JRBaseFiller.next(JRBaseFiller.java:1614)      
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:150)      
at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:963)      
at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:892)      
at net.sf.jasperreports.engine.fill.JRFiller.fill(JRFiller.java:114)      
at net.sf.jasperreports.engine.JasperFillManager.fill(JasperFillManager.java:584)      
at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:930)      
at com.jaspersoft.ireport.designer.compiler.IReportCompiler.run(IReportCompiler.java:928)      
at org.openide.util.RequestProcessor$Task.run(RequestProcessor.java:1423)      
at org.openide.util.RequestProcessor$Processor.run(RequestProcessor.java:2033) 
Caused by: java.lang.NoSuchMethodException: Unknown property 'den' on class 'class java.lang.String'      
at org.apache.commons.beanutils.PropertyUtilsBean.getSimpleProperty(PropertyUtilsBean.java:1322)      
at org.apache.commons.beanutils.PropertyUtilsBean.getNestedProperty(PropertyUtilsBean.java:770)      
at org.apache.commons.beanutils.PropertyUtilsBean.getProperty(PropertyUtilsBean.java:846)      
at org.apache.commons.beanutils.PropertyUtils.getProperty(PropertyUtils.java:426)      
at net.sf.jasperreports.engine.data.JRAbstractBeanDataSource.getBeanProperty(JRAbstractBeanDataSource.java:111)      ... 15 more 
Here is a bean:
public class ListDays {
public ListDays(String den) {
this.den = den;
}
public ListDays() {
}
public String den;
public String getDen() {
return den;
}
public void setDen(String den) {
this.den = den;
}
public static Collection getSeznamDnu(){
List<String> seznamDnu = new ArrayList<>();
try {
for (int i = 0; i<LocalDate.now().getMonth().length(LocalDate.now().isLeapYear()) ;i++){
LocalDate prvniDenMesice = LocalDate.now().withDayOfMonth(1);
seznamDnu.add(prvniDenMesice.plusDays(i).format(DateTimeFormatter.ofPattern("EE dd")));}
} catch (Exception e) {
System.out.println(e);
}
return seznamDnu;
}
}
And here is a report:
<?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="null" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="0f6a9791-50d4-4aff-9b3f-da0e5f528443">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<queryString>
<![CDATA[]]>
</queryString>
<field name="den" class="java.lang.String">
<fieldDescription><![CDATA[den]]></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">
<staticText>
<reportElement x="113" y="2" width="100" height="20" uuid="6b5fe0c5-60a2-4db6-96d6-cb9d065e17ff"/>
<text><![CDATA[den]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="125" splitType="Stretch">
<textField>
<reportElement x="113" y="29" width="100" height="20" uuid="f588ecab-7ede-4e2c-80b9-994efcc45dd8"/>
<textFieldExpression><![CDATA[$F{den}]]></textFieldExpression>
</textField>
</band>
</detail>
<columnFooter>
<band height="45" splitType="Stretch"/>
</columnFooter>
<pageFooter>
<band height="54" splitType="Stretch"/>
</pageFooter>
<summary>
<band height="42" splitType="Stretch"/>
</summary>
</jasperReport>
Thanks in advance. Been stacked on this for quite some time. Hopefully question is understandable.
Apart from some minor adjustments - creating separated package and class for generating collection and initialization of variable den - main problem was in this line:
seznamDnu.add(prvniDenMesice.plusDays(i).format(DateTimeFormatter.ofPattern("EE dd")));}
I was filling the collection with Strings instead of beans. Code changed to:
seznamDnu.add(new ListDays(prvniDenMesice.plusDays(i).format(DateTimeFormatter.ofPattern("EE dd"))));}
And everything works fine. While looking at the code now, I also have to note that I didn't choose bean's name really well.

.jrxml to .jasper compile error field not found exception

Hi i'm new to jasper report.i'm using ireport for designing report.
I have created a simple report design, which have 2 fields name,addr.
public class R {
private String name;
private String addr;
public R(String name, String addr) {
super();
this.name = name;
this.addr = addr;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddr() {
return addr;
}
public void setAddr(String addr) {
this.addr = addr;
}
}
.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="R" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="4fd2f78e-344d-4371-ba1e-f86c2a81ef75">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<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">
<staticText>
<reportElement x="296" y="41" width="228" height="20" uuid="19aa39a0-45cf-4b36-b327-25b640f81d8a"/>
<textElement textAlignment="Center"/>
<text><![CDATA[Address]]></text>
</staticText>
<staticText>
<reportElement x="36" y="41" width="215" height="20" uuid="baba8372-2215-439d-8b20-fd4ab054bbe5"/>
<textElement textAlignment="Center"/>
<text><![CDATA[Name]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="125" splitType="Stretch">
<textField>
<reportElement x="296" y="0" width="228" height="20" uuid="c4de17e6-e5bc-4454-b38e-cc50acf18f0b"/>
<textFieldExpression><![CDATA[$F{addr}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="36" y="0" width="215" height="20" uuid="8283d8b6-a288-48ec-bd4a-36c2875f5cff"/>
<textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
</textField>
</band>
</detail>
<columnFooter>
<band height="45" splitType="Stretch"/>
</columnFooter>
<pageFooter>
<band height="54" splitType="Stretch"/>
</pageFooter>
<summary>
<band height="42" splitType="Stretch"/>
</summary>
</jasperReport>
Here i'm using array list as a data source.
When i click compile button on i report it shows error that
net.sf.jasperreports.engine.design.JRValidationException: Report design not valid :       1. Field not found : addr      2. Field not found : name
java code
public void runReport(String fileName, String outFileName) throws JRException
{
List<R> list = new ArrayList<R>();
Map parameters = new HashMap();
list.add(new R("a1" ,"a2"));
list.add(new R("b1" ,"b2"));
list.add(new R("c1" ,"c2"));
JasperPrint print = JasperFillManager.fillReport( fileName, parameters, new JRBeanCollectionDataSource(list));
JRExporter exporter = new JRPdfExporter();
exporter.setParameter(
JRExporterParameter.OUTPUT_FILE_NAME,outFileName);
exporter.setParameter(
JRExporterParameter.JASPER_PRINT, print);
JasperExportManager.exportReportToPdfFile(print, outFileName);
print = null;
exporter = null;
}
You should add to your jrxml field:
<field name="addr" class="java.lang.String">
<fieldDescription><![CDATA[addr]]></fieldDescription></field>
You can do it by clicking on "fields" and selecting "create field".

Reuse same field structure / design in a report

I need to design a report to represent a pojo structure like the following:
class Obj{
field1;
field2;
List<Obj> substitutions;
}
My report will have the same field design, used one time in detail 1 band and then n-times in detail 2 with a subreport.
How can I reuse the same field design in both detail 1 and 2?
I have tryed to put a subreport in detail1 and a subreport in detail 2 pointing the same jrxml file. While the second report is ok because is filled by a collection in back object of report, I don't know how to pass current report datasource to first subreport.
Does someone have any ideas?
Here an example on the result report.
I have a pojo class named purchase.
public class Purchase {
private String name;
private String remark;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setRemark(String remark) {
this.remark = remark;
}
public String getRemark() {
return remark;
}
}
Main class to generate pdf file.
public class JasperReportIntro {
public static void main(String[] args) {
JasperReport jasperReport;
JasperPrint jasperPrint;
ArrayList<Purchase> list1 = new ArrayList<Purchase>();
ArrayList<Purchase> list2 = new ArrayList<Purchase>();
for(int i=0;i<20;i++) {
Purchase purchase = new Purchase();
purchase.setName("Vivek" + i);
purchase.setRemark("This is remark" + i);
purchase.setDiscount(10.0);
purchase.setId(i);
list1.add(purchase);
}
for(int i=0;i<20;i++) {
Purchase purchase = new Purchase();
purchase.setName("yadav" + i);
purchase.setRemark("This is remark" + i);
purchase.setDiscount(10.0);
purchase.setId(i);
list2.add(purchase);
}
try {
jasperReport = JasperCompileManager.compileReport("path/to/report.jrxml");
Map<String, List> result = new HashMap<String, List>();
JRDataSource beanCollectionDataSource = new JRBeanCollectionDataSource(list1);
Map<String, Object> map = new HashMap<String, Object>();
map.put("dataForSubreport1",list1);
map.put("dataForSubreport2",list2);
jasperPrint = JasperFillManager.fillReport(jasperReport,map, beanCollectionDataSource);
JasperExportManager.exportReportToPdfFile(jasperPrint,
"path/to/simple_report.pdf");
System.out.println("Completed");
} catch (JRException e) {
e.printStackTrace();
}
}
}
Main report class report.jrxml.
<?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="report" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="4ddfa540-9b62-4339-9334-18733f7469cc">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<parameter name="SUBREPORT_DIR" class="java.lang.String" isForPrompting="false">
<defaultValueExpression><![CDATA["/home/jft/code/jasper-report/Genratedreports/"]]></defaultValueExpression>
</parameter>
<parameter name="dataForSubreport1" class="java.util.List"/>
<parameter name="dataForSubreport2" class="java.util.List"/>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="100">
<subreport>
<reportElement x="0" y="20" width="555" height="40" uuid="8798ed7b-f389-4037-9381-3862d2f3e43a"/>
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{dataForSubreport1})]]></dataSourceExpression>
<subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "sub1.jasper"]]></subreportExpression>
</subreport>
<subreport>
<reportElement positionType="Float" x="0" y="60" width="555" height="40" uuid="f99fdb31-afc3-4127-8148-8e0426cd71a6"/>
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{dataForSubreport2})]]></dataSourceExpression>
<subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "sub1.jasper"]]></subreportExpression>
</subreport>
</band>
</title>
</jasperReport>
Here is subreport sub1.jrxml.
<?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="sub1" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="48bc0742-6980-42a7-9a52-6f125fb83bc2">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<field name="name" class="java.lang.String"/>
<background>
<band splitType="Stretch"/>
</background>
<detail>
<band height="20" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="100" height="20" uuid="ac3c1a86-6d0d-4b63-b339-744528573666"/>
<textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
Download file using main class of jasperreportintro. I have passed two parameter dataForSubreport1 and dataForSubreport2 using map. and send both parameter in subreport. It use same subreport with different -2 value. Hope it will help you.
Enjoy.

Creating Hindi PDF using UTF-8 in JasperReport

My requirement is to Create a PDF with some text in Hindi/Marathi/Other Indian Language while some text in English.
Text in Hindi/Marathi/Other Indian Language will be static text in Unicode (UTF-8) while text in English will be static and dynamic. I created a jrxml file in iReport 5.5.0. In iReport Internal Viewer, it shows proper output. However while exporting in pdf, it doesn't show the Hindi Text at all. It only shows English Text. While exporting in doc, the Hindi Text is coming as squares which I feel is word setting issue. In HTML or RTF export, it works perfectly. The same thing is happening when I create pdf/doc/html/rtf using Java Code. I am attaching Java and jrxml file over here for reference.
public class TestHindiPDF {
public static void main(String[] args) throws Exception {
String strJasperFilePath = null;
String strJRXMLFilePath = null;
String strReportPath = "D:\\Chintan\\Tech\\Input_Files\\TestMulti";
String strSaveReportPath = "D:\\Chintan\\Tech\\Input_Files\\TestMulti";
String strReportName = "multi1";
strJasperFilePath = strReportPath + "/multi1.jasper";
strJRXMLFilePath = strReportPath + "/multi1.jrxml";
strReportName = "multi1";
File reportFile = new File(strJasperFilePath);//your report_name.jasper file
if (!reportFile.exists()) {
try {
System.out.println("Compiling JRXML File : " + strJRXMLFilePath);
JasperCompileManager.compileReportToFile(strJRXMLFilePath, strJasperFilePath);
reportFile = new File(strJasperFilePath);
} catch (JRException e) {
e.printStackTrace();
}
}
if (reportFile != null) {
OutputStream fileOut = null;
try {
JasperReport jasperReport = (JasperReport) JRLoader.loadObject(reportFile);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null, new JREmptyDataSource());
JRExporter exporter2 = new JRRtfExporter();
exporter2.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8");
exporter2.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter2.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, strSaveReportPath + "/" + strReportName + ".rtf");
exporter2.exportReport();
JRExporter exporter3 = new JRHtmlExporter();
exporter3.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8");
exporter3.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter3.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, strSaveReportPath + "/" + strReportName + ".html");
exporter3.exportReport();
JRExporter exporter4 = new JRDocxExporter();
exporter4.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8");
exporter4.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter4.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, strSaveReportPath + "/" + strReportName + ".docx");
exporter4.exportReport();
JRExporter exporter1 = new JRPdfExporter();
exporter1.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8");
exporter1.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter1.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, strSaveReportPath + "/" + strReportName + ".pdf");
exporter1.exportReport();
JasperExportManager.exportReportToPdfStream(jasperPrint, fileOut);
} catch (JRException e) {
e.printStackTrace();
} finally {
if (fileOut != null) {
fileOut.close();
}
}
}
}
}
The JRXML File is :
<?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="multi1" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="c8a053f6-858e-427b-aec1-89231aadddd6">
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="79" splitType="Stretch">
<staticText>
<reportElement x="104" y="0" width="313" height="25" uuid="fa5e3e6e-26f7-4973-a2d5-3c3b7a5e8227"/>
<textElement textAlignment="Center">
<font size="16"/>
</textElement>
<text><![CDATA[अतिदेय राशि के पुनर्भुगतान हेतु अनुस्मारक]]></text>
</staticText>
<staticText>
<reportElement x="132" y="25" width="252" height="25" uuid="fa5e3e6e-26f7-4973-a2d5-3c3b7a5e8227"/>
<textElement textAlignment="Center">
<font size="16"/>
</textElement>
<text><![CDATA[Reminder for repaying Overdue Amount]]></text>
</staticText>
</band>
</title>
<pageHeader>
<band height="35" splitType="Stretch"/>
</pageHeader>
<columnHeader>
<band height="61" splitType="Stretch"/>
</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>

Generating a pdf report using JasperRunManager

I want to generate a pdf report using ejbql ,but I dont know how to do that.
I have tried to do thye following but an error occure:
parameterMap.put(JRJpaQueryExecuterFactory.PARAMETER_JPA_ENTITY_MANAGER, entityManager);
parameterMap.put("priority", "3");
try
{
JasperFillManager.fillReportToFile("C:/Documents and Settings/MyDocuments/NetBeansProjects/JiraMap/src/java/Reports/Test.jasper", parameterMap);
JasperRunManager.runReportToPdfFile("C:/Documents and Settings/MyDocuments/NetBeansProjects/JiraMap/src/java/Reports/Test.pdf", parameterMap);
}
catch (JRException ex)
{
ex.printStackTrace();
}
Tehb error is:
net.sf.jasperreports.engine.JRException: Error loading object from file : C:\Documents and Settings\My Documents\NetBeansProjects\JiraMap\src\java\Reports\Test.pdf
at net.sf.jasperreports.engine.util.JRLoader.loadObject(JRLoader.java:101)
at net.sf.jasperreports.engine.JasperRunManager.runReportToPdfFile(JasperRunManager.java:93)
at Reporting.ReportBean.main(ReportBean.java:38)
Caused by: java.io.EOFException
UPdate
I have use this code (I haven't use any parameter ) but always the report is empty:
JasperReport report = JasperCompileManager.compileReport("C:/Documents and Settings/My Documents/NetBeansProjects/JiraMap/src/java/Reports/Test.jrxml");
JasperPrint print = JasperFillManager.fillReport(report,null);
JasperExportManager.exportReportToPdfFile(print,"C:/Documents and Settings/My Documents/NetBeansProjects/JiraMap/src/java/Reports/Test.pdf");
My report with the Aggregate Function count(I have tried to use the alias as numebr but it didn't work):
<?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="Test" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<parameter name="priority" class="java.lang.String">
<defaultValueExpression><![CDATA[]]></defaultValueExpression>
</parameter>
<queryString language="ejbql">
<![CDATA[select count(j) from Jiraissue j where j.priority='3']]>
</queryString>
<field name="count(j)" class="java.lang.Long"/>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="79" splitType="Stretch">
</band>
</title>
<pageHeader>
<band height="35" splitType="Stretch"/>
</pageHeader>
<columnHeader>
<band height="84" splitType="Stretch">
<staticText>
<reportElement x="28" y="0" width="100" height="20"/>
<textElement/>
<text><![CDATA[count(j)]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="125" splitType="Stretch">
<textField>
<reportElement x="50" y="11" width="100" height="20"/>
<textElement/>
<textFieldExpression class="java.lang.Long"> <![CDATA[$F{count(j)}]]></textFieldExpression>
</textField>
</band>
</detail>
<columnFooter>
<band height="45" splitType="Stretch"/>
</columnFooter>
<pageFooter>
<band height="54" splitType="Stretch"/>
</pageFooter>
<summary>
<band height="42" splitType="Stretch"/>
</summary>
</jasperReport>
The error is :
net.sf.jasperreports.engine.JRException: Error retrieving field value from bean : count(j)
at net.sf.jasperreports.engine.data.JRAbstractBeanDataSource.getBeanProperty(JRAbstractBeanDat aSource.java:123)
at net.sf.jasperreports.engine.data.JRJpaDataSource$PropertyReader.getValue(JRJpaDataSource.java:206)
at net.sf.jasperreports.engine.data.JRJpaDataSource.getFieldValue(JRJpaDataSource.java:131)
at net.sf.jasperreports.engine.fill.JRFillDataset.setOldValues(JRFillDataset.java:821)
at net.sf.jasperreports.engine.fill.JRFillDataset.next(JRFillDataset.java:785)
at net.sf.jasperreports.engine.fill.JRBaseFiller.next(JRBaseFiller.java:1482)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:126)
at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:946)
at net.sf.jasperreports.engine.fill.JRFiller.fillReport(JRFiller.java:118)
at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:435)
at Reporting.ReportBean.main(ReportBean.java:41)
Caused by: java.lang.NoSuchMethodException: Unknown property 'count'+ on bean class 'class java.lang.Long'
[EL Info]: 2011-06-29 11:05:12.606--ServerSession(27055962)--file:/C:/Documents and Settings/a497165/My Documents/NetBeansProjects/JiraMap/build/web/WEB-INF/classes/_JiraMapPU logout successful
at org.apache.commons.beanutils.PropertyUtilsBean.getMappedProperty(PropertyUtilsBean.java:624)
at org.apache.commons.beanutils.PropertyUtilsBean.getMappedProperty(PropertyUtilsBean.java:570)
at org.apache.commons.beanutils.PropertyUtilsBean.getNestedProperty(PropertyUtilsBean.java:758)
at org.apache.commons.beanutils.PropertyUtilsBean.getProperty(PropertyUtilsBean.java:837)
at org.apache.commons.beanutils.PropertyUtils.getProperty(PropertyUtils.java:426)
at net.sf.jasperreports.engine.data.JRAbstractBeanDataSource.getBeanProperty(JRAbstractBeanDataSource.java:111)
... 10 more
This is part of the code I used for generating with JasperPrint; and sending the file back to a browser response. This using JDBC though, not EJBQL. But the JasperReports part of the code is the same.
FileResolver fileResolver = new FileResolver() {
#Override
public File resolveFile(String fileName) {
return new File(getServletContext().getRealPath("/") + fileName);
}
};
Map<String, Object> parameters = new HashMap<String, Object>();
parameters.put("REPORT_FILE_RESOLVER", fileResolver);
...... // blah blah
JasperPrint jrprint = JasperFillManager.fillReport(input, parameters, conn);
byte[] reportBytes;
jrprint.setName("MyFile." + fileFormat);
response.setContentType("APPLICATION/OCTET-STREAM");
String disHeader = "Attachment;Filename=\"MyFile.";
ServletOutputStream os = response.getOutputStream();
response.setHeader("Content-Disposition", disHeader);
reportBytes = JasperExportManager.exportReportToPdf(jrprint);
os.write(reportBytes);
You need to pass all the parameters needed as below:
JasperPrint jrprint = JasperFillManager.fillReport(input, parameters, connections);

Categories