Seam page list with enum param

Goal

Create a list page with criteria based on Seam’s EntityQuery whose param is an enum

Description

This recipe explains how we can use an enum as a parameter for filtering a result list in a page that is implemented in Seam 2.

How to

First, we make it possible for the enum converter from Seam be registered in faces-config.xml so that it may be referenced by name:

 <?xml version="1.0" encoding="UTF-8"?>
<faces-config version="1.2" xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xi="http://www.w3.org/2001/XInclude" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
  http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd">
  ...
  <converter>
    <description>Converter for Enums</description>
    <converter-id>enumConverter</converter-id>
    <converter-class>org.jboss.seam.ui.converter.EnumConverter</converter-class>
  </converter>
  ...
</faces-config>

Next, we specify the parameter in the page.xml file corresponding to the list page we are using. E.g., let us suppose we have the following scenario, with a class that will show a list of things in a page named MyListOfThings.xhtml, where thing has an enum property named type:

import com.linkare.seam.datamodel.EntityQuery;
import org.jboss.seam.annotations.Name;

@Name("myListOfThings")
public class MyListOfThings extends EntityQuery<Thing> {
  ... query and restrictions 
  private Thing thing = new Thing();
  public Thing getThing() {
    return thing;
  }
}

with the following corresponding MyListOfThings.page.xml (only the relevant code is shown):

<?xml version="1.0" encoding="UTF-8"?>
<page xmlns="http://jboss.com/products/seam/pages" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://jboss.com/products/seam/pages 
  http://jboss.com/products/seam/pages-2.2.xsd">
  ...
  <param name="organ" value="#{myListOfThings.thing.type}" 
    converterId="enumConverter" />
  ...
</page>

And that is all. Notice, by the way, that if we would like to have the same effect but instead of an enum property, we had a property named owner that was a relation to another JPA identity, our definiton of the converterId would be similar (the definition of the converter in the file faces-config.xml is not required, though, as that converter is already a Seam named component:

<?xml version="1.0" encoding="UTF-8"?>
<page xmlns="http://jboss.com/products/seam/pages" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://jboss.com/products/seam/pages 
  http://jboss.com/products/seam/pages-2.2.xsd">
  ...
  <param name="organ" value="#{myListOfThings.thing.owner}" 
    converterId="org.jboss.seam.ui.EntityConverter" />
  ...
</page>

Explanations

When no converter is specified in the param definition, that parameter seems to be interpreted as a String which causes erroneous behaviour. I cannot fully explain the reason why the definition of that property in the XHTML page with the appropriate converter associated to the input component is not enough for the filtering to process successfully and it was more or less a matter of luck or, as I would like to call it, an educated guess that took me to this implementation and the correction of my original problem.

By the way, one final note, with something that I realized it was not explained in this recipe, after discussing this issue with a colleague: the use of the parameters in the XML corresponding page is necessary, only if we want to be able to filter our results through a URL that contains the necessary parameter values!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s