Developing XML Publisher Report – Using Data Template as Data Source

Background: 
Developing XML Publisher Report – Using Data Template(.xml) as Data Source and Template(.rtf) as Layout.
Note that, we can use .rdf file as data source. But for this demo we are using Date XML Template.
Prerequisite for the below Example:

1. Create a table
CREATE TABLE demo_products
(  product_code   NUMBER,
   product_name   VARCHAR2 (100));

2. Insert Values
INSERT INTO demo_products
     VALUES (569, ‘Oracle Cost Management’);
3. Issue Commit

Step1: Define Data Template:

The data template is the method by which you communicate your request for data to the data engine.
The data template is an XML document that consists of four basic sections:  

  • Define parameters: In which parameters are declared in child <parameter> elements
  • Define triggers:
  • Define data query: In which the SQL queries are defined in child <sqlStatement> elements
  • Define data structure: In which the output XML structure is defined
 Create Data Template: (Save this file as XXDPDT.xml)
<?xml version=”1.0″ encoding=”UTF-8″?>
<dataTemplate name=”demoProductsDT” description=”Demo Products Details” version=”1.0″>
   <parameters>
     <parameter name=”p_product_id” datatype=”number”/>
   </parameters>
  <dataQuery>
    <sqlStatement name=”DQ”>
         <![CDATA[ SELECT product_code, product_name FROM demo_products 
                            WHERE product_code = NVL(:p_product_id,product_code) ]]>
    </sqlStatement>
  </dataQuery>
  <dataStructure>
    <group name=”G_DP” source=”DQ”>
      <element name=”PRODUCT_CODE” value=”product_code”/>
      <element name=”PRODUCT_NAME” value=”product_name”/>
    </group>
  </dataStructure>
</dataTemplate>
  • This Data Template selects the product details from the demo_products table. It uses a bind parameter to find the product name against the product code.
  • For each bind parameter in the query , we need to define a Parameter in the Concurrent Program

Step 2: Create Data Definition & Associate with Data Template
Navigation: XML Publisher Administrator -> Data Definitions -> Create Data Definition
Screen 1 : Create Data Definition
       Enter the data definition Details and click on Apply. Note down the Code. 
       The code should be used as the short name of the concurrent program.

Screen 2: Associate Data Template
       View Data Definition
Click on ‘Add File’ button to upload Data Template file that was created through step 1
Screen 3: Data Definiton
       Data Template is associated with Data Definition

Step 3: Define a Concurrent Program to generate the Data XML output. 
Note:
1. Output format should be XML
2. Short Name in the concurrent program and Code in the data definition should be same.
Screen 2: Concurrent Program – Parameters
For each parameter in the Data Template, define a parameter in the concurrent program.
The Data Template parameter name should match the concurrent program parameter token
Note:
Token is p_product_id. This is the bind parameter we have used in date template. For every bind parameter used in the data template, we have to define parameter in the concurrent program.
Screen 3: Associate the Concurrent Program to a request group.
Screen 4: Execute the concurrent program “Product Demo Report”and click on the output button get the Data XML. Save the XML file. We will use it to generate the RTF Template.
Screen 5: Concurrent Program Output
Note:
We are getting the output in xml because we didn’t define template & associated yet.

Step 4: Define the RTF Template using the Generated Data XML

Pre-requisite : Install XML Publisher Desktop
After installation following Menus & Toolbars gets added to the MS Word.

Load XML Data generated by Concurrent Program
Data -> Load XML Data
Message after loading the data
Using the Table Wizard as below to create the ‘Table Report Format’ with the columns of demo_products.

Final Output layout look like this.

Save this file with .rtf extension
Step 5: Registering the Template with BI Publisher
Navigation: XML Publisher Administrator -> Templates -> Create Template

Step 6: Run the concurrent program to see the output

Note:

As already mentioned output format can be anything. Here it is pdf. We can select format that we want at the runtime.

References:
http://www.oracle.com/technology/products/xml-publisher/index.htm
http://www.oracle.com/technetwork/middleware/bi-publisher/overview/index.html
http://xdo.us.oracle.com
Oracle® XML Publisher Administration and Developer’s Guide

FAQ:
What is XDODTEXE used in the Executable section of Concurrent Program?

XDODTEXE is a BI Publisher Data Template Executable. The purpose of this executable is to identify data template file (.xml) and execute the data template to generate the raw xml data, that later can be used by BI Publisher formatting engine to format as as per the layout (RTF, PDF etc).
This executable will be used by all the BI Publisher reports (Concurrent Program) which are using Data Template to generate the xml data. 

Here I am trying to describe R12 SLA(Sub Ledger Accounting) Procedure.
1) All accounting performed before transfer to the GL. Accounting data generated and stored in “Accounting Events” tables prior to transfer to GL


2) Run “Create Accounting” to populate accounting events (SLA) tables. User can “View Accounting” only after “Create Accounting” is run. Create Accounting process


– Applies accounting rules

 Loads SLA tables, GL tables
 Creates detailed data per accounting rules, stores in SLA “distribution links” table

3) Below are the key tables for SLA in R12


XLA_AE_HEADERS xah

XLA_AE_LINES xal


XLA_TRANSACTION_ENTITIES xte


XLA_DISTRIBUTION_LINKS xdl


GL_IMPORT_REFERENCES gir


Below are the possible joins between these XLA Tables

xah.ae_header_id = xal.ae_header_id



xah.application_id = xal.application_id


xal.application_id = xte.application_id


xte.application_id = xdl.application_id


xah.entity_id = xte.entity_id


xah.ae_header_id = xdl.ae_header_id


xah.event_id = xdl.event_id


xal.gl_sl_link_id = gir.gl_sl_link_id


xal.gl_sl_link_table = gir.gl_sl_link_table


xah.application_id = (Different value based on Module)



xte.entity_code =

‘TRANSACTIONS’ or


‘RECEIPTS’ or


‘ADJUSTMENTS’ or


‘PURCHASE_ORDER’ or


‘AP_INVOICES’ or


‘AP_PAYMENTS’ or


‘MTL_ACCOUNTING_EVENTS’ or


‘WIP_ACCOUNTING_EVENTS’



xte.source_id_int_1 =


‘INVOICE_ID’ or


‘CHECK_ID’ or


‘TRX_NUMBER’


XLA_DISTRIBUTION_LINKS table join based on Source Distribution Types

xdl.source_distribution_type = ‘AP_PMT_DIST’


and xdl.source_distribution_id_num_1 = AP_PAYMENT_HIST_DISTS.payment_hist_dist_id


—————


xdl.source_distribution_type = ‘AP_INV_DIST’


and xdl.source_distribution_id_num_1 = AP_INVOICE_DISTRIBUTIONS_ALL.invoice_distribution_id


—————


xdl.source_distribution_type = ‘AR_DISTRIBUTIONS_ALL’


and xdl.source_distribution_id_num_1 = AR_DISTRIBUTIONS_ALL.line_id


and AR_DISTRIBUTIONS_ALL.source_id = AR_RECEIVABLE_APPLICATIONS_ALL.receivable_application_id


—————


xdl.source_distribution_type = ‘RA_CUST_TRX_LINE_GL_DIST_ALL’


and xdl.source_distribution_id_num_1 = RA_CUST_TRX_LINE_GL_DIST_ALL.cust_trx_line_gl_dist_id


—————


xdl.source_distribution_type = ‘MTL_TRANSACTION_ACCOUNTS’


and xdl.source_distribution_id_num_1 = MTL_TRANSACTION_ACCOUNTS.inv_sub_ledger_id


—————


xdl.source_distribution_type = ‘WIP_TRANSACTION_ACCOUNTS’


and xdl.source_distribution_id_num_1 = WIP_TRANSACTION_ACCOUNTS.wip_sub_ledger_id


—————


xdl.source_distribution_type = ‘RCV_RECEIVING_SUB_LEDGER’


and xdl.source_distribution_id_num_1 = RCV_RECEIVING_SUB_LEDGER.rcv_sub_ledger_id.

Hope this will help you.
FND MESSAGES:
Here i am Explaining  how to create Fnd Messages  via  E-Business suite  and the implementation of message retrieval via the pl/sql API package provided with Oracle Applications.
Creating an Oracle E-Business Suite Message

To create a message in the E-Business suite message library you will need the “Application Developer” responsibility.
Navigate to Application Developer > Application > Messages. This will launch a form

Enter a unique name for your message
Eg: XX_CUSTOMER_MSG
Select the language that your message is written in and the application that the message belongs
Enter the message text in the “Current Message Text” box.
Eg: This is my first message
Click the save icon.

Retrieving a message using PL/SQL:

In order to retrieve the message from the database we need to use a standard API’s in the FND_MESSAGE package.
An E-Business suite message should be retrieved as follows:
1. Clear the current session of any message variables that may already be set
2. Tell E-Business suite which message you wish to retrieve
3. Retrieve the actual message string
4. Clear the session (Optional)

Below is the PL/SQL Block to retrive the message

DECLARE
 my_message VARCHAR2(100);

 BEGIN

  –Initialize Apps Session
  fnd_global.apps_initialize( user_id      => 1234
                             ,resp_id      => 1235
                             ,resp_appl_id => 1236
                           );
                         
  /*–Note: You will get the uer_id, resp_id and Resp_appl_id using below Query
    select fnd.user_id ,
         fresp.responsibility_id,
         fresp.application_id
  from   fnd_user fnd,
         fnd_responsibility_tl fresp
  where  fnd.user_name = ‘OEAG’
  and    fresp.responsibility_name = ‘Custom HRMS Responsibility‘;
  */
 
  –Clear the existing session
  FND_MESSAGE.CLEAR;

  –Tell e business suite which message you want (custom application short name/message name) 
  FND_MESSAGE.SET_NAME(‘XXERP’,’XX_CUSTOMER_MSG’);

  –Retrieve the message
  my_message := FND_MESSAGE.GET;  

  –Output the message
  DBMS_OUTPUT.PUT_LINE(my_message);
 END;

Output for the Above Block Is : This is my first message

Using Tokens in the message:

The Oracle E-Business suite allows the substitution of tokens within a message string to enable the programmer to add dynamic content to the message at run time.
Open the E-Business Suite message create a New Message
Navigate to Application Developer > Application > Messages. This will launch a form

Enter a unique name for your message
Eg: XX_UNAME_TOKEN_MSG
Select the language that your message is written in and the application that the message belongs
Enter the message text in the “Current Message Text” box.
Eg: This is my second message and the Token User name is &USERNAME
Click the save icon.
Note: In order to insert a token into a message it is necessary to prefix the token with a ampersand e.g. &USERNAME
Retrieving message With Token Substitution
Here USERNAME is called as TOKEN, we will Add the value dynamically

Example Block  is below:

DECLARE
 my_message VARCHAR2(100);
BEGIN
 –Initialize Apps Session
 fnd_global.apps_initialize( user_id      => 1234
                            ,resp_id      => 1235
                            ,resp_appl_id => 1236
                           );
                         
  /*–Note: You will get the uer_id, resp_id and Resp_appl_id using below Query
    select fnd.user_id ,
         fresp.responsibility_id,
         fresp.application_id
  from   fnd_user fnd,
         fnd_responsibility_tl fresp
  where  fnd.user_name = ‘OEAG’
  and    fresp.responsibility_name = ‘Custom HRMS Responsibility’;
  */
 
 –Clear the existing session
 FND_MESSAGE.CLEAR;

 –Tell e business suite which message you want (Application short name/message name)
 FND_MESSAGE.SET_NAME(‘XXERP‘,’XX_UNAME_TOKEN_MSG’);

  –Set the username message token with the current applications user
 FND_MESSAGE.SET_TOKEN(‘USERNAME’,FND_GLOBAL.USER_NAME);

 –Retrieve the message
 my_message := FND_MESSAGE.GET;

 –Output the message
 DBMS_OUTPUT.PUT_LINE(my_message);
END;

Out put for above block is : 

This is my second message and the Token User name is IAMKRISHNA

Downloading and Uploading Messages using the Generic Loader

To download our example message we would use the following command at the Unix prompt on the mid-tier:
 
FNDLOAD apps/apps 0 Y DOWNLOAD $FND_TOP/patch/115/import/afmdmsg.lct XX_UNAME_TOKEN_MSG.ldt
FND_NEW_MESSAGES APPLICATION_SHORT_NAME=’PER’ MESSAGE_NAME=”XX_UNAME_TOKEN_MSG”

To Upload our example message we would use the following command at the Unix prompt on the mid-tier:

FNDLOAD apps/apps 0 Y UPLOAD $FND_TOP/patch/115/import/afmdmsg.lct XX_UNAME_TOKEN_MSG.ldt

CURSOR : A cursors is a pointer used to fetch rows from a result set 
Two types of classification s:


I.STATIC CURSOR S: 
Static : Normal cursor (implicit or explicit)

Cursor attributes  for implicit and explicit:

%FOUND – records fetched successfully
%NOTFOUND – no records fetched
%ROWCOUNT – Number of records fetched
%ISOPEN – returns TRUE if cursor is open

a. Implicit : 
Cannot be opened outside the statement
More fast and less coding effort.
Will never raise INVALID_CURSOR error
Raises NO_DATA_FOUND and TOO_MANY_ROWS exceptions (eg: select <stmt>)

Example Implicit Cursor:

select * from emp

If SQL%FOUND then

v_count:= SQL%ROWCOUNT

end if;


b. Explicit : 2 network round trips. Store data first then retrieve data. 
More programmatic control.
Programmer could open; fetch data, close, check attributes etc.

Syntax:
open c1; — cursor c1 is select <stmt>

fetch <>

exit when c1%NOTFOUND

Example Explicit cursor:

Without Using Loop s
Declare

Cursor cur1 is

select ename,empno,sal from emp

where sal<50000 and deptno=50

begin

open cur1;

fetch cur1 into v_ename,v_empno,v_sal;

exit when cur1%notfound;

—<do processing>

close cur1;

end;

Using Loops:

Declare

Cursor cur1 is

select ename,empno,sal from emp

where sal<50000 and deptno=50

begin

For rec in cur1
loop
dbms_output.put_line(‘Employee Number ‘||rec.empno);
end loop;

end;

Using Loops with Cursor Parameters:

Declare

Cursor cur1( cp_deptNo Number) 
is
select ename,empno,sal from emp
where sal<50000 and deptno=cp_deptNo

l_deptNo Number :=50;
begin

For rec in cur1(l_deptNo)
loop
dbms_output.put_line(‘Employee Number ‘||rec.empno);
end loop;

end;

II. DYNAMIC CURSOR s : 

Oracle REF CURSOR Types:
With the REF_CURSOR you can return a recordset/cursor from a stored procedure
(i.e Ref Cursors can have Record/s as return types.)
Could be declared once and defined many times in different procedures. 

a)Strong : For the strong ref cursor the returning columns with data type and length need to be known at compile time.
b)Weak :For the weak ref cursor the structure does not need to be known at compile time.

Example For the Ref Cursor :


–SPECK PACKAGE 
CREATE OR REPLACE PACKAGE REFCURSOR_PKG
 AS
  TYPE WEAK_REF_CURSOR IS REF CURSOR; — Until 9i
  TYPE STRONG_REF_CURSOR IS REF CURSOR RETURN EMP%ROWTYPE;

END REFCURSOR_PKG;

The pl/sql procedure that returns a ref-cursor looks like this:


–BODY PACKAGE 
CREATE OR REPLACE PACKAGE BODY REFCURSOR_PKG
AS
— For Weak Ref Cursor: 
PROCEDURE 
WEAK_REF_CUR_PRC( p_deptno IN number,
                  p_cursor OUT REFCURSOR_PKG.WEAK_REF_CURSOR — Until 9i
                  —- From 9i (p_cursor OUT SYS_REFCURSOR )—-
                 )
IS

BEGIN

  OPEN p_cursor FOR
  SELECT *  FROM   emp
  WHERE  deptno = p_deptno;
end WEAK_REF_CUR_PRC;

— For Strong Ref Cursor: 
PROCEDURE 
STRONG_REF_CUR_PRC( p_deptno IN number,
                    p_cursor OUT REFCURSOR_PKG.STRONG_REF_CURSOR
                  )
IS

BEGIN
  SELECT *  FROM   emp
  WHERE  deptno = p_deptno;
  end STRONG_REF_CUR_PRC;
 END REFCURSOR_PKG;
We usually use cursor for loops to process data.(i.e declare a cursor, open it, fetch from it row by row in a loop and process the row they fetch) statements in plsql programs causes a context switch between the plsql engine and the sql engine.Too many context switches may degrade performance dramatically.

In order to reduce the number of these context switches we can use bulk collecting feature
Bulk collecting lets us to transfer rows between the sql engine and the plsql engine as collections.
Bulk collecting is available for select, insert, delete and update statements.

Below are some examples:

create table BULK_COLLECT_TEST as select * from PER_ALL_PEOPLE_F;

Table created.

insert into BULK_COLLECT_TEST

select * from BULK_COLLECT_TEST;

20000 rows created.

–BLOCK1:Using Loops
declare
 cursor c1
 is select object_name from BULK_COLLECT_TEST;
 rec1 c1%rowtype;
 begin
      open c1;
       loop
       fetch c1 into rec1;
    exit when c1%notfound;
    null;
    end loop;
 end;

total Elapsed Time is : 45 Secs

–BLOCK2: Using Bulk Collecting
declare
  cursor c1 is select object_name from BULK_COLLECT_TEST;
  type c1_type is table of c1%rowtype;
  rec1 c1_type;
begin
open c1;
   fetch c1 bulk collect into rec1;
end;

total Elapsed Time is : 5 Sec

So bulk collecting the rows shows a huge performance improvement over fetching row by row.

Some cases there are many rows to process, we can limit the number of rows to bulk collect, process those rows and fetch again.
Otherwise process memory gets bigger and bigger as you fetch the rows.

–Bulk Collect Example using LIMIT :
declare
 cursor c1 is select object_name from BULK_COLLECT_TEST;
 type c1_type is  table of c1%rowtype;
 rec1 c1_type;
begin
    open c1;
    loop
    fetch c1 bulk collect into rec1 limit 200;
    for i in 1..rec1.count loop
    null;
    end loop;
    exit when c1%notfound;
    end loop;
end;