KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > order > shoppingcart > product > ProductStoreCartAwareEvents


1 /*
2  * $Id: ProductStoreCartAwareEvents.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24 package org.ofbiz.order.shoppingcart.product;
25
26 import java.util.Map JavaDoc;
27
28 import javax.servlet.http.HttpServletRequest JavaDoc;
29 import javax.servlet.http.HttpServletResponse JavaDoc;
30 import javax.servlet.http.HttpSession JavaDoc;
31
32 import org.ofbiz.base.util.Debug;
33 import org.ofbiz.base.util.UtilHttp;
34 import org.ofbiz.entity.GenericDelegator;
35 import org.ofbiz.entity.GenericValue;
36 import org.ofbiz.order.shoppingcart.ShoppingCart;
37 import org.ofbiz.order.shoppingcart.ShoppingCartEvents;
38 import org.ofbiz.order.shoppingcart.WebShoppingCart;
39 import org.ofbiz.product.store.ProductStoreWorker;
40
41 /**
42  * ProductStoreWorker - Worker class for store related functionality
43  *
44  * @author <a HREF="mailto:jaz@ofbiz.org">David E. Jones</a>
45  * @version $Rev: 5462 $
46  * @since 3.5
47  */

48 public class ProductStoreCartAwareEvents {
49
50     public static final String JavaDoc module = ProductStoreCartAwareEvents.class.getName();
51
52     public static String JavaDoc setSessionProductStore(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
53         Map JavaDoc parameters = UtilHttp.getParameterMap(request);
54         String JavaDoc productStoreId = (String JavaDoc) parameters.get("productStoreId");
55         
56         try {
57             ProductStoreCartAwareEvents.setSessionProductStore(productStoreId, request);
58         } catch (Exception JavaDoc e) {
59             String JavaDoc errMsg = "Problem setting new store: " + e.toString();
60             Debug.logError(e, errMsg, module);
61             request.setAttribute("_ERROR_MESSAGE_", errMsg);
62             return "error";
63         }
64         
65         return "success";
66     }
67
68     public static void setSessionProductStore(String JavaDoc productStoreId, HttpServletRequest JavaDoc request) {
69         if (productStoreId == null) {
70             return;
71         }
72         
73         HttpSession JavaDoc session = request.getSession();
74         String JavaDoc oldProductStoreId = (String JavaDoc) session.getAttribute("productStoreId");
75         
76         if (productStoreId.equals(oldProductStoreId)) {
77             // great, nothing to do, bye bye
78
return;
79         }
80         
81         GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator");
82         
83         // get the ProductStore record, make sure it's valid
84
GenericValue productStore = ProductStoreWorker.getProductStore(productStoreId, delegator);
85         if (productStore == null) {
86             throw new IllegalArgumentException JavaDoc("Cannot set session ProductStore, passed productStoreId [" + productStoreId + "] is not valid/not found.");
87         }
88         
89         // set the productStoreId in the session (we know is different by this point)
90
session.setAttribute("productStoreId", productStoreId);
91         
92         // have set the new store, now need to clear out the current catalog so the default for the new store will be used
93
session.removeAttribute("CURRENT_CATALOG_ID");
94         
95         // if there is no locale or currencyUom in the session, set the defaults from the store, but don't do so through the CommonEvents methods setSessionLocale and setSessionCurrencyUom because we don't want these to be put on the UserLogin entity
96
// note that this is different from the normal default setting process because these will now override the settings on the UserLogin; this is desired when changing stores and the user should be given a chance to change their personal settings after the store change
97
UtilHttp.setCurrencyUomIfNone(session, productStore.getString("defaultCurrencyUomId"));
98         UtilHttp.setLocaleIfNone(session, productStore.getString("defaultLocaleString"));
99         
100         // if a shoppingCart exists in the session and the productStoreId on it is different,
101
// - leave the old cart as-is (don't clear it, want to leave the auto-save list intact)
102
// - but create a new cart (which will load from auto-save list if applicable) and put it in the session
103

104         ShoppingCart cart = ShoppingCartEvents.getCartObject(request);
105         // this should always be different given the previous session productStoreId check, but just in case...
106
if (!productStoreId.equals(cart.getProductStoreId())) {
107             // this is a really simple operation now that we have prepared all of the data, as done above in this method
108
cart = new WebShoppingCart(request);
109             session.setAttribute("shoppingCart", cart);
110         }
111     }
112 }
113
Popular Tags