001: package com.ibatis.jpetstore.presentation;
002:
003: import java.util.ArrayList;
004: import java.util.Collections;
005: import java.util.List;
006:
007: import org.huihoo.jfox.soaf.container.ServiceFactory;
008:
009: import com.ibatis.common.util.PaginatedList;
010: import com.ibatis.jpetstore.domain.Account;
011: import com.ibatis.jpetstore.service.AccountService;
012: import com.ibatis.jpetstore.service.CatalogService;
013: import com.ibatis.struts.ActionContext;
014: import com.ibatis.struts.BaseBean;
015: import com.ibatis.struts.BeanActionException;
016:
017: public class AccountBean extends BaseBean {
018:
019: /* Constants */
020:
021: private AccountService accountService = (AccountService) ServiceFactory
022: .getInstance().getService(AccountService.class);
023:
024: private CatalogService catalogService = (CatalogService) ServiceFactory
025: .getInstance().getService(CatalogService.class);
026:
027: private static final String VALIDATE_NEW_ACCOUNT = "new";
028:
029: private static final String VALIDATE_EDIT_ACCOUNT = "edit";
030:
031: private static final List LANGUAGE_LIST;
032:
033: private static final List CATEGORY_LIST;
034:
035: /* Private Fields */
036:
037: private Account account;
038:
039: private String repeatedPassword;
040:
041: private String pageDirection;
042:
043: private String validation;
044:
045: private PaginatedList myList;
046:
047: private boolean authenticated;
048:
049: /* Static Initializer */
050:
051: static {
052: List langList = new ArrayList();
053: langList.add("english");
054: langList.add("japanese");
055: LANGUAGE_LIST = Collections.unmodifiableList(langList);
056:
057: List catList = new ArrayList();
058: catList.add("FISH");
059: catList.add("DOGS");
060: catList.add("REPTILES");
061: catList.add("CATS");
062: catList.add("BIRDS");
063: CATEGORY_LIST = Collections.unmodifiableList(catList);
064: }
065:
066: /* Constructors */
067:
068: public AccountBean() {
069: account = new Account();
070: }
071:
072: /* JavaBeans Properties */
073:
074: public String getUsername() {
075: return account.getUsername();
076: }
077:
078: public void setUsername(String username) {
079: account.setUsername(username);
080: }
081:
082: public String getPassword() {
083: return account.getPassword();
084: }
085:
086: public void setPassword(String password) {
087: account.setPassword(password);
088: }
089:
090: public PaginatedList getMyList() {
091: return myList;
092: }
093:
094: public void setMyList(PaginatedList myList) {
095: this .myList = myList;
096: }
097:
098: public String getRepeatedPassword() {
099: return repeatedPassword;
100: }
101:
102: public void setRepeatedPassword(String repeatedPassword) {
103: this .repeatedPassword = repeatedPassword;
104: }
105:
106: public Account getAccount() {
107: return account;
108: }
109:
110: public void setAccount(Account account) {
111: this .account = account;
112: }
113:
114: public List getLanguages() {
115: return LANGUAGE_LIST;
116: }
117:
118: public List getCategories() {
119: return CATEGORY_LIST;
120: }
121:
122: public String getPageDirection() {
123: return pageDirection;
124: }
125:
126: public void setPageDirection(String pageDirection) {
127: this .pageDirection = pageDirection;
128: }
129:
130: public String getValidation() {
131: return validation;
132: }
133:
134: public void setValidation(String validation) {
135: this .validation = validation;
136: }
137:
138: /* Public Methods */
139:
140: public String newAccount() {
141: try {
142: accountService.insertAccount(account);
143: account = accountService.getAccount(account.getUsername());
144: myList = catalogService.getProductListByCategory(account
145: .getFavouriteCategoryId());
146: authenticated = true;
147: repeatedPassword = null;
148: return "success";
149: } catch (Exception e) {
150: throw new BeanActionException(
151: "There was a problem creating your Account Information. Cause: "
152: + e, e);
153: }
154: }
155:
156: public String editAccountForm() {
157: try {
158: account = accountService.getAccount(account.getUsername());
159: return "success";
160: } catch (Exception e) {
161: throw new BeanActionException(
162: "There was a problem retrieving your Account Information. Cause: "
163: + e, e);
164: }
165: }
166:
167: public String editAccount() {
168: try {
169: accountService.updateAccount(account);
170: account = accountService.getAccount(account.getUsername());
171: myList = catalogService.getProductListByCategory(account
172: .getFavouriteCategoryId());
173: return "success";
174: } catch (Exception e) {
175: throw new BeanActionException(
176: "There was a problem updating your Account Information. Cause: "
177: + e, e);
178: }
179: }
180:
181: public String switchMyListPage() {
182: if ("next".equals(pageDirection)) {
183: myList.nextPage();
184: } else if ("previous".equals(pageDirection)) {
185: myList.previousPage();
186: }
187: return "success";
188: }
189:
190: public String signon() {
191:
192: account = accountService.getAccount(account.getUsername(),
193: account.getPassword());
194:
195: if (account == null || account == null) {
196: ActionContext.getActionContext().setSimpleMessage(
197: "Invalid username or password. Signon failed.");
198: clear();
199: return "failure";
200: } else {
201: account.setPassword(null);
202:
203: myList = catalogService.getProductListByCategory(account
204: .getFavouriteCategoryId());
205:
206: authenticated = true;
207:
208: return "success";
209: }
210: }
211:
212: public String signoff() {
213: ActionContext.getActionContext().getRequest().getSession()
214: .invalidate();
215: clear();
216: return "success";
217: }
218:
219: public boolean isAuthenticated() {
220: return authenticated && account != null
221: && account.getUsername() != null;
222: }
223:
224: public void reset() {
225: if (account != null) {
226: account.setBannerOption(false);
227: account.setListOption(false);
228: }
229: }
230:
231: public void clear() {
232: account = new Account();
233: repeatedPassword = null;
234: pageDirection = null;
235: myList = null;
236: authenticated = false;
237: }
238:
239: public void validate() {
240: ActionContext ctx = ActionContext.getActionContext();
241: if (validation != null) {
242: if (VALIDATE_EDIT_ACCOUNT.equals(validation)
243: || VALIDATE_NEW_ACCOUNT.equals(validation)) {
244: if (VALIDATE_NEW_ACCOUNT.equals(validation)) {
245: account.setStatus("OK");
246: validateRequiredField(account.getUsername(),
247: "User ID is required.");
248: if (account.getPassword() == null
249: || account.getPassword().length() < 1
250: || !account.getPassword().equals(
251: repeatedPassword)) {
252: ctx
253: .addSimpleError("Passwords did not match or were not provided. Matching passwords are required.");
254: }
255: }
256: if (account.getPassword() != null
257: && account.getPassword().length() > 0) {
258: if (!account.getPassword().equals(repeatedPassword)) {
259: ctx.addSimpleError("Passwords did not match.");
260: }
261: }
262: validateRequiredField(account.getFirstName(),
263: "First name is required.");
264: validateRequiredField(account.getLastName(),
265: "Last name is required.");
266: validateRequiredField(account.getEmail(),
267: "Email address is required.");
268: validateRequiredField(account.getPhone(),
269: "Phone number is required.");
270: validateRequiredField(account.getAddress1(),
271: "Address (1) is required.");
272: validateRequiredField(account.getCity(),
273: "City is required.");
274: validateRequiredField(account.getState(),
275: "State is required.");
276: validateRequiredField(account.getZip(),
277: "ZIP is required.");
278: validateRequiredField(account.getCountry(),
279: "Country is required.");
280: }
281: }
282:
283: }
284:
285: }
|