2009/05/20 - Apache Shale has been retired.
For more information, please explore the Attic.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
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";
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
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
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
136 EditProfileState state = (EditProfileState) getValue("#{dialog.data}");
137 boolean okUsername = true;
138 boolean okPassword = true;
139
140
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
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
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
169 if (!okUsername) {
170 return USERNAME;
171 } else if (!okPassword) {
172 return PASSWORD;
173 }
174
175
176 User user = null;
177 if (state.isCreating()) {
178 user = logic.createUser();
179 } else {
180 user = (User) getBean(getUserKey());
181 }
182
183
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
196 if (state.isCreating()) {
197 logic.insertUser(user);
198 } else {
199 logic.updateUser(user);
200 }
201
202
203
204 if (state.isCreating()) {
205 if (user.isConfirmed()) {
206 getSessionMap().put(getUserKey(),user);
207 } else {
208 info(messages.getMessage("profile.confirm"));
209
210 }
211 }
212
213
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
233 User user = (User) getBean(getUserKey());
234
235
236
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
260 setValue("#{dialog.data}", state);
261 return SUCCESS;
262
263 }
264
265
266 }