001: /*******************************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *******************************************************************************/package org.ofbiz.order.shoppingcart.product;
019:
020: import java.util.Map;
021:
022: import javax.servlet.http.HttpServletRequest;
023: import javax.servlet.http.HttpServletResponse;
024: import javax.servlet.http.HttpSession;
025:
026: import org.ofbiz.base.util.Debug;
027: import org.ofbiz.base.util.UtilHttp;
028: import org.ofbiz.entity.GenericDelegator;
029: import org.ofbiz.entity.GenericValue;
030: import org.ofbiz.order.shoppingcart.ShoppingCart;
031: import org.ofbiz.order.shoppingcart.ShoppingCartEvents;
032: import org.ofbiz.order.shoppingcart.WebShoppingCart;
033: import org.ofbiz.product.store.ProductStoreWorker;
034:
035: /**
036: * ProductStoreWorker - Worker class for store related functionality
037: */
038: public class ProductStoreCartAwareEvents {
039:
040: public static final String module = ProductStoreCartAwareEvents.class
041: .getName();
042:
043: public static String setSessionProductStore(
044: HttpServletRequest request, HttpServletResponse response) {
045: Map parameters = UtilHttp.getParameterMap(request);
046: String productStoreId = (String) parameters
047: .get("productStoreId");
048:
049: try {
050: ProductStoreCartAwareEvents.setSessionProductStore(
051: productStoreId, request);
052: } catch (Exception e) {
053: String errMsg = "Problem setting new store: "
054: + e.toString();
055: Debug.logError(e, errMsg, module);
056: request.setAttribute("_ERROR_MESSAGE_", errMsg);
057: return "error";
058: }
059:
060: return "success";
061: }
062:
063: public static void setSessionProductStore(String productStoreId,
064: HttpServletRequest request) {
065: if (productStoreId == null) {
066: return;
067: }
068:
069: HttpSession session = request.getSession();
070: String oldProductStoreId = (String) session
071: .getAttribute("productStoreId");
072:
073: if (productStoreId.equals(oldProductStoreId)) {
074: // great, nothing to do, bye bye
075: return;
076: }
077:
078: GenericDelegator delegator = (GenericDelegator) request
079: .getAttribute("delegator");
080:
081: // get the ProductStore record, make sure it's valid
082: GenericValue productStore = ProductStoreWorker.getProductStore(
083: productStoreId, delegator);
084: if (productStore == null) {
085: throw new IllegalArgumentException(
086: "Cannot set session ProductStore, passed productStoreId ["
087: + productStoreId
088: + "] is not valid/not found.");
089: }
090:
091: // set the productStoreId in the session (we know is different by this point)
092: session.setAttribute("productStoreId", productStoreId);
093:
094: // have set the new store, now need to clear out the current catalog so the default for the new store will be used
095: session.removeAttribute("CURRENT_CATALOG_ID");
096:
097: // 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
098: // 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
099: UtilHttp.setCurrencyUomIfNone(session, productStore
100: .getString("defaultCurrencyUomId"));
101: UtilHttp.setLocaleIfNone(session, productStore
102: .getString("defaultLocaleString"));
103:
104: // if a shoppingCart exists in the session and the productStoreId on it is different,
105: // - leave the old cart as-is (don't clear it, want to leave the auto-save list intact)
106: // - but create a new cart (which will load from auto-save list if applicable) and put it in the session
107:
108: ShoppingCart cart = ShoppingCartEvents.getCartObject(request);
109: // this should always be different given the previous session productStoreId check, but just in case...
110: if (!productStoreId.equals(cart.getProductStoreId())) {
111: // this is a really simple operation now that we have prepared all of the data, as done above in this method
112: cart = new WebShoppingCart(request);
113: session.setAttribute("shoppingCart", cart);
114: }
115: }
116: }
|