concept collection in category seam

This is an excerpt from Manning's book Seam in Action.
To demonstrate some of the core principles of Seam, I’m going to step you through a basic application that manages a collection of golf tips. Don’t worry about trying to understand everything that you see here. Instead, focus on how Seam relies on annotations to define components, how the layers of the application are pulled together through the unified component model, and the high signal-to-noise ratio in the business logic thanks to configuration by exception. I demonstrate a densely packed set of features in this example, so don’t think that you have to use all of these techniques in order to use Seam.
Before we dive into factories, I want to clarify the benefit they provide over just using a JavaBean-style “getter” method on a component to retrieve data (such as the list of new golfers). What you must understand is that the EL resolver is very persistent about resolving value expressions. If you put logic in a getter method, it may get called a ridiculous number of times in one request. In a JSF view, there are a lot of places where you need to repeatedly access a calculated value to perform such tasks as conditional rendering (checking whether a collection is empty, perhaps), and the branches of the UI component tree where these expressions are located are each visited several times during the JSF life cycle. It’s just a natural part of how JSF and the EL work.
Listing 6.5. A component that manages the collection of new golfers
package org.open18.action; import org.jboss.seam.ScopeType; import org.jboss.seam.annotations.*; import org.open18.model.Golfer; import javax.persistence.EntityManager; import java.util.*; @Name("newGolfersList") @Scope(ScopeType.APPLICATION) public class NewGolfersList { private int poolSize = 25; private int displaySize = 5; @In protected EntityManager entityManager; protected List<Golfer> newGolfers; public void setPoolSize(int poolSize) { this.poolSize = poolSize; } public void setDisplaySize(int displaySize) { this.displaySize = displaySize; } @Create public void onCreate() { fetchNewGolfers(); } @Unwrap public List<Golfer> getNewGolfers() { return newGolfers; } @Observer(value = "golferRegistered", create = false) synchronized public void fetchNewGolfers() { List<Golfer> results = entityManager.createQuery( "select g from Golfer g order by g.dateJoined desc") .setMaxResults(poolSize).getResultList(); Collections.shuffle(results); Random random = new Random(); while (results.size() > displaySize) { results.remove(random.nextInt(results.size())); } newGolfers = results; } }
In JSF, UIData components, such as <h:dataTable>, are backed by a special collection wrapper called a data model. A data model is a way for JSF to adapt various types of collections to the UI component model in a consistent way and to support capturing a row selection made by the user. The collection wrappers extend from the abstract class javax.faces.DataModel and support the major collection types. Seam builds on this set by adding support for the Query component from the Seam Application Framework. The Query component manages the result of a JPQL/HQL query, which Seam retrieves and wraps in a ListDataModel. You’ll learn about the Query component in chapter 10. The mapping between the collection types and their wrappers is shown in table 6.3.
Table 6.3. The corresponding JSF data model wrapper for each type of collection
Native Collection
JSF DataModel Wrapper javax.faces.model.*
java.util.List ListDataModel Array ArrayDataModel java.util.Map MapDataModel java.util.Set SetDataModel org.jboss.seam.framework.Query ListDataModelwraps Query#getResultList()