Sunday, November 8, 2009

Automatically remove HTML remarks with Facelets

If you are working with Facelets, you can easily remove all you HTML remarks on the resulted page, by simply adding this directive in your web.xml:

<context-param>
  <param-name>facelets.SKIP_COMMENTS</param-name>
  <param-value>true</param-value>
</context-param>

Saturday, November 7, 2009

Using jQuery with JSF

JSF is using its own ids naming convention in order to prevent duplicate ids on the generated HTML page. JSF uses “:” as id separator. When you wish to use jQuery with JSF, this raises a problem: jQuery uses the “:” character in it’s core libraries as a selector of elements. Therefore, suppose we have a field named: someInput, which used in a form named: someForm, JSF will name the field in the resulted HTML as: someForm:someInput. In order to get the filed element in JQuery we should do:

var value = $('#someForm:someInput').val();
But, unfortunately jQuery treats the “:” character as a special character and therefore misunderstands our request. In order to override this problem, jQuery allows escaping the “:” character. escaping is done by putting “\\” before the “:”. In our example it will look like:
var value = $('#someForm\\:someInput').val();

Friday, November 6, 2009

Hibernate Paging with Previous and Next

Usually in web applications we have to show some table or list of data to the user. Over time this data is growing. Most of the times, the amount is data is not too big. Therefore, in many cases, it is enough loading all the data to memory, then showing it to the user. But, there are time in which the amount of data is too big. Therefore, it wouldn't be wise loading all the data on the server, simply in order to show just a part of it. Hibernate gives us a neat way of partially loading data by supplying 2 methods for the Query class:

  • setFirstResult: This method sets the first result we would like to see in our our list. Suppose we are getting all photos of a user, by doing something like: “select user.photos from User user”. The user is currently having 1000 pictures, but we want to show pictures starting from the 100th picture. We can use setFirstResult to accomplish that task.
  • setMaxResults: This method sets the maximum results that will be returned by the query. For example, if we would like to return only 100 photos for our user, we would do something like: query.setMaxResults(100).

As we can see we can use setFirstResult and setMaxResults to control which part of our list will be actually returned by the query. Of course, that Hibernate utilizes the specific dialect of the database server we are using to efficiently accomplish the task of fetching only a portion of the data.

We would like to use these 2 hibernate methods to construct paging effect in Hibernate. By saying “paging” we mean, that we would like to give Hibernate 2 parameters: in which page we are, and what is the size of our page. In return we would like to receive the items in that specific page. Accomplishing this task in Hibernate is quite easy and can be done by using simple math on the page and page size parameters. For example, if we would like Hibernate to return items in page 5 and in each page we would like to show 20 items, we can do:

query.setFirstResult(5 * 20).setMaxResults(20).list();

or in more general form:

query.setFirstResult(page * pageSize).setMaxResults(pageSize).list();

Showing the items for a specific page isn’t enough. We also would like to be able to tell if in a specific page there are more items. This will allow us to add “Next” button on out user interface. The trick to know if there is a next page, it to get one more additional item. If there is such item, we can know that there is a next page. This trick costs us in getting only one more item and therefore is pretty efficient in terms of performance. So, the general form for getting items shown above can be changed to:

query.setFirstResult(page * pageSize).setMaxResults(pageSize + 1).list();

Let’s take a look at a class named PagedResults, that gets an Hibernate query, page number, page size and in return gives us list of items according to the desired page and page size, and 2 more methods: isNextPage, isPreviousPage, that gives us indication if there are more pages or if we are at the first page:

package com.bashan.blog.persistence;
import org.hibernate.Query;
import java.util.List;
public class PagedResults {
  private List results;
  private int pageSize;
  private int page;
  public PagedResults(Query query, int page, int pageSize) {
    this.page = page;
    this.pageSize = pageSize;
    results = query.setFirstResult(page * pageSize).setMaxResults(pageSize + 1).list();
  }
  public boolean isNextPage() {
    return results.size() > pageSize;
  }
  public boolean isPreviousPage() {
    return page > 0;
  }
  public List getList() {
    return isNextPage() ?
        results.subList(0, pageSize) : results;
  }
}

Note that the method getList returns sub list of the original list. That is because the original list may return one additional item that we use only to know if there is a next page. We don’t want to show this item on our user interface.

Nicely truncating text with Java (by adding “…”)

Sometimes we have to show long text in place that is too small. Simply truncating text is not so nice, and it does not visually implies that there is more text after the truncation. We can use Java to easily truncate text nicely by adding “…” instead of the truncated part. We will construct 2 functions:

  • Truncate a string to a fixed maximum length and leave a request amount of characters from the end of the string. For example, if we have the text: “This is some long text test, This is some long text test, This is some long text test”, and we would like to truncate the text to be maximum size of 30 characters and leave 12 characters from the end of the string, the result would look like: “This is some lo...ng text test”. Note, that the “…” along with the string itself constructs no more than 30 characters.
  • The second function is a special case of the first function: we would like to truncate a string to a fixed maximum length, by leaving “…” at the end of the string. For example if we use the previous string, the result of truncating it will look like: “This is some long text test...”. Note, that again, the maximum length of string along with the “…” is no more than 30 characters.

Writing these 2 functions in Java is a very simple task that mainly takes advantage substring method:

package com.bashan.blog.string;
public class StringUtils {
  public static String truncateText(String str, int maxLen, int lenFromEnd) {
    int len;
    if (str != null && (len = str.length()) > maxLen) {
      return str.substring(0, maxLen - lenFromEnd - 3) + "..." +
          str.substring(len - lenFromEnd, len);
    }
    return str;
  }
  public static String truncateText(String str, int maxLen) {
    return truncateText(str, maxLen, 0);
  }
  public static void main(String[] args) {
    String str = "This is some long text test, This is some long text test, This is some long text test";
    System.out.println(truncateText(str, 30, 12));
    System.out.println(truncateText(str, 30));
  }
}

At the end we can see a small test program. The output for it is:

This is some lo...ng text test
This is some long text test...