Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/RuntimeCollective/webapps/form/NewUserEmailForm.java


1   /* $Header: /home/CVS/rjp/src/com/RuntimeCollective/webapps/form/NewUserEmailForm.java,v 1.3 2003/09/30 15:13:14 joe Exp $
2    * $Revision: 1.3 $
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.User; 
33  import com.RuntimeCollective.webapps.bean.SimpleUser;
34  import com.RuntimeCollective.webapps.form.BeanForm; 
35  import com.RuntimeCollective.webapps.bean.EntityBean; 
36  import javax.servlet.http.HttpServletRequest; 
37  import java.sql.SQLException;
38  import org.apache.struts.action.ActionError; 
39  import org.apache.struts.action.ActionErrors; 
40  import org.apache.struts.action.ActionForm; 
41  import org.apache.struts.action.ActionMapping; 
42  
43  import com.RuntimeCollective.webapps.RuntimeParameters;
44  
45  /** 
46   * Form used for a new user to type his email. The email is
47   * checked for validity, and that it's not used already.
48   *
49   * @version $Id: NewUserEmailForm.java,v 1.3 2003/09/30 15:13:14 joe Exp $
50   * 
51   */ 
52  public class NewUserEmailForm extends ActionForm { 
53  
54      // == Constructor ==================================================
55      public NewUserEmailForm() {
56          super();
57      }
58  
59      // == Properties =================================================== 
60  
61      /** The email address of the user. Also used as the unique login identifier. */ 
62      protected String email = ""; 
63      /** Get the email address of the user. Also used as the unique login identifier. */ 
64      public String getEmail() { return this.email; } 
65      /** Set the email address of the user. Also used as the unique login identifier. */ 
66      public void setEmail(String email) { this.email = email; } 
67  
68  
69      // == Other Methods =================================================== 
70  
71      /** Validate logon field entries.
72       * <p>Error codes returned:
73       * <li><code>error.logon.null</code> - if either the email or password is "".
74       * <li><code>error.logon.invalidEmail</code> - if the email address is not valid.
75       * <li><code>error.user.confirm</code> - if the confirmation does not match the password.
76       * <li><code>error.logon.passwordShort</code> - if the password was < 6 letters
77       */
78      public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
79          ActionErrors errors = new ActionErrors();
80  
81          if (email.equals("")) {
82              errors.add(ActionErrors.GLOBAL_ERROR,
83                         new ActionError("error.register.nullEmail"));
84              RuntimeParameters.logDebug(this, "Email is null");
85  
86          } else if (!validEmail(email)) {
87              errors.add(ActionErrors.GLOBAL_ERROR,
88                         new ActionError("error.register.invalidEmail"));
89              RuntimeParameters.logDebug(this, "Email address is not valid");
90  
91          } else {
92              try {
93                  if (SimpleUser.findByEmail(email, true) != -1) {
94                      errors.add(ActionErrors.GLOBAL_ERROR,
95                                 new ActionError("error.register.usedEmail"));
96                      RuntimeParameters.logDebug(this, "Email address is already used");
97                  }
98              } catch (SQLException e) {
99                  errors.add(ActionErrors.GLOBAL_ERROR,
100                            new ActionError("error.db.connection"));
101                 RuntimeParameters.logDebug(this, "Could not check db for email unicity.", e);
102                 e.printStackTrace();
103             }
104         }
105        
106         return errors;
107     }
108 
109     /** Validate an email address.
110      * @param email Address to validate
111      * @return Returns 'false' if the email does not contain an '@' or does contain a space.
112      */
113     public static boolean validEmail(String email) {
114         // don't accept -1 nor 0
115         if (email.indexOf("@") < 1)
116             return false;
117         if (email.indexOf(" ") != -1)
118             return false;
119         return true;
120     }
121     
122     // == BeanForm Methods =================================================== 
123 
124     /** Reset all properties to default values. 
125      * @param mapping The mapping used to select this instance 
126      * @param request The servlet request we are processing 
127      */ 
128     public void reset(ActionMapping mapping, HttpServletRequest request) { 
129         setEmail(""); 
130     } 
131 
132     /** Populate a User bean from this form. */ 
133     public EntityBean populateBean( EntityBean bean ) throws SQLException {  
134         User user = (User) bean; 
135         user.setEmail( getEmail() ); 
136         return (EntityBean) user; 
137     } 
138     
139     /** Populate this form from a User bean. Sets formAction to "edit". */ 
140     public void populateForm( EntityBean bean ) {  
141         User user = (User) bean; 
142         setEmail(user.getEmail()); 
143     } 
144 }