Wednesday, February 3, 2010

Attaching AutoSubmit Property to a OA page.

Hi All,
I recently received couple of mails where people more or less have the requirement like :
1) Data of the page gets refresh every 10 sec automatically.
2) Data of the page should be autosaved every 10 seconds etc.

This is very similar to autosave feature of microsoft word or popular email websites like yahoo and google. A small javascript function can do this for you.Here are the steps how, you can achieve this functionality in an OAF page :

/**
* @param pageContext -current page context in CO
* @param evt_name - javascript event will be registered with this name.
* @param time_in_milli_sec - time in milli sec after which page will refresh
* e.g if you wanna submit page every 10 sec , enter 10000
* This api should be used in process request and not process form request.
*/
public void attachAutoSubmitPropertyToPage(OAPageContext pageContext,
String evt_name,
String time_in_milli_sec)
{
OABodyBean bodyBean = (OABodyBean) pageContext.getRootWebBean();
String javaS =
"javascript:setTimeout(\"submitForm('DefaultFormName',0,{'" +
evt_name + "':'Y'});\"," + time_in_milli_sec + ");";
bodyBean.setOnLoad(javaS);
}

/**
* @param pageContext -current page context in CO
* @param event_name - pass javascript event name that you registered
* in process request.
* @return return true/false accordinging whether the event has occured or not.
* This api call should be there in process form request.
*/
public boolean isAutoSubmitEvent(OAPageContext pageContext,
String event_name)
{
boolean b = false;
String s =
pageContext.getRenderingContext().getServletRequest().getParameter(event_name);
if (!((s == null) || ("".equals(s.trim()))))
{
if("Y".equals(s))
{
b = true;
pageContext.removeParameter(event_name);
}
}
return b;
}

Then , in process request add the api call

public void processRequest(OAPageContext pageContext, OAWebBean webBean)
{
super.processRequest(pageContext, webBean);
// see javadocs for parameter description
//10000 for 10 sec as we are paaing in millsec
attachAutoSubmitPropertyToPage(pageContext,"XX_EVT","10000");

.
.
.
}



In process form request... you can catch this event in every 10 sec:
public void processFormRequest(OAPageContext pageContext,
OAWebBean webBean)
{
super.processFormRequest(pageContext, webBean);
.
.

if(isAutoSubmitEvent(pageContext,"XX_EVT"))
{
// YOUR LOGIC TO SAVE THE PAGE CONTENTS.
//or to refresh the data... etc
}

I hope this is interesting and will help developers of such kind of requirement.
Happy Coding..!

9 comments:

Unknown said...

You have a very good blog. This post gives so much information.

Sridhar Yerram said...

Mukul,
i implemented the solution which you provided, it is working perfect.
I need some enhancement to it but it failed, please help me in this.
My requirement is as follows ;

1. Refresh the page after certain duration. For ex: Refresh the OAF page for every 30mins.
2. Before refreshing that is at 29th minute , i want to display a conformation message informing the user that page will get refreshed with in the next second.

I need following clarification too:
The following
attachAutoSubmitPropertyToPage(pageContext,"XX_EVT","10000");
creates an event and calls the function which is specified for the event "XX_EVT". But for the body i can register any number of events isnt it ???

for ex :

attachAutoSubmitPropertyToPage(pageContext,"XX_EVT_1","10000");

attachAutoSubmitPropertyToPage(pageContext,"XX_EVT_2","10000");

Please clarify Mukul.

Thanks
Sridhar

Mukul Gupta said...

Hi Sridhar,
Here are my responses :
1. Refresh the page after certain duration. For ex: Refresh the OAF page for every 30mins.

The above article exactly answers this part. But just one important point , the duration of time you are using in this function, make sure it is less than apps session timeout, which is by default 30 min. The reason being after session timeout even if the js function will try to submit the page, but since transaction context is lost due to session timeout, you will get errors on page. One more suggestion , where ever you are using this function you can make it profile dependent, i.e. you can get time duration of refresh from this profile, which will also help you to change this duration later without changing code.

2. Before refreshing that is at 29th minute , i want to display a conformation message informing the user that page will get refreshed with in the next second

For this you can use following function, this will show alert message(that u will pass param, see javadoc) as well :
/*
* @param pageContext -current page context in CO
* @param evt_name - javascript event will be registered with this name.
* @param time_in_milli_sec - time in milli sec after which page will refresh
* e.g if you wanna submit page every 10 sec , enter 10000
* @param alert_message - alert message that needs to be shown in alert just before submit.
* This api should be used in process request and not process form request.
*/
public void attachAutoSubmitWithAlertToPage(OAPageContext pageContext,
String evt_name,
String time_in_milli_sec,
String alert_message)
{
OABodyBean bodyBean = (OABodyBean) pageContext.getRootWebBean();
String javaS =
"javascript:setTimeout(\"alert('"+alert_message+"');submitForm('DefaultFormName',0,{'" +
evt_name + "':'Y'});\"," + time_in_milli_sec + ");";
bodyBean.setOnLoad(javaS);
}

The page will refresh only after user presses Ok button on alert.

3.I need following clarification too:
The following
attachAutoSubmitPropertyToPage(pageContext,"XX_EVT","10000");
creates an event and calls the function which is specified for the event "XX_EVT". But for the body i can register any number of events
isnt it ???

for ex :
attachAutoSubmitPropertyToPage(pageContext,"XX_EVT_1","10000");
attachAutoSubmitPropertyToPage(pageContext,"XX_EVT_2","10000");

You can not register two events on body bean , reason being if you see code of API attachAutoSubmitPropertyToPage, it attaches a simple js function on "on load" event of body bean, if you call this API twice the second call to API will take effect.

I hope this clarifies all your queries.

--Mukul

Sridhar Yerram said...

Thanks alot mukul,
your replies clarified my doubts.

The custom pages which we developed are getting timeout after the session expires, to keep the session active we followed the approach of timely refresh using javaScript, but we faced some problems like conformation/exception messages are wiped out, LOV are getting closed, some SPEL expressions are not working properly. What is the solution suggested for this timely refresh ?

rbojja said...

Hi Mukul,
I have implemeted autoSave functionlaity as per your suggestion. Thanks for your efforts. I have a few enhancement.I have a form which should be autosaved only if button is enabled and shouldnt submit if button is disabled.i have multiple forms. how to implement this. please give u r suggestions. thanks in advance

Ragni said...

Hi Mukul,

The same way you re submittted the page after few mins, is there any way we can auto submit LOV by setting any value to it at run time?
My problem is I am setting a valid value to the LOV programatically but the value is not rendered until I click in Torch icon.

Any help would be really appreciated.
Thanks in Adv.

Ragni Gupta

Blogger said...

Wonderful blog & good post.Its really helpful for me, awaiting for more new post. Keep Blogging!
Oracle Advanced Procurement

Unknown said...

Kamlesh Nikhade,

I have similar requirement as yours.I am able to upload file but in form server. How can I use NFS Mount utility to upload the file into db server in unix?

Thanks!

Unknown said...

Hi, If I want to refresh the page based on the system Idle time, what are the changes needs to be in above code?