1 | |
package org.paneris.jammyjoes.shopping; |
2 | |
|
3 | |
import java.util.Enumeration; |
4 | |
import java.util.Iterator; |
5 | |
import java.util.LinkedList; |
6 | |
import java.util.List; |
7 | |
|
8 | |
import javax.servlet.ServletConfig; |
9 | |
import javax.servlet.ServletException; |
10 | |
|
11 | |
import org.melati.Melati; |
12 | |
import org.melati.servlet.Form; |
13 | |
import org.melati.poem.Field; |
14 | |
import org.melati.servlet.InvalidUsageException; |
15 | |
import org.melati.PoemContext; |
16 | |
import org.melati.servlet.PathInfoException; |
17 | |
import org.melati.servlet.TemplateServlet; |
18 | |
import org.melati.template.ServletTemplateContext; |
19 | |
import org.melati.util.InstantiationPropertyException; |
20 | |
import org.melati.util.MelatiException; |
21 | |
import org.paneris.jammyjoes.model.JammyjoesDatabase; |
22 | |
import org.paneris.jammyjoes.mvp.CatalogueEntryPresenter; |
23 | |
import org.paneris.jammyjoes.mvp.Context; |
24 | |
import org.paneris.jammyjoes.mvp.ContextWrapper; |
25 | |
import org.paneris.jammyjoes.mvp.Presenter; |
26 | |
import org.paneris.jammyjoes.util.JammyJoesContextUtil; |
27 | |
import org.paneris.jammyjoes.util.JammyJoesUtil; |
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | 0 | public class Trolley extends TemplateServlet { |
51 | |
|
52 | |
private static final long serialVersionUID = 1L; |
53 | |
|
54 | |
public MelatiShoppingConfig config; |
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
public void init(ServletConfig conf ) throws ServletException |
66 | |
{ |
67 | 0 | super.init(conf ); |
68 | |
try { |
69 | 0 | config = new MelatiShoppingConfig(); |
70 | 0 | } catch (MelatiException e) { |
71 | 0 | throw new ServletException(e.toString()); |
72 | 0 | } |
73 | 0 | } |
74 | |
|
75 | |
|
76 | |
public String getSysAdminName() { |
77 | 0 | return "Tim Joyce"; |
78 | |
} |
79 | |
public String getSysAdminEmail() { |
80 | 0 | return "timj@hoop.co.uk"; |
81 | |
} |
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
protected String doTemplateRequest(Melati melati, ServletTemplateContext context) |
95 | |
throws Exception { |
96 | |
|
97 | 0 | if (config==null) |
98 | 0 | throw new ShoppingConfigException("Shopping Trolley not Configured"); |
99 | 0 | JammyjoesDatabase db = (JammyjoesDatabase) melati.getDatabase(); |
100 | 0 | context.put("menutypes", db.getTypeTable().selection()); |
101 | 0 | context.put("jjutil", new JammyJoesContextUtil()); |
102 | 0 | if (JammyJoesUtil.telesales(melati)) assertLogin(melati); |
103 | 0 | ShoppingContext shoppingContext = (ShoppingContext)melati.getPoemContext(); |
104 | 0 | if (shoppingContext.getMethod().equals("Load")) |
105 | 0 | return Load(melati, shoppingContext.stid); |
106 | 0 | if (shoppingContext.getMethod().equals("View")) return View(melati); |
107 | 0 | if (shoppingContext.getMethod().equals("Update")) return Update(melati); |
108 | 0 | if (shoppingContext.getMethod().equals("Add")) |
109 | 0 | return Add(melati, shoppingContext.stid, shoppingContext.quantity); |
110 | 0 | if (shoppingContext.getMethod().equals("MultipleAdd")) |
111 | 0 | return MultipleAdd(melati); |
112 | 0 | if (shoppingContext.getMethod().equals("Remove")) |
113 | 0 | return Remove(melati, shoppingContext.stid); |
114 | 0 | if (shoppingContext.getMethod().equals("Set")) |
115 | 0 | return Set(melati, shoppingContext.stid, shoppingContext.quantity); |
116 | 0 | if (shoppingContext.getMethod().equals("Details")) return Details(melati); |
117 | 0 | if (shoppingContext.getMethod().equals("Confirm")) return Confirm(melati); |
118 | 0 | if (shoppingContext.getMethod().equals("Paid")) return Paid(melati); |
119 | 0 | if (shoppingContext.getMethod().equals("Abandon")) return Abandon(melati); |
120 | 0 | if (shoppingContext.getMethod().equals("CatalogueEntry")) return CatalogueEntry(melati); |
121 | 0 | throw new InvalidUsageException(this, shoppingContext); |
122 | |
} |
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
protected String Load(Melati melati, Integer id) |
137 | |
throws InstantiationPropertyException { |
138 | 0 | ShoppingTrolley trolley = ShoppingTrolley.newTrolley(config); |
139 | 0 | trolley.initialise(melati,config,id); |
140 | 0 | melati.getTemplateContext().put("trolley", trolley); |
141 | 0 | return JammyJoesUtil.shoppingTemplate(melati, "Trolley"); |
142 | |
} |
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
protected String Save(Melati melati) |
157 | |
throws InstantiationPropertyException { |
158 | 0 | ShoppingTrolley trolley = ShoppingTrolley.getInstance(melati,config); |
159 | 0 | trolley.save(); |
160 | 0 | melati.getTemplateContext().put("trolley", trolley); |
161 | 0 | return JammyJoesUtil.shoppingTemplate(melati, "Trolley"); |
162 | |
} |
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
protected String CatalogueEntry(Melati melati) |
175 | |
throws InstantiationPropertyException { |
176 | 0 | JammyjoesDatabase db = (JammyjoesDatabase) melati.getDatabase(); |
177 | 0 | Context context = new ContextWrapper(melati.getServletTemplateContext()); |
178 | 0 | Presenter presenter = new CatalogueEntryPresenter(db.getProductTable(),context); |
179 | 0 | presenter.handleInteraction(presenter.createSelection(), presenter.createCommand()); |
180 | 0 | context.put("type", new Field(null, db.getProductTable().getTypeColumn())); |
181 | 0 | context.put("jjutil", new JammyJoesContextUtil()); |
182 | 0 | context.put("trolley", ShoppingTrolley.getInstance(melati,config)); |
183 | 0 | context.put("ages", db.getAgeTable().selection()); |
184 | 0 | return JammyJoesUtil.shoppingTemplate(melati, "CatalogueEntry"); |
185 | |
} |
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
protected String View(Melati melati) |
199 | |
throws InstantiationPropertyException { |
200 | 0 | melati.getTemplateContext().put("trolley", ShoppingTrolley.getInstance(melati,config)); |
201 | 0 | return JammyJoesUtil.shoppingTemplate(melati, "Trolley"); |
202 | |
} |
203 | |
|
204 | |
|
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | |
|
213 | |
|
214 | |
|
215 | |
|
216 | |
|
217 | |
|
218 | |
|
219 | |
|
220 | |
|
221 | |
protected String Update(Melati melati) throws InstantiationPropertyException { |
222 | 0 | ShoppingTrolley trolley = ShoppingTrolley.getInstance(melati,config); |
223 | 0 | List removeItems = new LinkedList(); |
224 | 0 | for (Enumeration c = trolley.getItems(); c.hasMoreElements();) { |
225 | 0 | ShoppingTrolleyItem item = (ShoppingTrolleyItem)c.nextElement(); |
226 | 0 | String formName = "trolleyitem_" + item.getId(); |
227 | 0 | String formQuantity = formName + "_quantity"; |
228 | 0 | String formDeleted = formName + "_deleted"; |
229 | 0 | String deleted = |
230 | |
Form.getFormNulled(melati.getServletTemplateContext(),formDeleted); |
231 | 0 | String quantity = |
232 | |
Form.getFormNulled(melati.getServletTemplateContext(),formQuantity); |
233 | 0 | System.err.println(deleted + " " + quantity); |
234 | 0 | if (deleted != null || quantity == null || quantity.equals("0")) { |
235 | 0 | removeItems.add(item); |
236 | |
} else { |
237 | 0 | item.setQuantity(new Integer(quantity)); |
238 | |
} |
239 | |
|
240 | 0 | } |
241 | 0 | Iterator removeItemsIterator = removeItems.iterator(); |
242 | 0 | while (removeItemsIterator.hasNext()) { |
243 | 0 | ShoppingTrolleyItem element = (ShoppingTrolleyItem) removeItemsIterator.next(); |
244 | 0 | trolley.removeItem(element); |
245 | 0 | } |
246 | 0 | melati.getTemplateContext().put("trolley",trolley); |
247 | 0 | return JammyJoesUtil.shoppingTemplate(melati, "Trolley"); |
248 | |
} |
249 | |
|
250 | |
|
251 | |
|
252 | |
|
253 | |
|
254 | |
|
255 | |
|
256 | |
|
257 | |
|
258 | |
|
259 | |
|
260 | |
|
261 | |
|
262 | |
|
263 | |
|
264 | |
|
265 | |
|
266 | |
|
267 | |
|
268 | |
|
269 | |
|
270 | |
protected String MultipleAdd(Melati melati) |
271 | |
throws InstantiationPropertyException { |
272 | 0 | ShoppingTrolley trolley = ShoppingTrolley.getInstance(melati,config); |
273 | 0 | for (Enumeration e = melati.getRequest().getParameterNames(); |
274 | 0 | e.hasMoreElements();) { |
275 | 0 | String name = (String)e.nextElement(); |
276 | 0 | if (name.length() > 8) { |
277 | 0 | String p = name.substring(0,7); |
278 | 0 | if (p.equals("product")) { |
279 | 0 | String id = name.substring(8); |
280 | 0 | Integer idInt = new Integer(id); |
281 | 0 | ShoppingTrolleyItem item = trolley.getItem(idInt); |
282 | 0 | String quantityName = "quantity_" + id; |
283 | 0 | String priceName = "price_" + id; |
284 | 0 | String descriptionName = "description_" + id; |
285 | 0 | Integer quantity = new Integer(1); |
286 | 0 | Double price = null; |
287 | 0 | String quantitySring = |
288 | |
Form.getFormNulled |
289 | |
(melati.getServletTemplateContext(), quantityName); |
290 | 0 | String priceString = |
291 | |
Form.getFormNulled |
292 | |
(melati.getServletTemplateContext(), priceName); |
293 | 0 | String description = |
294 | |
Form.getFormNulled |
295 | |
(melati.getServletTemplateContext(), descriptionName); |
296 | 0 | if (quantitySring != null) |
297 | 0 | quantity = new Integer(quantitySring); |
298 | 0 | if (priceString != null) price = new Double(priceString); |
299 | 0 | if (item == null) { |
300 | 0 | item = newItem(trolley,idInt,price,description); |
301 | |
} |
302 | 0 | item.setQuantity(new Integer(item.getQuantity().intValue() + quantity.intValue())); |
303 | |
} |
304 | |
} |
305 | 0 | } |
306 | 0 | melati.getTemplateContext().put("trolley",trolley); |
307 | 0 | return JammyJoesUtil.shoppingTemplate(melati, "Trolley"); |
308 | |
} |
309 | |
|
310 | |
|
311 | |
|
312 | |
|
313 | |
|
314 | |
|
315 | |
|
316 | |
|
317 | |
|
318 | |
|
319 | |
|
320 | |
|
321 | |
|
322 | |
|
323 | |
|
324 | |
|
325 | |
|
326 | |
|
327 | |
|
328 | |
|
329 | |
|
330 | |
protected String Add(Melati melati, Integer id, Integer quantity) |
331 | |
throws InstantiationPropertyException { |
332 | 0 | System.err.println("Adding"); |
333 | |
|
334 | 0 | if (quantity == null || quantity.intValue() == 0) quantity = new Integer(1); |
335 | 0 | ShoppingTrolley trolley = ShoppingTrolley.getInstance(melati,config); |
336 | 0 | ShoppingTrolleyItem item = trolley.getItem(id); |
337 | 0 | if (item == null) { |
338 | 0 | Double price = null; |
339 | 0 | String priceString = |
340 | |
Form.getFormNulled(melati.getServletTemplateContext(), "price"); |
341 | 0 | if (priceString != null) price = new Double(priceString); |
342 | 0 | item = newItem(trolley,id,price, |
343 | |
Form.getFormNulled(melati.getServletTemplateContext(), "description")); |
344 | |
} |
345 | 0 | Integer currentQuantity = item.getQuantity(); |
346 | 0 | if (currentQuantity == null) currentQuantity = new Integer(0); |
347 | 0 | item.setQuantity(new Integer(currentQuantity.intValue() + quantity.intValue())); |
348 | 0 | melati.getTemplateContext().put("trolley",trolley); |
349 | 0 | return JammyJoesUtil.shoppingTemplate(melati, "Trolley"); |
350 | |
} |
351 | |
|
352 | |
|
353 | |
|
354 | |
|
355 | |
|
356 | |
|
357 | |
|
358 | |
|
359 | |
|
360 | |
|
361 | |
|
362 | |
|
363 | |
|
364 | |
|
365 | |
|
366 | |
protected String Remove(Melati melati, Integer id) |
367 | |
throws InstantiationPropertyException { |
368 | 0 | ShoppingTrolley trolley = ShoppingTrolley.getInstance(melati,config); |
369 | 0 | ShoppingTrolleyItem item = trolley.getItem(id); |
370 | 0 | trolley.removeItem(item); |
371 | 0 | melati.getTemplateContext().put("trolley",trolley); |
372 | 0 | return JammyJoesUtil.shoppingTemplate(melati, "Trolley"); |
373 | |
} |
374 | |
|
375 | |
|
376 | |
|
377 | |
|
378 | |
|
379 | |
|
380 | |
|
381 | |
|
382 | |
|
383 | |
|
384 | |
|
385 | |
|
386 | |
|
387 | |
|
388 | |
|
389 | |
|
390 | |
|
391 | |
protected String Set(Melati melati, Integer id, Integer quantity) |
392 | |
throws InstantiationPropertyException { |
393 | 0 | ShoppingTrolley trolley = ShoppingTrolley.getInstance(melati,config); |
394 | 0 | ShoppingTrolleyItem item = trolley.getItem(id); |
395 | 0 | if (item == null) item = newItem(trolley,id, null, null); |
396 | 0 | item.setQuantity(quantity); |
397 | 0 | melati.getTemplateContext().put("trolley",trolley); |
398 | 0 | return JammyJoesUtil.shoppingTemplate(melati, "Trolley"); |
399 | |
} |
400 | |
|
401 | |
|
402 | |
|
403 | |
|
404 | |
|
405 | |
|
406 | |
|
407 | |
|
408 | |
|
409 | |
|
410 | |
|
411 | |
protected String Details(Melati melati) |
412 | |
throws InstantiationPropertyException { |
413 | 0 | ShoppingTrolley trolley = ShoppingTrolley.getInstance(melati,config); |
414 | 0 | trolley.setDefaultDetails(melati); |
415 | 0 | melati.getTemplateContext().put("trolley",trolley); |
416 | 0 | return JammyJoesUtil.shoppingTemplate(melati, "Details"); |
417 | |
} |
418 | |
|
419 | |
|
420 | |
|
421 | |
|
422 | |
|
423 | |
|
424 | |
|
425 | |
|
426 | |
|
427 | |
|
428 | |
|
429 | |
protected String Confirm(Melati melati) |
430 | |
throws InstantiationPropertyException { |
431 | 0 | JammyJoesShoppingTrolley trolley = (JammyJoesShoppingTrolley)ShoppingTrolley.getInstance(melati,config); |
432 | 0 | if (Form.getFormNulled(melati.getServletTemplateContext(),"submittoken") != null) |
433 | 0 | trolley.setFromForm(melati); |
434 | 0 | trolley.save(); |
435 | 0 | melati.getServletTemplateContext().put("trolley",trolley); |
436 | 0 | return JammyJoesUtil.shoppingTemplate(melati, "Confirm"); |
437 | |
} |
438 | |
|
439 | |
|
440 | |
|
441 | |
|
442 | |
|
443 | |
|
444 | |
|
445 | |
|
446 | |
|
447 | |
|
448 | |
|
449 | |
|
450 | |
|
451 | |
|
452 | |
|
453 | |
|
454 | |
|
455 | |
|
456 | |
|
457 | |
|
458 | |
|
459 | |
|
460 | |
|
461 | |
|
462 | |
|
463 | |
protected String Paid(Melati melati) |
464 | |
throws InstantiationPropertyException { |
465 | 0 | ShoppingTrolley trolley = ShoppingTrolley.getInstance(melati,config); |
466 | 0 | trolley.confirmPayment(melati); |
467 | |
|
468 | 0 | trolley.remove(melati); |
469 | 0 | return JammyJoesUtil.shoppingTemplate(melati, "Paid"); |
470 | |
} |
471 | |
|
472 | |
|
473 | |
|
474 | |
|
475 | |
|
476 | |
|
477 | |
|
478 | |
|
479 | |
|
480 | |
|
481 | |
protected String Abandon(Melati melati) |
482 | |
throws InstantiationPropertyException { |
483 | 0 | ShoppingTrolley trolley = ShoppingTrolley.getInstance(melati,config); |
484 | |
|
485 | 0 | trolley.remove(melati); |
486 | 0 | return JammyJoesUtil.shoppingTemplate(melati, "Trolley"); |
487 | |
} |
488 | |
|
489 | |
|
490 | |
|
491 | |
|
492 | |
|
493 | |
|
494 | |
protected void assertLogin(Melati melati) |
495 | |
throws InstantiationPropertyException { |
496 | 0 | ShoppingTrolley trolley = ShoppingTrolley.getInstance(melati,config); |
497 | |
|
498 | 0 | trolley.assertLogin(melati); |
499 | 0 | } |
500 | |
|
501 | |
|
502 | |
|
503 | |
|
504 | |
|
505 | |
|
506 | |
|
507 | |
|
508 | |
|
509 | |
|
510 | |
|
511 | |
|
512 | |
private ShoppingTrolleyItem newItem(ShoppingTrolley trolley, Integer id, |
513 | |
Double price, String description) |
514 | |
throws InstantiationPropertyException { |
515 | 0 | return trolley.newItem(id, description, price); |
516 | |
} |
517 | |
|
518 | |
|
519 | |
|
520 | |
|
521 | |
|
522 | |
|
523 | |
|
524 | |
|
525 | |
|
526 | |
|
527 | |
|
528 | |
protected PoemContext poemContext(Melati melati) |
529 | |
throws PathInfoException { |
530 | 0 | ShoppingContext it = new ShoppingContext(); |
531 | 0 | String[] parts = melati.getPathInfoParts(); |
532 | 0 | if (parts.length < 2) |
533 | 0 | throw new PathInfoException( |
534 | |
"The servlet expects to see pathinfo in the form " + |
535 | |
"/db/method/ or /db/method/troid or /db/method/troid/quantity"); |
536 | 0 | it.setLogicalDatabase(parts[0]); |
537 | 0 | it.setMethod(parts[1]); |
538 | |
try { |
539 | 0 | if (parts.length > 2 && !parts[2].equals("")) |
540 | 0 | it.stid = new Integer(parts[2]); |
541 | 0 | if (parts.length > 3 && !parts[3].equals("")) |
542 | 0 | it.quantity = new Integer(parts[3]); |
543 | 0 | } catch (NumberFormatException e) { |
544 | 0 | throw new PathInfoException( |
545 | |
"The servlet expects to see pathinfo in the form " + |
546 | |
"/db/method/ or /db/troid/method/ or /db/troid/quantity/method/ " + |
547 | |
"where the troid is an integer and the quantity is a number"); |
548 | 0 | } |
549 | 0 | return it; |
550 | |
} |
551 | |
|
552 | |
} |