Instant field conversion – Rapid Data entry and SMS Speak converter – using ADF Faces PPR with Customer JSF Converters

0

While preparing for my paper Getting the most AJAX out of ADF Faces – Developing really rich web applications for the upcoming ODTUG 2007 Kaleidoscope conference, I dabbled a little deeper in various aspects of the declarative AJAX (partial page rendering) functionality in ADF Faces. I ran into a nice little application of this feature, that could actually be quite useful to end users. 

In short: using a customer converter on a text input field with autoSubmit=true and the field in its own partialTriggers attribute, we can easily turn user input into something else immediately after the user tabs out of the field. This would allow us to provide, for example:

  • SMS Speak to normal language conversion (for rapid text entry) (or vice versa, for the older generations)
  • rapid code entry – for example enter ISO Country Code and turn it into the full Country Name
  • Spelling Corrector on text areas
  • Language Translator (turn to German or French, for example using BableFish)

And many more nice little productivity enhancing pieces of functionality.

 

One of the really nice things about all this, is the relative ease of implementation. Not a single letter of JavaScript is required, and only very straightforward Java programming and a little bit of....
configuration.

The steps

  1. create a custom converter that turns the value entered by the user into whatever value it should have
  2. register that converter in the faces-config.xml file 
  3. go to the InputText field that we want to have instant conversion added to.
  4. make sure you specify a value for the id attribute
  5. set autoSubmit to true for the component
  6. add its own id value to the partialTriggers attribute
  7. add an f:converter child element to the InputText. Set its converterId attribute to the custom converter registered in step 2.

 

That’s it. Now you can run the page and see the instant conversion in action.

Demonstration

Suppose we have a country input field in a JSF page. It’s a simple inputText where the user can enter the name of the country of say a customer. However, we will provide a short-cut for our end users: they can simply type the two letter ISO country code – for example us, uk, nl and de – and have the application turn that entry into the full country name. We need a CountryConverter – that turns the two letter code into the full name -, add that converter to the country field, set autoSubmit to true – to initiate the PPR cycle that will also activate the CountryConverter and include the id of the country field in its own partialTriggers attribute.

The CountryConverter class – a very simple rendition that only supports a few countries – could look like this:

package nl.amis.crm.model;<br /><br />import java.util.HashMap;<br />import java.util.Map;<br /><br />import javax.faces.component.UIComponent;<br />import javax.faces.context.FacesContext;<br />import javax.faces.convert.Converter;<br /><br /><br />public class CountryConverter implements Converter {<br /><br />    Map countries = new HashMap(10);<br />    public CountryConverter() {<br />        countries.put(&quot;us&quot;,&quot;United States of America&quot;);<br />        countries.put(&quot;nl&quot;,&quot;The Netherlands&quot;);<br />        countries.put(&quot;fr&quot;,&quot;France&quot;);<br />        countries.put(&quot;uk&quot;,&quot;United Kingdom&quot;);<br />        countries.put(&quot;de&quot;,&quot;Germany&quot;);<br />        countries.put(&quot;in&quot;,&quot;India&quot;);<br />    }<br />    <br />    public Object getAsObject(FacesContext facesContext, <br />                              UIComponent uiComponent, String string) {<br />        String country = string;<br />        if (country.length()==2 &amp;&amp; countries.containsKey(country)) {<br />          country= (String)countries.get(country);<br />        }<br />        return country;<br />    }<br /><br />    public String getAsString(FacesContext facesContext, <br />                              UIComponent uiComponent, Object object) {<br />        return object.toString();<br />    }<br /><br />}<br />&nbsp;

The registration of this converter in faces-config.xml file:

&lt;converter&gt;<br />   &lt;converter-id&gt;CountryConverter&lt;/converter-id&gt;<br />   &lt;converter-class&gt;nl.amis.crm.model.CountryConverter&lt;/converter-class&gt;<br />&lt;/converter&gt;&nbsp;

Now the configuration of this converter on the inputText element:

&lt;af:inputText  id=&quot;country&quot; label=&quot;Country&quot; partialTriggers=&quot;country&quot; autoSubmit=&quot;true&quot;&gt;<br />  &lt;f:converter converterId=&quot;CountryConverter&quot;/&gt;<br />&lt;/af:inputText&gt;<br />&nbsp;

And that is all we need, we are ready to run this little nugget.

if you enter a value such as us or uk or nl in the Country field and tab out of it, the autosubmit + converter + PPR action will update the field near-instanteously with the full country name.

SMS Speak Converter – From CU and L8R to See You and Later

Using this mechanism the possibilities for making life easier for our end users are virtually limitless. And an abundance of Converters especially to serve this need could become available. Let’s look at an another simple example for applying this idea. Suppose we have a text area where our end users can write summaries of telephone conversations they have had with customers or write any sort of report. The de facto steno language of today is probably SMS Speak (see for example http://sites.ninemsn.com.au/minisite/web2sms/help/smsdict.asp for some of the most popular terms in SMS Speak).

 

The steps are the same as before:

  • Develop Converter class
  • Register Converter
  • Configure inputText component

Converter Class:

package nl.amis.crm.model;<br /><br />import java.util.HashMap;<br />import java.util.Map;<br /><br />import java.util.Set;<br /><br />import javax.faces.component.UIComponent;<br />import javax.faces.context.FacesContext;<br />import javax.faces.convert.Converter;<br /><br />public class SMSSpeakConverter implements Converter {<br /><br />    Map smsTranslation = new HashMap(10);<br />    public SMSSpeakConverter() {<br />        smsTranslation.put(&quot;AFAIK&quot;,&quot;as far as I know&quot;);<br />        smsTranslation.put(&quot;ASAP&quot;,&quot;as soon as possible&quot;);<br />        smsTranslation.put(&quot;A3&quot;,&quot;anytime, anywhere, anyplace&quot;);<br />        smsTranslation.put(&quot;CU&quot;,&quot;see you&quot;);<br />        smsTranslation.put(&quot;MTE&quot;,&quot;my thoughts exactly&quot;);<br />        smsTranslation.put(&quot;10Q&quot;,&quot;thank you&quot;);<br />    }<br />    <br />    public Object getAsObject(FacesContext facesContext, <br />                              UIComponent uiComponent, String string) {<br />        String text = string;<br />        for (String key:(Set&lt;String&gt;)smsTranslation.keySet()) {<br />          text = text.replaceAll(key.toLowerCase(), (String)smsTranslation.get(key));<br />        }<br />        <br />        return text;<br />    }<br /><br />    public String getAsString(FacesContext facesContext, <br />                              UIComponent uiComponent, Object object) {<br />        return object.toString();<br />    }<br /><br />}<br /><br />&nbsp;

Registration in faces-config.xml:

&lt;converter&gt;<br />   &lt;converter-id&gt;SMSSpeakConverter&lt;/converter-id&gt;<br />   &lt;converter-class&gt;nl.amis.crm.model.SMSSpeakConverter&lt;/converter-class&gt;<br />&lt;/converter&gt;<br />&nbsp;

InputText component:

&lt;af:inputText label=&quot;Summary&quot; id=&quot;summary&quot; partialTriggers=&quot;summary&quot;  autoSubmit=&quot;true&quot; <br />              columns=&quot;50&quot; rows=&quot;6&quot;&gt;<br />  &lt;f:converter converterId=&quot;SMSSpeakConverter&quot; /&gt;<br />&lt;/af:inputText&gt;<br />&nbsp;

And run the application with the above shown 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.