Java Cart .

Java Cart CategoryScreenWrangler java

package cart.screenWranglers;

import cart.Category;
import cart.Item;
import cart.applicationBeans.CategoryBean;
import cart.applicationBeans.ItemBean;
import cart.applicationBeans.ItemCategoryBean;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.ListIterator;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.ServletContext;

//CategoryScreenWrangler
//author - Lawrence Truett - FluffyCat.com
//date - June 6, 2003 - San Diego, CA
public class CategoryScreenWrangler extends BaseScreenWrangler {
    
    public CategoryScreenWrangler() {this("/cart/jsps/Category.jsp");}
    
    public CategoryScreenWrangler(String nextScreenIn) {
        super(nextScreenIn);
    }
    
    public void wrangleScreen(HttpServletRequest request, 
        ServletContext servletContext) 
    {
        
        char action = baseNextScreenSwitch(request, 
                                           servletContext);

        if (action != 'C') {
            getNextWrangler().prepFirstScreen(request, 
                                              servletContext);
        } else {
            StringBuffer catSubs = this.allScreens(request, 
                                                   servletContext);
            request.setAttribute("jspCatList", catSubs);
            HttpSession httpSession = request.getSession();
            httpSession.setAttribute("prevQueryString", 
                                      request.getQueryString());
            httpSession.setAttribute("prevAction", action + " ");
        }
    }
    
    void prepFirstScreen(HttpServletRequest request, 
                         ServletContext servletContext) {
        StringBuffer catSubs = allScreens(request, servletContext);
        request.setAttribute("jspCatList", catSubs);
    }
    
    StringBuffer allScreens(HttpServletRequest request, 
                            ServletContext servletContext) {
        StringBuffer catSubs = new StringBuffer();
        
        CategoryBean catBean = (CategoryBean)servletContext.getAttribute(
            "categoryBean");
        
        String stringKey = request.getParameter("category");
        if (stringKey == null) {stringKey = "0";}
        request.getSession().setAttribute("lastCategory", stringKey);
        long key = Long.parseLong(stringKey);
        Category currentCat;
        
        if (key != 0) {
            Category current = catBean.getCategory(key);
            if (current != null  ) {
                catSubs.append("
CURRENT
" + current.getDescription() + "
"); } } catSubs.append("
DOWN
"); ListIterator csli = catBean.getSubCategories(key).listIterator(); if (!csli.hasNext()) { catSubs.append("no sub categories
"); } else { while (csli.hasNext()) { currentCat = (Category)csli.next(); catSubs.append( "" + currentCat.getDescription() + "" + "
"); } } if (key != 0) { catSubs.append("
SIBLINGS
"); ArrayList siblingsAndSelf = catBean.getChildrenForParent(key); csli = siblingsAndSelf.listIterator(); if (!csli.hasNext() || siblingsAndSelf.size() == 1) { catSubs.append("no siblings
"); } else { while (csli.hasNext()) { currentCat = (Category)csli.next(); if (currentCat.getKey() != key) { //don't show current cat as a sibling catSubs.append("" + currentCat.getDescription() + "" + "
"); } } } } if (key != 0) { Category parent = catBean.getParentCategory(key); if (parent != null ) { catSubs.append("UP" + parent.getDescription() + "" + "
"); } } if (key != 0) { //now we get some actual items, not sure what //screen this would ultimately be done on... catSubs.append("
ITEMS
"); ItemCategoryBean itemCatBean = (ItemCategoryBean)servletContext. getAttribute("itemCategoryBean"); ItemBean itemBean = (ItemBean)servletContext. getAttribute("itemBean"); ArrayList items = itemCatBean.getItems(key); if ((items == null) || (items.size() == 0)) { catSubs.append("no items
"); } else { ListIterator itemsLi = items.listIterator(); while (itemsLi.hasNext()) { Long iKey = (Long)itemsLi.next(); Item item = itemBean.getItem(iKey); if (item != null) { catSubs.append("" + item.getDescription()); catSubs.append(""); catSubs.append("
"); catSubs.append("
"); } } } } return catSubs; } }
To download source right-click here and "Save As...".
Comments
Sign in to be the first to comment on Java Cart CategoryScreenWrangler java.