KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > olstore > action > UserSaveAction


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
30 import javax.ejb.TransactionRolledbackLocalException;
31 import javax.servlet.http.HttpServletRequest;
32 import javax.servlet.http.HttpServletResponse;
33
34 import olstore.dto.AddressValue;
35 import olstore.dto.CreateUserValue;
36 import olstore.form.DemoDynaBaseForm;
37 import olstore.framework.EJBHomeFactory;
38
39 import org.apache.commons.beanutils.BeanUtils;
40 import org.apache.struts.action.ActionError;
41 import org.apache.struts.action.ActionErrors;
42 import org.apache.struts.action.ActionForm;
43 import org.apache.struts.action.ActionForward;
44 import org.apache.struts.action.ActionMapping;
45
46 import olstore.session.helper.UserHelperLocal;
47 import olstore.session.helper.UserHelperLocalHome;
48
49
50 public class UserSaveAction extends DemoBaseAction {
51     
52     /**
53      * Action to create a new user profile.
54      */

55     public ActionForward execute ( ActionMapping mapping,
56             ActionForm form,
57             HttpServletRequest request,
58             HttpServletResponse response
59     ) throws Exception {
60         
61         try {
62             
63             //get information from the form
64
DemoDynaBaseForm createUserForm=(DemoDynaBaseForm) form;
65             CreateUserValue createUserValue=new CreateUserValue ();
66             AddressValue addressValue=new AddressValue();
67             
68             //copy values from the form to the dto
69
BeanUtils.copyProperties (addressValue, createUserForm);
70             BeanUtils.copyProperties (createUserValue, createUserForm);
71             
72             EJBHomeFactory factory=EJBHomeFactory.getInstance ();
73             UserHelperLocalHome userHelperHome=(UserHelperLocalHome) factory.getLocalHome (EJBHomeFactory.USER_HELPER);
74             UserHelperLocal userHelper=userHelperHome.create();
75             
76             //create a new user entity bean
77
userHelper.CreateUser (createUserValue, addressValue);
78             
79             //forward to a confirmation page
80
return mapping.findForward ("CreateConfirm");
81             
82         }
83         
84         // if the username has already been choosen, this exception will be thrown
85
catch ( TransactionRolledbackLocalException n){
86             ActionErrors errors = new ActionErrors();
87             errors.add ("error", new ActionError("errors.duplicate.username", n.getMessage() ));
88             saveErrors(request,errors);
89             return (new ActionForward(mapping.getInput()));
90         }
91         
92         catch ( Exception e ) {
93             //Logging code here
94
ActionErrors errors = new ActionErrors();
95             errors.add("error", new ActionError("errors.type.save", e.getMessage() ));
96             saveErrors(request, errors);
97             // Return to same page
98
return (new ActionForward(mapping.getInput()));
99         }
100         
101     }
102     
103 }
104
Popular Tags