Monday, September 24, 2012

Sorting a bean List with Commons

Nowadays whenever I surf the net, comparable interface is widely used to sort a List of objects. I am very sure it's the best approach, no doubt in that :)

However, if you want to sort in more than one place of your projects, I feel the code will be redundant. Am I right?

The alternate and the best solution is to use the Apache Commons Bean Utils


And this is how I did it:

Asceding order:
For ascending order, we should give the parameter alone. For instance, if we want sort by “endDate”. We should pass the parameter endDate to the BeanComparator class instance.


Reverse Order Sorting(Descending Order):
For descending order, we should give the parameter along with the instance of ReverseComparator class. For instance, if we want to sort by “firstName”. We should pass two parameters, such as the endDate and the instance of ReverseComparator class to the BeanComparator class.


Note:
Just keep in mind that your fild must be NON-NULL otherwise you'll get a pretty NPEx slap in your face.

Saturday, September 15, 2012

Keep parameter in session with Injection and Outjection

This time something kind of weird happened to me.

To be honest, each time I have to work with Jboss Seam, I always say to myself: "I wonder what kind of surprise waits for me this time". Because I'm a Spring Framework dude, and JBoss Seam documentation, well, is not really clear at all.

This is my case:
I have a page that receives a parameter from an external source, the page is displayed, user pays around with it, page gets refreshed a lot of times. And then, user submits page and everything and goes away, then is when I need that parameter received at the beginning. Because after the submit I have to pass it to another page which need it

The solution will be something like: come on dude! that's Servlets 101. Just set the parameter in the session the first time and that's it! Now the question is, what happens to that value set into the session? Because I want to use it on my pages.xml file to redirect the request to another page though!
And, very important, I want to write as less code as possible (by using Seam Annotations)

But better if we see some code:

The param received (passed by an external caller) is:
By doing this I'm keeping in Session this variable:
Very important to keep the parameter stored in session only once during page Creation!

This is my pages.xml, where I'm passing as param resume the value of my In/Outjected variable resumePersistentParam