1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.mybatis.jpetstore.web.actions;
17
18 import java.util.Arrays;
19 import java.util.Collections;
20 import java.util.List;
21
22 import javax.servlet.http.HttpSession;
23
24 import net.sourceforge.stripes.action.DefaultHandler;
25 import net.sourceforge.stripes.action.ForwardResolution;
26 import net.sourceforge.stripes.action.RedirectResolution;
27 import net.sourceforge.stripes.action.Resolution;
28 import net.sourceforge.stripes.action.SessionScope;
29 import net.sourceforge.stripes.integration.spring.SpringBean;
30 import net.sourceforge.stripes.validation.Validate;
31
32 import org.mybatis.jpetstore.domain.Account;
33 import org.mybatis.jpetstore.domain.Product;
34 import org.mybatis.jpetstore.service.AccountService;
35 import org.mybatis.jpetstore.service.CatalogService;
36
37
38
39
40
41
42 @SessionScope
43 public class AccountActionBean extends AbstractActionBean {
44
45 private static final long serialVersionUID = 5499663666155758178L;
46
47 private static final String NEW_ACCOUNT = "/WEB-INF/jsp/account/NewAccountForm.jsp";
48 private static final String EDIT_ACCOUNT = "/WEB-INF/jsp/account/EditAccountForm.jsp";
49 private static final String SIGNON = "/WEB-INF/jsp/account/SignonForm.jsp";
50
51 private static final List<String> LANGUAGE_LIST;
52 private static final List<String> CATEGORY_LIST;
53
54 @SpringBean
55 private transient AccountService accountService;
56 @SpringBean
57 private transient CatalogService catalogService;
58
59 private Account account = new Account();
60 private List<Product> myList;
61 private boolean authenticated;
62
63 static {
64 LANGUAGE_LIST = Collections.unmodifiableList(Arrays.asList("english", "japanese"));
65 CATEGORY_LIST = Collections.unmodifiableList(Arrays.asList("FISH", "DOGS", "REPTILES", "CATS", "BIRDS"));
66 }
67
68 public Account getAccount() {
69 return this.account;
70 }
71
72 public String getUsername() {
73 return account.getUsername();
74 }
75
76 @Validate(required = true, on = { "signon", "newAccount", "editAccount" })
77 public void setUsername(String username) {
78 account.setUsername(username);
79 }
80
81 public String getPassword() {
82 return account.getPassword();
83 }
84
85 @Validate(required = true, on = { "signon", "newAccount", "editAccount" })
86 public void setPassword(String password) {
87 account.setPassword(password);
88 }
89
90 public List<Product> getMyList() {
91 return myList;
92 }
93
94 public void setMyList(List<Product> myList) {
95 this.myList = myList;
96 }
97
98 public List<String> getLanguages() {
99 return LANGUAGE_LIST;
100 }
101
102 public List<String> getCategories() {
103 return CATEGORY_LIST;
104 }
105
106 public Resolution newAccountForm() {
107 return new ForwardResolution(NEW_ACCOUNT);
108 }
109
110
111
112
113
114
115 public Resolution newAccount() {
116 accountService.insertAccount(account);
117 account = accountService.getAccount(account.getUsername());
118 myList = catalogService.getProductListByCategory(account.getFavouriteCategoryId());
119 authenticated = true;
120 return new RedirectResolution(CatalogActionBean.class);
121 }
122
123
124
125
126
127
128 public Resolution editAccountForm() {
129 return new ForwardResolution(EDIT_ACCOUNT);
130 }
131
132
133
134
135
136
137 public Resolution editAccount() {
138 accountService.updateAccount(account);
139 account = accountService.getAccount(account.getUsername());
140 myList = catalogService.getProductListByCategory(account.getFavouriteCategoryId());
141 return new RedirectResolution(CatalogActionBean.class);
142 }
143
144
145
146
147
148
149 @DefaultHandler
150 public Resolution signonForm() {
151 return new ForwardResolution(SIGNON);
152 }
153
154
155
156
157
158
159 public Resolution signon() {
160
161 account = accountService.getAccount(getUsername(), getPassword());
162
163 if (account == null) {
164 String value = "Invalid username or password. Signon failed.";
165 setMessage(value);
166 clear();
167 return new ForwardResolution(SIGNON);
168 } else {
169 account.setPassword(null);
170 myList = catalogService.getProductListByCategory(account.getFavouriteCategoryId());
171 authenticated = true;
172 HttpSession s = context.getRequest().getSession();
173
174 s.setAttribute("accountBean", this);
175 return new RedirectResolution(CatalogActionBean.class);
176 }
177 }
178
179
180
181
182
183
184 public Resolution signoff() {
185 context.getRequest().getSession().invalidate();
186 clear();
187 return new RedirectResolution(CatalogActionBean.class);
188 }
189
190
191
192
193
194
195 public boolean isAuthenticated() {
196 return authenticated && account != null && account.getUsername() != null;
197 }
198
199
200
201
202 public void clear() {
203 account = new Account();
204 myList = null;
205 authenticated = false;
206 }
207
208 }