001: /**
002: * Copyright (c) 2004 Red Hat, Inc. All rights reserved.
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
017: * USA
018: *
019: * Component of: Red Hat Application Server
020: *
021: * Initial Developers: Aizaz Ahmed
022: * Vivek Lakshmanan
023: * Andrew Overholt
024: * Matthew Wringe
025: *
026: */package olstore.action;
027:
028: import javax.servlet.http.HttpServletRequest;
029: import javax.servlet.http.HttpServletResponse;
030: import javax.servlet.http.HttpSession;
031:
032: import olstore.dto.ItemValue;
033: import olstore.dto.PictureValue;
034: import olstore.framework.EJBHomeFactory;
035: import olstore.util.WebUtils;
036:
037: import org.apache.struts.action.ActionError;
038: import org.apache.struts.action.ActionErrors;
039: import org.apache.struts.action.ActionForm;
040: import org.apache.struts.action.ActionForward;
041: import org.apache.struts.action.ActionMapping;
042: import org.apache.struts.action.ActionMessage;
043: import org.apache.struts.action.ActionMessages;
044:
045: import olstore.session.ShoppingCartLocal;
046: import olstore.session.helper.ItemHelperLocal;
047: import olstore.session.helper.ItemHelperLocalHome;
048:
049: public class ViewItemAction extends DemoBaseAction {
050:
051: /**
052: * Acts as a relay for all item related tasks with a default action
053: *
054: */
055: public ActionForward execute(ActionMapping mapping,
056: ActionForm form, HttpServletRequest request,
057: HttpServletResponse response) throws Exception {
058:
059: ActionErrors errors = new ActionErrors();
060: ActionMessages messages = new ActionMessages();
061:
062: String itemId = WebUtils.getValue("itemId", request);
063: String interest = WebUtils.getValue("interest", request);
064: String purchase = WebUtils.getValue("purchase", request);
065:
066: EJBHomeFactory factory = EJBHomeFactory.getInstance();
067: ItemHelperLocalHome itemHelperHome = (ItemHelperLocalHome) factory
068: .getLocalHome(EJBHomeFactory.ITEM_HELPER);
069: ItemHelperLocal itemHelper = itemHelperHome.create();
070:
071: // Load up the item
072: ItemValue item = null;
073: if (itemId != null) {
074: try {
075: item = itemHelper.getItemValueForId(itemId);
076: } catch (Exception e) {
077: //Logging code here
078: errors.add("error", new ActionError("errors.item.load",
079: e.getMessage()));
080: saveErrors(request, errors);
081: return (new ActionForward(mapping.getInput()));
082: }
083: }
084: if (item == null) {
085: errors.add("error", new ActionError("errors.item.view"));
086: saveErrors(request, errors);
087: return (new ActionForward(mapping.getInput()));
088: }
089:
090: request.setAttribute("item", item);
091: PictureValue pic = itemHelper.getPicture(itemId);
092: if (pic != null) {
093: request.setAttribute("pic", pic);
094: }
095:
096: // Mark the item as interesting
097: if (interest != null && interest.equals("true")) {
098: try {
099: itemHelper.markItemAsInteresting(itemId, request
100: .getRemoteUser());
101: ActionMessage msg = new ActionMessage(
102: "item.interest.success", item.getName());
103: messages.add("success", msg);
104: saveMessages(request, messages);
105: } catch (Exception e) {
106: errors.add("error", new ActionError(
107: "errors.item.interesting", item.getName(),
108: request.getRemoteUser(), e.getMessage()));
109: saveErrors(request, errors);
110: return (new ActionForward(mapping.getInput()));
111: }
112: }
113:
114: // Add the item to the shoppng cart
115: if (purchase != null && purchase.equals("true")) {
116: try {
117:
118: HttpSession session = request.getSession();
119: ShoppingCartLocal cart = (ShoppingCartLocal) session
120: .getAttribute("shoppingCart");
121: cart
122: .updateQuantity(new Integer(itemId),
123: new Integer(1));
124:
125: ActionMessage msg = new ActionMessage(
126: "item.purchase.success", item.getName());
127: messages.add("success", msg);
128: saveMessages(request, messages);
129: } catch (Exception e) {
130: errors.add("error", new ActionError(
131: "errors.item.purchase", item.getName(), request
132: .getRemoteUser(), e.getMessage()));
133: saveErrors(request, errors);
134: return (new ActionForward(mapping.getInput()));
135: }
136: }
137:
138: return mapping.findForward("updateItemView");
139:
140: }
141: }
|