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