KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > olstore > action > ItemSaveAction


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.*;
30 import olstore.session.helper.ItemHelperLocal;
31 import olstore.session.helper.ItemHelperLocalHome;
32 import olstore.dto.ItemValue;
33 import olstore.form.CreateItemForm;
34 import olstore.framework.EJBHomeFactory;
35
36 import org.apache.struts.action.*;
37 import org.apache.commons.beanutils.BeanUtils;
38
39
40 public class ItemSaveAction extends DemoBaseAction {
41
42     /**
43      * Acts as a relay for all item related tasks with a default action
44      *
45      */

46     public ActionForward execute ( ActionMapping mapping,
47                                    ActionForm form,
48                                    HttpServletRequest request,
49                                    HttpServletResponse response
50                                  ) throws Exception {
51
52         try {
53
54             CreateItemForm createForm = (CreateItemForm)form;
55             ItemValue itemVal = new ItemValue();
56             BeanUtils.copyProperties ( itemVal , createForm );
57             
58             EJBHomeFactory factory = EJBHomeFactory.getInstance();
59             ItemHelperLocalHome itemHelperHome = (ItemHelperLocalHome) factory.getLocalHome ( EJBHomeFactory.ITEM_HELPER );
60             ItemHelperLocal itemHelper = itemHelperHome.create();
61
62             itemHelper.saveItem ( itemVal );
63
64             ActionMessages messages = new ActionMessages();
65             ActionMessage msg = new ActionMessage("item.save.success",
66                                                   itemVal.getName() );
67             messages.add("success", msg);
68             saveMessages(request, messages);
69
70             return mapping.findForward ( "updateItem" );
71         } catch ( Exception e ) {
72             //Logging code here
73
e.printStackTrace();
74             ActionErrors errors = new ActionErrors();
75             errors.add("error", new ActionError("errors.item.save", e.getMessage() ));
76             saveErrors(request, errors);
77             // Return to same page
78
return (new ActionForward(mapping.getInput()));
79         }
80
81     }
82         
83 }
84
Popular Tags