Author: hansbak
Date: Thu Dec 8 10:35:58 2011 New Revision: 1211810 URL: http://svn.apache.org/viewvc?rev=1211810&view=rev Log: will persist shopping cart even when a user is not logged (over cookie) Modified: ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppinglist/ShoppingListEvents.java ofbiz/trunk/specialpurpose/ecommerce/webapp/ecommerce/WEB-INF/controller.xml Modified: ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppinglist/ShoppingListEvents.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppinglist/ShoppingListEvents.java?rev=1211810&r1=1211809&r2=1211810&view=diff ============================================================================== --- ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppinglist/ShoppingListEvents.java (original) +++ ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppinglist/ShoppingListEvents.java Thu Dec 8 10:35:58 2011 @@ -24,7 +24,9 @@ import java.util.Iterator; import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.Properties; +import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; @@ -627,4 +629,80 @@ public class ShoppingListEvents { } return arr; } -} + + /** + * Create the guest cookies for a shopping list + */ + public static String createGuestShoppingListCookies (HttpServletRequest request, HttpServletResponse response){ + Delegator delegator = (Delegator) request.getAttribute("delegator"); + LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher"); + HttpSession session = request.getSession(true); + ShoppingCart cart = (ShoppingCart) session.getAttribute("shoppingCart"); + GenericValue userLogin = (GenericValue) session.getAttribute("userLogin"); + Properties systemProps = System.getProperties(); + String guestShoppingUserName = "GuestShoppingListId_" + systemProps.getProperty("user.name"); + String productStoreId = ProductStoreWorker.getProductStoreId(request); + int cookieAge = (60 * 60 * 24 * 30); + String autoSaveListId = null; + Cookie[] cookies = request.getCookies(); + + // check userLogin + if (UtilValidate.isNotEmpty(userLogin)) { + String partyId = userLogin.getString("partyId"); + if (UtilValidate.isEmpty(partyId)) { + return "success"; + } + } + + // find shopping list ID + if (cookies != null) { + for (Cookie cookie: cookies) { + if (cookie.getName().equals(guestShoppingUserName)) { + autoSaveListId = cookie.getValue(); + break; + } + } + } + + // clear the auto-save info + if (ProductStoreWorker.autoSaveCart(delegator, productStoreId)) { + if (UtilValidate.isEmpty(autoSaveListId)) { + try { + Map<String, Object> listFields = UtilMisc.<String, Object>toMap("userLogin", userLogin, "productStoreId", productStoreId, "shoppingListTypeId", "SLT_SPEC_PURP", "listName", PERSISTANT_LIST_NAME); + Map<String, Object> newListResult = dispatcher.runSync("createShoppingList", listFields); + if (newListResult != null) { + autoSaveListId = (String) newListResult.get("shoppingListId"); + } + } catch (GeneralException e) { + Debug.logError(e, module); + } + Cookie guestShoppingListCookie = new Cookie(guestShoppingUserName, autoSaveListId); + guestShoppingListCookie.setMaxAge(cookieAge); + guestShoppingListCookie.setPath("/"); + response.addCookie(guestShoppingListCookie); + } + } + if (UtilValidate.isNotEmpty(autoSaveListId)) { + if (UtilValidate.isNotEmpty(cart)) { + cart.setAutoSaveListId(autoSaveListId); + } else { + cart = ShoppingCartEvents.getCartObject(request); + cart.setAutoSaveListId(autoSaveListId); + } + } + return "success"; + } + + /** + * Clear the guest cookies for a shopping list + */ + public static String clearGuestShoppingListCookies (HttpServletRequest request, HttpServletResponse response){ + Properties systemProps = System.getProperties(); + String guestShoppingUserName = "GuestShoppingListId_" + systemProps.getProperty("user.name"); + Cookie guestShoppingListCookie = new Cookie(guestShoppingUserName, null); + guestShoppingListCookie.setMaxAge(0); + guestShoppingListCookie.setPath("/"); + response.addCookie(guestShoppingListCookie); + return "success"; + } +} \ No newline at end of file Modified: ofbiz/trunk/specialpurpose/ecommerce/webapp/ecommerce/WEB-INF/controller.xml URL: http://svn.apache.org/viewvc/ofbiz/trunk/specialpurpose/ecommerce/webapp/ecommerce/WEB-INF/controller.xml?rev=1211810&r1=1211809&r2=1211810&view=diff ============================================================================== --- ofbiz/trunk/specialpurpose/ecommerce/webapp/ecommerce/WEB-INF/controller.xml (original) +++ ofbiz/trunk/specialpurpose/ecommerce/webapp/ecommerce/WEB-INF/controller.xml Thu Dec 8 10:35:58 2011 @@ -48,6 +48,7 @@ under the License. <event name="autoLoginCheck" type="java" path="org.ofbiz.webapp.control.LoginWorker" invoke="autoLoginCheck"/> <event name="checkTrackingCodeCookies" type="java" path="org.ofbiz.marketing.tracking.TrackingCodeEvents" invoke="checkTrackingCodeCookies"/> <event name="setDefaultStoreSettings" type="java" path="org.ofbiz.product.product.ProductEvents" invoke="setDefaultStoreSettings"/> + <event name="createGuestShoppingListCookies" type="java" path="org.ofbiz.order.shoppinglist.ShoppingListEvents" invoke="createGuestShoppingListCookies"/> <event name="restoreAutoSaveList" type="java" path="org.ofbiz.order.shoppinglist.ShoppingListEvents" invoke="restoreAutoSaveList"/> </firstvisit> @@ -75,6 +76,7 @@ under the License. <event name="restoreAutoSaveList" type="java" path="org.ofbiz.order.shoppinglist.ShoppingListEvents" invoke="restoreAutoSaveList"/> <!-- after login and restoring from the auto-save list, save everything to the auto-save list to handle anything that may have already been in the cart before login --> <event name="saveCartToAutoSaveList" type="java" path="org.ofbiz.order.shoppinglist.ShoppingListEvents" invoke="saveCartToAutoSaveList"/> + <event name="clearGuestShoppingListCookies" type="java" path="org.ofbiz.order.shoppinglist.ShoppingListEvents" invoke="clearGuestShoppingListCookies"/> </after-login> <!-- default request; call into CMS --> |
Free forum by Nabble | Edit this page |