Skip Headers
Oracle® Application Development Framework Developer's Guide For Forms/4GL Developers
10g (10.1.3.1.0)

Part Number B25947-01
Go to Documentation Home
Home
Go to Book List
Book List
Go to Table of Contents
Contents
Go to Index
Index
Go to Master Index
Master Index
Go to Feedback page
Contact Us

Go to previous page
Previous
Go to next page
Next
View PDF

9.10 How to Store Information About the Current User Session

If you need to store information related to the current user session in a way that entity object business logic can reference, you can use the user data hashtable provided by the Session object. Consider how the SRDemo application is using it. When a new user accesses an application module for the first time, the prepareSession() method is called. As shown in Example 9-14, the SRService application module overrides prepareSession() to automatically retrieve information about the authenticated user by calling the retrieveUserInfoForAuthenticatedUser() method on the LoggedInUser view object instance. Then, it calls the setUserIdIntoUserDataHashtable() helper method to save the user's numerical ID into the user data hashtable.

Example 9-14 Overriding prepareSession() to Automatically Query User Information

// In SRServiceImpl.java in SRDemo Sample
protected void prepareSession(Session session) {
  super.prepareSession(session);
  /*
   * Automatically query up the correct row in the LoggedInUser VO
   * based on the currently logged-in user, using a custom method
   * on the LoggedInUser view object component.
   */
  getLoggedInUser().retrieveUserInfoForAuthenticatedUser();     
  setUserIdIntoUserDataHashtable();
}

Example 9-15 shows the code for the LoggedInUser view object's retrieveUserInfoForAuthenticatedUser() method. It sets its own EmailAddress bind variable to the name of the authenticated user from the session and then calls executeQuery() to retrieve the additional user information from the USERS table.

Example 9-15 Accessing Authenticated User Name to Retrieve Additional User Details

// In LoggedInUserImpl.java
public void retrieveUserInfoForAuthenticatedUser() {
  SessionImpl session = (SessionImpl)getDBTransaction().getSession();
  setEmailAddress(session.getUserPrincipalName());
  executeQuery();
  first();
}

One of the pieces of information about the authenticated user that the LoggedInUser view object retrieves is the user's numerical ID number, which that method returns as its result. For example, the user sking has the numeric UserId of 300.

Example 9-16 shows the setUserIdIntoUserDataHashtable() helper method — used by the prepareSession() code above — that stores this numerical user ID in the user data hashtable, using the key provided by the string constant SRConstants.CURRENT_USER_ID.

Example 9-16 Setting Information into the UserData Hashtable for Access By Entity Objects

// In SRServiceImpl.java
private void setUserIdIntoUserDataHashtable() {
  Integer userid = getUserIdForLoggedInUser();
  Hashtable userdata = getDBTransaction().getSession().getUserData();
  if (userdata == null) {
    userdata = new Hashtable();
  }
  userdata.put(SRConstants.CURRENT_USER_ID, userid);
}  

Both the ServiceRequest and the ServiceHistory entity objects have an overridden create() method that references this numerical user ID using a helper method like the following to set the CreatedBy attribute programmatically to the value of the currently authenticated user's numerical user ID.

protected Number getCurrentUserId() {
  Hashtable userdata = getDBTransaction().getSession().getUserData();
  Integer userId = (Integer)userdata.get(SRConstants.CURRENT_USER_ID);
  return userdata != null ? Utils.intToNumber(userId):null;
}