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.List;
19  
20  import net.sourceforge.stripes.action.DefaultHandler;
21  import net.sourceforge.stripes.action.ForwardResolution;
22  import net.sourceforge.stripes.action.SessionScope;
23  import net.sourceforge.stripes.integration.spring.SpringBean;
24  
25  import org.mybatis.jpetstore.domain.Category;
26  import org.mybatis.jpetstore.domain.Item;
27  import org.mybatis.jpetstore.domain.Product;
28  import org.mybatis.jpetstore.service.CatalogService;
29  
30  /**
31   * The Class CatalogActionBean.
32   *
33   * @author Eduardo Macarron
34   */
35  @SessionScope
36  public class CatalogActionBean extends AbstractActionBean {
37  
38    /** The serial version uid. */
39    private static final long serialVersionUID = 5849523372175050635L;
40  
41    /** The main. */
42    private static final String MAIN = "/WEB-INF/jsp/catalog/Main.jsp";
43    /** The view category. */
44    private static final String VIEW_CATEGORY = "/WEB-INF/jsp/catalog/Category.jsp";
45    /** The view product. */
46    private static final String VIEW_PRODUCT = "/WEB-INF/jsp/catalog/Product.jsp";
47    /** The view item. */
48    private static final String VIEW_ITEM = "/WEB-INF/jsp/catalog/Item.jsp";
49    /** The search products. */
50    private static final String SEARCH_PRODUCTS = "/WEB-INF/jsp/catalog/SearchProducts.jsp";
51  
52    /** The catalog service. */
53    @SpringBean
54    private transient CatalogService catalogService;
55  
56    /** The keyword. */
57    private String keyword;
58  
59    /** The category id. */
60    private String categoryId;
61    /** The category. */
62    private Category category;
63    /** The category list. */
64    private List<Category> categoryList;
65  
66    /** The product id. */
67    private String productId;
68    /** The product. */
69    private Product product;
70    /** The product list. */
71    private List<Product> productList;
72  
73    /** The item id. */
74    private String itemId;
75    /** The item. */
76    private Item item;
77    /** The item list. */
78    private List<Item> itemList;
79  
80    /**
81     * Gets the keyword.
82     *
83     * @return the keyword
84     */
85    public String getKeyword() {
86      return keyword;
87    }
88  
89    /**
90     * Sets the keyword.
91     *
92     * @param keyword
93     *          the keyword
94     */
95    public void setKeyword(String keyword) {
96      this.keyword = keyword;
97    }
98  
99    /**
100    * Gets the category id.
101    *
102    * @return the category id
103    */
104   public String getCategoryId() {
105     return categoryId;
106   }
107 
108   /**
109    * Sets the category id.
110    *
111    * @param categoryId
112    *          the category id
113    */
114   public void setCategoryId(String categoryId) {
115     this.categoryId = categoryId;
116   }
117 
118   /**
119    * Gets the product id.
120    *
121    * @return the product id
122    */
123   public String getProductId() {
124     return productId;
125   }
126 
127   /**
128    * Sets the product id.
129    *
130    * @param productId
131    *          the product id
132    */
133   public void setProductId(String productId) {
134     this.productId = productId;
135   }
136 
137   /**
138    * Gets the item id.
139    *
140    * @return the item id
141    */
142   public String getItemId() {
143     return itemId;
144   }
145 
146   /**
147    * Sets the item id.
148    *
149    * @param itemId
150    *          the item id
151    */
152   public void setItemId(String itemId) {
153     this.itemId = itemId;
154   }
155 
156   /**
157    * Gets the category.
158    *
159    * @return the category
160    */
161   public Category getCategory() {
162     return category;
163   }
164 
165   /**
166    * Sets the category.
167    *
168    * @param category
169    *          the category
170    */
171   public void setCategory(Category category) {
172     this.category = category;
173   }
174 
175   /**
176    * Gets the product.
177    *
178    * @return the product
179    */
180   public Product getProduct() {
181     return product;
182   }
183 
184   /**
185    * Sets the product.
186    *
187    * @param product
188    *          the product
189    */
190   public void setProduct(Product product) {
191     this.product = product;
192   }
193 
194   /**
195    * Gets the item.
196    *
197    * @return the item
198    */
199   public Item getItem() {
200     return item;
201   }
202 
203   /**
204    * Sets the item.
205    *
206    * @param item
207    *          the item
208    */
209   public void setItem(Item item) {
210     this.item = item;
211   }
212 
213   /**
214    * Gets the category list.
215    *
216    * @return the category list
217    */
218   public List<Category> getCategoryList() {
219     return categoryList;
220   }
221 
222   /**
223    * Sets the category list.
224    *
225    * @param categoryList
226    *          the category list
227    */
228   public void setCategoryList(List<Category> categoryList) {
229     this.categoryList = categoryList;
230   }
231 
232   /**
233    * Gets the product list.
234    *
235    * @return the product list
236    */
237   public List<Product> getProductList() {
238     return productList;
239   }
240 
241   /**
242    * Sets the product list.
243    *
244    * @param productList
245    *          the product list
246    */
247   public void setProductList(List<Product> productList) {
248     this.productList = productList;
249   }
250 
251   /**
252    * Gets the item list.
253    *
254    * @return the item list
255    */
256   public List<Item> getItemList() {
257     return itemList;
258   }
259 
260   /**
261    * Sets the item list.
262    *
263    * @param itemList
264    *          the item list
265    */
266   public void setItemList(List<Item> itemList) {
267     this.itemList = itemList;
268   }
269 
270   /**
271    * View main.
272    *
273    * @return the forward resolution
274    */
275   @DefaultHandler
276   public ForwardResolution viewMain() {
277     return new ForwardResolution(MAIN);
278   }
279 
280   /**
281    * View category.
282    *
283    * @return the forward resolution
284    */
285   public ForwardResolution viewCategory() {
286     if (categoryId != null) {
287       productList = catalogService.getProductListByCategory(categoryId);
288       category = catalogService.getCategory(categoryId);
289     }
290     return new ForwardResolution(VIEW_CATEGORY);
291   }
292 
293   /**
294    * View product.
295    *
296    * @return the forward resolution
297    */
298   public ForwardResolution viewProduct() {
299     if (productId != null) {
300       itemList = catalogService.getItemListByProduct(productId);
301       product = catalogService.getProduct(productId);
302     }
303     return new ForwardResolution(VIEW_PRODUCT);
304   }
305 
306   /**
307    * View item.
308    *
309    * @return the forward resolution
310    */
311   public ForwardResolution viewItem() {
312     item = catalogService.getItem(itemId);
313     product = item.getProduct();
314     return new ForwardResolution(VIEW_ITEM);
315   }
316 
317   /**
318    * Search products.
319    *
320    * @return the forward resolution
321    */
322   public ForwardResolution searchProducts() {
323     if (keyword == null || keyword.length() < 1) {
324       setMessage("Please enter a keyword to search for, then press the search button.");
325       return new ForwardResolution(ERROR);
326     } else {
327       productList = catalogService.searchProductList(keyword.toLowerCase());
328       return new ForwardResolution(SEARCH_PRODUCTS);
329     }
330   }
331 
332   /**
333    * Clear.
334    */
335   public void clear() {
336     keyword = null;
337 
338     categoryId = null;
339     category = null;
340     categoryList = null;
341 
342     productId = null;
343     product = null;
344     productList = null;
345 
346     itemId = null;
347     item = null;
348     itemList = null;
349   }
350 
351 }