Monday, May 12, 2008

Controller class extension in Oracle Applications

A Controller .class (CO) file is the Java code that controls the user interface on a Self-Service/OA Framework page within Oracle Applications. Events such as button clicks and other user activities are handled via the CO.

To demonstrate how to perform a simple Controller .class extension, we will show a Create Vacancy transaction from iRecruitment. It just so happens most of the JDeveloper extensions requested are in the iRecruitment module. In this example, we are going to default the posting Date on step 4 to a date 365 days in the future.

The reasoning behind this extension is to prevent or delay the job posting from being pushed to the Job Sites. There is always at least one root CO per page but there can be any number of additional COs. If there is much disparate data on the page, it is likely there are multiple COs. Click "About this Page" to find out which CO needs to be extended:


Scroll down further on the "About this Page" and click the "+" sign next to the Business Component Reference Details. Scroll down until you find the Controllers:


The Controller oracle.apps.per.irc.vacancy.webui.VacNewPostingPageCO is located on the application tier at:

$JAVA_TOP/oracle/apps/per/irc/vacancy/webui/VacNewPostingPageCO.class

Unless you are curious about what is in the .class file, you don't need to view the file. An extension of this file is being performed, so any code will be in addition to what Oracle has written in this file. To complete a CO extension, a new Oracle Applications Workspace and project will be created in JDeveloper.

The following screen prints will step through this procedure:


Use the package name of the Controller class, adding a prefix of your custom application name. As always, I use 'hack' for my custom package:


Choose a .dbc file and user for the Runtime Connection. If you are uncertain of how to create a connection, see the steps outlined in the VO Extension how-to. Continue until you reach the last page of the wizard and click Finish. We will take a short detour here in order to import the VacNewPostingPG into JDeveloper. If you are adept at JDeveloper already, it is not necessary to import the .xml page's layout before creating the Controller .class. However, these steps will allow you to point-and-click and let JDeveloper write some Java code for you so you don't have to. Letting Oracle create the code will allow you to maximize your role as a hack as well. The page layout is stored in the database and can be retrieved by running the following code:


set feedback off
set serveroutput on format wrapped
set linesize 100
spool hack.lst
--
execute jdr_utils.printDocument('/oracle/apps/per/irc/vacancy/webui/VacNewPostingPG',100);
--
spool off


This will spool the VacNewPostingPG page to a flat file, which can be saved and imported into JDeveloper.



Save the page's .xml to C:\JDEVOAF\jdevhome\jdev\myprojects\oracle\apps\per\irc\vacancy\webui\VacNewPostingPG.xml where "JDEVOAF" is the directory where you installed JDeveloper:

Do a File -> Import to bring it into JDeveloper:




Browse to the directory where you saved it:


Right click the IRC_VACANCY_NEW_POSTING_PAGE and click Set New Controller...



There are two required methods in an OA Framework controller .class:
  1. ProcessRequest - This method is called when the page loads. Any events you want to occur when a user first visits the page should be put here.
  2. ProcessFormRequest - This method is called when the Form is submitted. If you want to override any data inputted by the user or add additional business rules you would add code here to change the behavior of the transaction prior to it being submitted.
It is best practice not to access the View Objects (VOs) directly in the Controller .class as shown below. This was done to demonstrate a basic CO extension. Heavier business logic should be placed in the Application Module (AM) versus the CO. Further row-level validation and code can be pushed down to the Java .class files associated with the View Objects. The super.processRequest call is what makes this an "extension". That is, our code extends the functionality of Oracle's delivered application by being done after it completes.

Your code should look as follows:


/*===========================================================================+
| Copyright (c) 2001, 2005 Oracle Corporation, Redwood Shores, CA, USA |
| All rights reserved. |
+===========================================================================+
| HISTORY |
+===========================================================================*/
package hack.oracle.apps.per.irc.vacancy.webui;
import oracle.apps.fnd.common.VersionInfo;
import oracle.apps.fnd.framework.webui.OAControllerImpl;
import oracle.apps.fnd.framework.webui.OAPageContext;
import oracle.apps.fnd.framework.webui.beans.OAWebBean;
import oracle.apps.fnd.framework.OAApplicationModule;
import oracle.apps.fnd.framework.server.OADBTransaction;
import oracle.apps.fnd.framework.OAViewObject;
import oracle.jbo.Row;
import oracle.jbo.domain.Date;
/**
* Controller for ...
*/
public class hackVacNewPostingPageCO extends OAControllerImpl
{
public static final String RCS_ID="$Header$";
public static final boolean RCS_ID_RECORDED =
VersionInfo.recordClassVersion(RCS_ID, "%packagename%");
/**
* Layout and page setup logic for a region.
* @param pageContext the current OA page context
* @param webBean the web bean corresponding to the region
*/
public void assignFutureDate(OAPageContext pageContext, OAWebBean webBean)
{
OAApplicationModule oaapplicationmodule = pageContext.getApplicationModule(webBean);
OADBTransaction dbtrans = oaapplicationmodule.getOADBTransaction();
Date currentDate = dbtrans.getCurrentDBDate();
Date currentDatePlus365 = new Date(currentDate.addJulianDays(365, 0));
OAViewObject vo = (OAViewObject)oaapplicationmodule.findViewObject("IrcEditRecruitmentActivitiesVO");
for(Row row = vo.first(); row != null; row = vo.next())
{
row.setAttribute("DateStart", currentDatePlus365);
}
}
public void processRequest(OAPageContext pageContext, OAWebBean webBean)
{
// this next line forces Oracle to fire the delivered code
super.processRequest(pageContext, webBean);
// default a date 365 days in the future to the external site
assignFutureDate(pageContext, webBean);
}

/**
* Procedure to handle form submissions for form elements in
* a region.
* @param pageContext the current OA page context
* @param webBean the web bean corresponding to the region
*/
public void processFormRequest(OAPageContext pageContext, OAWebBean webBean)
{
super.processFormRequest(pageContext, webBean);
}

}


Make and rebuild your project:


The compiled files are now available in C:\JDEVOAF\jdevhome\jdev\myclasses\hack\oracle\apps\per\irc\vacancy\webui. Zip this and FTP it to $JAVA_TOP. Unzip the files to create $JAVA_TOP/hack/oracle/apps/per/irc/vacancy/webui and then bounce Apache. In order for Oracle to use the new Controller, the page must be personalized to point at the new CO. Browse to the Vacancy New Posting Page and click Personalize Page in the upper right-hand corner:


Add the string hack.oracle.apps.per.irc.vacancy.webui.hackVacNewPostingPageCO to the Responsibility level personalization:


Marvel at your new defaulted Job Posting Site fields:


To prevent the users from overriding the date, the assignFutureDate method can be copied to the ProcessFormRequest. This would cause the dates to default again when the form is submitted, thereby overriding whatever the user inputs.

Thursday, March 27, 2008

How to Extend a View Object in OA Framework

View Objects are the queries that provide the data seen on a Self Service web page. Oracle offers many attributes in each View Object, but often an additional attribute is needed. Oracle allows developers to extend the delivered View Objects to meet this need. The following iRecruitment Vacancy Search Page will be used for demonstration, but any page developed using the OAF architecture is a candidate for this exercise.



In our example, the Posting's Job Title and Organization Name that were entered during step 4 of the Vacancy Create process are not offered in the delivered VO. These values are shown during data entry in the below screen print. We will write PL/SQL functions to retrieve them; our extended VO will then call these functions to return the values so they can be rendered on the page:



In order to find which VO to extend, the page definition must be analyzed. There is a distinct VO behind the fields shown in the results table. Only a portion of the available fields are shown by default. Click the "About this Page" link in the lower left corner to see the page definition.


Click Expand All and find the Results Table:


Click the IrcVacancyVO link to see the View Object Details:


Any attribute listed on this page can be added "declaratively", or without programming required by personalizing the results table. You're are not limited to what Oracle provides in the delivered View Objects, however, as you will soon come to know.

For our example, we will be extending the oracle.apps.per.irc.vacancy.server.IrcVacancyVO View Object. Extending a View Object involves the following steps:

1. Importing the delivered View Object into JDeveloper
2. Importing dependent classes into JDeveloper
3. Extending the View Object
4. Deploying the Extended VO to the application server
5. Add extended VO columns to the page using personalizations

To find the version of JDeveloper that's right for you, see Note:416708.1 or 787209.1 (newer) on Metalink titled "How to find the correct version of JDeveloper to use with eBusiness Suite 11i or Release 12".

Open JDeveloper and Create a new OA Workspace and project following these steps. Naturally, my workspace and project is called 'hack':


It is best practice to place extended objects in a custom package. If you are extending an object in the delivered package oracle.apps.per.irc.vacancy.server, the custom package would be hack.oracle.apps.per.irc.vacancy.server


Continue to step 3 and specify a DBC file. This file is found on the application tier at $FND_TOP/secure. It must be copied to C:\JDEVOAF\jdevbin\jdev\multi\system\oa\dbc_files\secure on your local machine, where "JDEVOAF" is the directory where you installed JDeveloper. Enter an Oracle Applications user and valid short name and responsibility key before finishing:


When you have finished marveling over your newly created project, right click it and choose "New Business Components":




If you don't have a connection to use already defined, follow these steps below to create a new one. The connection name is arbitrary:


Enter a Valid Oracle Applications user name/password:


Enter the appropriate box information:


Test and Finish:


Click Finish and return to the Navigator. The custom package where the extended VO will be created is complete, but we must import the delivered VO before continuing. These files are stored in the $JAVA_TOP directory on the application (middle) tier and must be downloaded. Since we are extending the View Object (VO) oracle.apps.per.irc.vacancy.server.IrcVacancyVO, we must import the oracle.apps.per.irc.vacancy.server pacakge and all dependent packages. In order to do this efficiently, I perform the following steps:

1. telnet to middle tier
2. cd $JAVA_TOP/oracle/apps
3. tar -cvf ota_top.tar ota
4. tar -cvf per_top.tar per
5. tar -cvf pay_top.tar pay
6. tar -cvf ben_top.tar ben ... repeat for as many modules as you might need
7. FTP each .tar to C:\JDEVOAF\jdevhome\jdev\myclasses
8. extract each .tar in the C:\JDEVOAF\jdevhome\jdev\myclasses directory

Once this task is complete, right click your project and "Import Business Components..."


and answer "Yes" when presented with the dialog "The following XML file is on the classpath.
Would you like to add these objects to your project?"


Upon importing the first package, you may see an information message telling you that additional packages are needed. In this example, import the oracle.apps.per.schema.server (server.xml) package next. Repeat this process until no additional messages appear.

If you see an Information message like the following, it means that the server.xml has defined a file not contained in the classpath directory. It can be ignored; if it really bothers you, open an SR and request that Oracle fix the server.xml file.


Be sure to close JDeveloper and re-open after importing all packages. You may see an additional warning to import more. Your project will look like the following:


We must now create a new View Object in our custom package that extends the delivered VO. A common practice is to use a PL/SQL function for the new column so that any custom logic can be maintained easily. Rather than re-implementing the VO extension, the PL/SQL can be updated anytime the business rules change.

Before proceeding in JDeveloper, lettuce first create the PL/SQL function:

Package spec:

create or replace package hack_jdev_pkg as
--
function get_vac_posting_org (p_vacancy_id in number) return varchar2;
--
function get_vac_posting_job (p_vacancy_id in number) return varchar2;
--
end hack_jdev_pkg;
/
show errors;


Package body:

create or replace package body hack_jdev_pkg as
--
cursor c_vacancy_posting_data (cp_posting_id number) is
select org_name
,job_title
from irc_posting_contents_vl ipcv
,per_vacancies pv
where pv.primary_posting_id = cp_posting_id
and pv.primary_posting_id = ipcv.posting_content_id;
--
cursor c_posting_id (cp_vacancy_id number) is
select primary_posting_id
from per_all_vacancies
where vacancy_id = cp_vacancy_id;
--
function get_vac_posting_org (p_vacancy_id in number) return varchar2 is
--
ln_posting_id number;
rec_vac_posting_data c_vacancy_posting_data%rowtype;
--
begin
--
open c_posting_id (p_vacancy_id);
fetch c_posting_id into ln_posting_id;
close c_posting_id;
--
open c_vacancy_posting_data (ln_posting_id);
fetch c_vacancy_posting_data into rec_vac_posting_data;
if c_vacancy_posting_data%notfound then
rec_vac_posting_data := null;
end if;
close c_vacancy_posting_data;
--
return rec_vac_posting_data.org_name;
--
end get_vac_posting_org;
--
function get_vac_posting_job (p_vacancy_id in number) return varchar2 is
--
ln_posting_id number;
rec_vac_posting_data c_vacancy_posting_data%rowtype;
--
begin
--
open c_posting_id (p_vacancy_id);
fetch c_posting_id into ln_posting_id;
close c_posting_id;
--
open c_vacancy_posting_data (ln_posting_id);
fetch c_vacancy_posting_data into rec_vac_posting_data;
if c_vacancy_posting_data%notfound then
rec_vac_posting_data := null;
end if;
close c_vacancy_posting_data;
--
return rec_vac_posting_data.job_title;
--
end get_vac_posting_job;
--
end hack_jdev_pkg;
/
show errors;



Right click the project and create a new View Object:


Name your new VO; the best practice is to give it a prefix before the delivered name. Be sure to enter oracle.apps.per.irc.vacancy.server.IrcVacancyVO in the "Extends" field.


Continue to step 3 and click New and configure as shown in the following two screen prints. This is the actual extension to the VO where the values from Step 4 of the Vacancy Create process are added to the VO:


Repeat for the Posting Organization:


Continue to step 6 and configure as shown below and Finish. Check the "Generate Java File" if you need to extend the methods delivered with the VO:

Double click the .jpx file in your project to add a substitution, which tells Oracle to use your View Object instead of the delivered VO. Highlight the delivered VO and the extended VO and click "Add".

Make and Rebuild your project:


The remaining steps are:

1. upload substitution to database
2. deploy Java and .xml files to middle tier
3. bounce Apache
4. Personalize page to display new items

To upload the substitution, open a command prompt on the local machine and run the following command:


C:\JDEVOAF\jdevbin\jdev\bin\jpximport C:\JDEVOAF\jdevhome\jdev\myclasses\hack.jpx -username apps -password apps -dbconnection "(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=oracle.hack.com)(PORT=1521))(CONNECT_DATA=(SID=hack)))"






To deploy the files, zip the compiled project files in C:\\jdevhome\jdev\myclasses\hack and FTP them to $JAVA_TOP. Unzip them:


Bounce Apache and then return to iRecruitment Recruiter -> Search for Vacancies -> About this Page. The delivered VO has been replaced by your extended VO:


Create the necessary personalization fields on the Advanced Table Region of the page to render the new column, beginning with the Job Posting:



Yay-yeah...you are now an Oracle Hack:

Now challenge yourself by repeating the personalizations for the Posting Organization without the assistance of screen prints. Duplicate this effort on another Self Service page to practice your newly minted VO substitution hacking capabilities.