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.controllers;
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 org.mybatis.jpetstore.domain.Account;
25  import org.mybatis.jpetstore.domain.Product;
26  import org.mybatis.jpetstore.service.AccountService;
27  import org.mybatis.jpetstore.service.CatalogService;
28  import org.springframework.beans.factory.annotation.Autowired;
29  import org.springframework.stereotype.Controller;
30  import org.springframework.ui.Model;
31  import org.springframework.web.bind.annotation.GetMapping;
32  import org.springframework.web.bind.annotation.ModelAttribute;
33  import org.springframework.web.bind.annotation.PostMapping;
34  import org.springframework.web.bind.annotation.RequestMapping;
35  import org.springframework.web.bind.annotation.RequestParam;
36  
37  /**
38   * The Class AccountController.
39   */
40  @Controller
41  @RequestMapping("/account")
42  public class AccountController {
43  
44    /** The signon view. */
45    private static final String SIGNON_VIEW = "account/SignonForm";
46    /** The new account view. */
47    private static final String NEW_ACCOUNT_VIEW = "account/NewAccountForm";
48    /** The edit account view. */
49    private static final String EDIT_ACCOUNT_VIEW = "account/EditAccountForm";
50  
51    /** The language list. */
52    private static final List<String> LANGUAGE_LIST = Collections.unmodifiableList(Arrays.asList("english", "japanese"));
53    /** The category list. */
54    private static final List<String> CATEGORY_LIST = Collections
55        .unmodifiableList(Arrays.asList("FISH", "DOGS", "REPTILES", "CATS", "BIRDS"));
56  
57    /** The account service. */
58    @Autowired
59    private AccountService accountService;
60    /** The catalog service. */
61    @Autowired
62    private CatalogService catalogService;
63  
64    /**
65     * Signon form.
66     *
67     * @return the string
68     */
69    @GetMapping({ "", "/" })
70    public String signonForm() {
71      return SIGNON_VIEW;
72    }
73  
74    /**
75     * Signon.
76     *
77     * @param username
78     *          the username
79     * @param password
80     *          the password
81     * @param session
82     *          the session
83     * @param model
84     *          the model
85     *
86     * @return the string
87     */
88    @PostMapping("/signon")
89    public String signon(@RequestParam("username") String username, @RequestParam("password") String password,
90        HttpSession session, Model model) {
91      Account account = accountService.getAccount(username, password);
92      if (account == null) {
93        model.addAttribute("message", "Invalid username or password.  Signon failed.");
94        return SIGNON_VIEW;
95      }
96      account.setPassword(null);
97      List<Product> myList = catalogService.getProductListByCategory(account.getFavouriteCategoryId());
98      session.setAttribute("accountBean", new AccountSession(account, myList, true));
99      return "redirect:/catalog";
100   }
101 
102   /**
103    * Signoff.
104    *
105    * @param session
106    *          the session
107    *
108    * @return the string
109    */
110   @GetMapping("/signoff")
111   public String signoff(HttpSession session) {
112     session.invalidate();
113     return "redirect:/catalog";
114   }
115 
116   /**
117    * New account form.
118    *
119    * @param model
120    *          the model
121    *
122    * @return the string
123    */
124   @GetMapping("/new")
125   public String newAccountForm(Model model) {
126     model.addAttribute("languages", LANGUAGE_LIST);
127     model.addAttribute("categories", CATEGORY_LIST);
128     model.addAttribute("account", new Account());
129     return NEW_ACCOUNT_VIEW;
130   }
131 
132   /**
133    * New account.
134    *
135    * @param account
136    *          the account
137    *
138    * @return the string
139    */
140   @PostMapping("/new")
141   public String newAccount(@ModelAttribute Account account) {
142     accountService.insertAccount(account);
143     return "redirect:/catalog";
144   }
145 
146   /**
147    * Edit account form.
148    *
149    * @param session
150    *          the session
151    * @param model
152    *          the model
153    *
154    * @return the string
155    */
156   @GetMapping("/edit")
157   public String editAccountForm(HttpSession session, Model model) {
158     AccountSession accountSession = (AccountSession) session.getAttribute("accountBean");
159     if (accountSession != null) {
160       model.addAttribute("account", accountSession.getAccount());
161     }
162     model.addAttribute("languages", LANGUAGE_LIST);
163     model.addAttribute("categories", CATEGORY_LIST);
164     return EDIT_ACCOUNT_VIEW;
165   }
166 
167   /**
168    * Edit account.
169    *
170    * @param account
171    *          the account
172    * @param session
173    *          the session
174    *
175    * @return the string
176    */
177   @PostMapping("/edit")
178   public String editAccount(@ModelAttribute Account account, HttpSession session) {
179     accountService.updateAccount(account);
180     Account updatedAccount = accountService.getAccount(account.getUsername());
181     List<Product> myList = catalogService.getProductListByCategory(updatedAccount.getFavouriteCategoryId());
182     session.setAttribute("accountBean", new AccountSession(updatedAccount, myList, true));
183     return "redirect:/account/edit";
184   }
185 
186   /**
187    * Inner class to hold account session data, compatible with JSP ${sessionScope.accountBean.*} references.
188    */
189   public static class AccountSession implements java.io.Serializable {
190     /** The serial version uid. */
191     private static final long serialVersionUID = 1L;
192     /** The account. */
193     private final Account account;
194     /** The my list. */
195     private final List<Product> myList;
196     /** The authenticated. */
197     private final boolean authenticated;
198 
199     /**
200      * Instantiates a new account session.
201      *
202      * @param account
203      *          the account
204      * @param myList
205      *          the my list
206      * @param authenticated
207      *          the authenticated
208      */
209     public AccountSession(Account account, List<Product> myList, boolean authenticated) {
210       this.account = account;
211       this.myList = myList;
212       this.authenticated = authenticated;
213     }
214 
215     /**
216      * Gets the account.
217      *
218      * @return the account
219      */
220     public Account getAccount() {
221       return account;
222     }
223 
224     /**
225      * Gets the my list.
226      *
227      * @return the my list
228      */
229     public List<Product> getMyList() {
230       return myList;
231     }
232 
233     /**
234      * Checks if is authenticated.
235      *
236      * @return true, if successful
237      */
238     public boolean isAuthenticated() {
239       return authenticated && account != null && account.getUsername() != null;
240     }
241   }
242 }