1 20 21 package golfShop.presentation.xmlc.main; 22 23 import org.enhydra.xml.xmlc.*; 24 import org.enhydra.xml.xmlc.html.*; 25 import com.lutris.appserver.server.httpPresentation.*; 26 import java.io.*; 27 import java.net.URLEncoder ; 28 import java.util.*; 29 import org.w3c.dom.*; 30 import org.w3c.dom.html.*; 31 import golfShop.presentation.xmlc.utilities.*; 32 import golfShop.spec.inventory.*; 33 34 47 public class Shelf implements HttpPresentation { 48 51 private static final String SHELF_URL = "Shelf.po"; 52 53 56 private static final String ITEM_URL = "Item.po"; 57 58 61 private static final String OPEN_CAT_IMAGE = "../media/true.gif"; 62 private static final String CLOSED_CAT_IMAGE = "../media/false.gif"; 63 64 68 private Long buttonCatId; 69 70 74 private Hashtable openCatIds = new Hashtable(); 75 76 80 private String openCatCgiArg; 81 82 85 private ShelfHTML shelfHtml; 86 87 90 HTMLParagraphElement sampleCatBlock; 91 HTMLAnchorElement sampleCatAnchor; 92 HTMLImageElement sampleCatImg; 93 Text sampleCatText; 94 95 98 HTMLParagraphElement sampleItemBlock; 99 HTMLAnchorElement sampleItemAnchor; 100 Text sampleItemText; 101 102 107 private void prepareHtml(HttpPresentationComms comms) { 108 109 shelfHtml = (ShelfHTML)comms.xmlcFactory.create(ShelfHTML.class); 110 111 sampleCatBlock = shelfHtml.getElementSampleCatBlock(); 113 sampleCatAnchor = shelfHtml.getElementSampleCatAnchor(); 114 sampleCatImg = shelfHtml.getElementSampleCatImg(); 115 sampleCatText = XMLCUtil.getFirstText(shelfHtml.getElementSampleCatText()); 116 117 sampleItemBlock = shelfHtml.getElementSampleItemBlock(); 118 sampleItemAnchor = shelfHtml.getElementSampleItemAnchor(); 119 sampleItemText = XMLCUtil.getFirstText(shelfHtml.getElementSampleItemText()); 120 121 Node listNode = shelfHtml.getElementList(); 123 Node child = listNode.getFirstChild(); 124 while (child != null) { 125 Node nextChild = child.getNextSibling(); 126 listNode.removeChild(child); 127 child = nextChild; 128 } 129 130 } 131 132 135 private void makeCategoryHtml(Inventory category, Node parent, 136 boolean isOpen, int level) { 137 long id = category.getObjectId(); 138 139 String url = SHELF_URL + "?button=" + id; 140 if (openCatCgiArg != null) { 141 url += "&" + openCatCgiArg; 142 } 143 sampleCatAnchor.setHref(url); 144 if (isOpen) { 145 sampleCatImg.setSrc(OPEN_CAT_IMAGE); 146 } else { 147 sampleCatImg.setSrc(CLOSED_CAT_IMAGE); 148 } 149 sampleCatText.setData(category.getDesc()); 150 sampleCatBlock.setClassName("level" + level); 151 parent.appendChild(sampleCatBlock.cloneNode(true)); 152 } 153 154 157 private void makeItemHtml(Inventory item, Node parent, int level) { 158 String url = ITEM_URL + "?ref=../contents/" + item.getRef() 159 + "&add=" + item.getObjectId(); 160 sampleItemAnchor.setHref(url); 161 sampleItemAnchor.setTarget("item"); 162 sampleItemText.setData(item.getDesc()); 163 sampleItemBlock.setClassName("level" + level); 164 parent.appendChild(sampleItemBlock.cloneNode(true)); 165 } 166 167 171 private void processInventoryEntry(Inventory entry, Node parent, int level) { 172 if (entry.isCategory()) { 173 boolean isOpen = openCatIds.contains(new Long (entry.getObjectId())); 174 makeCategoryHtml(entry, parent, isOpen, level); 175 if (isOpen) { 176 Vector entries = entry.getItems(entry.getObjectId()); 177 for (int idx = 0; idx < entries.size(); idx++) { 178 processInventoryEntry((Inventory)entries.elementAt(idx), parent, level+1); 179 } 180 } 181 } else { 182 makeItemHtml(entry, parent, level); 183 } 184 } 185 186 189 private void processInventory() { 190 try{ 191 Inventory inventory = InventoryFactory.getInventory("golfShop.business.inventory.InventoryImpl"); 192 processInventoryEntry(inventory.getInventoryById(0), 193 shelfHtml.getElementList(), 1); 194 199 200 } catch(NullPointerException ex) { 201 202 } 203 } 204 205 208 private void parseArgs(HttpPresentationRequest request) 209 throws HttpPresentationException { 210 211 String button = request.getParameter("button"); 213 if (button != null) { 214 try { 215 buttonCatId = new Long (button); 216 } catch (NumberFormatException except) { 217 } 219 } 220 221 String open = request.getParameter("open"); 222 if (open != null) { 223 StringTokenizer tokens = new StringTokenizer(open, ","); 224 while (tokens.hasMoreTokens()) { 225 String token = tokens.nextToken(); 226 try { 227 Long id = new Long (token); 228 openCatIds.put(id, id); 229 } catch (NumberFormatException except) { 230 } 232 } 233 } 234 } 235 236 240 private void adjustOpenCategories() { 241 if (buttonCatId != null) { 242 if (openCatIds.containsKey(buttonCatId)) { 244 openCatIds.remove(buttonCatId); 245 } else { 246 openCatIds.put(buttonCatId, buttonCatId); 247 } 248 } 249 250 StringBuffer buf = new StringBuffer (); 252 Enumeration openIds = openCatIds.keys(); 253 254 while (openIds.hasMoreElements()) { 255 if (buf.length() > 0) { 256 buf.append(","); 257 } 258 buf.append(((Long )openIds.nextElement()).toString()); 259 } 260 if (buf.length() > 0) { 261 openCatCgiArg = "open=" + buf.toString(); 262 } 263 } 264 265 266 269 public void run(HttpPresentationComms comms) 270 throws HttpPresentationException { 271 272 parseArgs(comms.request); 273 prepareHtml(comms); 274 adjustOpenCategories(); 275 processInventory(); 276 comms.response.writeDOM(shelfHtml); 277 } 278 } 279 | Popular Tags |