2009/05/20 - Apache Shale has been retired.

For more information, please explore the Attic.

View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to you under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   *
17   * $Id: Registration.java 464373 2006-10-16 04:21:54Z rahul $
18   */
19  
20  package org.apache.shale.examples.mailreader;
21  
22  import javax.faces.application.FacesMessage;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.apache.struts.apps.mailreader.dao.ExpiredPasswordException;
27  import org.apache.struts.apps.mailreader.dao.Subscription;
28  import org.apache.struts.apps.mailreader.dao.User;
29  import org.apache.struts.apps.mailreader.dao.UserDatabase;
30  
31  /***
32   * <p><code>ViewController</code> for the <code>registration</code> page.</p>
33   */
34  
35  public class Registration extends BaseViewController {
36  
37      
38      // -------------------------------------------------------- Static Variables
39  
40  
41      /***
42       * <p>The log instance for this bean.</p>
43       */
44      private static final Log log = LogFactory.getLog(Registration.class);
45  
46  
47      // -------------------------------------------------------------- Properties
48  
49  
50      /***
51       * <p>The from address of this user.</p>
52       */
53      private String fromAddress = null;
54  
55      /***
56       * @return Returns the fromAddress.
57       */
58      public String getFromAddress() {
59          return this.fromAddress;
60      }
61  
62      /***
63       * @param fromAddress The fromAddress to set.
64       */
65      public void setFromAddress(String fromAddress) {
66          this.fromAddress = fromAddress;
67      }
68  
69  
70      /***
71       * <p>The full name of this user.</p>
72       */
73      private String fullName = null;
74  
75      /***
76       * @return Returns the fullName.
77       */
78      public String getFullName() {
79          return this.fullName;
80      }
81  
82      /***
83       * @param fullName The fullName to set.
84       */
85      public void setFullName(String fullName) {
86          this.fullName = fullName;
87      }
88      
89      
90  
91  
92  
93  
94  
95  
96  
97      /***
98       * <p>The login password for this user.</p>
99       */
100     private String password = null;
101 
102     /***
103      * @return Returns the password.
104      */
105     public String getPassword() {
106         return this.password;
107     }
108 
109     /***
110      * @param password The password to set.
111      */
112     public void setPassword(String password) {
113         this.password = password;
114     }
115     
116     
117     /***
118      * <p>The confirmation password for this user.</p>
119      */
120     private String password2 = null;
121 
122     /***
123      * @return Returns the password2.
124      */
125     public String getPassword2() {
126         return this.password2;
127     }
128 
129     /***
130      * @param password2 The password2 to set.
131      */
132     public void setPassword2(String password2) {
133         this.password2 = password2;
134     }
135 
136 
137     /***
138      * <p>The reply to address of this user.</p>
139      */
140     private String replyToAddress = null;
141 
142     /***
143      * @return Returns the replyToAddress.
144      */
145     public String getReplyToAddress() {
146         return this.replyToAddress;
147     }
148 
149     /***
150      * @param replyToAddress The replyToAddress to set.
151      */
152     public void setReplyToAddress(String replyToAddress) {
153         this.replyToAddress = replyToAddress;
154     }
155 
156 
157     /***
158      * <p>The set of subscriptions for the currently logged in user.
159      */
160     private Subscription[] subscriptions = null;
161 
162     /***
163      * @return Returns the subscriptions.
164      */
165     public Subscription[] getSubscriptions() {
166         return this.subscriptions;
167     }
168 
169     /***
170      * @param subscriptions The subscriptions to set.
171      */
172     public void setSubscriptions(Subscription subscriptions[]) {
173         if (log.isTraceEnabled()) {
174             if (subscriptions == null) {
175                 log.trace("Erasing stored subscriptions");
176             } else {
177                 log.trace("Storing " + subscriptions.length + " subscriptions");
178             }
179         }
180         this.subscriptions = subscriptions;
181     }
182 
183 
184     /***
185      * <p>The logon username for this user.</p>
186      */
187     private String username = null;
188 
189     /***
190      * @return Returns the username.
191      */
192     public String getUsername() {
193         return this.username;
194     }
195     
196     /***
197      * @param username The username to set.
198      */
199     public void setUsername(String username) {
200         this.username = username;
201     }
202 
203 
204     // ---------------------------------------------------------- Event Handlers
205 
206 
207     /***
208      * <p>Return to the appropriate page depending on the current mode.</p>
209      */
210     public String cancel() {
211 
212         if ("CREATE".equals(getState().getMode())) {
213             return "welcome";
214         } else {
215             return "menu";
216         }
217 
218     }
219 
220 
221     /***
222      * <p>Create a new subscription.</p>
223      */
224     public String create() {
225 
226         State state = getState();
227         state.setHost(null);
228         state.setMode("CREATE");
229         if (log.isTraceEnabled()) {
230             log.trace("subscription(" + state.getMode() + "," + state.getHost() + ")");
231         }
232         return "subscription";
233 
234     }
235 
236 
237     /***
238      * <p>Delete an existing subscription.</p>
239      */
240     public String delete() {
241 
242         State state = getState();
243         state.setHost(((Subscription) getBean("current")).getHost());
244         state.setMode("DELETE");
245         if (log.isTraceEnabled()) {
246             log.trace("subscription(" + state.getMode() + "," + state.getHost() + ")");
247         }
248         return "subscription";
249 
250     }
251 
252 
253     /***
254      * <p>Edit an existing subscription.</p>
255      */
256     public String edit() {
257 
258         State state = getState();
259         state.setHost(((Subscription) getBean("current")).getHost());
260         state.setMode("EDIT");
261         if (log.isTraceEnabled()) {
262             log.trace("subscription(" + state.getMode() + "," + state.getHost() + ")");
263         }
264         return "subscription";
265 
266     }
267 
268 
269     /***
270      * <p>Create or update the user information.</p>
271      * @throws ExpiredPasswordException 
272      */
273     public String save() throws ExpiredPasswordException {
274 
275         UserDatabase database = getUserDatabase();
276         String mode = getState().getMode();
277         boolean ok = true;
278         User user = null;
279 
280         if ("CREATE".equals(mode)) {
281 
282             // Verify that the proposed username is not already taken
283             if (database.findUser(username) != null) {
284                 // FIXME - localization
285                 getFacesContext().addMessage("registration:username",
286                   new FacesMessage("That username is already taken"));
287                 ok = false;
288             }
289 
290             // Verify that the two password values match
291             if (!password.equals(password2)) {
292                 // FIXME - localization
293                 getFacesContext().addMessage("registration:password2",
294                   new FacesMessage("Password values do not match"));
295                 ok = false;
296             }
297 
298             // Create a new user with the specified username and password
299             // and log the new user on
300             if (ok) {
301                 user = database.createUser(username);
302                 getState().setUser(user);
303             }
304             
305 
306         } else /* if ("EDIT".equals(mode)) */ {
307 
308             // Verify that the two password values match (if entered)
309             if ((password != null) && (password.length() > 0) &&
310                 (password2 != null) && (password2.length() > 0) &&
311                 !password.equals(password2)) {
312                 // FIXME - localization
313                 getFacesContext().addMessage("registration:password2",
314                   new FacesMessage("Password values do not match"));
315                 ok = false;
316             }
317 
318             // Edit the currently logged on user
319             user = getState().getUser();
320 
321         }
322         
323         // Copy the remaining properties
324         if (ok) {
325             if ((password != null) && (password.length() > 0)) {
326                 user.setPassword(password);
327             }
328             user.setFullName(fullName);
329             user.setFromAddress(fromAddress);
330             user.setReplyToAddress(replyToAddress);
331         }
332 
333         // Save the updated information to the database
334         try {
335             database.save();
336         } catch (Exception e) {
337             getFacesContext().addMessage(null,
338               new FacesMessage(e.getMessage()));
339             log.error("Database save exception", e);
340             ok = false;
341         }
342 
343         // Return to the input page if there were any errors
344         if (!ok) {
345             return null;
346         } else {
347             return "menu";
348         }
349 
350     }
351 
352 
353     // -------------------------------------------------- ViewController Methods
354 
355 
356     /***
357      * <p>If this is a postback, and we are in EDIT mode, retrieve the
358      * subscriptions for the currently logged on user.  This is required
359      * in case the user clicked one of the Delete or Edit buttons in the
360      * table, so that the correct row can be positioned to before calling
361      * the event handler.</p>
362      */
363     public void preprocess() {
364 
365         State state = getState();
366         if ("EDIT".equals(state.getMode())) {
367             setSubscriptions(state.getUser().getSubscriptions());
368         }
369 
370     }
371 
372 
373     /***
374      * <p>If this is not a postback, and we are in EDIT mode, prepopulate
375      * the field values for the user registration update form and retrieve the
376      * subscriptions for the currently logged in user.  (If this is a
377      * postback, we will have done this already in <code>init()</code>.)</p>
378      */
379     public void prerender() {
380 
381         State state = getState();
382 
383         // If we are not in EDIT mode, there is nothing to do
384         if (!"EDIT".equals(state.getMode())) {
385             return;
386         }
387 
388         // The first time in, prepopulate our input field values
389         User user = state.getUser();
390         if (!isPostBack()) {
391             setUsername(user.getUsername()); //FIXME: NPE (after session timeout?)
392             setPassword("");
393             setPassword2("");
394             setFullName(user.getFullName());
395             setFromAddress(user.getFromAddress());
396             setReplyToAddress(user.getReplyToAddress());
397 
398         // Look up the subscriptions for the currently logged on user,
399         // if we have not done so already.
400         // If we were using a real database, this is where executing the
401         // query and opening the result set would occur
402             setSubscriptions(user.getSubscriptions());
403         }
404 
405     }
406 
407 
408     /***
409      * <p>Release our reference to to the retrieved subscriptions (if any).
410      * If we were using a real database, this is where closing the result set
411      * would go.</p>
412      */
413     public void destroy() {
414     	// TODO this is causing the page not to display the subscriptions
415         //setSubscriptions(null);
416 
417     }
418 
419 
420 }