1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.ibatis.jpetstore.presentation;
18
19 import com.ibatis.common.util.PaginatedList;
20 import com.ibatis.jpetstore.domain.Account;
21 import com.ibatis.jpetstore.service.AccountService;
22 import com.ibatis.jpetstore.service.CatalogService;
23 import com.ibatis.struts.ActionContext;
24 import com.ibatis.struts.BaseBean;
25 import com.ibatis.struts.BeanActionException;
26
27 import java.util.ArrayList;
28 import java.util.List;
29 import java.util.Collections;
30
31 public class AccountBean extends BaseBean {
32
33
34
35 private static final AccountService accountService = AccountService.getInstance();
36 private static final CatalogService catalogService = CatalogService.getInstance();
37
38 private static final String VALIDATE_NEW_ACCOUNT = "new";
39 private static final String VALIDATE_EDIT_ACCOUNT = "edit";
40
41 private static final List LANGUAGE_LIST;
42 private static final List CATEGORY_LIST;
43
44
45
46 private Account account;
47 private String repeatedPassword;
48 private String pageDirection;
49 private String validation;
50 private PaginatedList myList;
51 private boolean authenticated;
52 private boolean accountBannerOption;
53 private boolean accountListOption;
54
55
56
57 static {
58 List langList = new ArrayList();
59 langList.add("english");
60 langList.add("japanese");
61 LANGUAGE_LIST = Collections.unmodifiableList(langList);
62
63 List catList = new ArrayList();
64 catList.add("FISH");
65 catList.add("DOGS");
66 catList.add("REPTILES");
67 catList.add("CATS");
68 catList.add("BIRDS");
69 CATEGORY_LIST = Collections.unmodifiableList(catList);
70 }
71
72
73
74 public AccountBean() {
75 account = new Account();
76 }
77
78
79
80 public String getUsername() {
81 return account.getUsername();
82 }
83
84 public void setUsername(String username) {
85 account.setUsername(username);
86 }
87
88 public String getPassword() {
89 return account.getPassword();
90 }
91
92 public void setPassword(String password) {
93 account.setPassword(password);
94 }
95
96 public PaginatedList getMyList() {
97 return myList;
98 }
99
100 public void setMyList(PaginatedList myList) {
101 this.myList = myList;
102 }
103
104 public String getRepeatedPassword() {
105 return repeatedPassword;
106 }
107
108 public void setRepeatedPassword(String repeatedPassword) {
109 this.repeatedPassword = repeatedPassword;
110 }
111
112 public Account getAccount() {
113 return account;
114 }
115
116 public void setAccount(Account account) {
117 this.account = account;
118 if ( account != null ) {
119 setAccountBannerOption(account.isBannerOption());
120 setAccountListOption(account.isListOption());
121 }
122 }
123
124
125 public List getLanguages() {
126 return LANGUAGE_LIST;
127 }
128
129 public List getCategories() {
130 return CATEGORY_LIST;
131 }
132
133 public String getPageDirection() {
134 return pageDirection;
135 }
136
137 public void setPageDirection(String pageDirection) {
138 this.pageDirection = pageDirection;
139 }
140
141 public String getValidation() {
142 return validation;
143 }
144
145 public void setValidation(String validation) {
146 this.validation = validation;
147 }
148
149 public boolean isAccountBannerOption() {
150 return accountBannerOption;
151 }
152
153 public void setAccountBannerOption(boolean bannerOption) {
154 this.accountBannerOption = bannerOption;
155 }
156
157 public boolean isAccountListOption() {
158 return accountListOption;
159 }
160
161 public void setAccountListOption(boolean listOption) {
162 this.accountListOption = listOption;
163 }
164
165
166
167 public String newAccount() {
168 try {
169 account.setBannerOption(isAccountBannerOption());
170 account.setListOption(isAccountListOption());
171 accountService.insertAccount(account);
172 setAccount(accountService.getAccount(account.getUsername()));
173 myList = catalogService.getProductListByCategory(account.getFavouriteCategoryId());
174 authenticated = true;
175 repeatedPassword = null;
176 return "success";
177 } catch (Exception e) {
178 throw new BeanActionException ("There was a problem creating your Account Information. Cause: " + e, e);
179 }
180 }
181
182 public String editAccountForm() {
183 try {
184 setAccount(accountService.getAccount(account.getUsername()));
185 return "success";
186 } catch (Exception e) {
187 throw new BeanActionException ("There was a problem retrieving your Account Information. Cause: "+e, e);
188 }
189 }
190
191 public String editAccount() {
192 try {
193 account.setBannerOption(isAccountBannerOption());
194 account.setListOption(isAccountListOption());
195 accountService.updateAccount(account);
196 setAccount(accountService.getAccount(account.getUsername()));
197 myList = catalogService.getProductListByCategory(account.getFavouriteCategoryId());
198 return "success";
199 } catch (Exception e) {
200 throw new BeanActionException ("There was a problem updating your Account Information. Cause: "+e, e);
201 }
202 }
203
204 public String switchMyListPage () {
205 if ("next".equals(pageDirection)) {
206 myList.nextPage();
207 } else if ("previous".equals(pageDirection)) {
208 myList.previousPage();
209 }
210 return "success";
211 }
212
213 public String signon() {
214
215 setAccount(accountService.getAccount(account.getUsername(), account.getPassword()));
216
217 if (account == null || account == null) {
218 ActionContext.getActionContext().setSimpleMessage("Invalid username or password. Signon failed.");
219 clear();
220 return "failure";
221 } else {
222 account.setPassword(null);
223
224 myList = catalogService.getProductListByCategory(account.getFavouriteCategoryId());
225
226 authenticated = true;
227
228 return "success";
229 }
230 }
231
232 public String signoff() {
233 ActionContext.getActionContext().getRequest().getSession().invalidate();
234 clear();
235 return "success";
236 }
237
238 public boolean isAuthenticated() {
239 return authenticated && account != null && account.getUsername() != null;
240 }
241
242 public void reset() {
243 if (account != null) {
244 setAccountBannerOption(false);
245 setAccountListOption(false);
246 }
247 }
248
249 public void clear() {
250 setAccount(new Account());
251 repeatedPassword = null;
252 pageDirection = null;
253 myList = null;
254 authenticated = false;
255 }
256
257 public void validate() {
258 ActionContext ctx = ActionContext.getActionContext();
259 if (validation != null) {
260 if (VALIDATE_EDIT_ACCOUNT.equals(validation) || VALIDATE_NEW_ACCOUNT.equals(validation)) {
261 if (VALIDATE_NEW_ACCOUNT.equals(validation)) {
262 account.setStatus("OK");
263 validateRequiredField(account.getUsername(), "User ID is required.");
264 if (account.getPassword() == null || account.getPassword().length() < 1 || !account.getPassword().equals(repeatedPassword)) {
265 ctx.addSimpleError("Passwords did not match or were not provided. Matching passwords are required.");
266 }
267 }
268 if (account.getPassword() != null && account.getPassword().length() > 0) {
269 if (!account.getPassword().equals(repeatedPassword)) {
270 ctx.addSimpleError("Passwords did not match.");
271 }
272 }
273 validateRequiredField(account.getFirstName(), "First name is required.");
274 validateRequiredField(account.getLastName(), "Last name is required.");
275 validateRequiredField(account.getEmail(), "Email address is required.");
276 validateRequiredField(account.getPhone(), "Phone number is required.");
277 validateRequiredField(account.getAddress1(), "Address (1) is required.");
278 validateRequiredField(account.getCity(), "City is required.");
279 validateRequiredField(account.getState(), "State is required.");
280 validateRequiredField(account.getZip(), "ZIP is required.");
281 validateRequiredField(account.getCountry(), "Country is required.");
282 }
283 }
284
285 }
286
287 }