1 27 28 package olstore.action; 29 30 import java.util.ArrayList; 31 32 import javax.servlet.http.HttpServletRequest; 33 import javax.servlet.http.HttpServletResponse; 34 import javax.servlet.http.HttpSession; 35 36 import olstore.form.CheckoutForm; 37 38 import org.apache.struts.action.ActionError; 39 import org.apache.struts.action.ActionErrors; 40 import org.apache.struts.action.ActionForm; 41 import org.apache.struts.action.ActionForward; 42 import org.apache.struts.action.ActionMapping; 43 import org.apache.struts.action.ActionMessage; 44 import org.apache.struts.action.ActionMessages; 45 46 import olstore.session.ShoppingCartLocal; 47 48 public class CheckOutAction extends DemoBaseAction { 49 50 54 public ActionForward execute ( ActionMapping mapping, 55 ActionForm form, 56 HttpServletRequest request, 57 HttpServletResponse response 58 ) throws Exception { 59 60 61 String submitType = ((CheckoutForm)form).getSubmitType(); 62 if ( submitType==null || submitType.equals("")) { 63 copyShoppingCartToForm ( mapping, form, request, response ); 64 }else if(submitType.equals("update")){ 65 copyFormToShoppingCart(mapping, form, request, response); 66 copyShoppingCartToForm ( mapping, form, request, response ); 67 }else if( submitType.equals ( "submit" ) ) { 68 copyFormToShoppingCart(mapping, form, request, response); 69 copyShoppingCartToForm ( mapping, form, request, response ); 70 resetForm(mapping, form, request, response); 71 try{ 72 placeOrders ( mapping, form, request, response ); 73 ActionMessages messages = new ActionMessages(); 74 ActionMessage msg = new ActionMessage("checkout.save.success"); 75 messages.add("success", msg); 76 saveMessages(request, messages); 77 }catch(Exception e){ 78 ActionErrors errors = new ActionErrors(); 79 errors.add("error", new ActionError("errors.order.save", e.getMessage() )); 80 saveErrors(request, errors); 81 return (new ActionForward(mapping.getInput())); 82 } 83 } 84 85 return mapping.findForward ( "/checkout/updateCheckout" ); 86 87 } 88 89 public void resetForm ( ActionMapping mapping, 90 ActionForm form, 91 HttpServletRequest request, 92 HttpServletResponse response 93 ) throws Exception { 94 ((CheckoutForm) form).reset(); 95 } 96 97 public void copyShoppingCartToForm ( ActionMapping mapping, 98 ActionForm form, 99 HttpServletRequest request, 100 HttpServletResponse response 101 ) throws Exception { 102 HttpSession session = request.getSession(false); 103 ShoppingCartLocal shoppingCart = (ShoppingCartLocal)session.getAttribute("shoppingCart"); 104 ArrayList cartEntries = shoppingCart.shoppingCartToDTOs(); 105 ((CheckoutForm)form).setCartEntries ( cartEntries ); 106 ((CheckoutForm)form).setTotalCost ( shoppingCart.getTotalCost()); 107 } 108 109 public void copyFormToShoppingCart ( ActionMapping mapping, 110 ActionForm form, 111 HttpServletRequest request, 112 HttpServletResponse response 113 ) throws Exception { 114 HttpSession session = request.getSession(false); 115 ShoppingCartLocal shoppingCart = (ShoppingCartLocal) session.getAttribute("shoppingCart"); 116 shoppingCart.DTOsToShoppingCart(((CheckoutForm) form).getCartEntries()); 117 } 118 119 120 121 123 public void placeOrders ( ActionMapping mapping, 124 ActionForm form, 125 HttpServletRequest request, 126 HttpServletResponse response 127 ) throws Exception { 128 HttpSession session = request.getSession(false); 129 ShoppingCartLocal shoppingCart = (ShoppingCartLocal) session.getAttribute("shoppingCart"); 130 shoppingCart.createOrders(); 131 } 132 133 134 135 136 } 137 | Popular Tags |