Tuesday, August 25, 2009

Integrating BI Publisher into Jdeveloper & ADF

BI Publisher API can be integrated with your ADF faces application to generate professional quality reports. This document assumes that you have access to Oracle BI Publisher and
have the necessary license to use it.

Advantage of BI Publisher is that you can create the Report Layout Template using Microsoft Word.

Creating an RTF template document for BI Publisher is out of scope of this document.

The basic needs for generating report with BI Publisher

  1. Data in XML Format
  2. RTF Template

BI Publisher API can be used to convert the RTF template into an XSL file which will finally be used by the BI Publisher API for generating the final output

The example uses the sample schema SCOTT and tables DEPT and EMP

Create a new Fusion Web Application (ADF)

Run the 'Business Components from Tables' wizard, select tables DEPT and EMP and generate the view objects for both. In my sample application I generated only read only
view objects.

Edit DeptView and the navigate to Java Options and select the check box for Generate ViewObject Class

Open DeptViewImpl.java and add the following method.







public void saveAsXMLFile(String fileName) {

Node n = writeXML(-1,XMLInterface.XML_OPT_ALL_ROWS);

java.io.File file = new java.io.File(fileName);

PrintWriter output = null;

try {

output = new java.io.PrintWriter(file);

((XMLNode)n).print(output);

} catch (IOException e) {

System.out.println(e.getMessage());

} catch (Exception e) {

System.out.println(e.getMessage());

} finally {

if (output != null) {

try {

output.close();

} catch (Exception e) {

System.out.println(e.getMessage());

}

}

}

}


I have taken the idea for above from the site http://kohlivikram.blogspot.com/2009_04_01_archive.html

Many thanks for the sample scripts provided there.

The above method will generate the XML as shown below







<DeptView>

<DeptViewRow>

<Deptno>10</Deptno>

<Dname>ACCOUNTING</Dname>

<Loc>NEW YORK</Loc>

<EmpView>

<EmpViewRow>

<Empno>7782</Empno>

<Ename>CLARK</Ename>

<Job>MANAGER</Job>

<Mgr>7839</Mgr>

<Hiredate>1981-06-09</Hiredate>

<Sal>2450</Sal>

<Deptno>10</Deptno>

</EmpViewRow>

<EmpViewRow>

<Empno>7934</Empno>

<Ename>MILLER</Ename>

<Job>CLERK</Job>

<Mgr>7782</Mgr>

<Hiredate>1982-01-23</Hiredate>

<Sal>1300</Sal>

<Deptno>10</Deptno>

</EmpViewRow>

</EmpView>

</DeptViewRow>

</DeptView>


This finishes our first requirement of generating XML data

Open the Project Properties for the ViewController project and add the below libraries

Collections.jar

I18nAPI_v3.jar

Versioninfo.jar

Xdocore.jar

Xmlparserv2-904.jar

Create a new jspx page and insert a Panel Group Layout component into the page.

Insert a Button component inside the Panel Group Layout

Insert a File Download Action Listener component inside the Button and set the
below properties

Content Type: application/pdf
File Name: deptemp.pdf

Click the Edit menu for the Method property and create a new managed bean named
ADFBI

Add a new method for the above bean named generateReport and click OK

Jdeveloper will generate a new java file named ADFBI.java, open this file for editing
and modify it as shown below








package com.rkc.view;

import com.rkc.model.DeptViewImpl;

import java.io.IOException;

import java.io.OutputStream;

import javax.faces.context.FacesContext;

import oracle.adf.model.BindingContext;

import oracle.apps.xdo.XDOException;

import oracle.apps.xdo.template.FOProcessor;

import oracle.apps.xdo.template.RTFProcessor;

import oracle.binding.BindingContainer;

public class ADFBI {

public ADFBI() {

}

public void generateReport(FacesContext facesContext,

OutputStream outputStream) {

DeptViewImpl dept =
(DeptViewImpl)BindingContext.getCurrent().getDefaultDataControl().getApplicationModule().findViewObject("DeptView1");

dept.saveAsXMLFile("c:/temp/deptemp.xml");

try {

RTFProcessor rtfProcessor = new RTFProcessor("c:/temp/deptemptmpl.rtf");

rtfProcessor.setOutput("c:/temp/deptemp.xsl");

rtfProcessor.process();

FOProcessor processor = new FOProcessor();

processor.setData("c:/temp/deptemp.xml");

processor.setTemplate("c:/temp/deptemp.xsl");

processor.setOutput(outputStream);

processor.setOutputFormat(FOProcessor.FORMAT_PDF);

processor.generate();

} catch (IOException e) {

System.out.println("IOException " + e.getMessage());

} catch (XDOException e) {

System.out.println("XDOException" + e.getMessage());

}

}

}



In order to the above code to you need to do the below steps

Right click the jspx page and then select Go to Page Definition menu. If the Page Definition dose not exist it will be created and opened. Now you have to add an iterator under the executables section. I created DeptView1Iterator. This will ensure that the methods in BindingContext will not throw any NullPointerException

Also the deptemptmpl.rtf file must exist in the folder named C:\temp

Now run the application and click the Generate Report button to get your output
in a PDF file.

Sample application can be downloaded from here