001: /*
002: * Enhydra Java Application Server
003: * The Initial Developer of the Original Code is Lutris Technologies Inc.
004: * Portions created by Lutris are Copyright (C) 1997-2000 Lutris Technologies
005: * Inc.
006: * All Rights Reserved.
007: *
008: * The contents of this file are subject to the Enhydra Public License Version
009: * 1.0 (the "License"); you may not use this file except in compliance with the
010: * License. You may obtain a copy of the License at
011: * http://www.enhydra.org/software/license/epl.html
012: *
013: * Software distributed under the License is distributed on an "AS IS" basis,
014: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
015: * License for the specific language governing rights and limitations under the
016: * License.
017: *
018: *
019: */
020:
021: package golfShop.presentation.xmlc.cart;
022:
023: import org.enhydra.xml.xmlc.*;
024: import org.enhydra.xml.xmlc.html.*;
025: import com.lutris.appserver.server.httpPresentation.*;
026: import java.io.*;
027: import java.net.URLEncoder;
028: import org.w3c.dom.*;
029: import org.w3c.dom.html.*;
030: import golfShop.presentation.xmlc.utilities.*;
031: import java.math.BigDecimal;
032: import java.util.Enumeration;
033: import golfShop.spec.cart.*;
034:
035: /**
036: * This presentation object dynamically creates an HTML page showing the
037: * cart contents in an tabulated form or, if the user wants to go to the
038: * Checkout PO, they will be redirected appropriately.
039: */
040: public class Contents implements HttpPresentation {
041: //FIXME: Make this work...
042: /**
043: * Active and inactive buttons images and alt text.
044: */
045: private static final String ADD_INACTIVE_GIF = "";
046:
047: /**
048: * URL to reference the this presentation object.
049: */
050: private static final String CONTENTS_URL = "Contents.po";
051:
052: /**
053: * URL to reference the checkout presentation object.
054: */
055: private static final String CHECKOUT_URL = "../checkout/Main.po";
056:
057: /**
058: * URL of the undo-inactive images.
059: */
060: private static final String UNDO_INACTIVE_IMG = "../media/Undo_d.gif";
061:
062: /**
063: * URL of the redo-inactive images.
064: */
065: private static final String REDO_INACTIVE_IMG = "../media/Redo_d.gif";
066:
067: /**
068: * Requested action. If null, just display.
069: */
070: String action;
071:
072: /**
073: * Cart object for session.
074: */
075: Cart cart;
076:
077: /**
078: * Process the submission of an update.
079: */
080: private void processUpdate(HttpPresentationComms comms)
081: throws HttpPresentationException {
082: // Variable to track cart modifications.
083: boolean modified = false;
084:
085: // Search parameters for form update names.
086: Enumeration params = comms.request.getParameterNames();
087: while (params.hasMoreElements()) {
088: String name = (String) params.nextElement();
089: if (name.startsWith(ContentsTableFormatter.QTY_PREFIX)) {
090: // Change the quantities of the cart items
091: long objectId = Long.parseLong(name
092: .substring(ContentsTableFormatter.QTY_PREFIX
093: .length()));
094: String qtyString = comms.request.getParameter(name);
095:
096: int oldQty = 0;
097: try {
098: oldQty = cart.getQuantity(objectId);
099: /*
100: * Catch Null pointer exception ( we canot make a instances of classes from business layer when we run GolfShop_pres )
101: * if cart is null ( we set it in CartUtils when we catch NullPointerExceptiont ) the response will be default HTML page
102: *We need to allow GolfShop_pres to be functional
103: */
104: } catch (NullPointerException ex) {
105: throw new ClientPageRedirectException(CONTENTS_URL);
106: }
107: try {
108: int newQty = Integer.valueOf(qtyString).intValue();
109: // Only change if there is a new value.
110: if (newQty != oldQty && newQty >= 0
111: && newQty < 1000) {
112: cart.setQuantity(objectId, newQty);
113: modified = true;
114:
115: }
116: } catch (NumberFormatException nfe) {
117: // Ignore bogus numbers
118: }
119: } else if (name
120: .startsWith(ContentsTableFormatter.DEL_PREFIX)) {
121: // Delete items from the cart, if requested.
122: long objectId = Long.parseLong(name
123: .substring(ContentsTableFormatter.DEL_PREFIX
124: .length()));
125: if (cart.getQuantity(objectId) != -1) {
126: cart.removeItem(objectId);
127: modified = true;
128: }
129: }
130: }
131:
132: // If we made any changes above, notify the cart we are done.
133: if (modified) {
134: cart.doneModifying();
135: }
136:
137: // Must redirect to this page without the CGI arguments, or reload
138: // will result in page being updated again.
139: throw new ClientPageRedirectException(CONTENTS_URL);
140: }
141:
142: /**
143: * Disable one either the undo or redo anchor and image. It is expected
144: * that the button is in the enabled state by default.
145: *
146: * @param anchor The anchor element.
147: * @param img The contained img element.
148: * @param inactiveImgUrl The URL for the inactive image.
149: */
150: private void disableButton(HTMLAnchorElement anchor,
151: HTMLImageElement img, String inactiveImgUrl) {
152: // Disable anchor by clearing href and mouseOver attributes.
153: anchor.removeAttribute("href");
154: anchor.removeAttribute("onmouseover");
155: anchor.removeAttribute("onmouseout");
156:
157: // Set image to the inactive version.
158: img.setSrc(inactiveImgUrl);
159: }
160:
161: /**
162: * Configure the undo/redo `buttons' based on the state of the cart.
163: */
164: private void configureUndoRedo(ContentsHTML htmlObj) {
165: if (!cart.canUndo()) {
166: disableButton(htmlObj.getElementUndoAnchor(), htmlObj
167: .getElementUndoImg(), UNDO_INACTIVE_IMG);
168: }
169: if (!cart.canRedo()) {
170: disableButton(htmlObj.getElementRedoAnchor(), htmlObj
171: .getElementRedoImg(), REDO_INACTIVE_IMG);
172: }
173: }
174:
175: /**
176: * Output the page, setting dynamic values.
177: */
178: private void outputPage(HttpPresentationComms comms)
179: throws HttpPresentationException {
180: ContentsHTML htmlObj = (ContentsHTML) comms.xmlcFactory
181: .create(ContentsHTML.class);
182:
183: ContentsTableFormatter.fillInTable(htmlObj, cart, true);
184: try {
185: configureUndoRedo(htmlObj);
186: //same thing
187: } catch (NullPointerException ex) {
188: }
189: comms.response.writeDOM(htmlObj);
190: }
191:
192: /**
193: * Parse CGI arguments into fields.
194: */
195: private void parseArgs(HttpPresentationRequest request)
196: throws HttpPresentationException {
197: action = request.getParameter("action");
198: if (action == null) {
199: action = "display";
200: }
201: }
202:
203: /**
204: * Entry.
205: */
206: public void run(HttpPresentationComms comms)
207: throws HttpPresentationException {
208:
209: parseArgs(comms.request);
210: cart = CartUtils.getCart(comms.session);
211:
212: // Process specified action, if any.
213: if (action.equals("update")) {
214: processUpdate(comms);
215: } else if (action.equals("undo")) {
216: try {
217: if (cart.canUndo()) {
218: cart.undo();
219: }
220: /*
221: * Catch Null pointer exception ( we canot make a instances of classes from business layer when we run GolfShop_pres )
222: * if cart is null ( we set it in CartUtils when we catch NullPointerExceptiont ) the response will be default HTML page
223: *We need to allow GolfShop_pres to be functional
224: */
225: } catch (NullPointerException ex) {
226: throw new ClientPageRedirectException(CONTENTS_URL);
227: }
228:
229: } else if (action.equals("redo")) {
230: try {
231: if (cart.canRedo()) {
232: cart.redo();
233: }
234: //same thing
235: } catch (NullPointerException ex) {
236: throw new ClientPageRedirectException(CONTENTS_URL);
237: }
238:
239: }
240:
241: outputPage(comms);
242: }
243: }
|