# Oracle Business Intelligence Beans Sample

## Handling State Management

### Overview

The `BIStateServlet` class demonstrates state handling. The state of an application includes state strings that represent the state of the thin beans in the application and state strings that represent application state. The state strings can be placed onto the URLs/Forms that an application generates, so that the application can re-establish the thin-bean's state from a request's URL. This placing of an application's state onto the URL enables an application to re-establish application state from a bookmark.

To demonstrate the state handling, drill down on one dimension of the crosstab and use the browser's back button to go back to the initial state. Then, drill down on another dimension. You will notice that the application is able to identify the state of the application immediately before the second drill so that it just drills down the second dimension. Without state handling, the application would have drilled down on both the first and second dimensions.

### Code Highlights

The following section provides a walkthrough and explanations of the code fragments: The `processRequest` method is called by the base class after a successful user login. The first step of the application is to initialize the thin beans. The following code initializes a crosstab thin bean.

`presentation = new Presentation(ViewTypeTool.CROSSTAB);
presentation.setThinBeanName(PRESENTATION_NAME);
ThinDataviewCommon crosstab = presentation.getView();
crosstab.setPagingControlVisible(true);
crosstab.setDataSource(dataSource);
handler.registerThinBean(presentation);
`

Register the thin bean with the `BIAdvancedStateManager` object.

`BIAdvancedStateManager stateManager = biHttpSession.getStateManager();
stateManager.registerTransientStateBean(presentation);
`

Establish the base state from which state changes that are included in the state string are tracked. This call clears the thin-bean's current state string.

`presentation.setBaseState();
`

The application delegates to the `ServletRequestHandler` for handling thin-bean events.

`ServletRequestHandler handler = biHttpSession.getServletRequestHandler ( );
ServletQueryParameterProvider provider = new ServletQueryParameterProvider ( request, response );
handler.handleRequest ( provider );`

The application can render the HTML using a combination of raw HTML and thin beans. For rendering HTML, the application can retrieve the `PrintWriter` from the `HttpResponse` object and use `println` statements to output HTML to the client.

`response.setContentType ( "text/html" );
PrintWriter out = response.getWriter ( );`

The thin beans are added to a `FormBean` UIX component.

`FormBean rootNode = new FormBean ( FORM_NAME );
ThinCrosstab crosstab = ( ThinCrosstab ) handler.getThinBean ( CROSSTAB_NAME );
if ( crosstab != null ) {
CrosstabBean crosstabBean = new CrosstabBean ( crosstab );
rootNode.addIndexedChild ( crosstab );
}`

The application calls the `computeCurrentState` method on the `BIAdvancedStateManager` object to update the state for each thin bean that is registered with it. The states of thin beans are then added to the UIX component tree as hidden form fields.

`stateManager.computeCurrentState();
FormValueBean formValue = null;
String transientStateId = stateManager.getCurrentTransientStateID();
if (transientStateId != null) {
formValue = new FormValueBean(BIAdvancedStateManager.TRANSIENT_STATE_ID, transientStateId);
form.addIndexedChild(formValue);
}
String savedStateId = stateManager.getCurrentSavedStateID();
if (savedStateId != null) {
formValue = new FormValueBean(BIAdvancedStateManager.SAVED_STATE_ID, savedStateId);
form.addIndexedChild(formValue);
}
String stateString = stateManager.getCurrentBeanStates();
if (stateString != null) {
formValue = new FormValueBean(BIAdvancedStateManager.STATE_ID, stateString);
form.addIndexedChild(formValue);
}`

The thin beans in a `FormBean` UIX component are rendered as follows:

`ServletRenderingContext renderingContext = new ServletRenderingContext( this, request, response, out );
rootNode.render ( renderingContext );`

While the code cleans up the `BIHttpSession` that is related to a client, the resources allocated by the servlet need to be cleaned up. This includes beans that the servlet allocates for the session. The `close` method (implemented by `QueryClient`) should be called to release resources that are related to the data source. The `cleanUp` method should be called on the `ThinGraph` to release Graph-related resources.

Copyright © 2004 Oracle Corporation. All Rights Reserved.