KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > j2biz > blogunity > web > actions > my > EditUserExecAction


1 /*
2  * $Id: EditUserExecAction.java,v 1.5 2005/01/17 21:35:44 michelson Exp $
3  *
4  * Copyright (c) 2004 j2biz Group, http://www.j2biz.com
5  * Koeln / Duesseldorf , Germany
6  *
7  * @author Max Kalina
8  *
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23  *
24  */

25
26 package com.j2biz.blogunity.web.actions.my;
27
28 import java.util.Calendar JavaDoc;
29 import java.util.Locale JavaDoc;
30
31 import javax.servlet.http.HttpServletRequest JavaDoc;
32 import javax.servlet.http.HttpServletResponse JavaDoc;
33
34 import org.apache.commons.lang.StringUtils;
35
36 import com.j2biz.blogunity.dao.UserDAO;
37 import com.j2biz.blogunity.exception.BlogunityException;
38 import com.j2biz.blogunity.i18n.I18N;
39 import com.j2biz.blogunity.i18n.I18NStatusFactory;
40 import com.j2biz.blogunity.pojo.User;
41 import com.j2biz.blogunity.util.BlogUtils;
42 import com.j2biz.blogunity.util.PasswordEncryptionService;
43 import com.j2biz.blogunity.util.RenderUtils;
44 import com.j2biz.blogunity.web.ActionResultFactory;
45 import com.j2biz.blogunity.web.FormError;
46 import com.j2biz.blogunity.web.FormErrorList;
47 import com.j2biz.blogunity.web.IActionResult;
48
49 /**
50  * @author michelson
51  * @version $$
52  * @since 0.1
53  *
54  *
55  */

56 public class EditUserExecAction extends MyAbstractAction {
57     protected static final IActionResult PROFILE_FORM_FORWARD = ActionResultFactory
58             .buildForward("/jsp/my/editUserForm.jsp");
59
60     /*
61      * (non-Javadoc)
62      *
63      * @see com.j2biz.blogunity.web.actions.AbstractAction#execute(javax.servlet.http.HttpServletRequest,
64      * javax.servlet.http.HttpServletResponse)
65      */

66     public IActionResult execute(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
67             throws BlogunityException {
68
69         if (!user.isAdministrator())
70                 throw new BlogunityException(I18NStatusFactory
71                         .create(I18N.ERRORS.USER_NOT_AUTHORIZED_FOR_EXECUTION));
72
73         String JavaDoc userid = request.getParameter("userid");
74
75         if (StringUtils.isEmpty(userid)) { throw new BlogunityException(I18NStatusFactory.create(
76                 I18N.ERRORS.ID_NOT_SETTED, "User")); }
77
78         String JavaDoc nickname = request.getParameter("nickname");
79         String JavaDoc password = request.getParameter("password");
80         String JavaDoc firstname = request.getParameter("firstname");
81         String JavaDoc lastname = request.getParameter("lastname");
82         String JavaDoc email = request.getParameter("email");
83         String JavaDoc language = request.getParameter("language");
84         String JavaDoc icq = request.getParameter("icq");
85         String JavaDoc msn = request.getParameter("msn");
86         String JavaDoc yahoo = request.getParameter("yahoo");
87         String JavaDoc jabber = request.getParameter("jabber");
88         String JavaDoc homepage = request.getParameter("homepage");
89         String JavaDoc sex = request.getParameter("sex");
90         String JavaDoc showEmailStr = request.getParameter("showEmail");
91         boolean showEmail = false;
92         if (showEmailStr != null) showEmail = showEmailStr.equalsIgnoreCase("on") ? true : false;
93
94         String JavaDoc bio = request.getParameter("bio");
95         String JavaDoc day = request.getParameter("day");
96         String JavaDoc month = request.getParameter("month");
97         String JavaDoc year = request.getParameter("year");
98
99         int i_day = 1;
100         try {
101             i_day = Integer.parseInt(day);
102         } catch (NumberFormatException JavaDoc e1) {
103
104         }
105
106         int i_month = 0;
107         try {
108             i_month = Integer.parseInt(month);
109         } catch (NumberFormatException JavaDoc e1) {
110
111         }
112
113         int i_year = 2000;
114         try {
115             i_year = Integer.parseInt(year);
116         } catch (NumberFormatException JavaDoc e1) {
117
118         }
119         Calendar JavaDoc c = Calendar.getInstance();
120         c.set(i_year, i_month - 1, i_day);
121
122         UserDAO userDAO = new UserDAO();
123         User u = userDAO.getUserByID(Long.parseLong(userid));
124
125         if (u == null) { throw new BlogunityException(I18NStatusFactory.create(
126                 I18N.ERRORS.NOT_FOUND_BY_ID, new String JavaDoc[]{"user", "id"})); }
127
128         u.setEmail(email);
129         u.setFirstname(firstname);
130         u.setLastname(lastname);
131         u.setLanguage(StringUtils.isEmpty(language) ? null : new Locale JavaDoc(language));
132         u.setIcq(StringUtils.isEmpty(icq) ? null : icq);
133         u.setMsn(StringUtils.isEmpty(msn) ? null : msn);
134         u.setYahoo(StringUtils.isEmpty(yahoo) ? null : yahoo);
135         u.setJabber(StringUtils.isEmpty(jabber) ? null : jabber);
136         u.setHomepage(StringUtils.isEmpty(homepage) ? null : homepage);
137         u.setBioRaw(StringUtils.isEmpty(bio) ? null : bio);
138         u.setBio(StringUtils.isEmpty(bio) ? null : RenderUtils.renderText(bio));
139         u.setBirthday(c.getTime());
140
141         try {
142             u.setSex(Integer.parseInt(sex));
143         } catch (NumberFormatException JavaDoc e) {
144             u.setSex(User.MALE);
145         }
146         u.setShowEmail(showEmail);
147
148         request.setAttribute("requestedUser", u);
149
150         FormErrorList errors = new FormErrorList();
151
152         if (StringUtils.isEmpty(firstname))
153                 errors.add(new FormError("firstname", "Firstname is empty!"));
154         if (StringUtils.isEmpty(lastname))
155                 errors.add(new FormError("lastname", "Lastname is empty!"));
156
157         if (StringUtils.isEmpty(language)) {
158             errors.add(new FormError("language", "Language cannot be empty!"));
159         }
160
161         if (StringUtils.isEmpty(email)) errors.add(new FormError("email", "Email is empty!"));
162         else if (!BlogUtils.getInstance().isValidEmailAddress(email))
163                 errors.add(new FormError("email", "Email is not valid!"));
164
165         if (errors.size() > 0) {
166             request.setAttribute("errors", errors);
167             return PROFILE_FORM_FORWARD;
168         }
169
170         if (StringUtils.isNotEmpty(password)) {
171             u.setPassword(password);
172             u = PasswordEncryptionService.encryptPasswordIfNecessary(u);
173         }
174
175         userDAO.updateUser(u);
176         navigationStack.pop();
177         return navigationStack.peek();
178
179     }
180 }
Popular Tags