KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > olstore > action > ViewItemAction


1 /**
2  * Copyright (c) 2004 Red Hat, Inc. All rights reserved.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17  * USA
18  *
19  * Component of: Red Hat Application Server
20  *
21  * Initial Developers: Aizaz Ahmed
22  * Vivek Lakshmanan
23  * Andrew Overholt
24  * Matthew Wringe
25  *
26  */

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     /**
54      * Acts as a relay for all item related tasks with a default action
55      *
56      */

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         // Load up the item
75
ItemValue item = null;
76         if ( itemId != null ) {
77             try {
78                 item = itemHelper.getItemValueForId ( itemId );
79             } catch ( Exception e ) {
80                 //Logging code here
81
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         // Mark the item as interesting
100
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         // Add the item to the shoppng cart
119
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