Java Cart .

Java Cart ItemScreenWrangler java

package cart.screenWranglers;

import cart.Category;
import cart.Item;
import cart.ItemInventory;
import cart.ItemOfGroup;
import cart.ItemOfStuffWSizeColor;
import cart.applicationBeans.CategoryBean;
import cart.applicationBeans.ItemBean;
import cart.applicationBeans.ItemCategoryBean;
import cart.applicationBeans.ItemInventoryBean;
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;

//ItemScreenWrangler
//author - Lawrence Truett - FluffyCat.com
//date - June 18, 2003 - San Diego, CA
public class ItemScreenWrangler extends BaseScreenWrangler {
    
    public ItemScreenWrangler() {this("/cart/jsps/Item.jsp");}
    
    public ItemScreenWrangler(String nextScreenIn) {
        super(nextScreenIn);
    }
    
    public void wrangleScreen(HttpServletRequest request, 
                              ServletContext servletContext) {
        
        char action = baseNextScreenSwitch(request, servletContext);
        
        if (action != 'I') {
            getNextWrangler().prepFirstScreen(request, servletContext);
        } else {
            StringBuffer itemSubs = allScreens(request, servletContext);
            request.setAttribute("jspItem", itemSubs);
            jspCatAttribute(request);
        }
    }
    
    void prepFirstScreen(HttpServletRequest request, 
                         ServletContext servletContext) {
        StringBuffer itemSubs = allScreens(request, servletContext);
        request.setAttribute("jspItem", itemSubs);
        jspCatAttribute(request);
    }
    
    StringBuffer allScreens(HttpServletRequest request, 
                            ServletContext servletContext) {
        ItemBean itemBean = 
            (ItemBean)servletContext.getAttribute("itemBean");
        ItemInventoryBean itemInventoryBean = 
            (ItemInventoryBean)servletContext.getAttribute(
                "itemInventoryBean");
        StringBuffer itemSubs = new StringBuffer();
        String stringKey = request.getParameter("item");
        if (stringKey == null) {stringKey = "0";}
        request.getSession().setAttribute("lastItem", stringKey);
        long key = Long.parseLong(stringKey);
        if (key != 0) {
            Item item = itemBean.getItem(key);
            if (item instanceof ItemOfGroup) {
                itemSubs.append("Item Group");
                itemSubs.append("
Key: " + item.getKey()); itemSubs.append("
Description: " + item.getDescription()); long[] itemsInGroup = ((ItemOfGroup)item).getItemsInGroup(); itemSubs.append("
Number of items in group: " + itemsInGroup.length); itemSubs.append("
"); for (int counter = 0; counter < itemsInGroup.length; counter++) { Item groupItem = itemBean.getItem(itemsInGroup[counter]); itemSubs.append(this.itemCommon(request, servletContext, groupItem)); itemSubs.append(""); } } else { itemSubs.append(this.itemCommon(request, servletContext, item)); } } return itemSubs; } StringBuffer itemCommon(HttpServletRequest request, ServletContext servletContext, Item item) { ItemBean itemBean = (ItemBean)servletContext.getAttribute( "itemBean"); ItemInventoryBean itemInventoryBean = (ItemInventoryBean)servletContext.getAttribute( "itemInventoryBean"); StringBuffer itemSubs = new StringBuffer(); long key = item.getKey(); itemSubs.append("
Key: " + item.getKey()); itemSubs.append("
Price: " + item.getPrice()); itemSubs.append("
Description: " + item.getDescription()); if (item.getHasInventory()) { ItemInventory itemInventory = itemInventoryBean.getItemInventory(key); long inventory = itemInventory.getInventory(); long reserved = itemInventory.getReserved(); itemSubs.append("
Inventory: " + inventory); itemSubs.append("
Reserved: " + reserved); if ((inventory - reserved) > 0) { itemSubs.append( "<"+ "a href="/servlet/servlet.ControllerServlet?"+ "action=R&subaction=A&item="); itemSubs.append(item.getKey() + "&amount=1">Add To Cart"); } else { itemSubs.append("
Item is out of Stock"); } if (item instanceof ItemOfStuffWSizeColor) { itemSubs.append("
Size: " + ((ItemOfStuffWSizeColor)item).getSize()); itemSubs.append("
Color: " + ((ItemOfStuffWSizeColor)item).getColor()); } } else { itemSubs.append("
Inventory: NA"); itemSubs.append("
Reserved: NA"); itemSubs.append("<"+ "a href="/servlet/servlet.ControllerServlet?"+ "action=R&subaction=A&item="); itemSubs.append(item.getKey() + "&amount=1">Add To Cart"); } itemSubs.append("
"); itemSubs.append(""); return itemSubs; } }
To download source right-click here and "Save As...".
Comments
Sign in to be the first to comment on Java Cart ItemScreenWrangler java.