| 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 | 0 | public abstract class ShoppingTrolley { |
| 74 | |
|
| 75 | 0 | 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 | 0 | protected boolean hasDetails = false; |
| 88 | 0 | Vector orderedItems = new Vector(); |
| 89 | 0 | 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 | 0 | this.config = configIn; |
| 100 | 0 | this.melati = melatiIn; |
| 101 | 0 | } |
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
public void initialise(Melati melatiIn, |
| 107 | |
MelatiShoppingConfig configIn, Integer id) |
| 108 | |
throws InstantiationPropertyException { |
| 109 | 0 | initialise(melatiIn,configIn); |
| 110 | 0 | load(id); |
| 111 | 0 | HttpSession session = melati.getSession(); |
| 112 | 0 | session.setAttribute(name(),this); |
| 113 | 0 | } |
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
public void remove(Melati melatiIn) { |
| 119 | 0 | HttpSession session = melatiIn.getSession(); |
| 120 | 0 | session.removeAttribute(name()); |
| 121 | 0 | } |
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
public static synchronized ShoppingTrolley |
| 128 | |
getInstance(Melati melati, MelatiShoppingConfig config) |
| 129 | |
throws InstantiationPropertyException { |
| 130 | 0 | HttpSession session = melati.getSession(); |
| 131 | 0 | ShoppingTrolley instance = (ShoppingTrolley) session.getAttribute(name()); |
| 132 | 0 | if (instance == null) { |
| 133 | 0 | instance = newTrolley(config); |
| 134 | 0 | instance.initialise(melati,config); |
| 135 | 0 | session.setAttribute(name(),instance); |
| 136 | |
} |
| 137 | 0 | instance.configureRequest(melati); |
| 138 | 0 | return instance; |
| 139 | |
} |
| 140 | |
|
| 141 | |
public static synchronized ShoppingTrolley |
| 142 | |
newTrolley(MelatiShoppingConfig config) |
| 143 | |
throws InstantiationPropertyException { |
| 144 | 0 | 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 | 0 | this.locale = locale; |
| 158 | 0 | } |
| 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 | 0 | this.melati = melatiIn; |
| 181 | 0 | } |
| 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 | 0 | return TROLLEY; |
| 203 | |
} |
| 204 | |
|
| 205 | |
|
| 206 | |
|
| 207 | |
|
| 208 | |
public Enumeration getItems() { |
| 209 | 0 | return orderedItems.elements(); |
| 210 | |
} |
| 211 | |
|
| 212 | |
|
| 213 | |
|
| 214 | |
|
| 215 | |
public boolean isEmpty() { |
| 216 | 0 | return items.isEmpty(); |
| 217 | |
} |
| 218 | |
|
| 219 | |
|
| 220 | |
|
| 221 | |
|
| 222 | |
public boolean hasDetails() { |
| 223 | 0 | return hasDetails; |
| 224 | |
} |
| 225 | |
|
| 226 | |
|
| 227 | |
|
| 228 | |
|
| 229 | |
public ShoppingTrolleyItem getItem(Integer id) { |
| 230 | 0 | return (ShoppingTrolleyItem)items.get(id); |
| 231 | |
} |
| 232 | |
|
| 233 | |
|
| 234 | |
|
| 235 | |
|
| 236 | |
public void removeItem(ShoppingTrolleyItem item) { |
| 237 | 0 | items.remove(item.getId()); |
| 238 | 0 | orderedItems.removeElement(item); |
| 239 | 0 | } |
| 240 | |
|
| 241 | |
|
| 242 | |
|
| 243 | |
|
| 244 | |
public void addItem(ShoppingTrolleyItem item) { |
| 245 | |
|
| 246 | 0 | if (!items.containsKey(item.getId())) { |
| 247 | 0 | orderedItems.add(item); |
| 248 | |
} |
| 249 | 0 | items.put(item.getId(),item); |
| 250 | 0 | } |
| 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 | 0 | ShoppingTrolleyItem item = ShoppingTrolleyItem.newTrolleyItem(config); |
| 265 | 0 | item.initialise(this, melati, id, description, price); |
| 266 | 0 | addItem(item); |
| 267 | 0 | return item; |
| 268 | |
} |
| 269 | |
|
| 270 | |
|
| 271 | |
|
| 272 | |
|
| 273 | |
public double getValue() { |
| 274 | 0 | double value = 0; |
| 275 | 0 | for (Enumeration en = items.elements(); en.hasMoreElements();) { |
| 276 | 0 | ShoppingTrolleyItem product = (ShoppingTrolleyItem) en.nextElement(); |
| 277 | 0 | value += product.getValue(); |
| 278 | 0 | } |
| 279 | 0 | return value; |
| 280 | |
} |
| 281 | |
|
| 282 | |
|
| 283 | |
|
| 284 | |
|
| 285 | |
|
| 286 | |
public String getValueDisplay() { |
| 287 | 0 | return displayCurrency(getValue()); |
| 288 | |
} |
| 289 | |
|
| 290 | |
|
| 291 | |
|
| 292 | |
|
| 293 | |
public double getTotalValue() { |
| 294 | 0 | return getValue() + |
| 295 | |
getTotalDeliveryValue() + |
| 296 | |
getDiscountValue() + |
| 297 | |
getVATValue(); |
| 298 | |
} |
| 299 | |
|
| 300 | |
|
| 301 | |
|
| 302 | |
|
| 303 | |
|
| 304 | |
public String getTotalValueDisplay() { |
| 305 | 0 | return displayCurrency(getTotalValue()); |
| 306 | |
} |
| 307 | |
|
| 308 | |
|
| 309 | |
|
| 310 | |
|
| 311 | |
|
| 312 | |
public String getTotalValuePence() { |
| 313 | 0 | 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 | 0 | double value = 0; |
| 334 | 0 | if (hasDelivery()) { |
| 335 | 0 | value = getDeliveryValue(); |
| 336 | 0 | for (Enumeration en = items.elements(); en.hasMoreElements();) { |
| 337 | 0 | ShoppingTrolleyItem item = (ShoppingTrolleyItem)en.nextElement(); |
| 338 | 0 | value += item.getDeliveryValue(); |
| 339 | 0 | } |
| 340 | |
} |
| 341 | 0 | return value; |
| 342 | |
} |
| 343 | |
|
| 344 | |
|
| 345 | |
|
| 346 | |
|
| 347 | |
public String getDeliveryDisplay() { |
| 348 | 0 | 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 | 0 | double value = 0; |
| 368 | 0 | if (hasDiscount()) { |
| 369 | 0 | value = 0 - roundTo2dp(getValue()*getDiscountRate()); |
| 370 | |
} |
| 371 | 0 | return value; |
| 372 | |
} |
| 373 | |
|
| 374 | |
|
| 375 | |
|
| 376 | |
|
| 377 | |
public String getDiscountRateDisplay() { |
| 378 | 0 | if (hasDiscount()) { |
| 379 | |
try { |
| 380 | 0 | return (new Double(getDiscountRate())).intValue() + "%"; |
| 381 | 0 | } catch (NumberFormatException e) { |
| 382 | 0 | return getDiscountRate() + "%"; |
| 383 | |
} |
| 384 | |
} else { |
| 385 | 0 | return ""; |
| 386 | |
} |
| 387 | |
} |
| 388 | |
|
| 389 | |
|
| 390 | |
|
| 391 | |
|
| 392 | |
public String getDiscountValueDisplay() throws Exception { |
| 393 | 0 | 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 | 0 | if (!hasVAT()) { |
| 411 | 0 | return roundTo2dp((getValue() * |
| 412 | |
(-1 * (1.0 - (1000.0/VAT_PERCENT_TIMES_TEN))))); |
| 413 | |
} else { |
| 414 | 0 | return 0; |
| 415 | |
} |
| 416 | |
} |
| 417 | |
|
| 418 | |
|
| 419 | |
|
| 420 | |
|
| 421 | |
public String getVATDisplay() { |
| 422 | 0 | return displayCurrency(getVATValue()); |
| 423 | |
} |
| 424 | |
|
| 425 | |
|
| 426 | |
|
| 427 | |
|
| 428 | |
|
| 429 | |
public void setFromForm(Melati melati) { |
| 430 | 0 | ServletTemplateContext tc = melati.getServletTemplateContext(); |
| 431 | 0 | setName(Form.getFormNulled(tc,"trolley_name")); |
| 432 | 0 | setEmail(Form.getFormNulled(tc,"trolley_email")); |
| 433 | 0 | setTel(Form.getFormNulled(tc,"trolley_tel")); |
| 434 | 0 | setDeliveryAddress(Form.getFormNulled(tc,"trolley_deliveryaddress")); |
| 435 | 0 | setTown(Form.getFormNulled(tc,"trolley_town")); |
| 436 | 0 | setCounty(Form.getFormNulled(tc,"trolley_county")); |
| 437 | 0 | setCountry(Form.getFormNulled(tc,"trolley_country")); |
| 438 | 0 | setPostcode(Form.getFormNulled(tc,"trolley_postcode")); |
| 439 | 0 | setMessage(Form.getFormNulled(tc,"trolley_message")); |
| 440 | 0 | hasDetails = true; |
| 441 | 0 | } |
| 442 | |
|
| 443 | |
|
| 444 | |
|
| 445 | |
|
| 446 | |
public void setDeliveryAddress(String a) { |
| 447 | 0 | address = a; |
| 448 | 0 | } |
| 449 | |
|
| 450 | |
|
| 451 | |
|
| 452 | |
public String getDeliveryAddress() { |
| 453 | 0 | return address; |
| 454 | |
} |
| 455 | |
|
| 456 | |
|
| 457 | |
|
| 458 | |
|
| 459 | |
public void setName(String a) { |
| 460 | 0 | name = a; |
| 461 | 0 | } |
| 462 | |
|
| 463 | |
|
| 464 | |
|
| 465 | |
public String getName() { |
| 466 | 0 | return name; |
| 467 | |
} |
| 468 | |
|
| 469 | |
|
| 470 | |
|
| 471 | |
|
| 472 | |
public void setEmail(String a) { |
| 473 | 0 | email = a; |
| 474 | 0 | } |
| 475 | |
|
| 476 | |
|
| 477 | |
|
| 478 | |
public String getEmail() { |
| 479 | 0 | return email; |
| 480 | |
} |
| 481 | |
|
| 482 | |
|
| 483 | |
|
| 484 | |
|
| 485 | |
public void setPostcode(String a) { |
| 486 | 0 | postcode = a; |
| 487 | 0 | } |
| 488 | |
|
| 489 | |
|
| 490 | |
|
| 491 | |
public String getPostcode() { |
| 492 | 0 | return postcode; |
| 493 | |
} |
| 494 | |
|
| 495 | |
|
| 496 | |
|
| 497 | |
|
| 498 | |
public void setTel(String a) { |
| 499 | 0 | tel = a; |
| 500 | 0 | } |
| 501 | |
|
| 502 | |
|
| 503 | |
|
| 504 | |
public String getTel() { |
| 505 | 0 | return tel; |
| 506 | |
} |
| 507 | |
|
| 508 | |
|
| 509 | |
|
| 510 | |
|
| 511 | |
public void setTown(String a) { |
| 512 | 0 | town = a; |
| 513 | 0 | } |
| 514 | |
|
| 515 | |
|
| 516 | |
|
| 517 | |
public String getTown() { |
| 518 | 0 | return town; |
| 519 | |
} |
| 520 | |
|
| 521 | |
|
| 522 | |
|
| 523 | |
|
| 524 | |
public void setCounty(String a) { |
| 525 | 0 | county = a; |
| 526 | 0 | } |
| 527 | |
|
| 528 | |
|
| 529 | |
|
| 530 | |
public String getCounty() { |
| 531 | 0 | return county; |
| 532 | |
} |
| 533 | |
|
| 534 | |
|
| 535 | |
|
| 536 | |
|
| 537 | |
public void setCountry(String a) { |
| 538 | 0 | country = a; |
| 539 | 0 | } |
| 540 | |
|
| 541 | |
|
| 542 | |
|
| 543 | |
public String getCountry() { |
| 544 | 0 | return country; |
| 545 | |
} |
| 546 | |
|
| 547 | |
|
| 548 | |
|
| 549 | |
|
| 550 | |
public void setMessage(String a) { |
| 551 | 0 | message = a; |
| 552 | 0 | } |
| 553 | |
|
| 554 | |
|
| 555 | |
|
| 556 | |
public String getMessage() { |
| 557 | 0 | return message; |
| 558 | |
} |
| 559 | |
|
| 560 | |
|
| 561 | |
|
| 562 | |
|
| 563 | |
public String displayCurrency(double value) { |
| 564 | 0 | return new String(NumberFormat.getCurrencyInstance( |
| 565 | |
getLocale()).format(value)); |
| 566 | |
} |
| 567 | |
|
| 568 | |
|
| 569 | |
|
| 570 | |
|
| 571 | |
public String displayCurrency(Double value) { |
| 572 | 0 | return displayCurrency(value.doubleValue()); |
| 573 | |
} |
| 574 | |
|
| 575 | |
public String baseURL() { |
| 576 | 0 | return melati.getRequest().getServletPath() + "/" + |
| 577 | |
melati.getPoemContext().getLogicalDatabase() + "/"; |
| 578 | |
} |
| 579 | |
|
| 580 | |
public String viewURL() { |
| 581 | 0 | return baseURL() + "View/"; |
| 582 | |
} |
| 583 | |
|
| 584 | |
public String detailsURL() { |
| 585 | 0 | return baseURL() + "Details/"; |
| 586 | |
} |
| 587 | |
|
| 588 | |
public String confirmURL() { |
| 589 | 0 | return baseURL() + "Confirm/"; |
| 590 | |
} |
| 591 | |
|
| 592 | |
public String abandonURL() { |
| 593 | 0 | return baseURL() + "Abandon/"; |
| 594 | |
} |
| 595 | |
|
| 596 | |
public String updateURL() { |
| 597 | 0 | return baseURL() + "Update/"; |
| 598 | |
} |
| 599 | |
|
| 600 | |
public String paidURL() { |
| 601 | 0 | return baseURL() + "Paid/"; |
| 602 | |
} |
| 603 | |
|
| 604 | |
public static double roundTo2dp(double num) { |
| 605 | 0 | int a = Math.round(new Float(num * 100).floatValue()); |
| 606 | 0 | double b = new Double(a).doubleValue(); |
| 607 | 0 | return (b/100); |
| 608 | |
} |
| 609 | |
|
| 610 | |
} |
| 611 | |
|