Oracle® Application Development Framework Developer's Guide For Forms/4GL Developers 10g (10.1.3.1.0) Part Number B25947-01 |
|
|
View PDF |
It is fairly common practice to add custom user-defined information in the application module in the form of member variables or some custom information stored in oracle.jbo.Session
user data hashtable. The ADF state management facility provides a mechanism to save this custom information to the passivation snapshot as well. By overriding the following two methods on the ApplicationModuleImpl
class, you can write out and read back your custom information:
protected void passivateState(Document doc, Element parent) public void activateState(Element elem)
Consider the following simple example that shows how to override this pair of methods to ensure custom application module state is included in the passivation/activation cycle. Assume that you would like to keep some custom parameter in your application module called jbo.counter
whose value you want to preserve across passivation and activation of the application module state. Each application module has an oracle.jbo.Session
object associated with it that stores application module-specific session-level state. The session contains a "user data" hashtable where you can store transient information. For the user-specific data to "survive" across application module passivation and reactivation, you need to write code to save and restore this custom value into the application module state passivation snapshot. Example 28-7 shows the code you can write in your pair of overridden passivateState()
and activateState()
methods in a custom application module class to do the job.
The overridden passivateState()
method performs the following steps:
Retrieve the value of the value to save.
Create an XML element to contain the value.
Create an XML text node to represent the value.
Append the text node as a child of the element.
Append the element to the parent element passed in.
Note: The API's used to manipulate nodes in an XML document are provided by the Document Object Model (DOM) interfaces in theorg.w3c.dom package. These are part of the Java API for XML Processing (JAXP). See the JavaDoc for the Node , Element , Text , Document , and NodeList interfaces in this package for more details. |
The overridden activateState()
method performs the reverse job by doing the following:
Search the element for any jbo.counter elements.
If any are found, loop over the nodes found in the node list.
Get first child node of the jbo.counter element.
It should be a DOM Text node whose value is the string you saved when your passivateState()
method above got called, representing the value of the jbo.counter
attribute.
Set the counter value to the activated value from the snapshot.
Example 28-7 Passivating and Activating Custom Information in the State Snapshot XML Document
/** * Overridden framework method to passivate custom XML elements * into the pending state snapshot document */ public void passivateState(Document doc, Element parent) { // 1. Retrieve the value of the value to save int counterValue = getCounterValue(); // 2. Create an XML element to contain the value Node node = doc.createElement(COUNTER); // 3. Create an XML text node to represent the value Node cNode = doc.createTextNode(Integer.toString(counterValue)); // 4. Append the text node as a child of the element node.appendChild(cNode); // 5. Append the element to the parent element passed in parent.appendChild(node); } /** * Overridden framework method to activate custom XML elements * into the pending state snapshot document */ public void activateState(Element elem) { super.activateState(elem); if (elem != null) { // 1. Search the element for any <jbo.counter> elements NodeList nl = elem.getElementsByTagName(COUNTER); if (nl != null) { // 2. If any found, loop over the nodes found for (int i=0, length = nl.getLength(); i < length; i++) { // 3. Get first child node of the <jbo.counter> element Node child = nl.item(i).getFirstChild(); if (child != null) { // 4. Set the counter value to the activated value setCounterValue(new Integer(child.getNodeValue()).intValue()+1); break; } } } } } /* * Helper Methods */ private int getCounterValue() { String counterValue = (String)getSession().getUserData().get(COUNTER); return counterValue == null ? 0 : Integer.parseInt(counterValue); } private void setCounterValue(int i) { getSession().getUserData().put(COUNTER,Integer.toString(i)); } private static final String COUNTER = "jbo.counter";
Note: Similar methods are available on theViewObjectImpl class and the EntityObjectImpl class to save custom state for those objects to the passivation snapshot as well. |