package cart.applicationBeans; import cart.Category; import cart.dbUtils.CategoryDBUtil; import cart.dbUtils.DBConnection; import java.util.ArrayList; import java.util.HashMap; import java.util.ListIterator; //CategoryBean - loads all categories into a structure for display, // one data load for all users, // also could determine which categories are displayed // using avail dates, in stock, etc... // if you have too many categories for this structure // to work, consider calling the dbms directly or // a buffering structure like ItemBean //author - Lawrence Truett - FluffyCat.com //date - May 21, 2003 - San Diego, CA public class CategoryBean { private HashMap categories; //must have empty constructor to be a bean public CategoryBean() { //buildCategories(dbc); } public CategoryBean(DBConnection dbc) { buildCategories(dbc); } public Category getCategory(long key) { return ((CategoryPlus)(categories.get(new Long(key)))).getCategory(); } public ArrayList getSubCategories(long key) { return ((CategoryPlus)(categories.get(new Long(key)))).getChildren(); } //needs a dbc, could load into a HashMap if this is used much, // alternative is getChildrenFor Parent public ArrayList getSiblingCategories(long key, DBConnection dbc) { return CategoryDBUtil.getSiblingCategoriesForKey(key,dbc); } //an alternative to getSiblingCategories, // may actually turn out to be more efficent public ArrayList getChildrenForParent(long key) { return getSubCategories((getParentCategory(key).getKey())); } public Category getParentCategory(long key) { long parentKey = ((CategoryPlus)categories.get( new Long(key))).getCategory().getParent(); return ((CategoryPlus)categories.get( new Long(parentKey))).getCategory(); } private void buildCategories(DBConnection dbc) { long key = 0; ArrayList tempCatList = new ArrayList(); ArrayList childrenForKey; categories = new HashMap(); Category currentCat; currentCat = new Category(0,0,"root");//key 0 is the logical root childrenForKey = CategoryDBUtil.getChildCategoriesForKey((long)0,dbc); tempCatList.addAll(childrenForKey); categories.put(new Long(0), new CategoryPlus(currentCat, childrenForKey)); while (!tempCatList.isEmpty()) { currentCat = (Category)tempCatList.get(0); tempCatList.remove(currentCat); childrenForKey = CategoryDBUtil.getChildCategoriesForKey( currentCat.getKey(),dbc); if (childrenForKey.isEmpty()) { categories.put( new Long(currentCat.getKey()), new CategoryPlus(currentCat, null)); } else { categories.put(new Long(currentCat.getKey()), new CategoryPlus(currentCat, childrenForKey)); tempCatList.addAll(childrenForKey); } } } //force a rebuild, perhaps scheduled or if table maintenace is done public void rebuildCategories(DBConnection dbc) { buildCategories(dbc); } class CategoryPlus { Category category; boolean show; ArrayList children; ArrayList nullArrayList = new ArrayList(); CategoryPlus(Category categoryIn) { category = categoryIn; show = true; } CategoryPlus(Category categoryIn, boolean showIn) { category = categoryIn; show = showIn; } CategoryPlus(Category categoryIn, ArrayList children) { category = categoryIn; setChildren(children); } CategoryPlus(Category categoryIn, boolean showIn, ArrayList childrenIn) { category = categoryIn; show = showIn; setChildren(childrenIn); } public Category getCategory() {return category;} void noShowCategory() {show = false;} public boolean getShow() {return show;} void setChildren(ArrayList childrenIn) { if (null == childrenIn) { children = nullArrayList; } else { children = childrenIn; } } public ArrayList getChildren() {return children;} } }