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 java.util.Hashtable;
49 import java.util.Vector;
50 import java.util.Enumeration;
51 import org.melati.Melati;
52 import org.melati.servlet.Form;
53 import org.melati.template.ServletTemplateContext;
54 import javax.servlet.http.HttpSession;
55 import java.text.NumberFormat;
56 import java.util.Locale;
57 import org.melati.util.InstantiationPropertyException;
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73 public abstract class ShoppingTrolley {
74
75 private static String TROLLEY =
76 "org.paneris.melati.shopping.DefaultShoppingTrolley";
77 protected Locale locale;
78 protected String address;
79 protected String name;
80 protected String tel;
81 protected String town;
82 protected String county;
83 protected String country;
84 protected String postcode;
85 protected String message;
86 protected String email;
87 protected boolean hasDetails = false;
88 Vector orderedItems = new Vector();
89 Hashtable items = new Hashtable();
90
91 public static final double VAT_PERCENT_TIMES_TEN = 175.0;
92 public MelatiShoppingConfig config;
93 public Melati melati;
94
95
96
97
98 protected void initialise(Melati melatiIn, MelatiShoppingConfig configIn) {
99 this.config = configIn;
100 this.melati = melatiIn;
101 }
102
103
104
105
106 public void initialise(Melati melatiIn,
107 MelatiShoppingConfig configIn, Integer id)
108 throws InstantiationPropertyException {
109 initialise(melatiIn,configIn);
110 load(id);
111 HttpSession session = melati.getSession();
112 session.setAttribute(name(),this);
113 }
114
115
116
117
118 public void remove(Melati melatiIn) {
119 HttpSession session = melatiIn.getSession();
120 session.removeAttribute(name());
121 }
122
123
124
125
126
127 public static synchronized ShoppingTrolley
128 getInstance(Melati melati, MelatiShoppingConfig config)
129 throws InstantiationPropertyException {
130 HttpSession session = melati.getSession();
131 ShoppingTrolley instance = (ShoppingTrolley) session.getAttribute(name());
132 if (instance == null) {
133 instance = newTrolley(config);
134 instance.initialise(melati,config);
135 session.setAttribute(name(),instance);
136 }
137 instance.configureRequest(melati);
138 return instance;
139 }
140
141 public static synchronized ShoppingTrolley
142 newTrolley(MelatiShoppingConfig config)
143 throws InstantiationPropertyException {
144 return config.getShoppingTrolley();
145 }
146
147
148
149
150 public abstract Locale getLocale();
151
152
153
154
155
156 public void setLocale(Locale locale){
157 this.locale = locale;
158 }
159
160
161
162
163 public abstract void confirmPayment(Melati melatiIn);
164
165
166
167
168 public abstract void load(Integer id) throws InstantiationPropertyException;
169
170
171
172
173 public abstract void save();
174
175
176
177
178
179 public void configureRequest(Melati melatiIn) {
180 this.melati = melatiIn;
181 }
182
183
184
185
186
187
188 public abstract void assertLogin(Melati melatiIn);
189
190
191
192
193
194
195 public abstract void setDefaultDetails(Melati melatiIn);
196
197
198
199
200
201 public static String name() {
202 return TROLLEY;
203 }
204
205
206
207
208 public Enumeration getItems() {
209 return orderedItems.elements();
210 }
211
212
213
214
215 public boolean isEmpty() {
216 return items.isEmpty();
217 }
218
219
220
221
222 public boolean hasDetails() {
223 return hasDetails;
224 }
225
226
227
228
229 public ShoppingTrolleyItem getItem(Integer id) {
230 return (ShoppingTrolleyItem)items.get(id);
231 }
232
233
234
235
236 public void removeItem(ShoppingTrolleyItem item) {
237 items.remove(item.getId());
238 orderedItems.removeElement(item);
239 }
240
241
242
243
244 public void addItem(ShoppingTrolleyItem item) {
245
246 if (!items.containsKey(item.getId())) {
247 orderedItems.add(item);
248 }
249 items.put(item.getId(),item);
250 }
251
252
253
254
255
256
257
258
259
260
261 public ShoppingTrolleyItem newItem(Integer id, String description,
262 Double price)
263 throws InstantiationPropertyException {
264 ShoppingTrolleyItem item = ShoppingTrolleyItem.newTrolleyItem(config);
265 item.initialise(this, melati, id, description, price);
266 addItem(item);
267 return item;
268 }
269
270
271
272
273 public double getValue() {
274 double value = 0;
275 for (Enumeration en = items.elements(); en.hasMoreElements();) {
276 ShoppingTrolleyItem product = (ShoppingTrolleyItem) en.nextElement();
277 value += product.getValue();
278 }
279 return value;
280 }
281
282
283
284
285
286 public String getValueDisplay() {
287 return displayCurrency(getValue());
288 }
289
290
291
292
293 public double getTotalValue() {
294 return getValue() +
295 getTotalDeliveryValue() +
296 getDiscountValue() +
297 getVATValue();
298 }
299
300
301
302
303
304 public String getTotalValueDisplay() {
305 return displayCurrency(getTotalValue());
306 }
307
308
309
310
311
312 public String getTotalValuePence() {
313 return (new Double(roundTo2dp(getTotalValue() * 100))).intValue() + "";
314 }
315
316
317
318
319
320 public abstract boolean hasDelivery();
321
322
323
324
325
326 public abstract double getDeliveryValue();
327
328
329
330
331
332 public double getTotalDeliveryValue() {
333 double value = 0;
334 if (hasDelivery()) {
335 value = getDeliveryValue();
336 for (Enumeration en = items.elements(); en.hasMoreElements();) {
337 ShoppingTrolleyItem item = (ShoppingTrolleyItem)en.nextElement();
338 value += item.getDeliveryValue();
339 }
340 }
341 return value;
342 }
343
344
345
346
347 public String getDeliveryDisplay() {
348 return displayCurrency(getTotalDeliveryValue());
349 }
350
351
352
353
354
355 public abstract boolean hasDiscount();
356
357
358
359
360 public abstract double getDiscountRate();
361
362
363
364
365
366 public double getDiscountValue() {
367 double value = 0;
368 if (hasDiscount()) {
369 value = 0 - roundTo2dp(getValue()*getDiscountRate());
370 }
371 return value;
372 }
373
374
375
376
377 public String getDiscountRateDisplay() {
378 if (hasDiscount()) {
379 try {
380 return (new Double(getDiscountRate())).intValue() + "%";
381 } catch (NumberFormatException e) {
382 return getDiscountRate() + "%";
383 }
384 } else {
385 return "";
386 }
387 }
388
389
390
391
392 public String getDiscountValueDisplay() throws Exception {
393 return displayCurrency(getDiscountValue());
394 }
395
396
397
398
399
400 public abstract boolean hasVAT();
401
402
403
404
405
406
407
408
409 public double getVATValue() {
410 if (!hasVAT()) {
411 return roundTo2dp((getValue() *
412 (-1 * (1.0 - (1000.0/VAT_PERCENT_TIMES_TEN)))));
413 } else {
414 return 0;
415 }
416 }
417
418
419
420
421 public String getVATDisplay() {
422 return displayCurrency(getVATValue());
423 }
424
425
426
427
428
429 public void setFromForm(Melati melati) {
430 ServletTemplateContext tc = melati.getServletTemplateContext();
431 setName(Form.getFormNulled(tc,"trolley_name"));
432 setEmail(Form.getFormNulled(tc,"trolley_email"));
433 setTel(Form.getFormNulled(tc,"trolley_tel"));
434 setDeliveryAddress(Form.getFormNulled(tc,"trolley_deliveryaddress"));
435 setTown(Form.getFormNulled(tc,"trolley_town"));
436 setCounty(Form.getFormNulled(tc,"trolley_county"));
437 setCountry(Form.getFormNulled(tc,"trolley_country"));
438 setPostcode(Form.getFormNulled(tc,"trolley_postcode"));
439 setMessage(Form.getFormNulled(tc,"trolley_message"));
440 hasDetails = true;
441 }
442
443
444
445
446 public void setDeliveryAddress(String a) {
447 address = a;
448 }
449
450
451
452 public String getDeliveryAddress() {
453 return address;
454 }
455
456
457
458
459 public void setName(String a) {
460 name = a;
461 }
462
463
464
465 public String getName() {
466 return name;
467 }
468
469
470
471
472 public void setEmail(String a) {
473 email = a;
474 }
475
476
477
478 public String getEmail() {
479 return email;
480 }
481
482
483
484
485 public void setPostcode(String a) {
486 postcode = a;
487 }
488
489
490
491 public String getPostcode() {
492 return postcode;
493 }
494
495
496
497
498 public void setTel(String a) {
499 tel = a;
500 }
501
502
503
504 public String getTel() {
505 return tel;
506 }
507
508
509
510
511 public void setTown(String a) {
512 town = a;
513 }
514
515
516
517 public String getTown() {
518 return town;
519 }
520
521
522
523
524 public void setCounty(String a) {
525 county = a;
526 }
527
528
529
530 public String getCounty() {
531 return county;
532 }
533
534
535
536
537 public void setCountry(String a) {
538 country = a;
539 }
540
541
542
543 public String getCountry() {
544 return country;
545 }
546
547
548
549
550 public void setMessage(String a) {
551 message = a;
552 }
553
554
555
556 public String getMessage() {
557 return message;
558 }
559
560
561
562
563 public String displayCurrency(double value) {
564 return new String(NumberFormat.getCurrencyInstance(
565 getLocale()).format(value));
566 }
567
568
569
570
571 public String displayCurrency(Double value) {
572 return displayCurrency(value.doubleValue());
573 }
574
575 public String baseURL() {
576 return melati.getRequest().getServletPath() + "/" +
577 melati.getPoemContext().getLogicalDatabase() + "/";
578 }
579
580 public String viewURL() {
581 return baseURL() + "View/";
582 }
583
584 public String detailsURL() {
585 return baseURL() + "Details/";
586 }
587
588 public String confirmURL() {
589 return baseURL() + "Confirm/";
590 }
591
592 public String abandonURL() {
593 return baseURL() + "Abandon/";
594 }
595
596 public String updateURL() {
597 return baseURL() + "Update/";
598 }
599
600 public String paidURL() {
601 return baseURL() + "Paid/";
602 }
603
604 public static double roundTo2dp(double num) {
605 int a = Math.round(new Float(num * 100).floatValue());
606 double b = new Double(a).doubleValue();
607 return (b/100);
608 }
609
610 }
611