KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfox > petstore > action > AccountAction


1 /*
2  * JFox - The most lightweight Java EE Application Server!
3  * more details please visit http://www.huihoo.org/jfox or http://www.jfox.org.cn.
4  *
5  * JFox is licenced and re-distributable under GNU LGPL.
6  */

7 package org.jfox.petstore.action;
8
9 import java.io.IOException JavaDoc;
10 import java.util.ArrayList JavaDoc;
11 import java.util.List JavaDoc;
12 import javax.ejb.EJB JavaDoc;
13 import javax.security.auth.callback.Callback JavaDoc;
14 import javax.security.auth.callback.CallbackHandler JavaDoc;
15 import javax.security.auth.callback.UnsupportedCallbackException JavaDoc;
16
17 import org.jfox.ejb3.security.JAASLoginRequestCallback;
18 import org.jfox.ejb3.security.JAASLoginResponseCallback;
19 import org.jfox.ejb3.security.JAASLoginService;
20 import org.jfox.entity.EntityFactory;
21 import org.jfox.framework.annotation.Inject;
22 import org.jfox.framework.annotation.Service;
23 import org.jfox.mvc.ActionSupport;
24 import org.jfox.mvc.Invocation;
25 import org.jfox.mvc.InvocationContext;
26 import org.jfox.mvc.PageContext;
27 import org.jfox.mvc.SessionContext;
28 import org.jfox.mvc.annotation.ActionMethod;
29 import org.jfox.mvc.validate.StringValidation;
30 import org.jfox.mvc.validate.ValidateException;
31 import org.jfox.petstore.bo.AccountBO;
32 import org.jfox.petstore.bo.CategoryBO;
33 import org.jfox.petstore.entity.Account;
34 import org.jfox.petstore.entity.Category;
35
36 /**
37  * @author <a HREF="mailto:jfox.young@gmail.com">Young Yang</a>
38  */

39 @Service(id = "account")
40 public class AccountAction extends ActionSupport implements CallbackHandler JavaDoc {
41
42     @Inject
43     JAASLoginService loginService;
44
45     @EJB JavaDoc
46     AccountBO accountBO;
47
48     @EJB JavaDoc
49     CategoryBO categoryBO;
50
51     public static final String JavaDoc ACCOUNT_SESSION_KEY = "__ACCOUNT__";
52
53     private static List JavaDoc<String JavaDoc> languages = new ArrayList JavaDoc<String JavaDoc>();
54
55     static {
56         languages.add("English");
57 // languages.add("French");
58
languages.add("Chinese");
59     }
60
61
62     @ActionMethod(successView = "NewAccountForm.vhtml")
63     public void doGetNewAccount(InvocationContext invocationContext) throws Exception JavaDoc {
64         // do nothing
65
PageContext pageContext = invocationContext.getPageContext();
66         pageContext.setAttribute("languages", languages);
67
68         List JavaDoc<Category> categories = categoryBO.getCategoryList();
69         pageContext.setAttribute("categories", categories);
70     }
71
72     @ActionMethod(successView = "index.vhtml", errorView = "NewAccountForm.vhtml", invocationClass = NewAccountInvocation.class)
73     public void doPostCreate(InvocationContext invocationContext) throws Exception JavaDoc{
74         NewAccountInvocation invocation = (NewAccountInvocation)invocationContext.getInvocation();
75         Account newAccount = EntityFactory.newEntityObject(Account.class);
76         newAccount.setUsername(invocation.getUsername());
77         newAccount.setStatus("OK");
78         newAccount.setPassword(invocation.getPassword());
79         newAccount.setAddress1(invocation.getAddress1());
80         newAccount.setAddress2(invocation.getAddress2());
81         newAccount.setBannerOption(invocation.getBannerOption());
82         newAccount.setCity(invocation.getCity());
83         newAccount.setCountry(invocation.getCountry());
84         newAccount.setEmail(invocation.getEmail());
85         newAccount.setFavouriteCategoryId(invocation.getFavouriteCategoryId());
86         newAccount.setFirstName(invocation.getFirstName());
87         newAccount.setLanguagePreference(invocation.getLanguagePreference());
88         newAccount.setLastName(invocation.getLastName());
89         newAccount.setListOption(invocation.getListOption());
90         newAccount.setPassword(invocation.getPassword());
91         newAccount.setPhone(invocation.getPhone());
92         newAccount.setState(invocation.getState());
93         newAccount.setZip(invocation.getZip());
94
95         try {
96             accountBO.insertAccount(newAccount);
97         }
98         catch (Exception JavaDoc e) {
99             // update failed
100
throw e;
101         }
102     }
103
104
105     @ActionMethod(successView = "signon.vhtml")
106     public void doGetSignon(InvocationContext invocationContext) throws Exception JavaDoc {
107         // don't need do anything, just forward to successView
108
}
109
110     @ActionMethod(successView = "index.vhtml", errorView = "signon.vhtml", invocationClass = SignonInvocation.class)
111     public void doPostSignon(InvocationContext invocationContext) throws Exception JavaDoc {
112         SignonInvocation invocation = (SignonInvocation)invocationContext.getInvocation();
113
114         Account account = (Account)loginService.login(invocationContext.getSessionContext(), this, invocation.getUsername(),invocation.getPassword());
115         if (account == null) {
116             String JavaDoc msg = "Invalid username or password. Signon failed";
117             PageContext pageContext = invocationContext.getPageContext();
118             pageContext.setAttribute("errorMessage", msg);
119             throw new Exception JavaDoc(msg);
120         }
121         else {
122             SessionContext sessionContext = invocationContext.getSessionContext();
123             sessionContext.setAttribute(ACCOUNT_SESSION_KEY, account);
124         }
125     }
126
127     /**
128      * JAAS CallbackHandler method
129      */

130     public void handle(Callback JavaDoc[] callbacks) throws IOException JavaDoc, UnsupportedCallbackException JavaDoc {
131         JAASLoginRequestCallback requestCallback = (JAASLoginRequestCallback)callbacks[0];
132         JAASLoginResponseCallback responseCallback = (JAASLoginResponseCallback)callbacks[1];
133
134         // first parameter is username
135
String JavaDoc username = requestCallback.getParams().get(0);
136         // second parameter is password
137
String JavaDoc password = requestCallback.getParams().get(1);
138
139         Account account = accountBO.getAccount(username, password);
140
141         // set callback object, will return by LoginService.login
142
responseCallback.setCallbackObject(account);
143         // set principal name
144
responseCallback.setPrincipalName(username);
145         // set role
146
responseCallback.setRole(username);
147     }
148
149     @ActionMethod(successView = "index.vhtml")
150     public void doGetSignoff(InvocationContext invocationContext) throws Exception JavaDoc {
151         SessionContext sessionContext = invocationContext.getSessionContext();
152         sessionContext.destroy();
153     }
154
155     @ActionMethod(successView = "EditAccount.vhtml", errorView = "signon.vhtml")
156     public void doGetEditAccount(InvocationContext invocationContext) throws Exception JavaDoc {
157         SessionContext sessionContext = invocationContext.getSessionContext();
158         Account account = (Account)sessionContext.getAttribute(ACCOUNT_SESSION_KEY);
159
160         if(account == null) {
161             throw new IllegalArgumentException JavaDoc("Not login, please login first!");
162         }
163
164         PageContext pageContext = invocationContext.getPageContext();
165         pageContext.setAttribute("account", account);
166         pageContext.setAttribute("languages", languages);
167
168         List JavaDoc<Category> categories = categoryBO.getCategoryList();
169         pageContext.setAttribute("categories", categories);
170     }
171
172     @ActionMethod(successView = "index.vhtml", errorView = "EditAccount.vhtml", invocationClass = EditAccountInvocation.class)
173     public void doPostEdit(InvocationContext invocationContext) throws Exception JavaDoc {
174         EditAccountInvocation invocation = (EditAccountInvocation)invocationContext.getInvocation();
175
176         SessionContext sessionContext = invocationContext.getSessionContext();
177         Account account = (Account)sessionContext.getAttribute(ACCOUNT_SESSION_KEY);
178
179         Account newAccount = new Account();
180         newAccount.setBannerName(account.getBannerName());
181         newAccount.setUsername(account.getUsername());
182         newAccount.setStatus(account.getStatus());
183
184         newAccount.setPassword(invocation.getPassword());
185         newAccount.setAddress1(invocation.getAddress1());
186         newAccount.setAddress2(invocation.getAddress2());
187         newAccount.setBannerOption(invocation.getBannerOption());
188         newAccount.setCity(invocation.getCity());
189         newAccount.setCountry(invocation.getCountry());
190         newAccount.setEmail(invocation.getEmail());
191         newAccount.setFavouriteCategoryId(invocation.getFavouriteCategoryId());
192         newAccount.setFirstName(invocation.getFirstName());
193         newAccount.setLanguagePreference(invocation.getLanguagePreference());
194         newAccount.setLastName(invocation.getLastName());
195         newAccount.setListOption(invocation.getListOption());
196         newAccount.setPassword(invocation.getPassword());
197         newAccount.setPhone(invocation.getPhone());
198         newAccount.setState(invocation.getState());
199         newAccount.setZip(invocation.getZip());
200
201         try {
202             accountBO.updateAccount(newAccount);
203             sessionContext.setAttribute(ACCOUNT_SESSION_KEY, newAccount);
204         }
205         catch (Exception JavaDoc e) {
206             // update failed
207
throw e;
208         }
209     }
210
211     /**
212      * 在 doPostEdit ?生异常时,通过该方法设置 PageContext account,
213      * 以便跳转到 errorView 时,?以预设数?
214      *
215      * @param invocationContext invocationContext
216      */

217     protected void doActionFailed(InvocationContext invocationContext) {
218         if (invocationContext.getActionMethod().getName().equals("doPostEdit")) {
219             SessionContext sessionContext = invocationContext.getSessionContext();
220             Account account = (Account)sessionContext.getAttribute(ACCOUNT_SESSION_KEY);
221
222             PageContext pageContext = invocationContext.getPageContext();
223             pageContext.setAttribute("account", account);
224             pageContext.setAttribute("languages", languages);
225
226             List JavaDoc<Category> categories = categoryBO.getCategoryList();
227             pageContext.setAttribute("categories", categories);
228         }
229         else if(invocationContext.getActionMethod().getName().equals("doPostCreate")){
230             NewAccountInvocation invocation = (NewAccountInvocation)invocationContext.getInvocation();
231             Account newAccount = EntityFactory.newEntityObject(Account.class);
232             newAccount.setUsername(invocation.getUsername());
233             newAccount.setStatus("OK");
234             newAccount.setPassword(invocation.getPassword());
235             newAccount.setAddress1(invocation.getAddress1());
236             newAccount.setAddress2(invocation.getAddress2());
237             newAccount.setBannerOption(invocation.getBannerOption());
238             newAccount.setCity(invocation.getCity());
239             newAccount.setCountry(invocation.getCountry());
240             newAccount.setEmail(invocation.getEmail());
241             newAccount.setFavouriteCategoryId(invocation.getFavouriteCategoryId());
242             newAccount.setFirstName(invocation.getFirstName());
243             newAccount.setLanguagePreference(invocation.getLanguagePreference());
244             newAccount.setLastName(invocation.getLastName());
245             newAccount.setListOption(invocation.getListOption());
246             newAccount.setPassword(invocation.getPassword());
247             newAccount.setPhone(invocation.getPhone());
248             newAccount.setState(invocation.getState());
249             newAccount.setZip(invocation.getZip());
250             PageContext pageContext = invocationContext.getPageContext();
251             pageContext.setAttribute("account", newAccount);
252             try {
253                 doGetNewAccount(invocationContext);
254             }
255             catch(Exception JavaDoc e) {
256                 logger.error("doActionFailed error.", e);
257             }
258         }
259     }
260
261     public static class SignonInvocation extends Invocation {
262         @StringValidation(minLength = 4, nullable = false)
263         private String JavaDoc username;
264         
265         @StringValidation(minLength = 4, nullable = false)
266         private String JavaDoc password;
267
268         public String JavaDoc getPassword() {
269             return password;
270         }
271
272         public void setPassword(String JavaDoc password) {
273             this.password = password;
274         }
275
276         public String JavaDoc getUsername() {
277             return username;
278         }
279
280         public void setUsername(String JavaDoc username) {
281             this.username = username;
282         }
283     }
284
285     public static class EditAccountInvocation extends Invocation {
286
287         @StringValidation(minLength = 4, nullable = false)
288         private String JavaDoc password;
289
290         private String JavaDoc repeatpassword;
291
292         private String JavaDoc email;
293
294         private String JavaDoc firstName;
295
296         private String JavaDoc lastName;
297
298         private String JavaDoc address1;
299
300         private String JavaDoc address2;
301
302         private String JavaDoc city;
303
304         private String JavaDoc state;
305
306         private String JavaDoc zip;
307
308         private String JavaDoc country;
309
310         private String JavaDoc phone;
311
312         private String JavaDoc favouriteCategoryId;
313
314         private String JavaDoc languagePreference;
315
316         private int listOption;
317
318         private int bannerOption;
319
320         public String JavaDoc getAddress1() {
321             return address1;
322         }
323
324         public void setAddress1(String JavaDoc address1) {
325             this.address1 = address1;
326         }
327
328         public String JavaDoc getAddress2() {
329             return address2;
330         }
331
332         public void setAddress2(String JavaDoc address2) {
333             this.address2 = address2;
334         }
335
336         public int getBannerOption() {
337             return bannerOption;
338         }
339
340         public void setBannerOption(int bannerOption) {
341             this.bannerOption = bannerOption;
342         }
343
344         public String JavaDoc getCity() {
345             return city;
346         }
347
348         public void setCity(String JavaDoc city) {
349             this.city = city;
350         }
351
352         public String JavaDoc getCountry() {
353             return country;
354         }
355
356         public void setCountry(String JavaDoc country) {
357             this.country = country;
358         }
359
360         public String JavaDoc getEmail() {
361             return email;
362         }
363
364         public void setEmail(String JavaDoc email) {
365             this.email = email;
366         }
367
368         public String JavaDoc getFavouriteCategoryId() {
369             return favouriteCategoryId;
370         }
371
372         public void setFavouriteCategoryId(String JavaDoc favouriteCategoryId) {
373             this.favouriteCategoryId = favouriteCategoryId;
374         }
375
376         public String JavaDoc getFirstName() {
377             return firstName;
378         }
379
380         public void setFirstName(String JavaDoc firstName) {
381             this.firstName = firstName;
382         }
383
384         public String JavaDoc getLanguagePreference() {
385             return languagePreference;
386         }
387
388         public void setLanguagePreference(String JavaDoc languagePreference) {
389             this.languagePreference = languagePreference;
390         }
391
392         public String JavaDoc getLastName() {
393             return lastName;
394         }
395
396         public void setLastName(String JavaDoc lastName) {
397             this.lastName = lastName;
398         }
399
400         public int getListOption() {
401             return listOption;
402         }
403
404         public void setListOption(int listOption) {
405             this.listOption = listOption;
406         }
407
408         public String JavaDoc getPassword() {
409             return password;
410         }
411
412         public void setPassword(String JavaDoc password) {
413             this.password = password;
414         }
415
416         public String JavaDoc getPhone() {
417             return phone;
418         }
419
420         public void setPhone(String JavaDoc phone) {
421             this.phone = phone;
422         }
423
424         public String JavaDoc getState() {
425             return state;
426         }
427
428         public void setState(String JavaDoc state) {
429             this.state = state;
430         }
431
432         public String JavaDoc getZip() {
433             return zip;
434         }
435
436         public void setZip(String JavaDoc zip) {
437             this.zip = zip;
438         }
439
440         public String JavaDoc getRepeatpassword() {
441             return repeatpassword;
442         }
443
444         public void setRepeatpassword(String JavaDoc repeatpassword) {
445             this.repeatpassword = repeatpassword;
446         }
447
448         public void validateAll() throws ValidateException {
449             //验?密?是?一致
450
if (!getPassword().equals(getRepeatpassword())) {
451                 throw new ValidateException("password twice input are different.", "password", getPassword());
452             }
453         }
454
455     }
456
457     public static class NewAccountInvocation extends EditAccountInvocation {
458         private String JavaDoc username;
459
460         public String JavaDoc getUsername() {
461             return username;
462         }
463
464         public void setUsername(String JavaDoc username) {
465             this.username = username;
466         }
467     }
468
469     public static void main(String JavaDoc[] args) {
470
471     }
472 }
473
Popular Tags