You are currently browsing the JdevGuru weblog archives for August, 2006.
August 30, 2006 by burton.
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
Posted in JSF / ADF | Print | 4 Comments »
August 24, 2006 by burton.
Working with JDEV 10.13 and JSF is a lot better than the old version and UIX. One of the new features that I have found useful is the ShowOneTab and ShowDetailItem components of ADF Faces. One area that that I could not find built into the ShowDetailItem was the ability to have it automatically store the state of what detail item was being shown. Any time you navigated off the page and came back it would reset to the default first detail item. This article will show you a way to store the state and make the pages sticky.
7. Open your new class and enter your code for storing and getting the page state:
package demo.view;
public class pageStatus
{
private String _stickyActiveTab;
public pageStatus()
{
_stickyActiveTab = “”;
}
public String getStickyActiveTab()
{
return (String) _stickyActiveTab;
}
public void setStickyActiveTab(String Value)
{
_stickyActiveTab =Value;
}
}
8. Select the first ShowDetailItem in the structure panel, right click and select Create method binding for disclosurelistener:
9. In the disclosure listener you place your code to set the bean value for the page. There are probably better ways to handle this then the way I worked it. However, I needed to get this done in a hurry and my way is working just fine in my app. For those of you that have a better method please drop me a note. In face using the disclosure event date would probably be the best way to work it.
public void showDetailItem1_disclosureListener(DisclosureEvent disclosureEvent)
{
if (this.showDetailItem1.isDisclosed() )
setManagedBeanValue(”pageStatus.stickyActiveTab”, “TAB1″);
if (this.showDetailItem2.isDisclosed() )
setManagedBeanValue(”pageStatus.stickyActiveTab”, “TAB2″);
}
10. The setManagedBeanValue is from the SRDEMO that the oracle guys put together. You can place that file in your project or use the snippets from this article.
public static void setExpressionValue(String expression,
Object newValue) {
FacesContext ctx = FacesContext.getCurrentInstance();
Application app = ctx.getApplication();
ValueBinding bind = app.createValueBinding(expression);
Class bindClass = bind.getType(ctx);
if (bindClass.isPrimitive() || bindClass.isInstance(newValue)) {
bind.setValue(ctx, newValue);
}
}
public static void setManagedBeanValue(String beanName,
Object newValue) {
StringBuffer buff = new StringBuffer(”#{”);
buff.append(beanName);
buff.append(”}”);
setExpressionValue(buff.toString(), newValue);
}
Figure 1 - Selected Second Tab then moved to next Page
Figure 2 - On Second page
Figure 3 - Navigate back to the first page and our tab is still set.
Posted in JSF / ADF | Print | No Comments »
August 20, 2006 by burton.
Guru is really not what I am for JDEV matters. I barely have two years under my belt using JDEV, but in those two years I have learned quite a bit. This page is dedicated to providing things I find useful in JDEV on to others.
My first demo post will be for retaining the state of JSF pages with multiple tabs.
Look for it soon.
Kelly
Posted in JSF / ADF | Print | 2 Comments »