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

For more information, please explore the Attic.

Coverage Report - org.apache.shale.examples.mailreaderjpa.Master
 
Classes in this File Line Coverage Branch Coverage Complexity
Master
0%
0/99
0%
0/18
2.444
 
 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  
 
 18  
 package org.apache.shale.examples.mailreaderjpa;
 19  
 
 20  
 import java.util.List;
 21  
 import java.util.Map;
 22  
 import org.apache.mailreaderjpa.Subscription;
 23  
 import org.apache.mailreaderjpa.User;
 24  
 import org.apache.shale.view.AbstractViewController;
 25  
 
 26  
 /**
 27  
  * <p>Backing bean for the <code>/master.jsp</code> view.</p>
 28  
  */
 29  0
 public class Master extends AbstractViewController {
 30  
     
 31  
 
 32  
     // ------------------------------------------------------ Manifest Constants
 33  
 
 34  
 
 35  
     /**
 36  
      * <p>Cache key under which we save the current transaction mode.</p>
 37  
      */
 38  
     private static final String MODE_KEY = "mode";
 39  
 
 40  
 
 41  
     /**
 42  
      * <p>Cache key under which we save the <code>User</code> instance
 43  
      * we are creating or editing.</p>
 44  
      */
 45  
     private static final String USER_KEY = "user";
 46  
 
 47  
 
 48  
     // ------------------------------------------------------- Public Properties
 49  
 
 50  
 
 51  
     /**
 52  
      * <p>The business {@link Logic} for this application.  This value
 53  
      * will be injected, based on the managed bean configuration.</p>
 54  
      */
 55  0
     private Logic logic = null;
 56  
 
 57  
 
 58  
     /**
 59  
      * <p>Return the business {@link Logic} for this application.</p>
 60  
      */
 61  
     public Logic getLogic() {
 62  0
         return this.logic;
 63  
     }
 64  
 
 65  
 
 66  
     /**
 67  
      * <p>Set the business {@link Logic} for this application.</p>
 68  
      *
 69  
      * @param logic The new business logic instance
 70  
      */
 71  
     public void setLogic(Logic logic) {
 72  0
         this.logic = logic;
 73  0
     }
 74  
 
 75  
 
 76  
     /**
 77  
      * <p>The transaction mode for the current request (CREATE or EDIT).
 78  
      * Default mode is EDIT.</p>
 79  
      */
 80  0
     private String mode = Constants.EDIT_MODE;
 81  
 
 82  
 
 83  
     /**
 84  
      * <p>Return the transaction mode (CREATE or EDIT) for this request.</p>
 85  
      */
 86  
     public String getMode() {
 87  0
         return this.mode;
 88  
     }
 89  
 
 90  
 
 91  
     /**
 92  
      * <p>Set the transaction mode (CREATE or EDIT) for this request.</p>
 93  
      *
 94  
      * @param mode The new transaction mode
 95  
      */
 96  
     public void setMode(String mode) {
 97  0
         this.mode = mode;
 98  0
     }
 99  
 
 100  
 
 101  
     /**
 102  
      * <p>The per-user {@link State} instance we are associated with.  This
 103  
      * value will be injected, based on the managed bean configuration.</p>
 104  
      */
 105  0
     private State state = null;
 106  
 
 107  
 
 108  
     /**
 109  
      * <p>Return the per-user {@link State} instance we are associated with.</p>
 110  
      */
 111  
     public State getState() {
 112  0
         return this.state;
 113  
     }
 114  
 
 115  
 
 116  
     /**
 117  
      * <p>Set the per-user {@link State} instance we are associated with.</p>
 118  
      *
 119  
      * @param state The new State instance
 120  
      */
 121  
     public void setState(State state) {
 122  0
         this.state = state;
 123  0
     }
 124  
 
 125  
 
 126  
     /**
 127  
      * <p>The <code>User</code> instance being created or edited.</p>
 128  
      */
 129  0
     private User user = null;
 130  
 
 131  
 
 132  
     /**
 133  
      * <p>Return the <code>User</code> instance we are creating or editing.</p>
 134  
      */
 135  
     public User getUser() {
 136  0
         return this.user;
 137  
     }
 138  
 
 139  
 
 140  
     // -------------------------------------------------- Input Field Properties
 141  
 
 142  
 
 143  
     /**
 144  
      * <p>The specified password.</p>
 145  
      */
 146  0
     private String password = null;
 147  
 
 148  
 
 149  
     /**
 150  
      * <p>Return the specified password.</p>
 151  
      */
 152  
     public String getPassword() {
 153  0
         return this.password;
 154  
     }
 155  
 
 156  
 
 157  
     /**
 158  
      * <p>Set the specified password.</p>
 159  
      *
 160  
      * @param password The new specified password
 161  
      */
 162  
     public void setPassword(String password) {
 163  0
         this.password = password;
 164  0
     }
 165  
 
 166  
 
 167  
     /**
 168  
      * <p>The confirmation password.</p>
 169  
      */
 170  0
     private String password2 = null;
 171  
 
 172  
 
 173  
     /**
 174  
      * <p>Return the confirmation password.</p>
 175  
      */
 176  
     public String getPassword2() {
 177  0
         return this.password2;
 178  
     }
 179  
 
 180  
 
 181  
     /**
 182  
      * <p>Set the confirmation password.</p>
 183  
      *
 184  
      * @param password2 The new confirmation password
 185  
      */
 186  
     public void setPassword2(String password2) {
 187  0
         this.password2 = password2;
 188  0
     }
 189  
 
 190  
 
 191  
     // -------------------------------------------------------- Lifecycle Events
 192  
 
 193  
 
 194  
     /**
 195  
      * <p>Initialize transaction mode from the request URI, if specified.</p>
 196  
      */
 197  
     public void init() {
 198  
 
 199  0
         log("Master.init(" + isPostBack() + ")");
 200  
 
 201  0
         String mode = getRequestParameter("mode");
 202  0
         if ((mode != null) && (mode.length() > 0)) {
 203  0
             setMode(mode);
 204  
         }
 205  
 
 206  0
     }
 207  
 
 208  
 
 209  
     /**
 210  
      * <p>Restore the <code>User</code> instance we are creating or editing,
 211  
      * creating a new one if necessary.</p>
 212  
      */
 213  
     public void preprocess() {
 214  
 
 215  0
         log("Master.preprocess(" + isPostBack() + ")");
 216  
 
 217  0
         String mode = (String) retrieveData(MODE_KEY);
 218  0
         if (mode != null) {
 219  0
             setMode(mode);
 220  
         }
 221  
 
 222  0
         User user = (User) retrieveData(USER_KEY);
 223  0
         if (user != null) {
 224  0
             this.user = user;
 225  0
         } else {
 226  0
             setup();
 227  
         }
 228  
 
 229  0
     }
 230  
 
 231  
 
 232  
     /**
 233  
      * <p>Cache the <code>User</code> instance we are creating or editing,
 234  
      * as well as the transaction model</p>
 235  
      */
 236  
     public void prerender() {
 237  
 
 238  0
         log("Master.prerender(" + isPostBack() + ")");
 239  
 
 240  
         // Create a new user instance if necessary (i.e. we navigated
 241  
         // to this page directly, or we are in CREATE mode)
 242  0
         if (getUser() == null) {
 243  0
             setup();
 244  
         }
 245  
 
 246  
         // Cache the current transaction mode and user instance
 247  0
         saveData(MODE_KEY, getMode());
 248  0
         saveData(USER_KEY, getUser());
 249  
 
 250  0
     }
 251  
 
 252  
 
 253  
     /**
 254  
      * <p>Release resources used during this request.</p>
 255  
      */
 256  
     public void destroy() {
 257  
 
 258  0
         log("Master.destroy(" + isPostBack() + ")");
 259  
 
 260  
         ; // FIXME - destroy()
 261  
 
 262  0
     }
 263  
 
 264  
 
 265  
     // ------------------------------------------------------------- View Events
 266  
 
 267  
 
 268  
     /**
 269  
      * <p>Cancel saving the updated user information.</p>
 270  
      */
 271  
     public String cancel() {
 272  
 
 273  
         // Navigate based on the current transaction mode
 274  0
         if (Constants.CREATE_MODE.equals(getMode())) {
 275  0
             return "welcome";
 276  
         } else {
 277  0
             return "menu";
 278  
         }
 279  
     }
 280  
 
 281  
 
 282  
     /**
 283  
      * <p>Save away the updated user information.</p>
 284  
      */
 285  
     public String save() {
 286  
 
 287  
         // Special handling for unique username checking on a CREATE transaction
 288  0
         if (Constants.CREATE_MODE.equals(getMode())) {
 289  
             try {
 290  0
                 User existing = getLogic().findUserByUsername(getUser().getUsername());
 291  0
                 if (existing != null) {
 292  0
                     error("Username '" + getUser().getUsername() + "' is already in use, please try again");
 293  0
                     return null;
 294  
                 }
 295  0
             } catch (Exception e) {
 296  0
                 error("Exception checking unique username: " + e);
 297  0
                 log("Exception checking unique username", e);
 298  0
                 return null;
 299  0
             }
 300  
         }
 301  
 
 302  
         // Special handling for password and password2 values
 303  0
         boolean ok = true;
 304  0
         if (Constants.CREATE_MODE.equals(getMode())) {
 305  
             // In CREATE mode, both passwords are required and must match
 306  0
             if ((getPassword() == null)
 307  
                 || (getPassword().length() == 0)) {
 308  0
                 error("Password is required");
 309  0
                 ok = false;
 310  
             }
 311  0
             if ((getPassword2() == null)
 312  
                 || (getPassword2().length() == 0)) {
 313  0
                 error("Password 2 is required");
 314  0
                 ok = false;
 315  
             }
 316  0
             if ((getPassword() != null)
 317  
                 && (getPassword2() != null)
 318  
                 && !getPassword().equals(getPassword2())) {
 319  0
                 error("Password and Password 2 values do not match");
 320  0
                 ok = false;
 321  0
             }
 322  
         } else {
 323  
             // In EDIT mode, confirmation password is required and must
 324  
             // match, if specified password was entered
 325  0
             if ((getPassword() != null)
 326  
                 && (getPassword().length() > 0)) {
 327  0
                 if ((getPassword2() == null)
 328  
                     || (getPassword2().length() == 0)) {
 329  0
                     error("Password 2 is required and must match Password");
 330  0
                     ok = false;
 331  0
                 } else if (!getPassword().equals(getPassword2())) {
 332  0
                     error("Password 2 and Password values do not match");
 333  0
                     ok = false;
 334  
                 }
 335  
             }
 336  
         }
 337  0
         if (!ok) {
 338  0
             return null;
 339  
         }
 340  
 
 341  
         // Copy the specified password if it was entered (which will always
 342  
         // be true in CREATE mode)
 343  0
         if ((getPassword() != null)
 344  
             && (getPassword().length() > 0)) {
 345  0
             user.setPassword(getPassword());
 346  
         }
 347  
 
 348  
         try {
 349  
 
 350  0
             if (Constants.CREATE_MODE.equals(getMode())) {
 351  0
                 getLogic().createUser(getUser());
 352  0
             } else {
 353  0
                 this.user = getLogic().updateUser(getUser());
 354  
             }
 355  
 
 356  
             // Implicitly log the user on (if creating), or
 357  
             // update the logged on user information (if editing)
 358  0
             getState().setUser(user);
 359  
 
 360  0
         } catch (Exception e) {
 361  
 
 362  0
             error("Exception saving user information: " + e.getMessage());
 363  0
             log("Exception saving user information", e);
 364  0
             return null;
 365  0
         }
 366  
 
 367  0
         return "menu";
 368  
 
 369  
     }
 370  
 
 371  
 
 372  
     // --------------------------------------------------------- Support Methods
 373  
 
 374  
 
 375  
     /**
 376  
      * <p>Set up the <code>User</code> instance we are creating and editing,
 377  
      * by re-finding the currently logged on user (if any), or by creating
 378  
      * a new instance.</p>
 379  
      */
 380  
     private void setup() {
 381  
 
 382  0
         User current = getState().getUser();
 383  0
         if (current == null) {
 384  0
             this.user = new User();
 385  0
             return;
 386  
         } else {
 387  0
             this.user = getLogic().findUserById(current.getId());
 388  
         }
 389  
 
 390  0
     }
 391  
 
 392  
 
 393  
 }