KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibatis > jpetstore > presentation > AccountBean


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