How to use a session manager object to share information between a form and a subform.
You can run this technique code from:
NOTE First make sure that database is running on your localhost SilverStream Server | |
See the chapter on Working with invoked triggers in the Programmer's Guide See the chapter on the Form Designer in the Tools Guide |
This example shows how to use an invoked business object as a session manager for multiple forms. Here is how to run the form.
Note that session value displays on both the form and subform.
When the form is activated it passes a hashtable to an invoked business object. The object gets the session id and returns it as a serializable object. The form casts the return value to a string, stores it in a variable, then sets the value on the appropriate variable declared in the General section of the code.
This is the code for the formActivate event. (The formActivate event on the sub form is coded in a similar manner.)
protected void formActivate() { try { // Create a hashtable and set the request type to get //the session id Hashtable hshParms = new Hashtable(); hshParms.put("RequestType", "GetSessionID"); // Pass the hashtable to the invoked object // boinvSessionManager (name stored in a // Class variable). It is returned from the object // as serializable, so cast it to a string String sSession = (String) agGeneral.invokeBusinessObject (BUSINESS_OBJECT_NAME, hshParms); // set the value on the form lblSessionIDValue.setText(sSession); } catch (Exception __exx) { agDialog.displayError(__exx); } }
When the user clicks putValue, the code gets the key and value strings and passes them to the invoked object in a hashtable, where they can be referenced from other forms in this session.
This is the code for the actionPerformed event on btnPutValue.
private void handle_btnPutValue_actionPerformed(ActionEvent evt) { // Get keyword from field String sKeyword = fldKeyword.getText(); // Get value from field String sValue = fldValue.getText(); // Call the invoked object passing the parameters try { Hashtable hshParms = new Hashtable(); hshParms.put("RequestType", "PutValue"); hshParms.put("Keyword", sKeyword); hshParms.put("Value", sValue); agGeneral.invokeBusinessObject (BUSINESS_OBJECT_NAME ,hshParms); } catch (Exception __exx) { agDialog.displayError(__exx); } }
This code shows how another form can access the session object. When the user enters the keyword on the subform and clicks btnGetValue, the code calls the invoked object, passing the value in a hashtable. The invoked object returns the value for that keyword, which casts the result to a string and sets the value on the label variable declared in the General section of the code.
private void handle_btnPutValue_actionPerformed(ActionEvent evt) { // Verify that a keyword was entered String sKeyword = fldKeyword.getText(); if (sKeyword == null || sKeyword.length() <= 0) { agDialog.showMessage("Missing Keyword Name", "Please enter a Keyword Name for the session value and try again."); return; } // Verify that a value was entered String sValue = fldValue.getText(); if (sValue == null || sValue.length() <= 0) { agDialog.showMessage("Missing Keyword Value", "Please enter a value for the session keyword and try again."); return; } // Call the invoked object passing the parameters try { Hashtable hshParms = new Hashtable(); hshParms.put("RequestType", "PutValue"); hshParms.put("Keyword", sKeyword); hshParms.put("Value", sValue); agGeneral.invokeBusinessObject (BUSINESS_OBJECT_NAME,hshParms); } catch (Exception __exx) { agDialog.displayError(__exx); } }
Since the object can be invoked from multiple callers, the code in the session manger object checks the first hashtable parameter passed from the caller to determine the type of request, and returns the appropriate value.
public void invoked(AgoInvokedEvent evt) throws Exception { // First, get the Session ID AgiSession sessionID = (AgiSession) evt.getSession(); // Get the parameters passed to this object // If there are no parameters, throw an exception Hashtable hshParms = (Hashtable) evt.getParameter(); if (hshParms == null) throw new Exception(GS_THIS + ".invoked() - No parameter was passed to this object."); // Get the request type from the parameters String sRequest = (String) hshParms.get("RequestType"); if (sRequest == null) throw new Exception(GS_THIS + ".invoked() - No request type was passed to object."); // Process a get Session ID request if (sRequest.equals("GetSessionID")) { String sSession = sessionID.toString(); evt.setResult( (java.io.Serializable) sSession); return; } // Any other request requires a keyword. String sKeyword = (String) hshParms.get("Keyword"); if (sKeyword == null) throw new Exception(GS_THIS + ".invoked() - No keyword passed for the " + sRequest + " request."); // Process a PutValue request if (sRequest.equals("PutValue")) { String sValue = (String) hshParms.get("Value"); if (sValue == null) throw new Exception(GS_THIS + ".invoked() - No value passed for the putValue() request."); sessionID.putValue(sKeyword, sValue); String sResult = "Success"; evt.setResult( (java.io.Serializable) sResult); return; } // Process a GetValue request if (sRequest.equals("GetValue")) { String sValue = (String) sessionID.getValue(sKeyword); evt.setResult( (java.io.Serializable) sValue); return; } // Process a RemoveValue request if (sRequest.equals("RemoveValue")) { String sValue = (String) sessionID.getValue(sKeyword); if (sValue == null) throw new Exception(GS_THIS + ".invoked() - No value passed for the removeValue() request."); evt.setResult( (java.io.Serializable) sValue); return; } throw new Exception(GS_THIS + ".invoked() - Invalid request type of '" + sRequest + "' was passed to object."); }