passing value dynamically to the JASPER API to generate a Report - jasper-reports

I am generating the Report using the Jasper Reports .
Inside the JRXML file in queryString , the query that is formed is
SELECT * FROM Address WHERE city = $P{customerId}
Now inside my servlet please tell me how can i pass this value dynamically ??
int custid = Integer.parseInt(request.getParameter("customerId")) ;
Map parameters = new HashMap();
parameters.put("customerId", custid);
Please tell me is this the correct way of passing the data ??

You can implement your own datasource (must be net.sf.jasperreports.engine.JRDataSource interface implementation).
After filling (preparing) datasource it should be passed to the JasperFillManager.fillReport(java.lang.String sourceFileName, java.util.Map parameters, JRDataSource jrDataSource) method.
The sample:
JRMapArrayDataSource dataSource = new JRMapArrayDataSource(data);
JasperPrint jasperPrint = JasperFillManager.fillReport(sourceFileName, paramsMap, dataSource);

Related

Access parameters passed through Viewable in JSP

I am using Viewable in my REST environment to display JSPs. But when I am passing parameters through Viewable, I am not able access this parameter in JSP.
I have following code in my java file -
Map<String, Object> map = new HashMap<String, Object>();
map.put("posts", posts);
map.put("name", "satpute");
return new Viewable("/home",map);
I used different options to access this map parameter in JSP, but this returns null. I have used following ways -
<% String name = (String)request.getAttribute("name"); out.print(name); %>
Returns NULL.
Other way I tried to use it is -
<% String name = (String) pageContext.getAttribute("name"); out.print(name); %>
Returns NULL.
Sorry if this is a very basic question, but I have used request in JSP with servlet. But I am not able to figure out how to use JSP with Viewable.
Please help.
You have to receive the map parameter instead of name,
<% Map<String, Object> map= Map<String, Object>request.getAttribute("map");
for (Map.Entry<String, Object> entry : map.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
}%>
I'm not sure about your posts object (What kind of object). from value you can get the object, then cast it and iterate again to get the values.
But I would suggest to go with JSTL hereafter.

Chart localization in JasperReports

I'm using JasperReports 5.5.1 to generate reports from Java. I designed the reports using Jaspersoft Studio. The report has several charts of different types and I have some problems when trying to localize the report using a ResourceBundle passed in the REPORT_RESOURCE_BUNDLE parameter. All the texts in the report are translated correctly except for the ones in the charts. I filled the keyExpression, labelExpression and seriesExpression with $R{STRING_KEY}, but the report is filled with the STRING_KEY instead of its value in the properties file. The title of the charts though is correctly translated.
Can anyone help me with this issue?
I found a solution.
First create a class responsible for getting the localized string from a specific resource bundle.
public class ReportLocalizer {
private static String resourceBundleBaseName = "com.company.package.boundle_name";
public static String getLocalizedString(Locale locale, String key) {
ResourceBundle resourceBundle = ResourceBundle.getBundle(resourceBundleBaseName, locale);
return resourceBundle.getString(key);
}
}
Set the desired locale using JasperReports' parameters:
Map<String, Object> parameters = new HashMap<String, Object>();
...
parameters.put(JRParameter.REPORT_LOCALE, locale);
...
final JasperPrint print = JasperFillManager.fillReport(report, parameters, datasource);
To translate a string in a chart, pass it together with the report's locale to ReportLocalizer's getLocalizedString method.
[CDATA[com.company.package.ReportLocalizer.getLocalizedString($P{REPORT_LOCALE}, "string_key")]]>
For example, we can localize a pie chart showing the amount of men and women this way:
<pieChart>
<chart>
...
</chart>
<pieDataset>
<dataset>
...
</dataset>
<keyExpression><![CDATA[com.company.package.ReportLocalizer.getLocalizedString($P{REPORT_LOCALE}, $F{gender}.toString())]]></keyExpression>
<valueExpression><![CDATA[$F{amount}]]></valueExpression>
<labelExpression><![CDATA[String.valueOf($F{amount})]]></labelExpression>
</pieDataset>
<piePlot>
...
</piePlot>
</pieChart>

Add sub report to main report in JasperReport

I have a Report that shows its items as it should.
Now i need to add 3 sub reports to the main one.
For this i used iReports.
My question is, how do i pass the list of items to the sub reports ???
I found some tutorials but in those cases the sub reports's data is inside of the first reports.
In my case, i have a report with 4 detail band, 3 of this with a sub report.
My code:
JRBeanCollectionDataSource beanCollection = new JRBeanCollectionDataSource(report.getList());
jasperPrint = JasperFillManager.fillReport("C:\\Users\\Desktop\\Report.jasper", new HashMap(), beanCollection) ;
HttpServletResponse http = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
http.addHeader("Content-disposition", "attachment; filename=report.pdf");
ServletOutputStream stream = http.getOutputStream();
JasperExportManager.exportReportToPdfStream(jasperPrint, stream);
I am using jsf 2.0.
ps: i saw i can create 4 jasperPrint, and add each page to the main report but i would like to know if its possible what i looking for.
Regards.
Suppose your datasource has the following structure
public class MyPOJO{
private List<POJOSubData1> sub1;
private List<POJOSubData2> sub2;
private List<POJOSubData3> sub3;
//getters & setters
...
}
You can pass the desired datasource to your subreports by specifying the following to the subreport's properties
Connection type : Use a datasource expression
Data Source Expression : $F{sub1} //or sub2 or sub3
The Field Class of sub1 should be List.
Also, you dont need to put isolate 1 subreport to 1 detail band, you can put all the subreports in one detail band.
If you are using Ireport to create reports then open main report in Ireport and selelect Subreport and go to property section of the report, click on "Parameters" property and click on "Copy from master" tab, from there you can select the parameters which you want to pass to the sun report.
what i finally did :
for(JRPrintPage p : jasperPrint1.getPages()){
jasperPrint.addPage(p);
}
where jasperPrint is the main report and jasperPrint1 is the subreport(that is a report it self). On this way, i can pass the list of items to all the reports normally.
JRBeanCollectionDataSource beanCollection1 = new JRBeanCollectionDataSource(report.getSections().get(1).getRowsReports());
JasperPrint jasperPrint1 = JasperFillManager.fillReport("C:\\Users\\Desktop\\Meeting.jasper", new HashMap(), beanCollection1) ;

How Vector<Vector> Receipt used as JasperReports DataSource with Java?

I have a Vector receiptOrder and I want to print that receipt to thermal printer using iReport.
I get 3 classes:
MyDataSource which implements JRDataSource
MyDataSourceProvider which implements JRDataSourceProvider
JRBasicField which implements JRField
How I can setup the JasperReports datasource with that vector?
i made this changes .
load jrxml file
JasperDesign jasperDesign = JRXmlLoader.load("c:/PrintReceipt.jrxml");
JasperReport jr = JasperCompileManager.compileReport(jasperDesign);
pass orderRows which is Vector<Vector> to MyDataSourceProvider then Create datasource
JRDataSourceProvider dsp = new MyDataSourceProvider(orderRows);
JRDataSource dataSource = dsp.create(jr);
JasperPrint jasperPrint = JasperFillManager.fillReport(jr, new HashMap(), dataSource);
JasperExportManager.exportReportToPdfFile(jasperPrint, "c:/test.pdf");
JasperViewer.viewReport(jasperPrint);
but JasperReport show me empty view and also test.pdf is empty .
any idea ...?
I fixed my problem .
The problem because of MyDataSourceProvider fields didn't match ireport.jrxml fields
After i fix that problem All thing work good .

Showing an image after loading it from sql database

I have a problem showing the image form database in a view Details and a ImageController. Inside the view I have:
img src=Url.Action("GetFile","Image", new {id= Model.id})
and in controller:
public FileContentResult GetFile(int idl)
{
//int idl = 32;
SqlDataReader rdr; byte[] fileContent = null;
...........
return File(,,);
}
When the view is called, function GetFile it just won't work. But if I cut out the parameter int id1 and I instantiate it as a variable it does work.
public FileContentResult GetFile()
{
int idl = 32;
SqlDataReader rdr; byte[] fileContent = null;
...........
return File(,,);
}
Why?
I think your id value isn't getting bound because your action parameter is idl and the databinder doesn't have a route value to bind to the idl parameter.
You see, your action parameters should match route values. I'm guessing that you are simply using the default route of {controller}/{action}/{id} - if this is the case then you need to change your GetFile action to have a parameter of id and not idl. Otherwise you need to create your own route that looks something like `Image/GetFile/{idl}.
Savvy?
HTHs,
Charles