1 20 21 package golfShop.presentation.xmlc.cart; 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 org.w3c.dom.*; 29 import org.w3c.dom.html.*; 30 import golfShop.presentation.xmlc.utilities.*; 31 import java.math.BigDecimal ; 32 import java.util.Enumeration ; 33 import golfShop.spec.cart.*; 34 39 public class Contents implements HttpPresentation { 40 44 private static final String ADD_INACTIVE_GIF = ""; 45 46 49 private static final String CONTENTS_URL = "Contents.po"; 50 51 54 private static final String CHECKOUT_URL = "../checkout/Main.po"; 55 56 59 private static final String UNDO_INACTIVE_IMG = "../media/Undo_d.gif"; 60 61 64 private static final String REDO_INACTIVE_IMG = "../media/Redo_d.gif"; 65 66 69 String action; 70 71 74 Cart cart; 75 76 79 private void processUpdate(HttpPresentationComms comms) 80 throws HttpPresentationException { 81 boolean modified = false; 83 84 Enumeration params = comms.request.getParameterNames(); 86 while (params.hasMoreElements()) { 87 String name = (String )params.nextElement(); 88 if (name.startsWith(ContentsTableFormatter.QTY_PREFIX)) { 89 long objectId = Long.parseLong(name.substring(ContentsTableFormatter.QTY_PREFIX.length())); 91 String qtyString = comms.request.getParameter(name); 92 93 int oldQty=0; 94 try{ 95 oldQty = cart.getQuantity(objectId); 96 101 } catch(NullPointerException ex) { 102 throw new ClientPageRedirectException(CONTENTS_URL); 103 } 104 try { 105 int newQty = Integer.valueOf(qtyString).intValue(); 106 if (newQty != oldQty && newQty >= 0 && newQty < 1000) { 108 cart.setQuantity(objectId, newQty); 109 modified = true; 110 111 } 112 } catch (NumberFormatException nfe) { 113 } 115 } else if (name.startsWith(ContentsTableFormatter.DEL_PREFIX)) { 116 long objectId = Long.parseLong(name.substring(ContentsTableFormatter.DEL_PREFIX.length())); 118 if (cart.getQuantity(objectId) != -1) { 119 cart.removeItem(objectId); 120 modified = true; 121 } 122 } 123 } 124 125 if (modified) { 127 cart.doneModifying(); 128 } 129 130 throw new ClientPageRedirectException(CONTENTS_URL); 133 } 134 135 143 private void disableButton(HTMLAnchorElement anchor, HTMLImageElement img, 144 String inactiveImgUrl) { 145 anchor.removeAttribute("href"); 147 anchor.removeAttribute("onmouseover"); 148 anchor.removeAttribute("onmouseout"); 149 150 img.setSrc(inactiveImgUrl); 152 } 153 154 157 private void configureUndoRedo(ContentsHTML htmlObj) { 158 if (!cart.canUndo()) { 159 disableButton(htmlObj.getElementUndoAnchor(), 160 htmlObj.getElementUndoImg(), 161 UNDO_INACTIVE_IMG); 162 } 163 if (!cart.canRedo()) { 164 disableButton(htmlObj.getElementRedoAnchor(), 165 htmlObj.getElementRedoImg(), 166 REDO_INACTIVE_IMG); 167 } 168 } 169 170 173 private void outputPage(HttpPresentationComms comms) 174 throws HttpPresentationException { 175 ContentsHTML htmlObj 176 = (ContentsHTML)comms.xmlcFactory.create(ContentsHTML.class); 177 178 ContentsTableFormatter.fillInTable(htmlObj, cart, true); 179 try{ 180 configureUndoRedo(htmlObj); 181 } catch(NullPointerException ex) { 183 } comms.response.writeDOM(htmlObj); 184 } 185 186 189 private void parseArgs(HttpPresentationRequest request) 190 throws HttpPresentationException { 191 action = request.getParameter("action"); 192 if (action == null) { 193 action = "display"; 194 } 195 } 196 197 200 public void run(HttpPresentationComms comms) 201 throws HttpPresentationException { 202 203 parseArgs(comms.request); 204 cart = CartUtils.getCart(comms.session); 205 206 if (action.equals("update")) { 208 processUpdate(comms); 209 } else if (action.equals("undo")) { 210 try{ 211 if (cart.canUndo()) { 212 cart.undo(); 213 } 214 219 } catch(NullPointerException ex) { throw new ClientPageRedirectException(CONTENTS_URL); 220 } 221 222 } else if (action.equals("redo")) { 223 try{ 224 if (cart.canRedo()) { 225 cart.redo();} 226 } catch(NullPointerException ex) { throw new ClientPageRedirectException(CONTENTS_URL); 228 } 229 230 } 231 232 outputPage(comms); 233 } 234 } 235 | Popular Tags |