Sei sulla pagina 1di 14

Useful Coding

1) How to bounce Apache in 11i and R12


In 11i:
cd $COMMON_TOP/admin/scripts
ls l
cd SID
adapcctl.sh stop
adapcctl.sh start
For R12 :
cd $COMMON_TOP/admin/scripts
ls l
adapcctl.sh stop // Bounce Apache
adoacorectl.sh stop //Bounce OC4J
adapcctl.sh start
adoacorectl.sh start
2) How to call a concurrent program
String progAppl = "AR";
String progName = "TEST"; //Request Set Name
Vector test = new Vector();
ConcurrentRequest cr = new ConcurrentRequest(pConncection);
test.add(strFileUpldPath[0]);
test.add(strFileUpldPath[1]);
int reqId = cr.submitRequest(progAppl, progName,desc,null, false, test);
3) How to call Dialog page
String name = "Description";
MessageToken[] tokens = { new MessageToken("DESC",name) };
OAException confirmMessage = new
OAException("FND","XXSAPE_WARN_MSG",tokens);

OADialogPage dialogPage = new


OADialogPage(OAException.WARNING,confirmMessage,null,"OA.jsp?
page=/sapient/oracle/apps/pa/projupdate/webui/sapeEAProjectUpdatePG&re
tainAM=Y","OA.jsp?
page=/sapient/oracle/apps/pa/projupdate/webui/ProjectListPG");
pageContext.redirectToDialogPage(dialogPage);
4) How to capture LOV results returned from LOV in coding
if(pagecontext.isLovEvent()
{
String lovInputSourceId = pageContext.getParameter(SOURCE_PARAM);
if ("<LovItemId>".equals(lovInputSourceId))
{
Hashtable lovResults = pageContext.getLovResultsFromSession
(lovInputSourceId);
if(lovResults!=null)
{
String value =(String)lovResults.get("<LovFieldName>");
}
}
}
5) How to create LOV programmatically
(OAMessageLovInputBean)createWebBean(pageContext, LOV_TEXT,
null,
"inputTest");
webBean.addIndexedChild(lovInput);
// Specify the path to the base page.
lovInput.setAttributeValue(REGION_CODE,
"/oracle/apps/dem/webui/Basic");
// Specify the application id of the base page.
lovInput.setAttributeValue(REGION_APPLICATION_ID, new
Integer(20001));
// Specify the LOV region definition.
lovInput.setLovRegion("/oracle/apps/fnd/framework/toolbox/tutorial/webui/
Employee
sLovRN", 0);

// Validation should be enabled for LOVs unless it's essential for the field
to allow
// a partial value (in a "Search" region, for example).
lovInput.setUnvalidated(false);
// Configure the LOV mappings.
// Note that you must call this method after you add the messageLovInput
item
// to the web bean hierarchy.
lovInput.addLovRelations(pageContext, "inputTest", // base page item
"Empname", // lov item
LOV_RESULT, // direction
LOV_REQUIRED_NO);
lovInput.addLovRelations(pageContext, "inputTest", // base page item
"Empname", // lov item
LOV_CRITERIA, // direction
LOV_REQUIRED_NO);
6) How to create HTML Tabs on pages.
1. Create a function for each page that you want to display.
2. Create a menu of type "HTML Tab" for each tab you want to display.
3. Add each function to its corresponding tab menu. Do not specify any
Prompt values.
4. Create a menu of type "Home Page."
5. Add the tab submenus to the "Home Page" menu.
Add the tab menus in your desired display sequence from left to right. The
leftmost tab should be
added first.Remember to specify Prompt values for each tab submenu.
These values display as the tab text.
6. Add your "Home Page" menu to your responsibility menu.
7. Make sure your user has access to this responsibility before you try to test
the menu.
8. In the page specify the name of the Home Menu in application menu
property.
9) How to enable debugging.
pageContext.writeDiagnostics("oracle.apps.ap.oie.entry.accounting.webui.E
xpenseAllocationsPageCO", "start now before query...", 1);
10) How to open a pop window through javascript.

Take a simple button:


on destination url call javascript as
javascript:input=confirm('Your Changes will be lost.Do you wish to
continue?');if(input==true){window.location='OA.jsp?
page=/sapient/oracle/apps/pa/clientupdate/webui/sapeEAClientListPG'};if(in
put==false){void(0);};
11) How to pass table of records to java.
1)Define Object with the same structure as your table of records u need to
use in ur pl/sql procedureof some pkg.Remember define it as global object
type and not inside package specification, because in that case our jdbc code
would not be to find this object type.
/
*************************************************************
*****************/
/*The Script used to create Object type in this example is as
follows:*******/
/
*************************************************************
*****************/
create or replace type rectype as object(col1 varchar2(10),col2 Date,col3
Number);

2)Define table of object u have defined above which u need to use in ur


pl/sql procedure of some pkg.Remember define it as global table type and
not inside package specification, because in that case our jdbc code would
not be to find this object type.
/
*************************************************************
*****************/
/*The Script used to create table of Object type is as follows:*******/
/
*************************************************************
*****************/
create or replace type rectab as table of rectype;

3)Defining package specification and procedure definition


/
*************************************************************
********************/
/*The Script used to create package specification in this eg.is as
follows:*******/
/
*************************************************************
*****************/
create or replace package ioStructArray as
procedure testproc(iorec in rectab,orec out rectab);
end ioStructArray;
/

4)Defining package body and procedure


/
*************************************************************
********************/
/*The Script used to create package specification in this eg.is as
follows:*******/
/
*************************************************************
*****************/
create or replace package body ioStructArray as
procedure testproc(iorec in rectab,orec out rectab) is
begin
/*see how to loop and assign values*/
for i in 1..iorec.count loop
iorec(i).col1 := orec(i).col2;
iorec(i).col2 := orec(i).col1;
end loop;
end testproc;
end ioStructArray;
/
5)Getting connection object in JDBC Code :

//Getting db connection in a jdbc


DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
// Connect to the database
Connection conn=DriverManager.getConnection
("jdbc:oracle:oci8:@S692815.WORLD",
"scott", "tiger");

6)If writing in AM in OAF page, code to get connection:


OADBTransactionImpl oadbtransactionimpl =
(OADBTransactionImpl)getOADBTransaction();
OracleConnection conn =
(OracleConnection)oadbtransactionimpl.getJdbcConnection();
7)Lets, consider a simple scenario where we have a XXVO in AM and I
have to pass all VO rows data to the pl/sql procedure we just created and
then receieve a table of data back and then based on it values do something
in AM.Here is the Code:
//imports
import oracle.sql.*;
import oracle.jdbc.driver.OracleConnection;
import oracle.jdbc.driver.OracleCallableStatement;
import oracle.apps.fnd.framework.server.OADBTransaction;
import oracle.apps.fnd.framework.server.OADBTransactionImpl;
Public void arryToPLSQL()
{
//Getting Db connection
OADBTransactionImpl oadbtransactionimpl =
(OADBTransactionImpl)getOADBTransaction();
OracleConnection conn =
(OracleConnection)oadbtransactionimpl.getJdbcConnection();
//Defining variables
//oracle.sql.ARRAY we will use as out parameter from the package
//and will store pl/sql table
ARRAY message_display = null;

//ArrayList to store object of type struct


ArrayList arow= new ArrayList();
//StructDescriptor >> use to describe pl/sql object
//type in java.
StructDescriptor voRowStruct = null;
//ArrayDescriptor >> Use to describe pl/sql table
//as Array of objects in java
ArrayDescriptor arrydesc = null;
//Input array to pl/sql procedure
ARRAY p_message_list = null;
//Oracle callable statement used to execute procedure
OracleCallableStatement cStmt=null;
try
{
//initializing object types in java.
voRowStruct = StructDescriptor.createDescriptor("RECTYPE",conn);
arrydesc = ArrayDescriptor.createDescriptor("RECTAB",conn);
}
catch (Exception e)
{
throw OAException.wrapperException(e);
}
for(XXVORowImpl row = (XXVORowImpl)XXVO.first();
row!=null;
row = (XXVORowImpl)XXVO.next())
{
//We have made this method to create struct arraylist
// from which we will make ARRAY
//the reason being in java ARRAY length cannot be dynamic
//see the method defination below.
populateObjectArraylist(row,voRowStruct,arow);
}

//make array from arraylist


STRUCT [] obRows= new STRUCT[arow.size()];
for(int i=0;i{
obRows[i]=(STRUCT)arow.get(i);
}
try
{
p_message_list = new ARRAY(arrydesc,conn,obRows);
}
catch (Exception e)
{
throw OAException.wrapperException(e);
}
//jdbc code to execute pl/sql procedure
try
{
cStmt
=(OracleCallableStatement)conn.prepareCall("{CALL
ioStructArray.testproc(:1,:2)}");
cStmt.setArray(1,p_message_list);
cStmt.registerOutParameter(2,OracleTypes.ARRAY,"RECTAB");
cStmt.execute();
//getting Array back
message_display = cStmt.getARRAY(2);
//Getting sql data types in oracle.sql.datum array
//which will typecast the object types
Datum[] arrMessage = message_display.getOracleArray();
//getting data and printing it
for (int i = 0; i < arrMessage.length; i++)
{
oracle.sql.STRUCT os = (oracle.sql.STRUCT)arrMessage[i];
Object[] a = os.getAttributes();
System.out.println("a [0 ] >>attribute1=" + a[0]);
System.out.println("a [1 ] >>attribute2=" + a[1]);
System.out.println("a [2 ] >>attribute3=" + a[2]);
//You can typecast back these objects to java object type

}
}
catch (Exception e1)
{
throw OAException.wrapperException(e1);
}
}

/*Our custom method which will populate


arraylist with struct object type
*/
public void populateObjectArraylist( XXVORowImpl row,StructDescriptor
voRowStruct , ArrayList arow)
{
Object[] attribMessage = new Object[3];
String attr1 = null;
Date attr2 = null;
Number attr3 = null;
//Get value from Vo row and put in attr1,att2 and attr 3
//Putting values in object array
attribMessage[0]=attr1;
attribMessage[1]=attr2;
attribMessage[2]=attr3;
try
{
STRUCT loadedStructTime = new STRUCT(voRowStruct, conn,
attribMessage);
arow.add(loadedStructTime);
}
catch (Exception e)
{
}

}
======================================================
=====================
However for using pl/sql tables you can use
OracleTypes.PLSQL_INDEX_TABLE
stmt.setregisteroutparameter(1,OracleTypes.PLSQL_INDEX_TABLE);
======================================================
======================
protected void insertRow()
{
String OutError = "";
ArrayList al = new ArrayList();
OADBTransaction trans = getOADBTransaction();
String insStmnt = "BEGIN " +
"xxotl_handle_timestore.create_narvaro_timecard(:1, :2, :3, :
4, :5, :6, :7, :8, :9, :10," +
":11, :12, :13, :14, :15, :16, :17, :18, :19, :20, :21, :22); " +
"END;";
OracleCallableStatement insNarvaroCs =
(OracleCallableStatement)trans.createCallableStatement(insStmnt, 1);
try
{
insNarvaroCs.setString(1, "SUBMIT");
insNarvaroCs.setNUMBER(2, getPersonId());
insNarvaroCs.setString(3, getProjectId());
insNarvaroCs.setString(4, getTaskId());
insNarvaroCs.setString(5, getExpenditureType());
insNarvaroCs.setString(6, getTcCommentText());
insNarvaroCs.setString(7, getCommentText());
insNarvaroCs.setDATE(8, getDateMonday());
insNarvaroCs.setNUMBER(9, getHoursMonday());
insNarvaroCs.setDATE(10, getDateTuesday());
insNarvaroCs.setNUMBER(11, getHoursTuesday());
insNarvaroCs.setDATE(12, getDateWednesday());
insNarvaroCs.setNUMBER(13, getHoursWednesday());

insNarvaroCs.setDATE(14, getDateThursday());
insNarvaroCs.setNUMBER(15, getHoursThursday());
insNarvaroCs.setDATE(16, getDateFriday());
insNarvaroCs.setNUMBER(17, getHoursFriday());
insNarvaroCs.setDATE(18, getDateSaturday());
insNarvaroCs.setNUMBER(19, getHoursSaturday());
insNarvaroCs.setDATE(20, getDateSunday());
insNarvaroCs.setNUMBER(21, getHoursSunday());
insNarvaroCs.registerOutParameter(22, OracleTypes.ARRAY,
"XXOTL_ERROR_MESSAGE_TBL");
insNarvaroCs.execute();
// OAExceptionUtils.checkErrors as per PLSQL API standards
OAExceptionUtils.checkErrors(trans);
ARRAY arrayError = insNarvaroCs.getARRAY(22);
Datum[] arr = arrayError.getOracleArray();
for (int i = 0; i < arr.length; i++)
{
oracle.sql.STRUCT os = (oracle.sql.STRUCT)arr[i];
Object[] a = os.getAttributes();
//System.out.println("Column:" + a[0] + " Value:" + a[1]);
OutError = (String)a[1];
al.add(new OAException(OutError, OAException.ERROR));
}
insNarvaroCs.close();
}
catch(SQLException sqle){
try { insNarvaroCs.close(); }
catch (Exception e) {;}
throw OAException.wrapperException(sqle);
}
if (OutError != null)
{
OAException.raiseBundledOAException(al);

}
}
12) How to redirect to the same page and change properties.
if(pagecontext.getparameter("AddBtn") != null)
{
hashmap map = new hashmap();
map.put("Add", "Yes");
pagecontext.forwardimmediately to currentpage(map, true, Y);
}
in processrequest
hashmap map = new hashmap();
String addEvent = map.get("Add");
if("Yes".equals(addEvent ))
{
/// editable
}
else
{
////editable
}
13 Some other code snippets.

Code for Disabling Global Buttons (in process request)

OAPageLayoutBean page =
(OAPageLayoutBean)pageContext.getPageLayoutBean();
page.prepareForRendering(pageContext);
OAGlobalButtonBarBean buttons =
(OAGlobalButtonBarBean)page.getGlobalButtons();
buttons.setRendered(false);

Code for Setting Initial Focus on a field (in process request)

import oracle.apps.fnd.framework.webui.beans.OABodyBean;


OABodyBean oabean = (OABodyBean)pageContext.getRootWebBean();
oabean.setInitialFocusId(ProjectType1?);
Code for Handling enter key press after entering a value in the text
box.
(in process request)
OAMessageLovInputBean messagelovinputbean =
(OAMessageLovInputBean)webBean.findIndexedChildRecursive(SelectVe
rsion);
// SelectVersion is the id of the lov item in the page
if(messagelovinputbeanPerson != null)
{
Hashtable params = new Hashtable();
params.put(Go, true);
//Go is the name of the button, whose action should happen after enter key
press
messagelovinputbeanPerson.setAttributeValue(OAWebBeanConstants.ON_
KEY_PRESS_ATTR,new
OABoundValueEnterOnKeyPress(pageContext,DefaultFormName,params
,true,true)); }

Code for releasing lock on a database transaction (in AMImpl)

OADBTransaction oadbTransaction = getOADBTransaction();


oadbTransaction.setLockingMode(oadbTransaction.LOCK_NONE);

Code for wrapping text in read-only text area (in Process Request)

textBean.setWrap(SOFT_WRAP);

Handling Browser Back Button

(In Process Request)


if (!pageContext.isBackNavigationFired(false)) {
TransactionUnitHelper.startTransactionUnit(pageContext,
xxxxxxxxxxTxn);
// All your processRequest logic should be inside this IF loop.

} else {
if (!TransactionUnitHelper.isTransactionUnitInProgress(pageContext,
xxxxxxxxxxxTxn, true)) {
OADialogPage dialogPage = new OADialogPage(STATE_LOSS_ERROR);
pageContext.redirectToDialogPage(dialogPage);
}
}
(In Process Form Request, before redirecting to a different page or before
form submission)
TransactionUnitHelper.endTransactionUnit(pageContext,
xxxxxxxxxxxxxxxTxn);