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