ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

JBoss Seam
Pages: 1, 2, 3, 4, 5

We want to ensure that back buttons and multiple tabs or windows won't break our application and have the expected behavior. To do so, we will introduce a conversation that will start whenever the search starts and ends once the user purchases his order. Let's start with a session bean that will handle the search operations and navigation among the results:



package com.jboss.dvd.seam;
import [...]

@Stateful
@Name("search")
@Conversational(ifNotBegunOutcome="main")
@Scope(ScopeType.CONVERSATION)
@Interceptors(SeamInterceptor.class)
public class SearchAction
    implements Search,
               Serializable
{
    @In(create=true)
    ShoppingCart cart;

    @PersistenceContext(unitName="dvd")
    EntityManager em;

    private int     pageSize    = 20;
    private int     currentPage = 0; 
    private boolean hasMore     = false;

    private Category category = null;
    private String   title    = null;
    private String   actor    = null;

    @Out(scope=CONVERSATION,required=false)
    List <Product> searchResults;

    @Out(scope=CONVERSATION,required=false)
    Map <Product, Boolean> searchSelections;

    // Getters and setters for 
    // category, title and actor

    @Begin(join=true, 
           processDefinition="shopping")
    public String doSearch() {
        currentPage=0;
        updateResults();

        return "browse";
    }

    public String nextPage() {
        if (!isLastPage()) {
            currentPage++;
            updateResults();
        }
        return null;
    }

    public String prevPage() {
        if (!isFirstPage()) {
            currentPage--;
            updateResults();
        }
        return null;
    }

    public boolean isLastPage() {
        return (searchResults != null) && 
               !hasMore;
    }
    public boolean isFirstPage() {
        return (searchResults != null) && 
               (currentPage == 0);
    }

    private void updateResults() {
        [...]
        searchResults = [...]
        searchSelections = [...]
    }


    private Query searchQuery(String title, 
           String actor, Category category) {
        title = (title == null) ? "%" : "%" + 
                   title.toLowerCase() + "%";
        actor = (actor == null) ? "%" : "%" + 
                   actor.toLowerCase() + "%";

        if (category == null) {
            return em.createQuery(
    "from Product p where lower(p.title) like " + 
    ":title and lower(p.actor) LIKE :actor")
                .setParameter("title", title)
                .setParameter("actor", actor);
        } 
        else { 
            return em.createQuery(
    "from Product p where lower(p.title) like " + 
    ":title and lower(p.actor) like :actor " + 
    "and p.category = :category")
                .setParameter("title", title)
                .setParameter("actor", actor)
                .setParameter("category", category);
        }
    }

    public String addToCart() {
        for (Product item: searchResults) {
            Boolean selected = 
              searchSelections.get(item);
            if ( selected!=null && selected ) {
                searchSelections.put(item, false);
                cart.addProduct(item, 1);
            }
        }
        return "browse";
    }

    @Destroy 
    @Remove
    public void destroy() {}   
}

This stateful session bean is stored in the conversation context; it will live throughout the conversation and die whenever the conversation ends. The shopping cart is another session-bean component managed by Seam and is injected into the SearchAction session bean, so that we don't lose its content across multiple searches. The searchResults and searchSelections objects will live in the current conversation; the memory they occupy will be freed once the conversation will be over.

Pages: 1, 2, 3, 4, 5

Next Pagearrow