View Javadoc

1   package org.paneris.bibliomania.shopping;
2   
3   import java.util.Enumeration;
4   
5   import org.melati.Melati;
6   import org.paneris.bibliomania.BibliomaniaDatabase;
7   import org.paneris.bibliomania.Book;
8   import org.paneris.bibliomania.DeliveryCharge;
9   import org.paneris.bibliomania.DeliveryChargeBand;
10  import org.paneris.bibliomania.Product;
11  import org.paneris.bibliomania.ShopOrder;
12  import org.paneris.bibliomania.ShopOrderItem;
13  import org.paneris.melati.shopping.ShoppingTrolley;
14  import org.paneris.melati.shopping.ShoppingTrolleyItem;
15  
16  public class BibliomaniaShoppingTrolleyItem extends ShoppingTrolleyItem {
17  
18    private Product product;
19    private DeliveryCharge charge = null;
20    private boolean gotCharge = false;
21    //private boolean download = false; // when we sell downloads
22    private BibliomaniaShoppingTrolley bibShoppingTrolley;
23    
24    public void initialise(ShoppingTrolley trolleyP, Melati melatiP,
25                           Integer idP, String descriptionP, Double priceP) {
26      super.initialise(trolleyP, melatiP, idP, descriptionP, priceP);
27      bibShoppingTrolley = (BibliomaniaShoppingTrolley)trolley;
28    }  
29    
30   /**
31    * Load in information about this product given an id.  
32    * Perhaps this id represents a poem troid?
33    */
34    protected void load(Integer idP){
35      BibliomaniaDatabase db = (BibliomaniaDatabase)melati.getDatabase();
36      product = (Product)db.getProductTable().getIdColumn().firstWhereEq(idP);
37      description = product.getName();
38      if ((description == null || description.equals("")) && product.getBook() != null)
39        description = product.getBook().getTitle();
40      price = 0;
41      if (product.getPrice() != null) price = product.getPrice().doubleValue();
42      //if (product.getDownload() != null) download = true;
43    }
44      
45    protected void save(BibliomaniaDatabase db, ShopOrder order){
46      ShopOrderItem item = (ShopOrderItem)db.getShopOrderItemTable().newPersistent();
47      item.setUser(order.getUser());
48      item.setOrder(order);
49      item.setProduct(product);
50      item.setQuantity((new Double(getQuantity())).intValue());
51      item.setAmount(bibShoppingTrolley.convertFromUK(price * quantity));
52      item.setDelivery(bibShoppingTrolley.convertFromUK(getDeliveryValue()));
53      item.setAmountUK(price * quantity);
54      item.setDeliveryUK(getDeliveryValue());
55      item.setStatus(db.getOrderStatusTable().getNotAuthorised());
56      item.setSupplierTroid(new Integer(0));
57      db.getShopOrderItemTable().create(item);
58    }
59      
60   /**
61    * Work out the cost of delivery.
62    * Returns null if a charge cannot be calculated - an error should be displayed.
63    */
64    public double getDeliveryValue() {
65      getDeliveryCharge();
66      double cost = 0;
67      // can't get a charge - indicate error by returning null
68      if (charge != null) {
69        cost = charge.getCharge().doubleValue();
70      }
71      return cost * quantity;
72    }
73      
74      
75   /**
76    * Work out the cost of delivery.
77    * Returns null if a charge cannot be calculated - an error should be displayed.
78    */
79    public DeliveryCharge getDeliveryCharge() {
80      if (gotCharge) return charge;
81      charge = null;
82      if (bibShoppingTrolley.countryCode != null) {
83        DeliveryChargeBand band = bibShoppingTrolley.countryCode.getDeliverycharge();
84        if (band != null) {
85          BibliomaniaDatabase db = (BibliomaniaDatabase)melati.getDatabase();
86          String whereClause = "band = " + band.getTroid() + " and supplier in " +
87          " (select supplier from supplierproduct where product = " + product.getTroid() + ")";
88          String orderClause = "charge";
89          Enumeration them = db.getDeliveryChargeTable().selection
90                   (whereClause, orderClause, false);
91          if (them.hasMoreElements()) charge = (DeliveryCharge)them.nextElement();
92        }
93      }
94      gotCharge = true;
95      return charge;
96    }
97  
98    public void setGotCharge(boolean c) {
99      gotCharge = c;
100   }
101   
102   public Book getBook() {
103     return product.getBook();
104   }
105   
106   public Product getProduct() {
107     return product;
108   }
109 
110   public String displayCurrency(double value) {
111     return bibShoppingTrolley.convertFromUKandFormat(value);
112   }
113 
114 }
115