Personalizing ADF Applications through User Preferences

0

While of course the applications we build using ADF Faces technology are perfect and cater optimally to every end user’s needs, some end users seem to disagree. Even though we have just delivered the most wonderful and functional application to their door, they ask for changes. Frequently small changes, such as hide a certain component from a page, change the number of records visible in a table layout, change the set of columns shown in a summary table, change the list of allowable options for dropdownlists and radio groups and other such small changes. The screenshot demonstrates how for various users the numbers of records displayed in the table varies:

....

Unfortunately, small as these changes may be, they cannot be made in the application. Because if we change the application, we would do so for all users – and each and every user seems to have a different requirement. What I am looking for is a way to allow customization of the application based on individual user preferences – personalization of the application.

Note: we are not talking here about the personalization of resource bundles that allow users to define their own set of text-elements, for example in titles, prompts, hint texts and button labels. A solution for that particular challenge was presented in a previous blog article: Personalize Resource Bundles in ADF applications with JHeadstart 10.3.2 run time (http://technology.amis.nl/blog/?p=2547).

In this article I will show how we provide personalization capabilities to ADF Applications by going through a few simple steps:
- specify the application properties that have to be customizable using EL expresions rather than hard coded values; the EL expressions should refer to a managed bean that holds the current set of user preferences
- define a database table with application settings; in this table, we create records for the generic (default) settings of all customizable features in our application. As long as a user does not override a particular preference with his or her own value, the default value will apply
- create a view on top of the table that returns the application settings for the current user, typically composed of a mix of default values and specific user preferences
- create facilities in the application to set the database context with the current user, retrieve the values of the current application settings given that user and hold the application settings in the user profile bean.

Note: the Oracle Meta Data Store (MDS) is way of providing personalization capabilities to ADF 11g applications. I have to look into MDS in more detail before I can compare my approach in this article and the MDS. See http://download.oracle.com/otn_hosted_doc/jdeveloper/11/doc/b31974.pdf, chapter 31.

Let’s walk through the steps to set it up and get it working.

First we create the database objects we need for the user preferences mechanism. Note: steps 3, 4 and 5 are only required for supporting a UI for maintaining the application settings, you do not need them for the example illustrated in this article:

1. Create table application_settings_tbl

create table application_settings_tbl<br />( id&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; number(10)&nbsp;&nbsp;&nbsp;&nbsp; not null primary key<br />, name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; varchar2(50)&nbsp;&nbsp; not null<br />, description varchar2(4000)<br />, value&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; varchar2(4000)<br />, data_type&nbsp;&nbsp; varchar2(40)&nbsp; default 'string' not null <br />, for_user_id varchar2(100) &nbsp;<br />)<br />/

4. Create function get_user_profile that returns ref cursor with all active application settings

create or replace<br />&nbsp;function get_user_profile<br />&nbsp;( p_userid in varchar2<br />&nbsp;) return sys_refcursor<br />&nbsp;is<br />&nbsp;&nbsp; t_profile&nbsp; sys_refcursor;<br />&nbsp;begin<br />&nbsp;&nbsp; open t_profile for<br />&nbsp;&nbsp;      select ID<br />        ,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; name<br />        ,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; description<br />        ,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; value<br />        ,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; data_type<br />        from&nbsp; ( select ID<br />        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; name<br />        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; description<br />        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; value<br />        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; data_type<br />        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; row_number() over (partition by name order by for_user_id nulls last) rn<br />        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; from&nbsp;&nbsp;&nbsp; application_settings_tbl asg<br />               where&nbsp; nvl(asg.for_user_id, nvl(p_userid,'DEFAULT')) = nvl(p_userid,'DEFAULT')<br />              )<br />        where rn =1<br />&nbsp;&nbsp; ;<br />&nbsp;&nbsp; return t_profile;<br />end get_user_profile;<br />/<br />

 

3. Create view application_settings

create view application_settings<br />as<br />select ID&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;<br />,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; name<br />,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; description<br />,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; value&nbsp;&nbsp;&nbsp; &nbsp;<br />,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; data_type<br />from&nbsp; ( select ID&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; name<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; description <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; value&nbsp;&nbsp; &nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; data_type<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; row_number() over (partition by name order by for_user_id nulls last) rn<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; from&nbsp;&nbsp;&nbsp; application_settings_tbl asg<br />where&nbsp;<br />nvl(asg.for_user_id,<br />nvl(sys_context('APP_USER_CTXT','USER_ID'),'DEFAULT')) =<br />nvl(sys_context('APP_USER_CTXT','USER_ID'),'DEFAULT')<br />)<br />where rn =1 <br />/

4. Create Insert/Update trigger instead-of trigger on view – together with sequence

create sequence my_seq<br />/<br /><br /><br />create or replace trigger application_settings_iot<br />&nbsp;&nbsp; &nbsp;instead of insert or update on application_settings<br />for each row<br />declare<br />&nbsp; l_for_user varchar2(50):= sys_context('APP_USER_CTXT','USER_ID');<br />begin<br />&nbsp; if inserting<br />&nbsp; then<br />&nbsp;&nbsp;&nbsp; INSERT into application_settings_tbl<br />&nbsp;&nbsp;&nbsp; (id, name, value, description, for_user_id, data_type) &nbsp;<br />&nbsp;&nbsp;&nbsp; VALUES (nvl(:new.id, My_Seq.Nextval), :new.name, :new.value, :new.description, l_for_user, :new.data_type); <br />&nbsp; elsif updating<br />&nbsp; then<br />&nbsp;&nbsp;&nbsp; -- an update of a key/value entry (not user specific) should be turned into an insert (that is user specific)<br />&nbsp;&nbsp;&nbsp; merge into application_settings_tbl asg<br />&nbsp;&nbsp;&nbsp; using ( select :new.name name, :new.value value, l_for_user for_user , :new.description from dual) setting<br />&nbsp;&nbsp;&nbsp; on (asg.name = setting.name and nvl(asg.for_user_id,'DEFAULT') = nvl(setting.for_user, 'DEFAULT'))<br />&nbs
p;&
nbsp;&nbsp; WHEN MATCHED&nbsp;&nbsp;&nbsp;&nbsp; THEN UPDATE <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SET asg.text = setting.text <br />&nbsp;&nbsp;&nbsp; WHEN NOT MATCHED THEN INSERT ( id, name, value, description, for_user_id, data_type) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;<br />                          VALUES ( Jhs_Seq.Nextval, setting.name, setting.value ,<br />                                   setting.description, setting.for_user, data_type)<br />&nbsp;&nbsp;&nbsp; ;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;<br />&nbsp; end if;<br />end;<br />/

5. Set up an application context an a package to manage it

create context APP_USER_CTXT using APP_USER_CTXT_mgr<br />/<br /><br />create or replace package APP_USER_CTXT_mgr<br />is<br />procedure set_user<br />( p_userid in varchar2<br />);<br /><br />function get_user<br />return varchar2<br />;<br /><br />end;<br />/<br />create or replace package body APP_USER_CTXT_mgr<br />is<br />procedure set_user<br />( p_userid in varchar2<br />) is<br />begin<br />&nbsp; dbms_session.set_context ('APP_USER_CTXT','USER_ID', nvl(p_userid,'HENMK'));<br />end;<br /><br />function get_user<br />return varchar2<br />is<br />begin<br />&nbsp; return sys_context('APP_USER_CTXT','USER_ID');<br />end;<br /><br />end;<br />/

6. Create Application Setting MAX_RECORDS_IN_TABLE and set values for default and two users

INSERT into application_settings_tbl<br />(id, name, value, description, for_user_id, data_type) &nbsp;<br />VALUES (My_Seq.Nextval, 'MAX_RECORDS_IN_TABLE', '3', '', null, 'INTEGER')<br />/<br /><br />INSERT into application_settings_tbl<br />(id, name, value, description, for_user_id, data_type) &nbsp;<br />VALUES (My_Seq.Nextval, 'MAX_RECORDS_IN_TABLE', '2', '', 'SJOERD', 'INTEGER')<br />/<br /><br />INSERT into application_settings_tbl<br />(id, name, value, description, for_user_id, data_type) &nbsp;<br />VALUES (My_Seq.Nextval, 'MAX_RECORDS_IN_TABLE', '4','', 'HARM', 'INTEGER')<br />/

Over to the ADF application, in the middle tier

1. Add a method to retrieve the current’s user profile from the database to the ApplicationModuleImpl

 

&nbsp;&nbsp;&nbsp; protected<br /><br />&nbsp;&nbsp;&nbsp; Object callStoredFunction(int sqlReturnType, String stmt, <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Object[] bindVars) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CallableStatement st = null;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 1. Create a JDBC CallabledStatement&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; st = <br />&nbsp;getDBTransaction().createCallableStatement(&quot;begin ? := &quot; + stmt + &quot;;end;&quot;, 0);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 2. Register the first bind variable for the return value<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; st.registerOutParameter(1, sqlReturnType);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (bindVars != null) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 3. Loop over values for the bind variables passed in, if any<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int z = 0; z &lt; bindVars.length; z++) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 4. Set the value of user-supplied bind vars in the stmt<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; st.setObject(z + 2, bindVars[z]);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 5. Set the value of user-supplied bind vars in the stmt<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; st.executeUpdate();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 6. Return the value of the first bind variable<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return st.getObject(1);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } catch (SQLException e) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; throw new JboException(e);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } finally {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (st != null) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; st.close();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } catch (SQLException e) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //sLog.error(&quot;General SQLException &quot; + e.getMessage());<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; throw new JboException(e);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp; }<br /><br />&nbsp;&nbsp;&nbsp; public List getUserProfile(String userid) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // call aut_authorization.is_ip_valid(string ipaddress) return varchar2<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String statement = &quot;get_user_profile(?)&quot;;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ResultSet rs = null;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rs = <br />&nbsp;(ResultSet)callStoredFunction(OracleTypes.CURSOR, statement, new Object[] { userid });<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ArrayList results = new ArrayList(); // To hold copied list<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //while<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while (rs.next()) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Map newRow = new HashMap();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // retrieve and print the values for the current row<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; newRow.put(&quot;key&quot;, rs.getString(&quot;name&quot;));<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; newRow.put(&quot;value&quot;, rs.getString(&quot;value&quot;));<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; newRow.put(&quot;dataType&quot;, rs.getString(&quot;data_type&quot;));<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } catch (SQLException e) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // TODO<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs

p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; results.add(newRow);<br /><br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } catch (SQLException e) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // TODO<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rs.close();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } catch (SQLException e) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // TODO<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return results;<br />&nbsp;&nbsp;&nbsp; }

2. Add to the client interface of the applicationmodule

 

3. Drag method to a generic page (see blog article on generic pages and page definitions: Provide your ADF Faces application with a central BindingContainer for generic access to application wide services) in order to create methodbinding in pagedef.

The PageDef should have the following entry

&nbsp;&nbsp;&nbsp; &lt;methodAction id=&quot;getUserProfile&quot;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; InstanceName=&quot;GenericServiceDataControl.dataProvider&quot;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DataControl=&quot;GenericServiceDataControl&quot;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MethodName=&quot;getUserProfile&quot; RequiresUpdateModel=&quot;true&quot;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Action=&quot;999&quot; IsViewObjectMethod=&quot;false&quot; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  <br />ReturnName=&quot;GenericServiceDataControl.methodResults.GenericServiceDataControl_dataProvider_getUserProfile_result&quot;&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;NamedData NDName=&quot;userid&quot; NDValue=&quot;${userProfile.userId}&quot;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; NDType=&quot;java.lang.String&quot;/&gt;<br />&nbsp;&nbsp;&nbsp; &lt;/methodAction&gt;<br />

4. Create a class UserProfile (that is loaded into the session scope as a managed bean) containing the following method that can refresh the user’s application settings:

&nbsp;&nbsp;&nbsp; public void refreshApplicationSettings(String userId) {<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; List profile;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // call getUserProfile on the GenericService ApplicationModule to get hold<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // of the current user's set of application settings<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // now invoke the MethodBinding setUserContextInDB<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //DCBindingContainer&nbsp; bindings = (DCBindingContainer)JsfUtils.getExpressionValue(&quot;#{bindings}&quot;);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Object a = JsfUtils.getExpressionValue(&quot;#{data}&quot;);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; BindingContext bcx = <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (BindingContext)JsfUtils.getExpressionValue(&quot;#{data}&quot;);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DCBindingContainer container = <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bcx.findBindingContainer(&quot;pages_UtilsPageDefPageDef&quot;);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; container.refresh(DCBindingContainer.PREPARE_MODEL);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FacesCtrlActionBinding o = <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (FacesCtrlActionBinding)container.findNamedObject(&quot;getUserProfile&quot;);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; profile = (List)o.execute();<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (Map setting: (List&lt;Map&gt;)profile) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String key = (String)setting.get(&quot;key&quot;);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String value = (String)setting.get(&quot;value&quot;);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String dataType = (String)setting.get(&quot;dataType&quot;);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (&quot;INTEGER&quot;.equalsIgnoreCase(dataType)) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; applicationSettings.put(key, new Long(value));<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } else if (&quot;NUMBER&quot;.equalsIgnoreCase(dataType)) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; applicationSettings.put(key, new Double(value));<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } else if (&quot;BOOLEAN&quot;.equalsIgnoreCase(dataType)) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; applicationSettings.put(key, new Boolean(value));<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } else {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; applicationSettings.put(key, value);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp; }

Note: applicationSettings is a property of type Map in the UserProfile bean.
 
6. Define the the UserProfile as a managed bean in the faces-config.xml:

    &lt;managed-bean&gt;<br />        &lt;managed-bean-name&gt;userProfile&lt;/managed-bean-name&gt;<br />        &lt;managed-bean-class&gt;nl.amis.adffaces.UserProfile&lt;/managed-bean-class&gt;<br />        &lt;managed-bean-scope&gt;session&lt;/managed-bean-scope&gt;<br />    &lt;/managed-bean&gt; <br />

7. Add a toggle of some sort to the application to set and change the current user’s identity:

          &lt;af:selectOneChoice label=&quot;Logged in as&quot; value=&quot;#{userProfile.userId}&quot;   styleClass=&quot;txtBold&quot;   autoSubmit=&quot;true&quot;&gt;<br />            &lt;af:selectItem label=&quot;Sjoerd&quot; value=&quot;SJOERD&quot;/&gt;<br />            &lt;af:selectItem label=&quot;Lucas&quot; value=&quot;LUCAS&quot;/&gt;<br />            &lt;af:selectItem label=&quot;Harm&quot; value=&quot;HARM&quot;/&gt;<br />            &lt;af:selectItem label=&quot;Peter&quot; value=&quot;PETER&quot;/&gt;<br />          &lt;/af:selectOneChoice&gt;<br />&nbsp;

8. Add references to the Application Settings in the application, using EL Expressions referring to the userProfile:

rows=&quot;#{userProfile.applicationSettings.MAX_RECORDS_IN_TABLE}&quot;&nbsp;

Now we can run the application and see those personalizations taking effect.

Share.

About Author

Lucas Jellema, active in IT (and with Oracle) since 1994. Oracle ACE Director for Fusion Middleware. Consultant, trainer and instructor on diverse areas including Oracle Database (SQL & PLSQL), Service Oriented Architecture, BPM, ADF, Java in various shapes and forms and many other things. Author of the Oracle Press book: Oracle SOA Suite 11g Handbook. Frequent presenter on conferences such as JavaOne, Oracle OpenWorld, ODTUG Kaleidoscope, Devoxx and OBUG. Presenter for Oracle University Celebrity specials.

Comments are closed.