View Javadoc
1   /*
2    *    Copyright 2010-2026 the original author or authors.
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    *       https://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 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   * The Class AccountActionBean.
39   *
40   * @author Eduardo Macarron
41   */
42  @SessionScope
43  public class AccountActionBean extends AbstractActionBean {
44  
45    /** The serial version uid. */
46    private static final long serialVersionUID = 5499663666155758178L;
47  
48    /** The new account. */
49    private static final String NEW_ACCOUNT = "/WEB-INF/jsp/account/NewAccountForm.jsp";
50    /** The edit account. */
51    private static final String EDIT_ACCOUNT = "/WEB-INF/jsp/account/EditAccountForm.jsp";
52    /** The signon. */
53    private static final String SIGNON = "/WEB-INF/jsp/account/SignonForm.jsp";
54  
55    /** The language list. */
56    private static final List<String> LANGUAGE_LIST;
57    /** The category list. */
58    private static final List<String> CATEGORY_LIST;
59  
60    /** The account service. */
61    @SpringBean
62    private transient AccountService accountService;
63    /** The catalog service. */
64    @SpringBean
65    private transient CatalogService catalogService;
66  
67    /** The account. */
68    private Account account = new Account();
69    /** The my list. */
70    private List<Product> myList;
71    /** The authenticated. */
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     * Gets the account.
81     *
82     * @return the account
83     */
84    public Account getAccount() {
85      return this.account;
86    }
87  
88    /**
89     * Gets the username.
90     *
91     * @return the username
92     */
93    public String getUsername() {
94      return account.getUsername();
95    }
96  
97    /**
98     * Sets the username.
99     *
100    * @param username
101    *          the username
102    */
103   @Validate(required = true, on = { "signon", "newAccount", "editAccount" })
104   public void setUsername(String username) {
105     account.setUsername(username);
106   }
107 
108   /**
109    * Gets the password.
110    *
111    * @return the password
112    */
113   public String getPassword() {
114     return account.getPassword();
115   }
116 
117   /**
118    * Sets the password.
119    *
120    * @param password
121    *          the password
122    */
123   @Validate(required = true, on = { "signon", "newAccount", "editAccount" })
124   public void setPassword(String password) {
125     account.setPassword(password);
126   }
127 
128   /**
129    * Gets the my list.
130    *
131    * @return the my list
132    */
133   public List<Product> getMyList() {
134     return myList;
135   }
136 
137   /**
138    * Sets the my list.
139    *
140    * @param myList
141    *          the my list
142    */
143   public void setMyList(List<Product> myList) {
144     this.myList = myList;
145   }
146 
147   /**
148    * Gets the languages.
149    *
150    * @return the languages
151    */
152   public List<String> getLanguages() {
153     return LANGUAGE_LIST;
154   }
155 
156   /**
157    * Gets the categories.
158    *
159    * @return the categories
160    */
161   public List<String> getCategories() {
162     return CATEGORY_LIST;
163   }
164 
165   /**
166    * New account form.
167    *
168    * @return the resolution
169    */
170   public Resolution newAccountForm() {
171     return new ForwardResolution(NEW_ACCOUNT);
172   }
173 
174   /**
175    * New account.
176    *
177    * @return the resolution
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    * Edits the account form.
189    *
190    * @return the resolution
191    */
192   public Resolution editAccountForm() {
193     return new ForwardResolution(EDIT_ACCOUNT);
194   }
195 
196   /**
197    * Edits the account.
198    *
199    * @return the resolution
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    * Signon form.
210    *
211    * @return the resolution
212    */
213   @DefaultHandler
214   public Resolution signonForm() {
215     return new ForwardResolution(SIGNON);
216   }
217 
218   /**
219    * Signon.
220    *
221    * @return the resolution
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       // this bean is already registered as /actions/Account.action
238       s.setAttribute("accountBean", this);
239       return new RedirectResolution(CatalogActionBean.class);
240     }
241   }
242 
243   /**
244    * Signoff.
245    *
246    * @return the resolution
247    */
248   public Resolution signoff() {
249     context.getRequest().getSession().invalidate();
250     clear();
251     return new RedirectResolution(CatalogActionBean.class);
252   }
253 
254   /**
255    * Checks if is authenticated.
256    *
257    * @return true, if is authenticated
258    */
259   public boolean isAuthenticated() {
260     return authenticated && account != null && account.getUsername() != null;
261   }
262 
263   /**
264    * Clear.
265    */
266   public void clear() {
267     account = new Account();
268     myList = null;
269     authenticated = false;
270   }
271 
272 }