Source code: com/RuntimeCollective/webapps/form/TrackedUserForm.java
1 /* $Header: /home/CVS/rjp/src/com/RuntimeCollective/webapps/form/TrackedUserForm.java,v 1.6 2003/09/30 15:13:14 joe Exp $
2 * $Revision: 1.6 $
3 * $Date: 2003/09/30 15:13:14 $
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.bean.TrackedUser;
33 import com.RuntimeCollective.webapps.form.BeanForm;
34 import com.RuntimeCollective.webapps.bean.EntityBean;
35 import com.RuntimeCollective.webapps.RuntimeParameters;
36
37 import org.apache.struts.action.ActionErrors;
38 import org.apache.struts.action.ActionForm;
39 import org.apache.struts.action.ActionMapping;
40 import org.apache.struts.action.ActionError;
41
42 import javax.servlet.http.HttpServletRequest;
43 import java.sql.SQLException;
44 import java.util.Vector;
45 import java.util.Date;
46 import java.util.Iterator;
47 import org.apache.oro.text.regex.Perl5Matcher;
48 import org.apache.oro.text.regex.Perl5Compiler;
49 import org.apache.oro.text.regex.Util;
50
51
52 /**
53 * Object that represents basic user details. Can be used for registering
54 * a new user, or logging in or editing a new one.
55 *
56 * @version $Id: TrackedUserForm.java,v 1.6 2003/09/30 15:13:14 joe Exp $
57 */
58 public class TrackedUserForm extends ActionForm implements BeanForm {
59
60 // == Database constraints =========================================
61
62 /** Maximum email length */
63 public static final int EMAIL_LENGTH = 100;
64 /** Maximum password name length */
65 public static final int PASSWORD_LENGTH = 50;
66 /** Minimum password name length */
67 public static final int PASSWORD_LENGTH_MIN = 6;
68 /** Maximum first length */
69 public static final int FIRST_LENGTH = 50;
70 /** Maximum surname length */
71 public static final int SURNAME_LENGTH = 50;
72
73 // == Constructor ==================================================
74 /**
75 * Constructs a user form.
76 */
77 public TrackedUserForm() { }
78
79 // == Properties ===================================================
80
81 /** Type of action the form is being used for : default is "editUser". */
82 protected String formAction = "editUser";
83 /** Get type of action the form is being used for : could be "editUser" or "addUser". */
84 public String getFormAction() { return this.formAction; }
85 /** Set type of action the form is being used for : could be "editUser" or "addUser". */
86 public void setFormAction(String formAction) { this.formAction = formAction; }
87
88 /** First name. */
89 protected String first = "";
90 /** Get the user's email address first name. */
91 public String getFirstName() { return (this.first); }
92 /** Set the user's first name. */
93 public void setFirstName(String first) { this.first = first; }
94
95 /** Surname name. */
96 protected String surname = "";
97 /** Get the user's email address surname name. */
98 public String getLastName() { return (this.surname); }
99 /** Set the user's surname name. */
100 public void setLastName(String surname) { this.surname = surname; }
101
102 /** The password. */
103 protected String thePassword = "";
104 /** Get the password. */
105 public String getThePassword() { return this.thePassword; }
106 /** Set the password. */
107 public void setThePassword(String password) { this.thePassword = password; }
108
109 /** Password confirmation. */
110 protected String passwordConfirm = "";
111 /** Get password confirmation. */
112 public String getPasswordConfirm() { return this.passwordConfirm; }
113 /** Set password confirmation. */
114 public void setPasswordConfirm(String passwordConfirm) {
115 this.passwordConfirm = passwordConfirm;
116 }
117
118 /** The email address of the user. Also used as the unique login identifier. */
119 protected String email = "";
120 /** Get the email address of the user. Also used as the unique login identifier. */
121 public String getEmail() { return this.email; }
122 /** Set the email address of the user. Also used as the unique login identifier. */
123 public void setEmail(String email) { this.email = email; }
124
125 /** Simultaneous sessions. The maximum allowable number of sessions allowed. */
126 protected int simultaneousSessions = 2;
127 /** Simultaneous sessions as a String. */
128 protected String sessionString = "2";
129 /** Get the maximum number of simultaneous sessions. */
130 public int getSimultaneousSessions() { return this.simultaneousSessions; }
131 /** Set the maximum number of simultaneous sessions. */
132 public void setSimultaneousSessions(int simultaneousSessions) {
133 this.simultaneousSessions = simultaneousSessions;
134 }
135 /** Get the maximum number of simultaneous sessions as a String. */
136 public String getSimultaneousSessionsString() { return this.sessionString; }
137 /** Set the maximum number of simultaneous sessions as a String. */
138 public void setSimultaneousSessionsString(String sessions) {
139 this.sessionString = sessions;
140 }
141
142 /** Number of Logins */
143 protected int noLogins = 0;
144 /** Get the number of Logins. */
145 public int getNoOfLogins() { return noLogins; }
146 /** Set the number of credits. */
147 public void setNoOfLogins(int noLogins) { this.noLogins = noLogins; }
148
149 /** Last login date */
150 protected Date lastLoginDate = null;
151 /** Get the number of Logins. */
152 public Date getLastLoginDate() { return lastLoginDate; }
153 /** Set the number of credits. */
154 public void setLastLoginDate(Date lastLoginDate) { this.lastLoginDate = lastLoginDate; }
155
156
157 // == Other Methods ===================================================
158
159 /**
160 * Validates the form field entries.
161 *
162 * <p>Error codes returned:
163 * <ul>
164 * <li><code>error.user.emailNull</code> - if email is null</li>
165 * <li><code>error.user.invalidEmail</code> - if email is not valid</li>
166 * <li><code>error.user.passwordNull</code> - if the password field is empty</li>
167 * <li><code>error.user.passwordTooLong</code> - if the password is longer than allowed in the DB</li>
168 * <li><code>error.user.passwordTooShort</code> - if the password was less than 6 characters in length</li>
169 * <li><code>error.user.confirm</code> - if the confirmation does not match the password</li>
170 * <li><code>error.user.sessionNotNumber</code> - if the session field is not a number</li>
171 * <li><code>error.user.sessionTooSmall</code> - if max simultaneous sessions less than 1</li>
172 * </ul>
173 *
174 * @param mapping The mapping used to select this instance
175 * @param request The servlet request we are processing
176 * @return a set of errors encountered during validation
177 */
178 public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
179
180 ActionErrors errors = new ActionErrors();
181
182 if (email.equals("")) {
183 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.user.emailNull"));
184 } else if (email.length() > EMAIL_LENGTH) {
185 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.user.emailTooLong", ""+EMAIL_LENGTH));
186 } else if (!validEmail(email)) {
187 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.user.invalidEmail"));
188 }
189
190 if (thePassword.equals(""))
191 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.user.passwordNull"));
192 else if (thePassword.length() >= PASSWORD_LENGTH)
193 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.user.passwordTooLong", ""+PASSWORD_LENGTH));
194 else if (thePassword.length() < PASSWORD_LENGTH_MIN)
195 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.user.passwordTooShort", ""+PASSWORD_LENGTH_MIN));
196 else if (!thePassword.equals(passwordConfirm) ) {
197 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.user.confirm"));
198 }
199
200 // validate the name
201 if ((first == null) || first.equals(""))
202 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.user.firstNull"));
203 else if (first.length() > FIRST_LENGTH)
204 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.user.firstTooLong", ""+FIRST_LENGTH));
205
206 if ((surname == null) || surname.equals(""))
207 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.user.surnameNull"));
208 else if (surname.length() > SURNAME_LENGTH)
209 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.user.surnameTooLong", ""+SURNAME_LENGTH));
210
211
212 // validate number of simultaneous sessions
213 try {
214 simultaneousSessions = Integer.parseInt(sessionString);
215 }
216 catch (NumberFormatException nfe) {
217 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.user.sessionsNotNumber"));
218 }
219 if (simultaneousSessions < 1){
220 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.user.sessionsTooSmall"));
221 }
222
223 return errors;
224 }
225
226
227 /**
228 * Validates an email address.
229 *
230 * @param email the address to validate
231 * @return <code>false</code> if the email does not contain a '@' character
232 * or does contain a space, false otherwise
233 */
234 public static boolean validEmail(String email) {
235 if (email.indexOf("@") == -1)
236 return false;
237 if (email.indexOf(" ") != -1)
238 return false;
239 // check for at least one dot, in the domain name...
240 if (email.indexOf(".", email.indexOf("@")) == -1)
241 return false;
242 return true;
243 }
244
245
246 // == BeanForm Methods ===================================================
247
248 /**
249 * Reset all properties to default values.
250 *
251 * @param mapping The mapping used to select this instance
252 * @param request The servlet request we are processing
253 */
254 public void reset(ActionMapping mapping, HttpServletRequest request) {
255 setFormAction("editUser");
256 setFirstName("");
257 setLastName("");
258 setThePassword("");
259 setPasswordConfirm("");
260 setEmail("");
261 setSimultaneousSessions(2);
262 setSimultaneousSessionsString("2");
263 setNoOfLogins(0);
264 setLastLoginDate(null);
265 }
266
267
268 /**
269 * Populate a TrackedUser bean from this form.
270 *
271 * @param bean the bean to populate
272 * @return the bean, initialised by data from this form
273 */
274 public EntityBean populateBean(EntityBean bean) throws SQLException {
275 TrackedUser user = (TrackedUser) bean;
276 user.setFirstName(getFirstName());
277 user.setLastName(getLastName());
278 user.setPassword(getThePassword());
279 user.setEmail(getEmail());
280 user.setMaxNoOfSimultaneousSessions(getSimultaneousSessions());
281 // don't overwrite these
282 //user.setNoOfLogins(getNoOfLogins());
283 //user.setLastLoginDate(getLastLoginDate());
284 return (EntityBean) user;
285 }
286
287
288 /**
289 * Populate this form from a User bean.
290 *
291 * @param bean the bean to use in populating this form
292 */
293 public void populateForm(EntityBean bean) {
294 TrackedUser user = (TrackedUser) bean;
295 setFirstName(user.getFirstName());
296 setLastName(user.getLastName());
297 setThePassword(user.getPassword());
298 setPasswordConfirm(user.getPassword());
299 setEmail(user.getEmail());
300 setSimultaneousSessions(user.getMaxNoOfSimultaneousSessions());
301 setSimultaneousSessionsString(""+simultaneousSessions);
302 setNoOfLogins(user.getNoOfLogins());
303 setLastLoginDate(user.getLastLoginDate());
304 }
305 }