View Javadoc

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