KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nextime > ion > backoffice > action > security > EditMyProfileAction


1 package org.nextime.ion.backoffice.action.security;
2
3 import java.io.IOException JavaDoc;
4 import javax.servlet.ServletException JavaDoc;
5 import javax.servlet.http.HttpServletRequest JavaDoc;
6 import javax.servlet.http.HttpServletResponse JavaDoc;
7
8 import org.apache.struts.action.ActionErrors;
9 import org.apache.struts.action.ActionForm;
10 import org.apache.struts.action.ActionForward;
11 import org.apache.struts.action.ActionMapping;
12 import org.nextime.ion.backoffice.action.BaseAction;
13 import org.nextime.ion.backoffice.form.EditUserForm;
14
15 import org.nextime.ion.framework.business.User;
16 import org.nextime.ion.framework.mapping.Mapping;
17
18 public class EditMyProfileAction extends BaseAction {
19
20     public ActionForward perform(
21         ActionMapping mapping,
22         ActionForm form,
23         HttpServletRequest JavaDoc request,
24         HttpServletResponse JavaDoc response)
25         throws IOException JavaDoc, ServletException JavaDoc {
26
27         // check if user is correctly logged
28
checkUser(request);
29
30         // get the form
31
EditUserForm sform = (EditUserForm) form;
32         ActionErrors errors = sform.myValidate(request);
33
34         // user need cancel
35
if (request.getParameter("cancel") != null) {
36             // Forward to the next page
37
return (mapping.findForward("cancel"));
38         }
39
40         // retrieve id
41
String JavaDoc id = request.getSession().getAttribute("userLogin")+"";
42
43         // fill data | first time
44
if (sform.getName() == null) {
45             try {
46                 Mapping.begin();
47                 User user = User.getInstance(id);
48                 Mapping.rollback();
49                 
50                 sform.setEmail((String JavaDoc)user.getMetaData("email"));
51                 sform.setName((String JavaDoc)user.getMetaData("name"));
52                 sform.setGroups(user.getGroupsIds());
53                 sform.setPassword(user.getPassword());
54                 
55             } catch (Exception JavaDoc e) {
56                 Mapping.rollback();
57                 throw new ServletException JavaDoc(e);
58             }
59
60             // Forward to the view page
61
return (mapping.findForward("view"));
62         }
63
64         // fill data | errors
65
if (errors.size() > 0) {
66             try {
67                 request.setAttribute(ERROR_KEY, errors);
68                                 
69             } catch (Exception JavaDoc e) {
70                 Mapping.rollback();
71                 throw new ServletException JavaDoc(e);
72             }
73
74             // Forward to the view page
75
return (mapping.findForward("view"));
76         }
77
78         // all it's ok : update user
79
try {
80             Mapping.begin();
81             User user = User.getInstance(id);
82             user.setMetaData("name", sform.getName());
83             user.setMetaData("email", sform.getEmail());
84             user.setPassword(sform.getPassword());
85             Mapping.commit();
86             
87         } catch (Exception JavaDoc e) {
88             Mapping.rollback();
89             throw new ServletException JavaDoc(e);
90         }
91
92         // Forward to the next page
93
return (mapping.findForward("ok"));
94     }
95
96 }
97
Popular Tags