Source code: com/RuntimeCollective/webapps/form/RegisterForm.java
1 /* $Header: /home/CVS/rjp/src/com/RuntimeCollective/webapps/form/RegisterForm.java,v 1.8 2003/10/13 15:42:43 fabrice Exp $
2 * $Revision: 1.8 $
3 * $Date: 2003/10/13 15:42:43 $
4 *
5 * ====================================================================
6 *
7 * Josephine : http://www.runtime-collective.com/josephine/index.html
8 *
9 * Copyright (C) 2003 Runtime Collective
10 *
11 * This product includes software developed by the
12 * Apache Software Foundation (http://www.apache.org/).
13 *
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2.1 of the License, or (at your option) any later version.
18 *
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 *
28 */
29
30 package com.RuntimeCollective.webapps.form;
31
32 import com.RuntimeCollective.webapps.form.UserForm;
33 import com.RuntimeCollective.webapps.RuntimeParameters;
34
35 import javax.servlet.http.HttpServletRequest;
36 import org.apache.struts.action.ActionError;
37 import org.apache.struts.action.ActionErrors;
38 import org.apache.struts.action.ActionForm;
39 import org.apache.struts.action.ActionMapping;
40
41 /**
42 * Takes user details for registering/logging on a SimpleUser.
43 *
44 * @version $Id: RegisterForm.java,v 1.8 2003/10/13 15:42:43 fabrice Exp $
45 */
46 public class RegisterForm extends ActionForm {
47
48 // == Constructor ==================================================
49 public RegisterForm() {
50 RuntimeParameters.logDebug(this, "** Constructor of RegisterForm!");
51 setCookie = true;
52 sendNewsletter = true;
53 }
54
55 // == Properties ===================================================
56
57 /** What action the form is being used for. Should be "login", "register", or "emailPassword". */
58 protected String formAction = "login";
59 /** Get what action the form is being used for. Should be "login" or "register", or "emailPassword". */
60 public String getFormAction() { return this.formAction; }
61 /** Set what action the form is being used for. Should be "login" or "register", or "emailPassword". */
62 public void setFormAction(String formAction) { this.formAction = formAction; }
63
64 /** The user's login name */
65 protected String loginName = "";
66 /** Get the user's login name */
67 public String getLoginName() { return this.loginName; }
68 /** Set the user's login name */
69 public void setLoginName(String loginName) { this.loginName = loginName; }
70
71 /** The user's password */
72 protected String password = "";
73 /** Get the user's password */
74 public String getPassword() { return this.password; }
75 /** Set the user's password */
76 public void setPassword(String password) { this.password = password; }
77
78 /** The retyped password */
79 protected String retypePassword = "";
80 /** Get the retyped password */
81 public String getRetypePassword() { return this.retypePassword; }
82 /** Set the retyped password */
83 public void setRetypePassword(String retypePassword) { this.retypePassword = retypePassword; }
84
85 /** Whether the user wants persistent login, via cookies */
86 protected boolean setCookie = false;
87 /** Get whether the user wants persistent login, via cookies */
88 public boolean getSetCookie() { return this.setCookie; }
89 /** Set whether the user wants persistent login, via cookies */
90 public void setSetCookie(boolean setCookie) { this.setCookie = setCookie; }
91
92 /** User's email address */
93 protected String email = "";
94 /** Get user's email address */
95 public String getEmail() { return this.email; }
96 /** Set user's email address */
97 public void setEmail(String email) { this.email = email; }
98
99 /** The user's first name */
100 protected String firstName = "";
101 /** Get the user's first name */
102 public String getFirstName() { return this.firstName; }
103 /** Set the user's first name */
104 public void setFirstName(String firstName) { this.firstName = firstName; }
105
106 /** The user's last name */
107 protected String lastName = "";
108 /** Get the user's last name */
109 public String getLastName() { return this.lastName; }
110 /** Set the user's last name */
111 public void setLastName(String lastName) { this.lastName = lastName; }
112
113 /** The user's company name */
114 protected String companyName = "";
115 /** Get the user's company name */
116 public String getCompanyName() { return this.companyName; }
117 /** Set the user's company name */
118 public void setCompanyName(String companyName) { this.companyName = companyName; }
119
120 /** The user's phone number */
121 protected String phone = "";
122 /** Get the user's phone number */
123 public String getPhone() { return this.phone; }
124 /** Set the user's phone number */
125 public void setPhone(String phone) { this.phone = phone; }
126
127 /** The user's postcode */
128 protected String postcode = "";
129 /** Get the user's postcode */
130 public String getPostcode() { return this.postcode; }
131 /** Set the user's postcode */
132 public void setPostcode(String postcode) { this.postcode = postcode; }
133
134 /** The user's street number */
135 protected String streetNumber = "";
136 /** Get the user's streetNumber */
137 public String getStreetNumber() { return this.streetNumber; }
138 /** Set the user's streetNumber */
139 public void setStreetNumber(String streetNumber) { this.streetNumber = streetNumber; }
140
141 /** The user's country */
142 protected String country = "United Kingdom";
143 /** Get the user's country */
144 public String getCountry() { return this.country; }
145 /** Set the user's country */
146 public void setCountry(String country) { this.country = country; }
147
148 /** Whether to subscribe the user to the newsletter */
149 protected boolean sendNewsletter = false;
150 /** Get whether to subscribe the user to the enewsletter */
151 public boolean getSendNewsletter() { return this.sendNewsletter; }
152 /** Set whether to subscribe the user to the enewsletter */
153 public void setSendNewsletter(boolean sendNewsletter) { this.sendNewsletter = sendNewsletter; }
154
155
156
157
158
159 // == Other Methods ===================================================
160
161 /** Reset all properties to their default values.
162 * @param mapping The mapping used to select this instance
163 * @param request The servlet request we are processing
164 */
165 public void reset(ActionMapping mapping, HttpServletRequest request) {
166 RuntimeParameters.logDebug(this, "** reset of RegisterForm!");
167 this.formAction = "login";
168 this.loginName = "";
169 this.password = "";
170 this.retypePassword = "";
171 this.setCookie = false;
172 this.email = "";
173 this.firstName = "";
174 this.lastName = "";
175 this.companyName = "";
176 this.phone = "";
177 this.postcode = "";
178 this.streetNumber = "";
179 this.country = "United Kingdom";
180 this.sendNewsletter = false;
181 }
182
183 /** Validate logon field entries.
184 * When registering, this will make sure the phone number starts with (0).
185 */
186 public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
187
188 ActionErrors errors = new ActionErrors();
189
190 if (getFormAction().equals("login")) {
191 // User is trying to log in - make sure they put in both a username and a password
192
193 if (loginName == null || loginName.equals(""))
194 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.user.loginNameNull"));
195
196 if (password == null || password.equals(""))
197 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.user.passwordNull"));
198
199 } else if (getFormAction().equals("register")) {
200 // User is trying to register
201
202 // Is loginName null?
203 if (loginName == null || loginName.equals(""))
204 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.user.loginNameNull"));
205
206 // Is password null?
207 if (password == null || password.equals("")) {
208 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.user.passwordNull"));
209 } else {
210 // Check password length
211 // NOTE: this shouldn't be hard-coded. It ought to go in RuntimeParameters.
212 if (password.length() < Integer.parseInt(RuntimeParameters.get("minimumPasswordLength"))) {
213 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.logon.passwordShort"));
214 } else {
215 // Check if the passwords match (case insensitive)
216 if (!password.toLowerCase().equals(retypePassword.toLowerCase())) {
217 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.user.confirm"));
218 }
219 }
220 }
221
222 // Is email null/invalid?
223 if (email == null || email.equals(""))
224 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.user.emailNull"));
225 else
226 if ( !UserForm.validEmail(email) ) errors.add
227 (ActionErrors.GLOBAL_ERROR, new ActionError("error.logon.invalidEmail"));
228
229 // Is name null?
230 if (firstName == null || firstName.equals(""))
231 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.user.firstNull"));
232 if (lastName == null || lastName.equals(""))
233 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.user.surnameNull"));
234
235 // Logins without a company name are allowed
236 // if (companyName == null || companyName.equals(""))
237 // errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.user.companyNull"));
238
239 // if (phone == null || phone.equals("")) {
240 // RuntimeParameters.logDebug(this, "[RegisterForm:validate] phone was "+phone);
241 // errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.user.phoneNull"));
242 // } else {
243 // // Process the phone number.
244 // // If it starts with "0", change it to "(0)".
245 // // Otherwise, add a leading "(0)".
246 // if (!phone.startsWith("(0)")) {
247 // if (phone.startsWith("0")) {
248 // phone = "(0)"+phone.substring(1);
249 // } else {
250 // phone = "(0)"+phone;
251 // }
252 // }
253 // }
254
255 // if (streetNumber == null || streetNumber.equals("")) {
256 // RuntimeParameters.logDebug(this, "[RegisterForm:validate] streetNumber was "+streetNumber);
257 // errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.user.streetNumberNull"));
258 // }
259 // if (postcode == null || postcode.equals("")) {
260 // RuntimeParameters.logDebug(this, "[RegisterForm:validate] postcode was "+postcode);
261 // errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.user.postcodeNull"));
262 // }
263 // if (country == null || country.equals("")) {
264 // RuntimeParameters.logDebug(this, "[RegisterForm:validate] country was "+country);
265 // errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.user.countryNull"));
266 // }
267
268
269 } else if (getFormAction().equals("emailPassword")) {
270 // Email the password to the user
271
272 if (email == null || email.equals(""))
273 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.user.emailNull"));
274 else
275 if ( !UserForm.validEmail(email) ) errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.logon.invalidEmail"));
276
277 } else {
278 // FormAction is wrong!
279 RuntimeParameters.logDebug(this, "[RegisterForm:validate] FormAction is "+getFormAction());
280 RuntimeParameters.logDebug(this, "FormAction is not set correctly");
281 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.formActionMissing"));
282 }
283
284 return errors;
285 }
286
287 }