01 /**
02 *
03 * All content copyright (c) 2003-2008 Terracotta, Inc.,
04 * except as may otherwise be noted in a separate copyright notice.
05 * All rights reserved.
06 *
07 */
08 package demo.townsend.action;
09
10 import demo.townsend.common.Constants;
11 import demo.townsend.form.AddToListForm;
12 import demo.townsend.service.DataKeeper;
13 import demo.townsend.service.Product;
14 import demo.townsend.service.ProductCatalog;
15 import java.util.ArrayList;
16 import java.util.Iterator;
17 import javax.servlet.http.HttpServletRequest;
18 import javax.servlet.http.HttpServletResponse;
19 import javax.servlet.http.HttpSession;
20 import org.apache.struts.action.Action;
21 import org.apache.struts.action.ActionForm;
22 import org.apache.struts.action.ActionForward;
23 import org.apache.struts.action.ActionMapping;
24
25 /**
26 * AddToListAction processes the request to add an item to the user's list.
27 * User's list is fetched from the HttpSession object, the item indicated in
28 * the AddToListForm is added to the list, and the modified list is loaded
29 * back into the HttpSession object.
30 *
31 *@author Terracotta, Inc.
32 */
33 public class AddToListAction extends Action {
34 public ActionForward execute(ActionMapping mapping,
35 ActionForm form,
36 HttpServletRequest request,
37 HttpServletResponse response)
38 throws Exception {
39
40 String newProdId = ((AddToListForm) form).getId();
41 Product newProd = null;
42 ArrayList catalog = new ProductCatalog().getCatalog();
43 for (Iterator iter = catalog.iterator(); iter.hasNext(); ) {
44 Product p = (Product) iter.next();
45 if (p.getId().equals(newProdId)) {
46 newProd = p;
47 }
48 }
49
50 HttpSession session = (HttpSession) request.getSession();
51
52 DataKeeper dkeeper = (DataKeeper) session.getAttribute(Constants.DATA_KEY);
53 if (dkeeper == null) {
54 dkeeper = new DataKeeper();
55 }
56
57 dkeeper.addListItem(newProd);
58
59 session.setAttribute(Constants.DATA_KEY, dkeeper);
60
61 return mapping.findForward(Constants.SUCCESS_KEY);
62 }
63 }
|