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  
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  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      private Logic logic = null;
56  
57  
58      /***
59       * <p>Return the business {@link Logic} for this application.</p>
60       */
61      public Logic getLogic() {
62          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          this.logic = logic;
73      }
74  
75  
76      /***
77       * <p>The transaction mode for the current request (CREATE or EDIT).
78       * Default mode is EDIT.</p>
79       */
80      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          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          this.mode = mode;
98      }
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     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         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         this.state = state;
123     }
124 
125 
126     /***
127      * <p>The <code>User</code> instance being created or edited.</p>
128      */
129     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         return this.user;
137     }
138 
139 
140     // -------------------------------------------------- Input Field Properties
141 
142 
143     /***
144      * <p>The specified password.</p>
145      */
146     private String password = null;
147 
148 
149     /***
150      * <p>Return the specified password.</p>
151      */
152     public String getPassword() {
153         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         this.password = password;
164     }
165 
166 
167     /***
168      * <p>The confirmation password.</p>
169      */
170     private String password2 = null;
171 
172 
173     /***
174      * <p>Return the confirmation password.</p>
175      */
176     public String getPassword2() {
177         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         this.password2 = password2;
188     }
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         log("Master.init(" + isPostBack() + ")");
200 
201         String mode = getRequestParameter("mode");
202         if ((mode != null) && (mode.length() > 0)) {
203             setMode(mode);
204         }
205 
206     }
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         log("Master.preprocess(" + isPostBack() + ")");
216 
217         String mode = (String) retrieveData(MODE_KEY);
218         if (mode != null) {
219             setMode(mode);
220         }
221 
222         User user = (User) retrieveData(USER_KEY);
223         if (user != null) {
224             this.user = user;
225         } else {
226             setup();
227         }
228 
229     }
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         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         if (getUser() == null) {
243             setup();
244         }
245 
246         // Cache the current transaction mode and user instance
247         saveData(MODE_KEY, getMode());
248         saveData(USER_KEY, getUser());
249 
250     }
251 
252 
253     /***
254      * <p>Release resources used during this request.</p>
255      */
256     public void destroy() {
257 
258         log("Master.destroy(" + isPostBack() + ")");
259 
260         ; // FIXME - destroy()
261 
262     }
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         if (Constants.CREATE_MODE.equals(getMode())) {
275             return "welcome";
276         } else {
277             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         if (Constants.CREATE_MODE.equals(getMode())) {
289             try {
290                 User existing = getLogic().findUserByUsername(getUser().getUsername());
291                 if (existing != null) {
292                     error("Username '" + getUser().getUsername() + "' is already in use, please try again");
293                     return null;
294                 }
295             } catch (Exception e) {
296                 error("Exception checking unique username: " + e);
297                 log("Exception checking unique username", e);
298                 return null;
299             }
300         }
301 
302         // Special handling for password and password2 values
303         boolean ok = true;
304         if (Constants.CREATE_MODE.equals(getMode())) {
305             // In CREATE mode, both passwords are required and must match
306             if ((getPassword() == null)
307                 || (getPassword().length() == 0)) {
308                 error("Password is required");
309                 ok = false;
310             }
311             if ((getPassword2() == null)
312                 || (getPassword2().length() == 0)) {
313                 error("Password 2 is required");
314                 ok = false;
315             }
316             if ((getPassword() != null)
317                 && (getPassword2() != null)
318                 && !getPassword().equals(getPassword2())) {
319                 error("Password and Password 2 values do not match");
320                 ok = false;
321             }
322         } else {
323             // In EDIT mode, confirmation password is required and must
324             // match, if specified password was entered
325             if ((getPassword() != null)
326                 && (getPassword().length() > 0)) {
327                 if ((getPassword2() == null)
328                     || (getPassword2().length() == 0)) {
329                     error("Password 2 is required and must match Password");
330                     ok = false;
331                 } else if (!getPassword().equals(getPassword2())) {
332                     error("Password 2 and Password values do not match");
333                     ok = false;
334                 }
335             }
336         }
337         if (!ok) {
338             return null;
339         }
340 
341         // Copy the specified password if it was entered (which will always
342         // be true in CREATE mode)
343         if ((getPassword() != null)
344             && (getPassword().length() > 0)) {
345             user.setPassword(getPassword());
346         }
347 
348         try {
349 
350             if (Constants.CREATE_MODE.equals(getMode())) {
351                 getLogic().createUser(getUser());
352             } else {
353                 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             getState().setUser(user);
359 
360         } catch (Exception e) {
361 
362             error("Exception saving user information: " + e.getMessage());
363             log("Exception saving user information", e);
364             return null;
365         }
366 
367         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         User current = getState().getUser();
383         if (current == null) {
384             this.user = new User();
385             return;
386         } else {
387             this.user = getLogic().findUserById(current.getId());
388         }
389 
390     }
391 
392 
393 }