KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > ui > core > struts > actions > YourProfileAction


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. The ASF licenses this file to You
4  * under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License. For additional information regarding
15  * copyright in this work, please see the NOTICE file in the top level
16  * directory of this distribution.
17  */

18
19 package org.apache.roller.ui.core.struts.actions;
20
21 import java.io.IOException JavaDoc;
22
23 import javax.servlet.ServletException JavaDoc;
24 import javax.servlet.http.HttpServletRequest JavaDoc;
25 import javax.servlet.http.HttpServletResponse JavaDoc;
26
27 import org.apache.commons.lang.StringUtils;
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.apache.roller.RollerException;
31 import org.apache.roller.model.RollerFactory;
32 import org.apache.roller.model.UserManager;
33 import org.apache.roller.pojos.UserData;
34 import org.apache.roller.ui.authoring.struts.formbeans.UserFormEx;
35 import org.apache.roller.ui.core.BasePageModel;
36 import org.apache.roller.ui.core.RollerSession;
37 import org.apache.struts.action.ActionErrors;
38 import org.apache.struts.action.ActionForm;
39 import org.apache.struts.action.ActionForward;
40 import org.apache.struts.action.ActionMapping;
41 import org.apache.struts.action.ActionMessage;
42 import org.apache.struts.action.ActionMessages;
43
44 /**
45  * Allows user to edit his/her profile.
46  *
47  * @struts.action name="userFormEx" path="/roller-ui/yourProfile" parameter="method"
48  * @struts.action-forward name="yourProfile.page" path=".YourProfile"
49  */

50 public class YourProfileAction extends UserBaseAction {
51     
52     private static Log mLogger = LogFactory.getLog(YourProfileAction.class);
53     
54     
55     /** If method param is not specified, use HTTP verb to pick method to call */
56     public ActionForward unspecified(
57             ActionMapping mapping,
58             ActionForm actionForm,
59             HttpServletRequest JavaDoc request,
60             HttpServletResponse JavaDoc response)
61             throws Exception JavaDoc {
62         if (request.getMethod().equals("GET")) {
63             return edit(mapping, actionForm, request, response);
64         }
65         return save(mapping, actionForm, request, response);
66     }
67     
68     
69     public ActionForward cancel(
70             ActionMapping mapping,
71             ActionForm actionForm,
72             HttpServletRequest JavaDoc request,
73             HttpServletResponse JavaDoc response)
74             throws Exception JavaDoc {
75         return mapping.findForward("yourWebsites");
76     }
77     
78     
79     /** Load form with authenticated user and forward to your-profile page */
80     public ActionForward edit(
81             ActionMapping mapping,
82             ActionForm actionForm,
83             HttpServletRequest JavaDoc request,
84             HttpServletResponse JavaDoc response)
85             throws IOException JavaDoc, ServletException JavaDoc {
86         ActionForward forward = mapping.findForward("yourProfile.page");
87         try {
88             RollerSession rollerSession = RollerSession.getRollerSession(request);
89             UserData ud = rollerSession.getAuthenticatedUser();
90             UserFormEx form = (UserFormEx)actionForm;
91             form.copyFrom(ud, request.getLocale());
92             form.setPasswordText(null);
93             form.setPasswordConfirm(null);
94             form.setLocale(ud.getLocale());
95             form.setTimeZone(ud.getTimeZone());
96             request.setAttribute("model", new BasePageModel(
97                     "yourProfile.title", request, response, mapping));
98         } catch (Exception JavaDoc e) {
99             mLogger.error("ERROR in action",e);
100             throw new ServletException JavaDoc(e);
101         }
102         // if user logged in with a cookie, display a warning that they
103
// can't change passwords
104
if (mLogger.isDebugEnabled()) {
105             log.debug("checking for cookieLogin...");
106         }
107         if (request.getSession().getAttribute("cookieLogin") != null) {
108             ActionMessages messages = new ActionMessages();
109             
110             // add warning messages
111
messages.add(ActionMessages.GLOBAL_MESSAGE,
112                     new ActionMessage("yourProfile.cookieLogin"));
113             saveMessages(request, messages);
114         }
115         return forward;
116     }
117     
118     
119     /** Update user based on posted form data */
120     public ActionForward save(
121             ActionMapping mapping,
122             ActionForm actionForm,
123             HttpServletRequest JavaDoc request,
124             HttpServletResponse JavaDoc response)
125             throws IOException JavaDoc, ServletException JavaDoc {
126         UserFormEx form = (UserFormEx)actionForm;
127         ActionForward forward = mapping.findForward("yourProfile.page");
128         ActionMessages msgs = new ActionMessages();
129         try {
130             ActionMessages errors = validate(form, new ActionErrors());
131             if (errors.size() == 0) {
132                 // We ONLY modify the user currently logged in
133
RollerSession rollerSession = RollerSession.getRollerSession(request);
134                 UserData data = rollerSession.getAuthenticatedUser();
135                 
136                 // We want to be VERY selective about what data gets updated
137
data.setFullName(form.getFullName());
138                 data.setEmailAddress(form.getEmailAddress());
139                 data.setLocale(form.getLocale());
140                 data.setTimeZone(form.getTimeZone());
141                 
142                 // If user set both password and passwordConfirm then reset password
143
if ( !StringUtils.isEmpty(form.getPasswordText())
144                 && !StringUtils.isEmpty(form.getPasswordConfirm())) {
145                     try {
146                         data.resetPassword(RollerFactory.getRoller(),
147                                 form.getPasswordText(),
148                                 form.getPasswordConfirm());
149                     } catch (RollerException e) {
150                         msgs.add(ActionMessages.GLOBAL_MESSAGE,
151                                 new ActionMessage("yourProfile.passwordResetError"));
152                     }
153                 }
154                 
155                 // save the updated profile
156
UserManager mgr = RollerFactory.getRoller().getUserManager();
157                 mgr.saveUser(data);
158                 RollerFactory.getRoller().flush();
159                 
160                 request.setAttribute("model", new BasePageModel(
161                         "yourProfile.title", request, response, mapping));
162                 
163                 saveMessages(request, msgs);
164             } else {
165                 saveErrors(request, errors);
166             }
167             return mapping.findForward("yourWebsites");
168         } catch (Exception JavaDoc e) {
169             mLogger.error("ERROR in action",e);
170             throw new ServletException JavaDoc(e);
171         }
172     }
173     
174 }
175
Popular Tags