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
46 private static final long serialVersionUID = 5499663666155758178L;
47
48
49 private static final String NEW_ACCOUNT = "/WEB-INF/jsp/account/NewAccountForm.jsp";
50
51 private static final String EDIT_ACCOUNT = "/WEB-INF/jsp/account/EditAccountForm.jsp";
52
53 private static final String SIGNON = "/WEB-INF/jsp/account/SignonForm.jsp";
54
55
56 private static final List<String> LANGUAGE_LIST;
57
58 private static final List<String> CATEGORY_LIST;
59
60
61 @SpringBean
62 private transient AccountService accountService;
63
64 @SpringBean
65 private transient CatalogService catalogService;
66
67
68 private Account account = new Account();
69
70 private List<Product> myList;
71
72 private boolean authenticated;
73
74 static {
75 LANGUAGE_LIST = Collections.unmodifiableList(Arrays.asList("english", "japanese"));
76 CATEGORY_LIST = Collections.unmodifiableList(Arrays.asList("FISH", "DOGS", "REPTILES", "CATS", "BIRDS"));
77 }
78
79
80
81
82
83
84 public Account getAccount() {
85 return this.account;
86 }
87
88
89
90
91
92
93 public String getUsername() {
94 return account.getUsername();
95 }
96
97
98
99
100
101
102
103 @Validate(required = true, on = { "signon", "newAccount", "editAccount" })
104 public void setUsername(String username) {
105 account.setUsername(username);
106 }
107
108
109
110
111
112
113 public String getPassword() {
114 return account.getPassword();
115 }
116
117
118
119
120
121
122
123 @Validate(required = true, on = { "signon", "newAccount", "editAccount" })
124 public void setPassword(String password) {
125 account.setPassword(password);
126 }
127
128
129
130
131
132
133 public List<Product> getMyList() {
134 return myList;
135 }
136
137
138
139
140
141
142
143 public void setMyList(List<Product> myList) {
144 this.myList = myList;
145 }
146
147
148
149
150
151
152 public List<String> getLanguages() {
153 return LANGUAGE_LIST;
154 }
155
156
157
158
159
160
161 public List<String> getCategories() {
162 return CATEGORY_LIST;
163 }
164
165
166
167
168
169
170 public Resolution newAccountForm() {
171 return new ForwardResolution(NEW_ACCOUNT);
172 }
173
174
175
176
177
178
179 public Resolution newAccount() {
180 accountService.insertAccount(account);
181 account = accountService.getAccount(account.getUsername());
182 myList = catalogService.getProductListByCategory(account.getFavouriteCategoryId());
183 authenticated = true;
184 return new RedirectResolution(CatalogActionBean.class);
185 }
186
187
188
189
190
191
192 public Resolution editAccountForm() {
193 return new ForwardResolution(EDIT_ACCOUNT);
194 }
195
196
197
198
199
200
201 public Resolution editAccount() {
202 accountService.updateAccount(account);
203 account = accountService.getAccount(account.getUsername());
204 myList = catalogService.getProductListByCategory(account.getFavouriteCategoryId());
205 return new RedirectResolution(CatalogActionBean.class);
206 }
207
208
209
210
211
212
213 @DefaultHandler
214 public Resolution signonForm() {
215 return new ForwardResolution(SIGNON);
216 }
217
218
219
220
221
222
223 public Resolution signon() {
224
225 account = accountService.getAccount(getUsername(), getPassword());
226
227 if (account == null) {
228 String value = "Invalid username or password. Signon failed.";
229 setMessage(value);
230 clear();
231 return new ForwardResolution(SIGNON);
232 } else {
233 account.setPassword(null);
234 myList = catalogService.getProductListByCategory(account.getFavouriteCategoryId());
235 authenticated = true;
236 HttpSession s = context.getRequest().getSession();
237
238 s.setAttribute("accountBean", this);
239 return new RedirectResolution(CatalogActionBean.class);
240 }
241 }
242
243
244
245
246
247
248 public Resolution signoff() {
249 context.getRequest().getSession().invalidate();
250 clear();
251 return new RedirectResolution(CatalogActionBean.class);
252 }
253
254
255
256
257
258
259 public boolean isAuthenticated() {
260 return authenticated && account != null && account.getUsername() != null;
261 }
262
263
264
265
266 public void clear() {
267 account = new Account();
268 myList = null;
269 authenticated = false;
270 }
271
272 }