KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > order > shoppingcart > CartEventListener


1 /*
2  * $Id: CartEventListener.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;
25
26 import java.util.Iterator JavaDoc;
27
28 import javax.servlet.http.HttpSession JavaDoc;
29 import javax.servlet.http.HttpSessionEvent JavaDoc;
30 import javax.servlet.http.HttpSessionListener JavaDoc;
31
32 import org.ofbiz.base.util.Debug;
33 import org.ofbiz.base.util.UtilValidate;
34 import org.ofbiz.webapp.stats.VisitHandler;
35 import org.ofbiz.entity.GenericDelegator;
36 import org.ofbiz.entity.GenericEntityException;
37 import org.ofbiz.entity.GenericValue;
38 import org.ofbiz.entity.transaction.TransactionUtil;
39
40 /**
41  * HttpSessionListener that saves information about abandoned carts
42  *
43  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
44  * @version $Rev: 5462 $
45  * @since 2.0
46  */

47 public class CartEventListener implements HttpSessionListener JavaDoc {
48
49     public static final String JavaDoc module = CartEventListener.class.getName();
50
51     public CartEventListener() {}
52
53     public void sessionCreated(HttpSessionEvent JavaDoc event) {
54         //for this one do nothing when the session is created...
55
//HttpSession session = event.getSession();
56
}
57
58     public void sessionDestroyed(HttpSessionEvent JavaDoc event) {
59         HttpSession JavaDoc session = event.getSession();
60         ShoppingCart cart = (ShoppingCart) session.getAttribute("shoppingCart");
61         if (cart == null) {
62             Debug.logInfo("No cart to save, doing nothing.", module);
63             return;
64         }
65         
66         String JavaDoc delegatorName = (String JavaDoc) session.getAttribute("delegatorName");
67         GenericDelegator delegator = null;
68         if (UtilValidate.isNotEmpty(delegatorName)) {
69             delegator = GenericDelegator.getGenericDelegator(delegatorName);
70         }
71         if (delegator == null) {
72             Debug.logError("Could not find delegator with delegatorName in session, not saving abandoned cart info.", module);
73             return;
74         }
75         
76         boolean beganTransaction = false;
77         try {
78             beganTransaction = TransactionUtil.begin();
79         
80             GenericValue visit = VisitHandler.getVisit(session);
81             if (visit == null) {
82                 Debug.logError("Could not get the current visit, not saving abandoned cart info.", module);
83                 return;
84             }
85             
86             Debug.logInfo("Saving abandoned cart", module);
87             Iterator JavaDoc cartItems = cart.iterator();
88             int seqId = 1;
89             while (cartItems.hasNext()) {
90                 ShoppingCartItem cartItem = (ShoppingCartItem) cartItems.next();
91                 GenericValue cartAbandonedLine = delegator.makeValue("CartAbandonedLine", null);
92
93                 cartAbandonedLine.set("visitId", visit.get("visitId"));
94                 cartAbandonedLine.set("cartAbandonedLineSeqId", (new Integer JavaDoc(seqId)).toString());
95                 cartAbandonedLine.set("productId", cartItem.getProductId());
96                 cartAbandonedLine.set("prodCatalogId", cartItem.getProdCatalogId());
97                 cartAbandonedLine.set("quantity", new Double JavaDoc(cartItem.getQuantity()));
98                 cartAbandonedLine.set("reservStart", cartItem.getReservStart());
99                 cartAbandonedLine.set("reservLength", new Double JavaDoc(cartItem.getReservLength()));
100                 cartAbandonedLine.set("reservPersons", new Double JavaDoc(cartItem.getReservPersons()));
101                 cartAbandonedLine.set("unitPrice", new Double JavaDoc(cartItem.getBasePrice()));
102                 cartAbandonedLine.set("reserv2ndPPPerc", new Double JavaDoc(cartItem.getReserv2ndPPPerc()));
103                 cartAbandonedLine.set("reservNthPPPerc", new Double JavaDoc(cartItem.getReservNthPPPerc()));
104                 cartAbandonedLine.set("totalWithAdjustments", new Double JavaDoc(cartItem.getItemSubTotal()));
105                 //not doing pre-reservations now, so this is always N
106
cartAbandonedLine.set("wasReserved", "N");
107                 cartAbandonedLine.create();
108
109                 seqId++;
110             }
111         } catch (GenericEntityException e) {
112             try {
113                 // only rollback the transaction if we started one...
114
TransactionUtil.rollback(beganTransaction, "Error saving abandoned cart info", e);
115             } catch (GenericEntityException e2) {
116                 Debug.logError(e2, "Could not rollback transaction: " + e2.toString(), module);
117             }
118
119             Debug.logError(e, "An entity engine error occurred while saving abandoned cart information", module);
120         } finally {
121             // only commit the transaction if we started one... this will throw an exception if it fails
122
try {
123                 TransactionUtil.commit(beganTransaction);
124             } catch (GenericEntityException e) {
125                 Debug.logError(e, "Could not commit transaction for entity engine error occurred while saving abandoned cart information", module);
126             }
127         }
128     }
129 }
130
Popular Tags