1 27 package olstore.action; 28 29 import javax.servlet.http.HttpServletRequest; 30 import javax.servlet.http.HttpServletResponse; 31 import javax.servlet.http.HttpSession; 32 33 import olstore.dto.ItemValue; 34 import olstore.dto.PictureValue; 35 import olstore.framework.EJBHomeFactory; 36 import olstore.util.WebUtils; 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 import olstore.session.helper.ItemHelperLocal; 48 import olstore.session.helper.ItemHelperLocalHome; 49 50 51 public class ViewItemAction extends DemoBaseAction { 52 53 57 public ActionForward execute ( ActionMapping mapping, 58 ActionForm form, 59 HttpServletRequest request, 60 HttpServletResponse response 61 ) throws Exception { 62 63 ActionErrors errors = new ActionErrors(); 64 ActionMessages messages = new ActionMessages(); 65 66 String itemId = WebUtils.getValue ( "itemId", request ); 67 String interest = WebUtils.getValue ( "interest", request ); 68 String purchase = WebUtils.getValue ( "purchase", request ); 69 70 EJBHomeFactory factory = EJBHomeFactory.getInstance(); 71 ItemHelperLocalHome itemHelperHome = (ItemHelperLocalHome) factory.getLocalHome ( EJBHomeFactory.ITEM_HELPER ); 72 ItemHelperLocal itemHelper = itemHelperHome.create(); 73 74 ItemValue item = null; 76 if ( itemId != null ) { 77 try { 78 item = itemHelper.getItemValueForId ( itemId ); 79 } catch ( Exception e ) { 80 errors.add("error", new ActionError("errors.item.load", e.getMessage() )); 82 saveErrors(request, errors); 83 return (new ActionForward(mapping.getInput())); 84 } 85 } 86 if ( item == null ) { 87 errors.add("error", new ActionError("errors.item.view") ); 88 saveErrors(request, errors); 89 return (new ActionForward(mapping.getInput())); 90 } 91 92 request.setAttribute ( "item", item ); 93 PictureValue pic = itemHelper.getPicture ( itemId ); 94 if ( pic != null ) { 95 request.setAttribute ( "pic", pic ); 96 } 97 98 99 if ( interest != null && interest.equals ( "true" ) ) { 101 try { 102 itemHelper.markItemAsInteresting ( itemId, request.getRemoteUser() ); 103 ActionMessage msg = new ActionMessage("item.interest.success", 104 item.getName() ); 105 messages.add("success", msg); 106 saveMessages(request, messages); 107 } catch ( Exception e ) { 108 errors.add("error", new ActionError("errors.item.interesting", 109 item.getName(), 110 request.getRemoteUser(), 111 e.getMessage() )); 112 saveErrors(request, errors); 113 return (new ActionForward(mapping.getInput())); 114 } 115 } 116 117 118 if ( purchase != null && purchase.equals ( "true" ) ) { 120 try { 121 122 HttpSession session = request.getSession(); 123 ShoppingCartLocal cart = (ShoppingCartLocal ) session.getAttribute ( "shoppingCart" ); 124 cart.updateQuantity( new Integer ( itemId ), new Integer(1) ); 125 126 ActionMessage msg = new ActionMessage("item.purchase.success", 127 item.getName() ); 128 messages.add("success", msg); 129 saveMessages(request, messages); 130 } catch ( Exception e ) { 131 errors.add("error", new ActionError("errors.item.purchase", 132 item.getName(), 133 request.getRemoteUser(), 134 e.getMessage() )); 135 saveErrors(request, errors); 136 return (new ActionForward(mapping.getInput())); 137 } 138 } 139 140 return mapping.findForward ( "updateItemView" ); 141 142 } 143 } 144 | Popular Tags |