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.usecases.profile;
19  
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  import org.apache.shale.usecases.logic.LogonLogic;
23  import org.apache.shale.usecases.model.User;
24  import org.apache.shale.util.Messages;
25  import org.apache.shale.view.AbstractFacesBean;
26  
27  /***
28   * <p>Action methods for the Edit Profile dialog.</p>
29   *
30   * $Id: EditProfileActions.java 464373 2006-10-16 04:21:54Z rahul $
31   */
32  public class EditProfileActions extends AbstractFacesBean {
33      
34  
35      // -------------------------------------------------------- Static Variables
36  
37  
38      /***
39       * <p>Log instance for this class.</p>
40       */
41      private static final Log log = LogFactory.getLog(EditProfileActions.class);
42  
43  
44      /***
45       * <p>Message resources for this application.</p>
46       */
47      private static final Messages messages =
48        new Messages("org.apache.shale.usecases.view.Bundle");
49  
50  
51      // ------------------------------------------------------ Manifest Constants
52  
53  
54      /***
55       * <p>Managed bean name under which the business logic bean instance
56       * for this dialog is stored.</p>
57       */
58      static final String LOGIC_BEAN = "profile$logic"; // FIXME - shared name
59  
60  
61      /***
62       * <p>Logical outcome indicating a problem with the password field.</p>
63       */
64      static final String PASSWORD = "password";
65  
66  
67      /***
68       * <p>Logical outcome indicating successful completion.</p>
69       */
70      static final String SUCCESS = "success";
71  
72  
73      /***
74       * <p>Logical outcome indicating a problem with the username field.</p>
75       */
76      static final String USERNAME = "username";
77  
78  
79  
80      // --------------------------------------------------- Configured Properties
81  
82  
83      /***
84       * <p>Flag indicating that a confirmation email is required before a new
85       * profile may be activated.</p>
86       */
87      private boolean confirmation = false;
88      public boolean isConfirmation() { return this.confirmation; }
89      public void setConfirmation(boolean confirmation) { this.confirmation = confirmation; }
90  
91  
92      /***
93       * <p>Session scope attribute under which a {@link User} instance for the
94       * currently logged in user is stored.</p>
95       */
96      private String userKey = "user";
97      public String getUserKey() { return this.userKey; }
98      public void setUserKey(String userKey) { this.userKey = userKey; }
99  
100 
101     // ----------------------------------------------------------------- Actions
102 
103 
104     /***
105      * <p>Perform any processing necessary to cancel an edit profile dialog.</p>
106      *
107      * <p>The following logical outcome values are returned:</p>
108      * <ul>
109      * <li><code>SUCCESS</code> - Proceed to the next state normally.</li>
110      * </ul>
111      */
112     public String cancel() {
113 
114         return SUCCESS;
115 
116     }
117 
118 
119     /***
120      * <p>Finish processing by creating or updating the appropriate user
121      * profile.</p>
122      *
123      * <p>The following logical outcome values are returned:</p>
124      * <ul>
125      * <li><code>PASSWORD</code> - One or both password values were entered,
126      *     but they do not match (or neither were entered on a create)</li>
127      * <li><code>SUCCESS</code> - Creation or update succeeded, proceed to
128      *     the next state normally.</li>
129      * <li><code>USERNAME</code> - Missing or dupicate username on
130      *     a create.</li>
131      * </ul>
132      */
133     public String finish() {
134 
135         // Acquire our state information
136         EditProfileState state = (EditProfileState) getValue("#{dialog.data}");
137         boolean okUsername = true;
138         boolean okPassword = true;
139 
140         // Validate password match if both specified
141         if ((state.getPassword() != null) && (state.getPassword().length() > 0) &&
142             (state.getPassword2() != null) && (state.getPassword2().length() > 0)) {
143             if (!state.getPassword().equals(state.getPassword2())) {
144                 error(messages.getMessage("profile.mismatch"));
145                 okPassword = false;
146             }
147         }
148 
149         // Validate required fields if creating
150         if (state.isCreating()) {
151             if ((state.getUsername() == null) || (state.getUsername().length() < 1)) {
152                 error(messages.getMessage("profile.username"));
153                 okUsername = false;
154             }
155             if ((state.getPassword() == null) || (state.getPassword().length() < 1)) {
156                 error(messages.getMessage("profile.password"));
157                 okPassword = false;
158             }
159         }
160 
161         // Validate duplicate username if creating
162         LogonLogic logic = (LogonLogic) getBean(LOGIC_BEAN);
163         if (state.isCreating() && (logic.findUser(state.getUsername()) != null)) {
164             error(messages.getMessage("profile.duplicate"));
165             okUsername = false;
166         }
167 
168         // Return appropriate outcome on validation failures
169         if (!okUsername) {
170             return USERNAME;
171         } else if (!okPassword) {
172             return PASSWORD;
173         }
174 
175         // Create or acquire our User instance
176         User user = null;
177         if (state.isCreating()) {
178             user = logic.createUser();
179         } else {
180             user = (User) getBean(getUserKey());
181         }
182 
183         // Update to reflect changes during this dialog
184         user.setCategories(state.getCategories());
185         if (state.isCreating()) {
186             user.setConfirmed(!isConfirmation());
187         }
188         user.setEmailAddress(state.getEmailAddress());
189         user.setFullName(state.getFullName());
190         if ((state.getPassword() != null) && (state.getPassword().length() > 0)) {
191             user.setPassword(state.getPassword());
192         }
193         user.setUsername(state.getUsername());
194 
195         // Persist the changes made during this dialog
196         if (state.isCreating()) {
197             logic.insertUser(user);
198         } else {
199             logic.updateUser(user);
200         }
201 
202         // Log in a new user if already confirmed
203         // Otherwise, send the confirmation email
204         if (state.isCreating()) {
205             if (user.isConfirmed()) {
206                 getSessionMap().put(getUserKey(),user);
207             } else {
208                 info(messages.getMessage("profile.confirm"));
209                 // FIXME - send confirmation email
210             }
211         }
212 
213         // Return a success outcome
214         return SUCCESS;
215 
216 
217     }
218 
219 
220     /***
221      * <p>Register an instance of {@link EditProfileState} as the data object
222      * for our dialog.  If there is an existing logged on user, we are editing
223      * the existing profile; otherwise, we are creating a new one.</p>
224      *
225      * <p>The following logical outcome values are returned:</p>
226      * <ul>
227      * <li><code>SUCCESS</code> - Proceed to the next state.</li>
228      * </ul>
229      */
230     public String setup() {
231 
232         // Acquire a reference to the currently logged on user, if there is one
233         User user = (User) getBean(getUserKey());
234 
235         // Configure a state object based on the currently logged on user,
236         // or set up for a newly created profile
237         EditProfileState state = new EditProfileState();
238         if (user == null) {
239             state.setCreating(true);
240         } else {
241             state.setCreating(false);
242             int oldCategories[] = user.getCategories();
243             int categories[] = null;
244             if (oldCategories != null) {
245                 categories = new int[oldCategories.length];
246                 System.arraycopy
247                   (oldCategories, 0, categories, 0, oldCategories.length);
248             } else {
249                 categories = new int[0];
250             }
251             state.setCategories(categories);
252             state.setEmailAddress(user.getEmailAddress());
253             state.setFullName(user.getFullName());
254             state.setPassword(null);
255             state.setPassword2(null);
256             state.setUsername(user.getUsername());
257         }
258 
259         // Register our state as the data object for this dialog, and continue
260         setValue("#{dialog.data}", state);
261         return SUCCESS;
262 
263     }
264 
265 
266 }