KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > golfShop > presentation > xmlc > cart > Contents


1 /*
2  * Enhydra Java Application Server
3  * The Initial Developer of the Original Code is Lutris Technologies Inc.
4  * Portions created by Lutris are Copyright (C) 1997-2000 Lutris Technologies
5  * Inc.
6  * All Rights Reserved.
7  *
8  * The contents of this file are subject to the Enhydra Public License Version
9  * 1.0 (the "License"); you may not use this file except in compliance with the
10  * License. You may obtain a copy of the License at
11  * http://www.enhydra.org/software/license/epl.html
12  *
13  * Software distributed under the License is distributed on an "AS IS" basis,
14  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
15  * License for the specific language governing rights and limitations under the
16  * License.
17  *
18  *
19  */

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 JavaDoc;
28 import org.w3c.dom.*;
29 import org.w3c.dom.html.*;
30 import golfShop.presentation.xmlc.utilities.*;
31 import java.math.BigDecimal JavaDoc;
32 import java.util.Enumeration JavaDoc;
33 import golfShop.spec.cart.*;
34 /**
35  * This presentation object dynamically creates an HTML page showing the
36  * cart contents in an tabulated form or, if the user wants to go to the
37  * Checkout PO, they will be redirected appropriately.
38  */

39 public class Contents implements HttpPresentation {
40     //FIXME: Make this work...
41
/**
42      * Active and inactive buttons images and alt text.
43      */

44     private static final String JavaDoc ADD_INACTIVE_GIF = "";
45
46     /**
47      * URL to reference the this presentation object.
48      */

49     private static final String JavaDoc CONTENTS_URL = "Contents.po";
50
51     /**
52      * URL to reference the checkout presentation object.
53      */

54     private static final String JavaDoc CHECKOUT_URL = "../checkout/Main.po";
55
56     /**
57      * URL of the undo-inactive images.
58      */

59     private static final String JavaDoc UNDO_INACTIVE_IMG = "../media/Undo_d.gif";
60
61     /**
62      * URL of the redo-inactive images.
63      */

64     private static final String JavaDoc REDO_INACTIVE_IMG = "../media/Redo_d.gif";
65
66     /**
67      * Requested action. If null, just display.
68      */

69     String JavaDoc action;
70
71     /**
72      * Cart object for session.
73      */

74     Cart cart;
75
76     /**
77      * Process the submission of an update.
78      */

79     private void processUpdate(HttpPresentationComms comms)
80             throws HttpPresentationException {
81     // Variable to track cart modifications.
82
boolean modified = false;
83         
84         // Search parameters for form update names.
85
Enumeration JavaDoc params = comms.request.getParameterNames();
86         while (params.hasMoreElements()) {
87             String JavaDoc name = (String JavaDoc)params.nextElement();
88             if (name.startsWith(ContentsTableFormatter.QTY_PREFIX)) {
89                 // Change the quantities of the cart items
90
long objectId = Long.parseLong(name.substring(ContentsTableFormatter.QTY_PREFIX.length()));
91                 String JavaDoc qtyString = comms.request.getParameter(name);
92          
93          int oldQty=0;
94           try{
95                oldQty = cart.getQuantity(objectId);
96 /*
97  * Catch Null pointer exception ( we canot make a instances of classes from business layer when we run GolfShop_pres )
98  * if cart is null ( we set it in CartUtils when we catch NullPointerExceptiont ) the response will be default HTML page
99  *We need to allow GolfShop_pres to be functional
100  */

101     } catch(NullPointerException JavaDoc ex) {
102          throw new ClientPageRedirectException(CONTENTS_URL);
103      }
104                 try {
105                     int newQty = Integer.valueOf(qtyString).intValue();
106                     // Only change if there is a new value.
107
if (newQty != oldQty && newQty >= 0 && newQty < 1000) {
108                         cart.setQuantity(objectId, newQty);
109                         modified = true;
110
111                     }
112                 } catch (NumberFormatException JavaDoc nfe) {
113                     // Ignore bogus numbers
114
}
115         } else if (name.startsWith(ContentsTableFormatter.DEL_PREFIX)) {
116                 // Delete items from the cart, if requested.
117
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 we made any changes above, notify the cart we are done.
126
if (modified) {
127             cart.doneModifying();
128         }
129
130         // Must redirect to this page without the CGI arguments, or reload
131
// will result in page being updated again.
132
throw new ClientPageRedirectException(CONTENTS_URL);
133     }
134
135     /**
136      * Disable one either the undo or redo anchor and image. It is expected
137      * that the button is in the enabled state by default.
138      *
139      * @param anchor The anchor element.
140      * @param img The contained img element.
141      * @param inactiveImgUrl The URL for the inactive image.
142      */

143     private void disableButton(HTMLAnchorElement anchor, HTMLImageElement img,
144                                String JavaDoc inactiveImgUrl) {
145         // Disable anchor by clearing href and mouseOver attributes.
146
anchor.removeAttribute("href");
147         anchor.removeAttribute("onmouseover");
148         anchor.removeAttribute("onmouseout");
149
150         // Set image to the inactive version.
151
img.setSrc(inactiveImgUrl);
152     }
153
154     /**
155      * Configure the undo/redo `buttons' based on the state of the cart.
156      */

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     /**
171      * Output the page, setting dynamic values.
172      */

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         //same thing
182
} catch(NullPointerException JavaDoc ex) {
183      } comms.response.writeDOM(htmlObj);
184     }
185
186     /**
187      * Parse CGI arguments into fields.
188      */

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     /**
198      * Entry.
199      */

200     public void run(HttpPresentationComms comms)
201         throws HttpPresentationException {
202
203         parseArgs(comms.request);
204         cart = CartUtils.getCart(comms.session);
205         
206         // Process specified action, if any.
207
if (action.equals("update")) {
208             processUpdate(comms);
209         } else if (action.equals("undo")) {
210  try{
211             if (cart.canUndo()) {
212                 cart.undo();
213                 }
214 /*
215  * Catch Null pointer exception ( we canot make a instances of classes from business layer when we run GolfShop_pres )
216  * if cart is null ( we set it in CartUtils when we catch NullPointerExceptiont ) the response will be default HTML page
217  *We need to allow GolfShop_pres to be functional
218  */

219     } catch(NullPointerException JavaDoc ex) { throw new ClientPageRedirectException(CONTENTS_URL);
220      }
221            
222     } else if (action.equals("redo")) {
223     try{
224              if (cart.canRedo()) {
225                 cart.redo();}
226   //same thing
227
} catch(NullPointerException JavaDoc ex) { throw new ClientPageRedirectException(CONTENTS_URL);
228      }
229        
230         }
231
232         outputPage(comms);
233     }
234 }
235
Popular Tags