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

For more information, please explore the Attic.

Coverage Report - org.apache.shale.examples.mailreader.Subscription
 
Classes in this File Line Coverage Branch Coverage Complexity
Subscription
0%
0/79
0%
0/6
2.077
 
 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: Subscription.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  
 import org.apache.commons.logging.Log;
 24  
 import org.apache.commons.logging.LogFactory;
 25  
 import org.apache.struts.apps.mailreader.dao.User;
 26  
 import org.apache.struts.apps.mailreader.dao.UserDatabase;
 27  
 
 28  
 /**
 29  
  * <p><code>ViewController</code> for the <code>subscription</code> page.</p>
 30  
  */
 31  
 
 32  0
 public class Subscription extends BaseViewController {
 33  
 
 34  
     
 35  
     // -------------------------------------------------------- Static Variables
 36  
 
 37  
 
 38  
     /**
 39  
      * <p>The log instance for this bean.</p>
 40  
      */
 41  0
     private static final Log log = LogFactory.getLog(Subscription.class);
 42  
 
 43  
 
 44  
     // -------------------------------------------------------------- Properties
 45  
 
 46  
 
 47  
     /**
 48  
      * <p>The autoConnect state for this subscription.</p>
 49  
      */
 50  0
     private boolean autoConnect = false;
 51  
 
 52  
     /**
 53  
      * @return Returns the autoConnect.
 54  
      */
 55  
     public boolean isAutoConnect() {
 56  0
         return this.autoConnect;
 57  
     }
 58  
 
 59  
     /**
 60  
      * @param autoConnect The autoConnect to set.
 61  
      */
 62  
     public void setAutoConnect(boolean autoConnect) {
 63  0
         this.autoConnect = autoConnect;
 64  0
     }
 65  
 
 66  
 
 67  
     /**
 68  
      * <p>The host for this subscription.</p>
 69  
      */
 70  0
     private String host = null;
 71  
 
 72  
     /**
 73  
      * @return Returns the host.
 74  
      */
 75  
     public String getHost() {
 76  0
         return this.host;
 77  
     }
 78  
 
 79  
     /**
 80  
      * @param host The host to set.
 81  
      */
 82  
     public void setHost(String host) {
 83  0
         this.host = host;
 84  0
     }
 85  
 
 86  
 
 87  
     /**
 88  
      * <p>The login password for this subscription.</p>
 89  
      */
 90  0
     private String password = null;
 91  
 
 92  
     /**
 93  
      * @return Returns the password.
 94  
      */
 95  
     public String getPassword() {
 96  0
         return this.password;
 97  
     }
 98  
 
 99  
     /**
 100  
      * @param password The password to set.
 101  
      */
 102  
     public void setPassword(String password) {
 103  0
         this.password = password;
 104  0
     }
 105  
 
 106  
 
 107  
     /**
 108  
      * <p>The type for this subscription.</p>
 109  
      */
 110  0
     private String type = null;
 111  
 
 112  
     /**
 113  
      * @return Returns the type.
 114  
      */
 115  
     public String getType() {
 116  0
         return this.type;
 117  
     }
 118  
 
 119  
     /**
 120  
      * @param type The type to set.
 121  
      */
 122  
     public void setType(String type) {
 123  0
         this.type = type;
 124  0
     }
 125  
 
 126  
 
 127  
     /**
 128  
      * <p>The logon username for this subscription.</p>
 129  
      */
 130  0
     private String username = null;
 131  
 
 132  
     /**
 133  
      * @return Returns the username.
 134  
      */
 135  
     public String getUsername() {
 136  0
         return this.username;
 137  
     }
 138  
 
 139  
     /**
 140  
      * @param username The username to set.
 141  
      */
 142  
     public void setUsername(String username) {
 143  0
         this.username = username;
 144  0
     }
 145  
 
 146  
 
 147  
     // ---------------------------------------------------------- Event Handlers
 148  
 
 149  
 
 150  
     /**
 151  
      * <p>Return to the appropriate page depending on the current mode.</p>
 152  
      */
 153  
     public String cancel() {
 154  
 
 155  0
         if ("CREATE".equals(getState().getMode())) {
 156  0
             return "welcome";
 157  
         } else {
 158  0
             getState().setMode("EDIT");
 159  0
             return "registration";
 160  
         }
 161  
 
 162  
     }
 163  
 
 164  
 
 165  
     /**
 166  
      * <p>Create or update the user information.</p>
 167  
      */
 168  
     public String save() {
 169  
 
 170  0
         State state = getState();
 171  0
         UserDatabase database = getUserDatabase();
 172  0
         String mode = state.getMode();
 173  0
         User user = state.getUser();
 174  0
         org.apache.struts.apps.mailreader.dao.Subscription subscription =
 175  
                 user.findSubscription(state.getHost());
 176  
 
 177  0
         if ("CREATE".equals(mode)) {
 178  
 
 179  
             // Verify that the proposed hostname is not already taken
 180  0
             if (user.findSubscription(host) != null) {
 181  
                 // FIXME - localization
 182  0
                 getFacesContext().addMessage("subscription:host",
 183  
                         new FacesMessage("That hostname is already defined"));
 184  0
                 return null;
 185  
             }
 186  
 
 187  
             // Create a new subscription
 188  0
             subscription = user.createSubscription(host);
 189  0
             Registration registration = (Registration)getBean("registration");
 190  0
             user = getState().getUser();
 191  0
             registration.setFromAddress(user.getFromAddress());
 192  0
             registration.setFullName(user.getFullName());
 193  0
             registration.setPassword(user.getPassword());
 194  0
             registration.setPassword2(user.getPassword());
 195  0
             registration.setReplyToAddress(user.getReplyToAddress());
 196  0
             registration.setSubscriptions(user.getSubscriptions());
 197  0
             registration.setUsername(user.getUsername());
 198  
 
 199  0
         } else if ("DELETE".equals(mode)) {
 200  
 
 201  0
             user.removeSubscription(subscription);
 202  
             try {
 203  0
                 database.save();
 204  0
             } catch (Exception e) {
 205  0
                 getFacesContext().addMessage(null,
 206  
                         new FacesMessage(e.getMessage()));
 207  0
                 log.error("Database save exception", e);
 208  0
                 return null;
 209  0
             }
 210  0
             state.setMode("EDIT");
 211  0
             return "registration";
 212  
 
 213  
         } else /* if ("EDIT".equals(mode)) */ {
 214  
 
 215  
             ; // No special action required
 216  
 
 217  
         }
 218  
 
 219  
         // Copy the remaining properties
 220  0
         subscription.setUsername(username);
 221  0
         subscription.setPassword(password);
 222  0
         subscription.setType(type);
 223  0
         subscription.setAutoConnect(autoConnect);
 224  
 
 225  
         // Save the updated information to the database
 226  
         try {
 227  0
             database.save();
 228  0
         } catch (Exception e) {
 229  0
             getFacesContext().addMessage(null,
 230  
                     new FacesMessage(e.getMessage()));
 231  0
             log.error("Database save exception", e);
 232  0
             return null;
 233  0
         }
 234  
 
 235  
         // Return to the registration page
 236  0
         state.setMode("EDIT");
 237  0
         return "registration";
 238  
 
 239  
     }
 240  
 
 241  
 
 242  
     // -------------------------------------------------- ViewController Methods
 243  
 
 244  
 
 245  
     /**
 246  
      * <p>If this is not a postack, and we are in DELETE or EDIT mode,
 247  
      * prepopulate the field values for the subscription update form.</p>
 248  
      */
 249  
     public void prerender() {
 250  
 
 251  0
         State state = getState();
 252  
 
 253  
         // If we are not in DELETE or EDIT mode, there is nothing to do
 254  0
         if (!"DELETE".equals(state.getMode()) &&
 255  
                 !"EDIT".equals(state.getMode())) {
 256  0
             return;
 257  
         }
 258  
 
 259  
         // The first time in, prepopulate our input field values
 260  0
         User user = state.getUser();
 261  0
         org.apache.struts.apps.mailreader.dao.Subscription subscription =
 262  
                 user.findSubscription(state.getHost());
 263  0
         if (!isPostBack()) {
 264  0
             setHost(subscription.getHost());
 265  0
             setUsername(subscription.getUsername());
 266  0
             setPassword(subscription.getPassword());
 267  0
             setType(subscription.getType());
 268  0
             setAutoConnect(subscription.getAutoConnect());
 269  
         }
 270  
 
 271  0
     }
 272  
 
 273  
 
 274  
 }