KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > raptus > owxv3 > modules > usermgr > EAUserMgrEditBean


1 /*
2  * EAUserMgrEditBean.java
3  *
4  * Created on May 28, 2003, 11:44 AM
5  */

6
7 package com.raptus.owxv3.modules.usermgr;
8
9
10 import java.util.*;
11 import javax.servlet.http.HttpServletRequest JavaDoc;
12 import org.apache.struts.action.*;
13
14 import com.raptus.owxv3.*;
15 import com.raptus.owxv3.api.*;
16
17
18 /**
19  * bean used to store data on the add user/edit user form
20  * @author root
21  */

22 public class EAUserMgrEditBean extends OmniaWebBean
23 {
24     /**
25      * Roles of the user
26      */

27     protected List roles = null;
28     
29     /**
30      * All roles available in system
31      */

32     protected List allroles = null;
33     /**
34      * The user's name
35      */

36     protected String JavaDoc name = null;
37     
38     /**
39      * The user's password
40      */

41     protected String JavaDoc password = null;
42     
43     /**
44      * The user's email
45      */

46     protected String JavaDoc email = null;
47     
48     /**
49      * The user's locale
50      */

51     protected String JavaDoc locale = null;
52     
53     /**
54      * the user's username
55      */

56     protected String JavaDoc username = null;
57     
58     /**
59      * Creates a new instance of EAUserMgrEditBean
60      */

61     public EAUserMgrEditBean()
62     {
63         this.newuser = false;
64     }
65     
66     /**
67      * This is a new user?
68      */

69     protected boolean newuser=true;
70     
71     /**
72      * return true if the data stored for this user is a new user
73      *
74      * @return true if data stored is for a new user, not an existing one
75      */

76     public boolean isNewUser()
77     {
78         return newuser;
79     }
80     
81     public String JavaDoc getNewUser()
82     {
83         return ""+newuser;
84     }
85     
86     public void setNewUser(boolean newuser)
87     {
88         this.newuser = newuser;
89     }
90     
91     public void setNewUser(String JavaDoc newuser)
92     {
93         this.newuser = Boolean.valueOf(newuser).booleanValue();
94     }
95     
96     /**
97      * Return the locale of the user
98      *
99      * @return the locale of the user
100      */

101     public String JavaDoc getLocale() { return locale; }
102     
103     /**
104      * set the locale for this user
105      *
106      * @param locale the locale for this user
107      */

108     public void setLocale(String JavaDoc locale) { this.locale = locale; }
109     
110     /**
111      * return the password for this user
112      *
113      * @return the password for this user
114      */

115     public String JavaDoc getPassword() { return password; }
116     
117     /**
118      * set the password for this user
119      *
120      * @param password the password for this user
121      */

122     public void setPassword(String JavaDoc password) { this.password = password; }
123     
124     /**
125      * return the login name for this user
126      *
127      * @return the login name for this user
128      */

129     public String JavaDoc getUsername() { return username; }
130     
131     /**
132      * set the login name for this user
133      *
134      * @param username the login name for this user
135      */

136     public void setUsername(String JavaDoc username) { this.username = username; }
137     
138     /**
139      * return the email for this user
140      *
141      * @return the email for this user
142      */

143     public String JavaDoc getEmail() { return email; }
144     
145     /**
146      * set the email for this user
147      *
148      * @param email the email for this user.
149      */

150     public void setEmail(String JavaDoc email) {this.email = email;}
151     
152     /**
153      * return the dsplay name for this user
154      *
155      * @return the display name for this user
156      */

157     public String JavaDoc getName() { return name; }
158     
159     /**
160      * set the display name for this user
161      *
162      * @param name the display name for this user
163      */

164     public void setName(String JavaDoc name) { this.name = name; }
165     
166     /**
167      * Validate the form.
168      *
169      * @param request the http request
170      *
171      * @return Action errors on error
172      */

173     public ActionErrors validate(HttpServletRequest JavaDoc request)
174     {
175       ActionErrors ret=new ActionErrors();
176
177       // validate user name
178
if(name==null||name.length()<1)
179       {
180           ret.add(ActionErrors.GLOBAL_ERROR,new ActionError("module.usermgr.error.NoUserName"));
181       }
182
183       // validate login name
184
if(username==null||username.length()<1)
185       {
186           ret.add(ActionErrors.GLOBAL_ERROR,new ActionError("module.usermgr.error.NoLoginName"));
187       }
188
189       // validate email
190
if(email==null||email.length()<1)
191       {
192           ret.add(ActionErrors.GLOBAL_ERROR,new ActionError("module.usermgr.error.NoEmailAddress"));
193       }
194       
195       // validate password
196
if(password==null||password.length()<1)
197       {
198           ret.add(ActionErrors.GLOBAL_ERROR,new ActionError("module.usermgr.error.NoPassword"));
199       }
200
201       // validate locale
202
if(locale==null||locale.length()<1)
203       {
204           ret.add(ActionErrors.GLOBAL_ERROR,new ActionError("module.usermgr.error.NoLocale"));
205       }
206       
207       if(makeLocale(locale) == null)
208       {
209           ret.add(ActionErrors.GLOBAL_ERROR,new ActionError("module.usermgr.error.InvalidLocale"));
210       }
211       
212       if(!isValidEmail(email))
213       {
214           ret.add(ActionErrors.GLOBAL_ERROR,new ActionError("module.usermgr.error.InvalidEmail"));
215       }
216       
217       return ret;
218     }
219     
220     /**
221      * reset the state of the bean to initial state (no data)
222      */

223     public void reset()
224     {
225         this.email = null;
226         this.locale = null;
227         this.name = null;
228         this.password = null;
229         this.username = null;
230         this.newuser = false;
231         this.roles = null;
232     }
233     
234     /**
235      * Return the roles if this user
236      *
237      * @return the roles of this user
238      */

239     public List getRoles()
240     {
241         if(roles == null)
242         {
243             return new ArrayList();
244         }
245         else
246         {
247             return roles;
248         }
249     }
250
251     
252     /**
253      * Return all roles available in system
254      *
255      * @return all roles available in system
256      */

257     public List getAllRoles()
258     {
259         if(allroles == null)
260         {
261             return new ArrayList();
262         }
263         else
264         {
265             return allroles;
266         }
267     }
268
269     /**
270      * Set the roles available for this user
271      *
272      * @param roles the roles available for this user
273      */

274     public void setRoles(String JavaDoc[] roles)
275     {
276         this.roles = new ArrayList();
277         for(int i=0;i<roles.length;i++)
278         {
279             this.roles.add(roles[i]);
280         }
281     }
282     
283     /**
284      * set all roles available in system
285      *
286      * @param allroles all roles available in system
287      */

288     public void setAllRoles(List allroles)
289     {
290         this.allroles = allroles;
291     }
292     
293     public String JavaDoc[] split(String JavaDoc str, String JavaDoc delim)
294     {
295         StringTokenizer st = new StringTokenizer(str, delim);
296         String JavaDoc[] res = new String JavaDoc[st.countTokens()];
297         int i=0;
298         while(st.hasMoreTokens())
299         {
300             res[i++] = st.nextToken();
301         }
302         
303         return res;
304     }
305     
306     public Locale makeLocale(String JavaDoc locale)
307     {
308         VModuleManager vmm = VModuleManager.getInstance();
309         String JavaDoc[] locales = vmm.getVModule("usermgr").getLocales();
310         boolean found = false;
311         for(int i=0;i<locales.length;i++)
312         {
313             if(locales[i].equals(locale))
314             {
315                 found = true;
316                 break;
317             }
318         }
319         
320         if(!found)
321         {
322             // locale is invalid
323
return null;
324         }
325         
326         Locale result = null;
327         String JavaDoc sres[] = split(locale, "_");
328         if(sres.length==1)
329         {
330             result = new Locale(sres[0]);
331         }
332         else
333         {
334             result = new Locale(sres[0],sres[1]);
335         }
336         return result;
337     }
338     
339     protected boolean isValidEmail(String JavaDoc email)
340     {
341         if(email == null)
342         {
343             return false;
344         }
345         
346         if(email.indexOf("@")<0)
347         {
348             return false;
349         }
350         
351         if(email.endsWith("@"))
352         {
353             return false;
354         }
355         
356         if(email.endsWith("."))
357         {
358             return false;
359         }
360         
361         return true;
362     }
363 }
364
Popular Tags