So far, you have a Portal which surfaces two applications. But the Portal lacks any personalization features. The favorite color application has no way to know when a user is logged in and it has no way of remembering a user's favorite color after the user has logged out of the Portal.
In this step you will make the favorite color application capable of remembering the user's favorite color after the user has logged out. You will add a User Profile Control to the application, a control which can save information about individual Portal users. The User Profile Control saves the information in a database (a database entirely managed by the control) and accesses the information using a set of property/value pairs.
The tasks in this step are:
In this step you will edit the application so that it is accessible only to logged in users. You will accomplish this by setting properties on the favorite color application. First, you will set the application's login-required property to true. This will cause the application to throw a NotLoggedInException anytime a non-logged in user tries to access the application. Second, you will edit the application's catch property. The catch property provides error handling instructions to the application. In this case the application will handle the exception by sending non-logged in users to a JSP page that instructs them that they must first log on to use the application.
In this task you will add a User Profile Control to the favorite color application. A User Profile Control provides access to a user's User Profile: a store of information that is maintained by the Portal. The User Profile stores information about the user as a set of property/values pairs. Most importantly the User Profile persists after a user has logged out of the Portal. In this case you will save the user's favorite color in the User Profile.
The User Profile Control you have added contains methods for storing user
information (setProperties and setProperty)
and methods for retreiving user information (such as getProfile and getProfileForUser).
You can use these methods to store and retrieve user preferences and other
information over a history of visits
to the Portal.
In this task you will edit the favorite color application code to take advantage of the User Profile Control. When the user selects a favorite color, it will be saved in the User Profile. When the user revisits the Portal, the favorite color will be retrieved from the User Profile.
import com.bea.wlw.netui.pageflow.FormData; import com.bea.wlw.netui.pageflow.Forward; import com.bea.wlw.netui.pageflow.PageFlowController; import com.bea.p13n.usermgmt.profile.ProfileWrapper;
/** * @jpf:action * @jpf:forward name="showColor" path="color.jsp" */ protected Forward begin() throws Exception { // Get the current user's profile. ProfileWrapper pw = userProfileControl.getProfileFromRequest( getRequest() ); // If the favorite color retrieved from the user profile is not null... if( pw.getPropertyAsString( "favorites", "favoriteColor" ) != null ) { // ...load the favorite color into the member variable _fav _fav = pw.getPropertyAsString( "favorites", "favoriteColor" ); } // Render the color.jsp page return new Forward( "showColor" ); }
/** * @jpf:action * @jpf:forward name="showColor" path="color.jsp" */ protected Forward setFav(SelectFavForm form) throws Exception { // Get the current user's profile. ProfileWrapper pw = userProfileControl.getProfileFromRequest( getRequest() ); // Save the selected color in the user's profile. // This color will be read from the profile on the // user's next visit to the portal. pw.setProperty( "favorites", "favoriteColor", form.getFavoriteColor() ); // Load the selected color into the global variable _fav. // The JSP page color.jsp reads the display color from _fav. _fav = form.getFavoriteColor(); // Render the JSP page color.jsp. return new Forward("showColor"); }