Wednesday, 25 July 2007

Plugabble XML Validations - Use the Jakarta Struts Validations from your Swing application.

Even though Swing is not cool anymore (yes, it was back in the day because it was much better than AWT, believe me) and everybody out there is talking about RIA (you could say that Swing is RIA too ;) ) it's not very unlikely that you'd find yourself in the situation you'd to work with it. In fact, it happened to me this year twice.

There a lot of companies out there using Swing (specially financial ones), and even though some of them in the future will change to the "RIA experience" (if one does, everybody else will, yes SOA I'm looking at you ;D ), in the meantime it's good to improve the "Swing experience" :) .

Like I said, I found myself working in a Swing based project, and the first thing I realized (among others) is that the validations and errors messages were hard coded, repeated and spreaded all over the code. One amateur mistake, you'd say but it's not that uncommon.

To prevent that I integrated the "Jakarta Commons Validator" engine that helps you to reuse sets of validations and messages. If you have used Struts, you'd know it for sure.

The thing is that the JCV package does not provide any pluggable validations out of the box (it provides some validations routines to help you out).

I though the best way would be using something like the Struts Validation Plugin, that included many different validations configured by xml but for a standalone application.

So that's what I did. Now you can use the same Struts Validations (same config files, tags etc.. have a look at the Struts guide), but from your Swing application.

Links

Jakarta Commons Validator

Struts Validations Guide



Monday, 16 July 2007

YUI Live Search (aka AJAX AutoComplete) - Integration with Struts - II

Hi all,

I´ve been very busy lately landing into a new job, so I couldn't add any new entries.

I´ve received a couple of questions about the autocomplete component, and it seems that everybody gets confused in the same part.

The autocomplete component is esentially a ui component, so it does not provide any function to filter the results in the server. That means you have to implement you own search and return it to the client. If you don´t do that, more likely you will get all the results at once.

However, it has a filter function in the client side if you activate the cache, but you would have to return all the results the first time to make it work, and it´s not the way is intended.

Here it is the sources for an example of the autocomplete, provided by Rohitesh (thanks!).




In the next entries I will exploring some not very hot topics, but useful ones like an integration of the Jakarta Commons Validator with Swing (using the struts validations), a little Swing framework, and the use of Cactus Testing Framework with JBoss 4.

It´s amazing the number of companies that still use technologies like swing and ejbs (entities), and the thing is that using some helping tools, it works (with some effort, that is).

Thursday, 26 April 2007

YUI Live Search (aka AJAX AutoComplete) - Integration with Struts

Hi!

Well, if you hadn´t had a look at the YUI LiveSearch component (http://developer.yahoo.com/yui/autocomplete/) go for it. It´s worth the effort.

Autocomplete Component and DataSource.

YUI is just a library of AJAX components, so you would need to integrate it with you favourite framework. However, it´s shipped with some javascript libraries that wrap the XMLHttpRequestObject, so you don´t have to use it like that. Instead it uses the concept of DataSource to send queries and retrieve data:

var serverURL = "/search/liveSearch.do"; //service URL
var schema = ["Results","Id","Name", "Description"]; // data mapping format


var dataSource = new YAHOO.widget.DS_XHR(myServer, mySchema);
dataSource.responseType = YAHOO.widget.DS_XHR.TYPE_JSON;
dataSource.queryMatchSubset = true;
dataSource.maxCacheEntries = 50; // component internal cache size.

this datasource must be passed in the autocomplete constructor:

var liveSearchComponent = new YAHOO.widget.AutoComplete("textField","containerId", datasource);

textField: input search text field component.

containerId: div id that will hold the results.

Well, thats basically what you would need to know to create the datasource and the component (along with some configuration steps, have a look at the YUI tutorial).


Search Service.

We can get the results using a search service invoked by a struts action:

public String search(String searchText, int numMaxResults);

Using this service we should get the data following the format specified in the datasource. In this case we need to get the data in JSON (java script object notation) format:

{"Results":
[{"Id" : "1", "Name": "Lou Reed", "Description
": "NYC Man"},
[{"Id" : "2", "Name": "Lou Dylan","Description": "the man!"}]}
 
Service code using the JSONObject:
 
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class SearchServiceImpl implements SearchService {

public String search(String searchText,...) {
//here goes the code to do the search
//once obtained the results must be formatted using json

JSONObject json = new JSONObject();
JSONArray result = new JSONArray();
JSONObject rowOne = new JSONObject();
JSONObject rowTwo = new JSONObject();
try {
rowOne.put("id", "1");
rowOne.put("Name", "Lou Reed");
rowOne.put("Description", "NYC Man");
result.put(rowOne);

rowTwo.put("id", "2");
rowTwo.put("Name", "Lou Dylan");
rowTwo.put("Description", "the man!");
result.put(rowTwo);
json.put("Results", result);
}
catch (JSONException e) {
//umm a checked exception!! :)
//do something!
}
return json.toString();
};

That would be the search service, of course it could be whatever you want, a POJO, a Web Service etc.. the only thing to keep in mind is returning the data using the JSON format (specified in the datasource schema).

JSON Struts Action

It´s almost finished! we only need the controller code, to return the JSON String. In order to that, we need to write a special kind of action. Intead of returning and instance of action mapping, we should get the response object and flush the JSON object to the client:

public ActionForward execute (ActionMapping mapping,...) {

String json = searchService.search(searchText,...);

res.setContentType(TEXT_JAVASCRIPT);res.setHeader("Cache-Control", "no-cache");
PrintWriter pw = res.getWriter();

pw.write(json);pw.close();
return null;

Of course, you could do an abstract JSON Action class, or a helper to reuse it through the application.

Sunday, 22 April 2007

Another Entry, Another Hype

Hi guys!

I´m going to share with you some technical stuff about Ajax. And the reason is that I´ve been working lately a lot with it, because you know, these days you just can´t use a standard dropdown, you need an ajax driven dropdown or a web 2.0 dropdown.

I suppose you know what Ajax is, if not check out this link:

http://en.wikipedia.org/wiki/Ajax_%28programming%29. (where have you been, in the Lost Island?, by the way I´ve got a crazy theory that I´ll share with you :) )

Generally speaking, Ajax components could improve the user experience, if correctly used. For example the live search component, or the google search, I´m sure you know it; you just type some characters and the result appears without refreshing the page. You go on typing and more results appear.

This component could save a lot of time and pain in a web application, I´ve used it to replace a conventional search (yes, with a pop up and pagination buttons, oh my!), replacing the manual pagination through hundreds of results for a simple search box. You just type a 3 o more chars and the results appear in DIV, under the box or next to it. Even you can have links inside the results!.

So what I will do it´s explain to you how to integrate a Live Search component in your application using different frameworks and components.

Ok, let´s have a look at the frameworks then. There are many of them, almost every software company has one, it´s the sign of the times. I´ll talk about the ones I know and I have used:

- YUI (Yahoo User Interface): http://developer.yahoo.com/yui/.

- DWR (Direct Web Remoting): http://getahead.org/dwr.

- GWT (Google Web Toolkit): http://code.google.com/webtoolkit/.

- Moo.Fx : http://moofx.mad4milk.net.

- Rico: http://openrico.org/rico/home.page.

I´ll start with the YUI framework and the integration of the YUI live seach component with Struts, JSF and Spring MVC. That will be in the next entry.

See you!