KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > j2biz > blogunity > web > actions > exec > RegisterUserExecAction


1 /*
2  * $Id: RegisterUserExecAction.java,v 1.6 2005/01/17 21:36:13 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.exec;
27
28 import java.util.ArrayList JavaDoc;
29 import java.util.Calendar JavaDoc;
30 import java.util.Locale JavaDoc;
31
32 import javax.servlet.http.HttpServletRequest JavaDoc;
33 import javax.servlet.http.HttpServletResponse JavaDoc;
34 import javax.servlet.http.HttpSession JavaDoc;
35
36 import org.apache.commons.lang.StringUtils;
37
38 import com.j2biz.blogunity.dao.UserDAO;
39 import com.j2biz.blogunity.exception.BlogunityException;
40 import com.j2biz.blogunity.i18n.I18N;
41 import com.j2biz.blogunity.i18n.I18NStatusFactory;
42 import com.j2biz.blogunity.pojo.User;
43 import com.j2biz.blogunity.util.BlogUtils;
44 import com.j2biz.blogunity.util.PasswordEncryptionService;
45 import com.j2biz.blogunity.util.RenderUtils;
46 import com.j2biz.blogunity.web.ActionResultFactory;
47 import com.j2biz.blogunity.web.FormError;
48 import com.j2biz.blogunity.web.FormErrorList;
49 import com.j2biz.blogunity.web.IActionResult;
50 import com.j2biz.blogunity.web.actions.AbstractAction;
51
52 /**
53  * @author michelson
54  * @version $$
55  * @since 0.1
56  *
57  *
58  */

59 public class RegisterUserExecAction extends AbstractAction {
60
61     private static final IActionResult REGISTER_FORM_FORWARD = ActionResultFactory
62             .buildForward("/jsp/register.jsp");
63
64     private static final IActionResult REGISTER_SUCCESS_FORWARD = ActionResultFactory
65             .buildForward("/jsp/registerSuccess.jsp");
66
67     /*
68      * (non-Javadoc)
69      *
70      * @see com.j2biz.blogunity.web.actions.AbstractAction#execute(javax.servlet.http.HttpServletRequest,
71      * javax.servlet.http.HttpServletResponse)
72      */

73     public IActionResult execute(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
74             throws BlogunityException {
75
76         if (!com.j2biz.blogunity.BlogunityManager.getSystemConfiguration().isAllowNewUsers())
77                 throw new BlogunityException(I18NStatusFactory
78                         .create(I18N.ERRORS.USER_REGISTERING_NOT_ALLOWED));
79
80         User ux = null;
81         HttpSession JavaDoc session = request.getSession(false);
82         if (session != null) ux = (User) session.getAttribute("user");
83
84         if (ux != null)
85                 throw new BlogunityException(I18NStatusFactory
86                         .create(I18N.ERRORS.USER_REGISTERING_ERROR_CAUSE_ALREADY_EXISTS));
87
88         String JavaDoc nickname = request.getParameter("nickname");
89         String JavaDoc password = request.getParameter("password");
90         String JavaDoc firstname = request.getParameter("firstname");
91         String JavaDoc lastname = request.getParameter("lastname");
92         String JavaDoc email = request.getParameter("email");
93         String JavaDoc language = request.getParameter("language");
94         String JavaDoc icq = request.getParameter("icq");
95         String JavaDoc msn = request.getParameter("msn");
96         String JavaDoc yahoo = request.getParameter("yahoo");
97         String JavaDoc jabber = request.getParameter("jabber");
98         String JavaDoc homepage = request.getParameter("homepage");
99         String JavaDoc sex = request.getParameter("sex");
100         String JavaDoc showEmailStr = request.getParameter("showEmail");
101         boolean showEmail = false;
102         if (showEmailStr != null) showEmail = showEmailStr.equalsIgnoreCase("on") ? true : false;
103
104         String JavaDoc bio = request.getParameter("bio");
105         String JavaDoc day = request.getParameter("day");
106         String JavaDoc month = request.getParameter("month");
107         String JavaDoc year = request.getParameter("year");
108
109         int i_day = 1;
110         try {
111             i_day = Integer.parseInt(day);
112         } catch (NumberFormatException JavaDoc e1) {
113
114         }
115
116         int i_month = 0;
117         try {
118             i_month = Integer.parseInt(month);
119         } catch (NumberFormatException JavaDoc e1) {
120
121         }
122
123         int i_year = 2000;
124         try {
125             i_year = Integer.parseInt(year);
126         } catch (NumberFormatException JavaDoc e1) {
127
128         }
129         Calendar JavaDoc c = Calendar.getInstance();
130         c.set(i_year, i_month - 1, i_day);
131
132         User user = new User(nickname, password, email, firstname, lastname);
133         user.setLanguage(StringUtils.isEmpty(language) ? null : new Locale JavaDoc(language));
134         user.setIcq(StringUtils.isEmpty(icq) ? null : icq);
135         user.setMsn(StringUtils.isEmpty(msn) ? null : msn);
136         user.setYahoo(StringUtils.isEmpty(yahoo) ? null : yahoo);
137         user.setJabber(StringUtils.isEmpty(jabber) ? null : jabber);
138         user.setHomepage(StringUtils.isEmpty(homepage) ? null : homepage);
139         user.setBioRaw(StringUtils.isEmpty(bio) ? null : bio);
140         user.setBio(StringUtils.isEmpty(bio) ? null : RenderUtils.renderText(bio));
141         user.setBirthday(c.getTime());
142
143         try {
144             user.setSex(Integer.parseInt(sex));
145         } catch (NumberFormatException JavaDoc e) {
146             user.setSex(User.MALE);
147         }
148         user.setShowEmail(showEmail);
149
150         String JavaDoc[] localeArray = Locale.getISOLanguages();
151         ArrayList JavaDoc locales = new ArrayList JavaDoc();
152         for (int i = 0; i < localeArray.length; i++) {
153             locales.add(new Locale JavaDoc(localeArray[i]));
154         }
155         request.setAttribute("locales", locales);
156         request.setAttribute("newUser", user);
157
158         FormErrorList errors = new FormErrorList();
159
160         if (StringUtils.isEmpty(nickname)) errors.add(new FormError("nickname",
161                 "Nickname is empty!"));
162         else if (!BlogUtils.getInstance().isValidNickname(nickname))
163                 errors.add(new FormError("nickname", "Nickname is not valid!"));
164         if (StringUtils.isEmpty(password))
165                 errors.add(new FormError("password", "Password is empty!"));
166         if (StringUtils.isEmpty(firstname))
167                 errors.add(new FormError("firstname", "Firstname is empty!"));
168         if (StringUtils.isEmpty(lastname))
169                 errors.add(new FormError("lastname", "Lastname is empty!"));
170         if (StringUtils.isEmpty(language)) {
171             errors.add(new FormError("language", "Language cannot be empty!"));
172         }
173         if (StringUtils.isEmpty(email)) errors.add(new FormError("email", "Email is empty!"));
174         else if (!BlogUtils.getInstance().isValidEmailAddress(email))
175                 errors.add(new FormError("email", "Email is not valid!"));
176
177         if (errors.size() > 0) {
178             request.setAttribute("errors", errors);
179             return REGISTER_FORM_FORWARD;
180         }
181
182         // check, if user with given nickname already exists
183
UserDAO dao = new UserDAO();
184         User u = dao.getUserByName(nickname);
185         if (u != null) {
186             errors.add(new FormError("nickname", "User '" + nickname
187                     + "' already exists! Please specify another one nickname!"));
188             request.setAttribute("errors", errors);
189             return REGISTER_FORM_FORWARD;
190         }
191
192         user = PasswordEncryptionService.encryptPasswordIfNecessary(user);
193         dao.createUser(user);
194
195         return REGISTER_SUCCESS_FORWARD;
196     }
197
198 }
Popular Tags