package cart.applicationBeans; import cart.ItemCategory; import cart.dbUtils.ItemCategoryDBUtil; import cart.dbUtils.DBConnection; import java.util.ArrayList; import java.util.HashMap; import java.util.ListIterator; //ItemCategoryBean - loads ItemCategory into a structure for display, // keeping the most recently ones called // also could determine which items are displayed // using avail dates, in stock, etc... //author - Lawrence Truett - FluffyCat.com //date - June 16, 2003 - San Diego, CA public class ItemCategoryBean { private HashMap itemCats = new HashMap(); //must have empty constructor to be a bean public ItemCategoryBean() { //builditems(dbc); } //only a good idea if you have a limited number of item-categories public ItemCategoryBean(DBConnection dbc) { ListIterator li = (ItemCategoryDBUtil. getAllItemCategoriesByCategory(dbc)). listIterator(); long prevCategory = 0; long thisCategory = 0; ArrayList items = new ArrayList(); while (li.hasNext()) { ItemCategory itemCategory = (ItemCategory)li.next(); thisCategory = itemCategory.getCategoryKey(); if ((!items.isEmpty()) && (prevCategory != thisCategory)) { itemCats.put(new Long(prevCategory), items); items = new ArrayList(); } items.add(new Long(itemCategory.getItemKey())); prevCategory = thisCategory; } if (!items.isEmpty()) {//add last category itemCats.put(new Long(prevCategory), items); } } //returns item only if in hashMap public ArrayList getItems(long key) { Long lKey = new Long(key); if (itemCats.containsKey(lKey)) { return (ArrayList)itemCats.get(lKey); } else { return null; } } /* public Item getItem(long key, DBConnection dbc) { Long lKey = new Long(key); if (items.containsKey(lKey)) { return (Item)items.get(lKey); } else { Item item = ItemDBUtil.getItemForKey(key, dbc); if (null != item) {itemsUpdateAlgo(item);} return item; } } //puts new items into items HashMap, // with some algorythm to delete lesser used entries private void itemsUpdateAlgo(Item item) { items.put(new Long(item.getKey()), item); }*/ }