KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > olstore > action > UserUpdateAction


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

56     public ActionForward execute ( ActionMapping mapping,
57             ActionForm form,
58             HttpServletRequest request,
59             HttpServletResponse response
60     ) throws Exception {
61         
62         try {
63             
64             DemoDynaBaseForm modUserForm=(DemoDynaBaseForm) form;
65             UserValue userValue=new UserValue ();
66             AddressValue addressValue=new AddressValue();
67             
68             //copy properties from the form to the DTOs
69
BeanUtils.copyProperties (addressValue, modUserForm);
70             BeanUtils.copyProperties (userValue, modUserForm);
71             
72             String username=request.getRemoteUser();
73             EJBHomeFactory factory=EJBHomeFactory.getInstance ();
74             UserHelperLocalHome userHelperHome=(UserHelperLocalHome) factory.getLocalHome (EJBHomeFactory.USER_HELPER);
75             UserHelperLocal userHelper=userHelperHome.create();
76             
77             //save the information to the entity beans
78
userHelper.SaveUser (userValue, addressValue, username);
79             
80             //to load the page the next time
81
modUserForm.set ("submitType","new");
82             
83             //reload the saved info back into the form.
84
UserModAction modAction=new UserModAction();
85             modAction.loadUser(mapping, form, request, response);
86             
87             //create a message that the user's info was updated
88
ActionMessages messages = new ActionMessages();
89             ActionMessage msg = new ActionMessage("user.save.success", username);
90             messages.add("success", msg);
91             saveMessages(request, messages);
92             
93             //return back to the same page
94
return (new ActionForward(mapping.getInput()));
95             
96         } catch ( Exception e ) {
97             //Logging code here
98
ActionErrors errors = new ActionErrors();
99             errors.add("error", new ActionError("errors.type.save", e.getMessage() ));
100             saveErrors(request, errors);
101             // Return to same page
102
return (new ActionForward(mapping.getInput()));
103         }
104         
105     }
106 }
107
Popular Tags