AddToListAction.java
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.tasklist.action;
09 
10 import demo.tasklist.common.Constants;
11 import demo.tasklist.form.AddToListForm;
12 import demo.tasklist.service.DataKeeper;
13 import demo.tasklist.service.ErrorKeeper;
14 import javax.servlet.http.HttpServletRequest;
15 import javax.servlet.http.HttpServletResponse;
16 import javax.servlet.http.HttpSession;
17 import org.apache.struts.action.Action;
18 import org.apache.struts.action.ActionForm;
19 import org.apache.struts.action.ActionForward;
20 import org.apache.struts.action.ActionMapping;
21 
22 /**
23  *  AddToListAction processes the request to add an item to the task list.
24  *  Task list is fetched from the HttpSession object, the item indicated in
25  *  the AddToListForm is added to the list, and the modified list is loaded
26  *  back into the HttpSession object.
27  *
28  *@author    Terracotta, Inc.
29  */
30 public class AddToListAction extends Action {
31    public ActionForward execute(ActionMapping mapping,
32          ActionForm form,
33          HttpServletRequest request,
34          HttpServletResponse response)
35           throws Exception {
36       HttpSession session = (HttpSessionrequest.getSession();
37 
38       AddToListForm addToListForm = (AddToListFormform;
39       String newListItem = addToListForm.getNewListItem();
40       String errorMsg = addToListForm.getErrorMsg();
41 
42       if (errorMsg != null) {
43          session.setAttribute(Constants.ERROR_KEY, new ErrorKeeper(errorMsg));
44       }
45       else {
46          session.removeAttribute(Constants.ERROR_KEY);
47       }
48 
49       DataKeeper dkeeper = (DataKeepersession.getAttribute(Constants.DATA_KEY);
50       if (dkeeper == null) {
51          dkeeper = new DataKeeper();
52       }
53       dkeeper.addListItem(newListItem);
54 
55       session.setAttribute(Constants.DATA_KEY, dkeeper);
56 
57       return mapping.findForward(Constants.SUCCESS_KEY);
58    }
59 }