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

For more information, please explore the Attic.

Coverage Report - org.apache.shale.examples.mailreader.Registration
 
Classes in this File Line Coverage Branch Coverage Complexity
Registration
0%
0/108
0%
0/17
1.955
 
 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  0
 public class Registration extends BaseViewController {
 36  
 
 37  
     
 38  
     // -------------------------------------------------------- Static Variables
 39  
 
 40  
 
 41  
     /**
 42  
      * <p>The log instance for this bean.</p>
 43  
      */
 44  0
     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  0
     private String fromAddress = null;
 54  
 
 55  
     /**
 56  
      * @return Returns the fromAddress.
 57  
      */
 58  
     public String getFromAddress() {
 59  0
         return this.fromAddress;
 60  
     }
 61  
 
 62  
     /**
 63  
      * @param fromAddress The fromAddress to set.
 64  
      */
 65  
     public void setFromAddress(String fromAddress) {
 66  0
         this.fromAddress = fromAddress;
 67  0
     }
 68  
 
 69  
 
 70  
     /**
 71  
      * <p>The full name of this user.</p>
 72  
      */
 73  0
     private String fullName = null;
 74  
 
 75  
     /**
 76  
      * @return Returns the fullName.
 77  
      */
 78  
     public String getFullName() {
 79  0
         return this.fullName;
 80  
     }
 81  
 
 82  
     /**
 83  
      * @param fullName The fullName to set.
 84  
      */
 85  
     public void setFullName(String fullName) {
 86  0
         this.fullName = fullName;
 87  0
     }
 88  
     
 89  
     
 90  
 
 91  
 
 92  
 
 93  
 
 94  
 
 95  
 
 96  
 
 97  
     /**
 98  
      * <p>The login password for this user.</p>
 99  
      */
 100  0
     private String password = null;
 101  
 
 102  
     /**
 103  
      * @return Returns the password.
 104  
      */
 105  
     public String getPassword() {
 106  0
         return this.password;
 107  
     }
 108  
 
 109  
     /**
 110  
      * @param password The password to set.
 111  
      */
 112  
     public void setPassword(String password) {
 113  0
         this.password = password;
 114  0
     }
 115  
     
 116  
     
 117  
     /**
 118  
      * <p>The confirmation password for this user.</p>
 119  
      */
 120  0
     private String password2 = null;
 121  
 
 122  
     /**
 123  
      * @return Returns the password2.
 124  
      */
 125  
     public String getPassword2() {
 126  0
         return this.password2;
 127  
     }
 128  
 
 129  
     /**
 130  
      * @param password2 The password2 to set.
 131  
      */
 132  
     public void setPassword2(String password2) {
 133  0
         this.password2 = password2;
 134  0
     }
 135  
 
 136  
 
 137  
     /**
 138  
      * <p>The reply to address of this user.</p>
 139  
      */
 140  0
     private String replyToAddress = null;
 141  
 
 142  
     /**
 143  
      * @return Returns the replyToAddress.
 144  
      */
 145  
     public String getReplyToAddress() {
 146  0
         return this.replyToAddress;
 147  
     }
 148  
 
 149  
     /**
 150  
      * @param replyToAddress The replyToAddress to set.
 151  
      */
 152  
     public void setReplyToAddress(String replyToAddress) {
 153  0
         this.replyToAddress = replyToAddress;
 154  0
     }
 155  
 
 156  
 
 157  
     /**
 158  
      * <p>The set of subscriptions for the currently logged in user.
 159  
      */
 160  0
     private Subscription[] subscriptions = null;
 161  
 
 162  
     /**
 163  
      * @return Returns the subscriptions.
 164  
      */
 165  
     public Subscription[] getSubscriptions() {
 166  0
         return this.subscriptions;
 167  
     }
 168  
 
 169  
     /**
 170  
      * @param subscriptions The subscriptions to set.
 171  
      */
 172  
     public void setSubscriptions(Subscription subscriptions[]) {
 173  0
         if (log.isTraceEnabled()) {
 174  0
             if (subscriptions == null) {
 175  0
                 log.trace("Erasing stored subscriptions");
 176  0
             } else {
 177  0
                 log.trace("Storing " + subscriptions.length + " subscriptions");
 178  
             }
 179  
         }
 180  0
         this.subscriptions = subscriptions;
 181  0
     }
 182  
 
 183  
 
 184  
     /**
 185  
      * <p>The logon username for this user.</p>
 186  
      */
 187  0
     private String username = null;
 188  
 
 189  
     /**
 190  
      * @return Returns the username.
 191  
      */
 192  
     public String getUsername() {
 193  0
         return this.username;
 194  
     }
 195  
     
 196  
     /**
 197  
      * @param username The username to set.
 198  
      */
 199  
     public void setUsername(String username) {
 200  0
         this.username = username;
 201  0
     }
 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  0
         if ("CREATE".equals(getState().getMode())) {
 213  0
             return "welcome";
 214  
         } else {
 215  0
             return "menu";
 216  
         }
 217  
 
 218  
     }
 219  
 
 220  
 
 221  
     /**
 222  
      * <p>Create a new subscription.</p>
 223  
      */
 224  
     public String create() {
 225  
 
 226  0
         State state = getState();
 227  0
         state.setHost(null);
 228  0
         state.setMode("CREATE");
 229  0
         if (log.isTraceEnabled()) {
 230  0
             log.trace("subscription(" + state.getMode() + "," + state.getHost() + ")");
 231  
         }
 232  0
         return "subscription";
 233  
 
 234  
     }
 235  
 
 236  
 
 237  
     /**
 238  
      * <p>Delete an existing subscription.</p>
 239  
      */
 240  
     public String delete() {
 241  
 
 242  0
         State state = getState();
 243  0
         state.setHost(((Subscription) getBean("current")).getHost());
 244  0
         state.setMode("DELETE");
 245  0
         if (log.isTraceEnabled()) {
 246  0
             log.trace("subscription(" + state.getMode() + "," + state.getHost() + ")");
 247  
         }
 248  0
         return "subscription";
 249  
 
 250  
     }
 251  
 
 252  
 
 253  
     /**
 254  
      * <p>Edit an existing subscription.</p>
 255  
      */
 256  
     public String edit() {
 257  
 
 258  0
         State state = getState();
 259  0
         state.setHost(((Subscription) getBean("current")).getHost());
 260  0
         state.setMode("EDIT");
 261  0
         if (log.isTraceEnabled()) {
 262  0
             log.trace("subscription(" + state.getMode() + "," + state.getHost() + ")");
 263  
         }
 264  0
         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  0
         UserDatabase database = getUserDatabase();
 276  0
         String mode = getState().getMode();
 277  0
         boolean ok = true;
 278  0
         User user = null;
 279  
 
 280  0
         if ("CREATE".equals(mode)) {
 281  
 
 282  
             // Verify that the proposed username is not already taken
 283  0
             if (database.findUser(username) != null) {
 284  
                 // FIXME - localization
 285  0
                 getFacesContext().addMessage("registration:username",
 286  
                   new FacesMessage("That username is already taken"));
 287  0
                 ok = false;
 288  
             }
 289  
 
 290  
             // Verify that the two password values match
 291  0
             if (!password.equals(password2)) {
 292  
                 // FIXME - localization
 293  0
                 getFacesContext().addMessage("registration:password2",
 294  
                   new FacesMessage("Password values do not match"));
 295  0
                 ok = false;
 296  
             }
 297  
 
 298  
             // Create a new user with the specified username and password
 299  
             // and log the new user on
 300  0
             if (ok) {
 301  0
                 user = database.createUser(username);
 302  0
                 getState().setUser(user);
 303  0
             }
 304  
             
 305  
 
 306  
         } else /* if ("EDIT".equals(mode)) */ {
 307  
 
 308  
             // Verify that the two password values match (if entered)
 309  0
             if ((password != null) && (password.length() > 0) &&
 310  
                 (password2 != null) && (password2.length() > 0) &&
 311  
                 !password.equals(password2)) {
 312  
                 // FIXME - localization
 313  0
                 getFacesContext().addMessage("registration:password2",
 314  
                   new FacesMessage("Password values do not match"));
 315  0
                 ok = false;
 316  
             }
 317  
 
 318  
             // Edit the currently logged on user
 319  0
             user = getState().getUser();
 320  
 
 321  
         }
 322  
         
 323  
         // Copy the remaining properties
 324  0
         if (ok) {
 325  0
             if ((password != null) && (password.length() > 0)) {
 326  0
                 user.setPassword(password);
 327  
             }
 328  0
             user.setFullName(fullName);
 329  0
             user.setFromAddress(fromAddress);
 330  0
             user.setReplyToAddress(replyToAddress);
 331  
         }
 332  
 
 333  
         // Save the updated information to the database
 334  
         try {
 335  0
             database.save();
 336  0
         } catch (Exception e) {
 337  0
             getFacesContext().addMessage(null,
 338  
               new FacesMessage(e.getMessage()));
 339  0
             log.error("Database save exception", e);
 340  0
             ok = false;
 341  0
         }
 342  
 
 343  
         // Return to the input page if there were any errors
 344  0
         if (!ok) {
 345  0
             return null;
 346  
         } else {
 347  0
             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  0
         State state = getState();
 366  0
         if ("EDIT".equals(state.getMode())) {
 367  0
             setSubscriptions(state.getUser().getSubscriptions());
 368  
         }
 369  
 
 370  0
     }
 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  0
         State state = getState();
 382  
 
 383  
         // If we are not in EDIT mode, there is nothing to do
 384  0
         if (!"EDIT".equals(state.getMode())) {
 385  0
             return;
 386  
         }
 387  
 
 388  
         // The first time in, prepopulate our input field values
 389  0
         User user = state.getUser();
 390  0
         if (!isPostBack()) {
 391  0
             setUsername(user.getUsername()); //FIXME: NPE (after session timeout?)
 392  0
             setPassword("");
 393  0
             setPassword2("");
 394  0
             setFullName(user.getFullName());
 395  0
             setFromAddress(user.getFromAddress());
 396  0
             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  0
             setSubscriptions(user.getSubscriptions());
 403  
         }
 404  
 
 405  0
     }
 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  0
     }
 418  
 
 419  
 
 420  
 }