View Javadoc

1   package org.sourceforge.vlibrary.user.forms;
2   
3   import javax.servlet.http.HttpServletRequest;
4   import org.apache.struts.action.ActionMessage;
5   import org.apache.struts.action.ActionMessages;
6   import org.apache.struts.action.ActionErrors;
7   import org.apache.struts.action.ActionMapping;
8   import org.apache.struts.util.MessageResources;
9   import org.apache.log4j.Logger;
10  
11  import org.sourceforge.vlibrary.exceptions.LibraryException;
12  
13  /**
14   * Form bean for the Reader edit page.  This form has the following fields:
15   * <ul>
16   * <li><b>action</b> - The maintenance action we are performing (Create, Delete,
17   *     or Update).
18   * <li><b>email</b> - The EMAIL address of the reader [REQUIRED]
19   * <li><b>firstName</b> - The first name of the reader [REQUIRED]
20   * <li><b>lastName</b> - - The last name of the reader [REQUIRED]
21   * <li><b>deskPhone</b> - The desk phone of the reader [REQUIRED]
22   * <li><b>id</b> - The id of the reader
23   * <li><b>imService</b> - The im service of the reader
24   * <li><b>screenName</b> - The screen name of the reader
25   * <li><b>pager</b> - The pager number of the reader
26   * <li><b>mobilePhone</b> - The mobile phone of the reader
27   * <li><b>uid</b> - The user id of the reader [REQUIRED]
28   * <li><b>pwd</b> - The password of the reader [REQUIRED]
29   * <li><b>administrator</b> - boolean administrator status
30   * </ul>
31   *
32   * @version $Revision$ $Date$
33   */
34  
35  public final class ReaderForm extends LibraryForm {
36      private static final long serialVersionUID = 0;
37      
38      /** log4j Logger */
39      private static Logger logger =
40       Logger.getLogger(ReaderForm.class.getName());
41      
42      /** Message Resources */
43      private static MessageResources messages =
44       MessageResources.getMessageResources("ApplicationResources");
45      
46      /**
47       * The maintenance action we are performing (Create, Edit or delete).
48       */
49      
50      private String action = "Create";
51      private long id = 0;
52      private String lastName = null;
53      private String firstName = null;
54      private String email=null;
55      private String deskPhone=null;
56      private String mobilePhone=null;
57      private String pager=null;
58      private String screenName=null;
59      private String imService=null;
60      private String uid=null;
61      private String pwd=null;
62      private String confirmPwd=null;
63      private boolean administrator=false;
64      
65      public String getClassName() {
66          return ReaderForm.class.getName();
67      }
68      
69      public String getAction() {
70          return action;
71      }
72      
73      public void setAction(String action) {
74          this.action = action;
75      }
76      
77      public String getDeskPhone() {
78          return deskPhone;
79      }
80      
81      public void setDeskPhone(String deskPhone) {
82          this.deskPhone = deskPhone;
83      }
84      
85      public String getEmail() {
86          return email;
87      }
88      
89      public void setEmail(String email) {
90          this.email = email;
91      }
92      
93      public String getFirstName() {
94          return firstName;
95      }
96      
97      public void setFirstName(String firstName) {
98          this.firstName = firstName;
99      }
100     
101     public long getId() {
102         return id;
103     }
104     
105     public void setId(long id) {
106         this.id = id;
107     }
108     
109     public String getImService() {
110         return imService;
111     }
112     
113     public void setImService(String imService) {
114         this.imService = imService;
115     }
116     
117     public String getLastName() {
118         return lastName;
119     }
120     
121     public void setLastName(String lastName) {
122         this.lastName = lastName;
123     }
124     
125     public String getMobilePhone() {
126         return mobilePhone;
127     }
128     
129     public void setMobilePhone(String mobilePhone) {
130         this.mobilePhone = mobilePhone;
131     }
132     
133     public String getPager() {
134         return pager;
135     }
136     
137     public void setPager(String pager) {
138         this.pager = pager;
139     }
140     
141     public String getPwd() {
142         return pwd;
143     }
144     
145     public void setPwd(String pwd) {
146         this.pwd = pwd;
147     }
148     
149     public String getScreenName() {
150         return screenName;
151     }
152     
153     public void setScreenName(String screenName) {
154         this.screenName = screenName;
155     }
156     
157     public String getUid() {
158         return uid;
159     }
160     
161     public void setUid(String uid) {
162         this.uid = uid;
163     }
164     
165     /**
166      * Reset all properties to their default values.
167      *
168      * @param mapping The mapping used to select this instance
169      * @param request The servlet request we are processing
170      */
171     public void reset(ActionMapping mapping, HttpServletRequest request) {
172         this.action = "Create";
173         this.email = null;
174         this.lastName = null;
175         this.firstName = null;
176         this.imService = null;
177         this.screenName = null;
178         this.pager = null;
179         this.deskPhone = null;
180         this.mobilePhone = null;
181         this.uid = null;
182         this.pwd = null;
183         this.id=0;
184         
185     }
186     
187     /**
188      * Validate the properties that have been set from this HTTP request,
189      * and return an <code>ActionErrors</code> object that encapsulates any
190      * validation errors that have been found.  If no errors are found, return
191      * <code>null</code> or an <code>ActionErrors</code> object with no
192      * recorded error messages.
193      *
194      * @param mapping The mapping used to select this instance
195      * @param request The servlet request we are processing
196      */
197     public ActionErrors validate(ActionMapping mapping,
198      HttpServletRequest request) {
199         
200         if (logger.isDebugEnabled()) {
201             logger.debug(messages.getMessage("entering.validate"));
202         }
203         
204         ActionErrors errors = new ActionErrors();
205         
206         if ((email == null) || (email.length() < 1)) {
207             errors.add("email",
208              new ActionMessage("error.reader.email.required"));
209         }
210         
211         if ((firstName == null) || (firstName.length() < 1)) {
212             errors.add("firstName",
213              new ActionMessage("error.reader.firstName.required"));
214         }
215         
216         if ((lastName == null) || (lastName.length() < 1)) {
217             errors.add("lastName",
218              new ActionMessage("error.reader.lastName.required"));
219         }
220         
221         if ((uid == null) || (uid.length() < 1)) {
222             errors.add("uid",
223              new ActionMessage("error.reader.uid.required"));
224         }
225         
226         if (action.equals("Create")) {
227             if ((pwd == null) || (pwd.length() < 1)) {
228                 errors.add("pwd",
229                  new ActionMessage("error.reader.password.required"));
230             }
231             
232             if ((confirmPwd == null) || (confirmPwd.length() < 1)) {
233                 errors.add("confirmPwd",
234                  new ActionMessage("error.reader.password.confirmation.required"));
235             }
236             
237             // Check password/confirmation match
238             if (!(pwd.equals(confirmPwd))) {
239                 errors.add("password",
240                  new ActionMessage("error.reader.confirmPwdFailed"));
241             }
242             
243             // Make sure uid is not already taken
244             try {
245                 if (libraryManager.isUserIDValid(uid)) {
246                     errors.add("uid",
247                      new ActionMessage("error.reader.idTaken"));
248                 }
249             } catch (LibraryException le) {
250                 logger.error(le);
251                 errors.add(ActionMessages.GLOBAL_MESSAGE,
252                  new ActionMessage("error.reader.update"));
253             }
254             
255             // Make sure that first+last name is not already registered
256             try {
257                 if (libraryManager.readerExists(firstName,lastName)) {
258                     errors.add(ActionMessages.GLOBAL_MESSAGE,
259                      new ActionMessage("error.reader.already.registered"));
260                 }
261             } catch (LibraryException le) {
262                 logger.error(le);
263                 
264                 errors.add(ActionMessages.GLOBAL_MESSAGE,
265                  new ActionMessage("error.reader.update"));
266             }
267             
268         }
269         
270         if (logger.isDebugEnabled() && errors.isEmpty()) {
271             logger.debug(messages.getMessage("validate.successful"));
272         }
273         
274         return errors;
275     }
276     
277     public String getConfirmPwd() {
278         return confirmPwd;
279     }
280     
281     public void setConfirmPwd(String confirmPwd) {
282         this.confirmPwd = confirmPwd;
283     }
284     
285     public boolean isAdministrator() {
286         return administrator;
287     }
288     
289     public void setAdministrator(boolean administrator) {
290         this.administrator = administrator;
291     }
292 }