KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > samples > jpetstore > web > spring > AccountFormController


1 package org.springframework.samples.jpetstore.web.spring;
2
3 import java.util.HashMap JavaDoc;
4 import java.util.Map JavaDoc;
5
6 import javax.servlet.http.HttpServletRequest JavaDoc;
7 import javax.servlet.http.HttpServletResponse JavaDoc;
8
9 import org.springframework.beans.support.PagedListHolder;
10 import org.springframework.dao.DataIntegrityViolationException;
11 import org.springframework.samples.jpetstore.domain.Account;
12 import org.springframework.samples.jpetstore.domain.logic.PetStoreFacade;
13 import org.springframework.validation.BindException;
14 import org.springframework.validation.ValidationUtils;
15 import org.springframework.web.servlet.ModelAndView;
16 import org.springframework.web.servlet.mvc.SimpleFormController;
17 import org.springframework.web.util.WebUtils;
18
19 /**
20  * @author Juergen Hoeller
21  * @since 01.12.2003
22  */

23 public class AccountFormController extends SimpleFormController {
24
25     public static final String JavaDoc[] LANGUAGES = {"english", "japanese"};
26
27     private PetStoreFacade petStore;
28
29     public AccountFormController() {
30         setSessionForm(true);
31         setValidateOnBinding(false);
32         setCommandName("accountForm");
33         setFormView("EditAccountForm");
34     }
35
36     public void setPetStore(PetStoreFacade petStore) {
37         this.petStore = petStore;
38     }
39
40     protected Object JavaDoc formBackingObject(HttpServletRequest JavaDoc request) throws Exception JavaDoc {
41         UserSession userSession = (UserSession) WebUtils.getSessionAttribute(request, "userSession");
42         if (userSession != null) {
43             return new AccountForm(this.petStore.getAccount(userSession.getAccount().getUsername()));
44         }
45         else {
46             return new AccountForm();
47         }
48     }
49
50     protected void onBindAndValidate(HttpServletRequest JavaDoc request, Object JavaDoc command, BindException errors)
51             throws Exception JavaDoc {
52
53         AccountForm accountForm = (AccountForm) command;
54         Account account = accountForm.getAccount();
55
56         if (request.getParameter("account.listOption") == null) {
57             account.setListOption(false);
58         }
59         if (request.getParameter("account.bannerOption") == null) {
60             account.setBannerOption(false);
61         }
62
63         errors.setNestedPath("account");
64         getValidator().validate(account, errors);
65         errors.setNestedPath("");
66
67         if (accountForm.isNewAccount()) {
68             account.setStatus("OK");
69             ValidationUtils.rejectIfEmpty(errors, "account.username", "USER_ID_REQUIRED", "User ID is required.");
70             if (account.getPassword() == null || account.getPassword().length() < 1 ||
71                     !account.getPassword().equals(accountForm.getRepeatedPassword())) {
72              errors.reject("PASSWORD_MISMATCH",
73                      "Passwords did not match or were not provided. Matching passwords are required.");
74             }
75         }
76         else if (account.getPassword() != null && account.getPassword().length() > 0) {
77           if (!account.getPassword().equals(accountForm.getRepeatedPassword())) {
78                 errors.reject("PASSWORD_MISMATCH",
79                         "Passwords did not match. Matching passwords are required.");
80           }
81       }
82     }
83
84     protected Map JavaDoc referenceData(HttpServletRequest JavaDoc request) throws Exception JavaDoc {
85         Map JavaDoc model = new HashMap JavaDoc();
86         model.put("languages", LANGUAGES);
87         model.put("categories", this.petStore.getCategoryList());
88         return model;
89     }
90
91     protected ModelAndView onSubmit(
92             HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, Object JavaDoc command, BindException errors)
93             throws Exception JavaDoc {
94
95         AccountForm accountForm = (AccountForm) command;
96         try {
97             if (accountForm.isNewAccount()) {
98                 this.petStore.insertAccount(accountForm.getAccount());
99             }
100             else {
101                 this.petStore.updateAccount(accountForm.getAccount());
102             }
103         }
104         catch (DataIntegrityViolationException ex) {
105             errors.rejectValue("account.username", "USER_ID_ALREADY_EXISTS",
106                     "User ID already exists: choose a different ID.");
107             return showForm(request, response, errors);
108         }
109         
110         UserSession userSession = new UserSession(this.petStore.getAccount(accountForm.getAccount().getUsername()));
111         PagedListHolder myList = new PagedListHolder(
112                 this.petStore.getProductListByCategory(accountForm.getAccount().getFavouriteCategoryId()));
113         myList.setPageSize(4);
114         userSession.setMyList(myList);
115         request.getSession().setAttribute("userSession", userSession);
116         return super.onSubmit(request, response, command, errors);
117     }
118
119 }
120
Popular Tags