1 package de.orangecafe.amazonrcp.helper;
2
3 import de.orangecafe.amazonrcp.service.IAmazonService;
4 import de.orangecafe.amazonrcp.service.ServiceException;
5 import de.orangecafe.amazonrcp.AmazonRCP;
6 import com.amazon.webservices.awsecommerceservice._2007_04_04.Cart;
7 import com.amazon.webservices.awsecommerceservice._2007_04_04.Price;
8 import org.springframework.context.ApplicationContextAware;
9 import org.springframework.context.ApplicationContext;
10 import org.springframework.beans.BeansException;
11
12 import java.util.prefs.Preferences;
13
14
15
16
17 public class CartHolder implements ApplicationContextAware {
18 public final static String KEY_CART_ID = "CART_ID";
19 public final static String KEY_CART_HMAC = "CART_HMAC";
20
21 private IAmazonService _service;
22 private ApplicationContext _context;
23 private Preferences _preferences = Preferences.userNodeForPackage(AmazonRCP.class);
24
25 public void setAmazonService(final IAmazonService service) {
26 _service = service;
27 }
28
29 public void setApplicationContext(final ApplicationContext applicationContext) throws BeansException {
30 _context = applicationContext;
31 }
32
33 public Cart getCart() throws ServiceException {
34 String cartId = _preferences.get(KEY_CART_ID, null);
35 String cartHMAC = _preferences.get(KEY_CART_HMAC, null);
36
37 Cart cart;
38
39 if ((cartId != null) && (cartHMAC != null)) {
40 cart = _service.getCart(cartId, cartHMAC);
41 } else {
42 cart = _service.createCart();
43
44 _preferences.put(KEY_CART_ID, cart.getCartId());
45 _preferences.put(KEY_CART_HMAC, cart.getHMAC());
46 }
47
48 return initialize(cart);
49 }
50
51 private Cart initialize(final Cart cart) {
52 if (cart.getSubTotal() == null) {
53 Price price = (Price) _context.getBean("price");
54 cart.setSubTotal(price);
55 }
56
57 return cart;
58 }
59 }