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