- JdevGuru - http://jdevguru.com -
Using Jasper Reports in JDeveloper
Posted By burton On August 30, 2006 @ 6:33 pm In JSF / ADF | 4 Comments
I often see the question in the JDEV forums “How can I access Jasper Reports in Jdeveloper?”. Jasper reports is a great tool for reporting, it’s written in 100% java and its free to use. If you dont want to be burdened with handcoding your reports, I sugguest using IReport, which like Jasper reports its written in java and free.
Below is a sample of code that I use in my application to call a jasper report and create a PDF or RTF file for output to the browser. The parts in Bold are the actual parts needed to do the reporting. The rest of the code is actually a system I setup to be able to have dynamic reporting with the user selecting the parameters from views.
public void PrintReport()
{
//load the parameters
ReportViewImpl myReportView = getReportView();
PatchViewImpl myPatchView = getPatchView1();
RequestViewImpl myRequestView = getRequestView();PatchViewRowImpl currentPatchRow = (PatchViewRowImpl)myPatchView.getCurrentRow();
RequestViewRowImpl currentRequestRow = (RequestViewRowImpl)myRequestView.getCurrentRow();
Row currentReportRow = myReportView.getCurrentRow();String ReportName = (String) currentReportRow.getAttribute(”Filename”);
String ReportPara = (String) currentReportRow.getAttribute(”ParameterName”);
String parameter = “”;if (ReportPara.compareTo(”PATCH_ID”)==0) parameter = currentPatchRow.getPatchId().getSequenceNumber().toString();
if (ReportPara.compareTo(”REQUEST_ID”)==0) parameter = currentRequestRow.getRequestId().getSequenceNumber().toString();
if (ReportPara.compareTo(”NONE”)==0) parameter = “0″;
//if (ReportPara.compareTo(”TICKET_ID”)==0) parameter = currentTicketRow.getTicketId().getSequenceNumber().toString();Map parameters = new HashMap();
parameters.put(ReportPara, new Integer(parameter));
try
{
String UserName = getApplicationUserName();
File inFileName = new File (”applications/reports/”+ReportName+”.jasper”);
File outPutPDF = new File (”applications/reports/output/”+UserName+”_temp.pdf”);
File outPutRTF = new File (”applications/reports/output/”+UserName+”_temp.rtf”);JasperPrint jasperPrint = JasperFillManager.fillReport(inFileName.getPath(), parameters, getCurrentConnection());
JasperExportManager.exportReportToPdfFile(jasperPrint, outPutPDF.getPath());
}
catch (JRException e) {e.printStackTrace();}
catch (Exception e) {e.printStackTrace();}
}
As you can see there is not much work to do in order to call a report. One of the problems that I had, was determining where to put my report files, using OC4J 10.2.1. I created a directory under the j2eeapplications directory on my server called reports. This allows me to copy my files out there without having to use a deployment file, or rebooting the server, it also allows my testing and production boxes to access the same reports.
The next step is calling the fill manager for Jasper, this takes your report and populates it with data. There are many different ways to pass the fill manager a connection to the database, I use a routine that I found on the JDEV forums for accessing the current connection in my Application Module. The getCurrentConnection() is a function in my base application module class:
public Connection getCurrentConnection() {
Statement st = null;
try {
st = getDBTransaction().createStatement(0);
return st.getConnection();
}
catch (SQLException s) {
s.printStackTrace();
return null;
}
finally {
if (st != null) try { st.close(); } catch (SQLException s2) {}
}
}
Using this function I am able to use the same database connection as my application module. After the report is filled you call the Jasper Export routine that matches the output that you want to create.
This is not the only way to work a jasper report into your application, I have also seen it done coding it in the view layer of an MVC application. I prefer to place my routines that access data into my Application Modules. This way if I need to change my front end, I do not have to rewrite as much code. (Which I am currently doing converting my application from UIX to JSF).
Please post your comments on this article good or bad.
Till next time…….
Kelly Burton
Article printed from JdevGuru: http://jdevguru.com
URL to article: http://jdevguru.com/?p=30
Click here to print.