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.Category;
20  import com.ibatis.jpetstore.domain.Item;
21  import com.ibatis.jpetstore.domain.Product;
22  import com.ibatis.jpetstore.service.CatalogService;
23  import com.ibatis.struts.ActionContext;
24  import com.ibatis.struts.BaseBean;
25  
26  /***
27   * <p/>
28   * Date: Mar 9, 2004 10:16:59 PM
29   *
30   * @author Clinton Begin
31   */
32  public class CatalogBean extends BaseBean {
33  
34    /* Constants */
35  
36    private static final CatalogService catalogService = CatalogService.getInstance();
37  
38    /* Private Fields */
39  
40    private String keyword;
41    private String pageDirection;
42  
43    private String categoryId;
44    private Category category;
45    private PaginatedList categoryList;
46  
47    private String productId;
48    private Product product;
49    private PaginatedList productList;
50  
51    private String itemId;
52    private Item item;
53    private PaginatedList itemList;
54  
55    /* JavaBeans Properties */
56  
57    public String getKeyword() {
58      return keyword;
59    }
60  
61    public void setKeyword(String keyword) {
62      this.keyword = keyword;
63    }
64  
65    public String getPageDirection() {
66      return pageDirection;
67    }
68  
69    public void setPageDirection(String pageDirection) {
70      this.pageDirection = pageDirection;
71    }
72  
73    public String getCategoryId() {
74      return categoryId;
75    }
76  
77    public void setCategoryId(String categoryId) {
78      this.categoryId = categoryId;
79    }
80  
81    public String getProductId() {
82      return productId;
83    }
84  
85    public void setProductId(String productId) {
86      this.productId = productId;
87    }
88  
89    public String getItemId() {
90      return itemId;
91    }
92  
93    public void setItemId(String itemId) {
94      this.itemId = itemId;
95    }
96  
97    public Category getCategory() {
98      return category;
99    }
100 
101   public void setCategory(Category category) {
102     this.category = category;
103   }
104 
105   public Product getProduct() {
106     return product;
107   }
108 
109   public void setProduct(Product product) {
110     this.product = product;
111   }
112 
113   public Item getItem() {
114     return item;
115   }
116 
117   public void setItem(Item item) {
118     this.item = item;
119   }
120 
121   public PaginatedList getCategoryList() {
122     return categoryList;
123   }
124 
125   public void setCategoryList(PaginatedList categoryList) {
126     this.categoryList = categoryList;
127   }
128 
129   public PaginatedList getProductList() {
130     return productList;
131   }
132 
133   public void setProductList(PaginatedList productList) {
134     this.productList = productList;
135   }
136 
137   public PaginatedList getItemList() {
138     return itemList;
139   }
140 
141   public void setItemList(PaginatedList itemList) {
142     this.itemList = itemList;
143   }
144 
145   /* Public Methods */
146 
147   public String viewCategory() {
148     if (categoryId != null) {
149       productList = catalogService.getProductListByCategory(categoryId);
150       category = catalogService.getCategory(categoryId);
151     }
152     return "success";
153   }
154 
155   public String searchProducts() {
156     if (keyword == null || keyword.length() < 1) {
157       ActionContext.getActionContext().setSimpleMessage("Please enter a keyword to search for, then press the search button.");
158       return "failure";
159     } else {
160       productList = catalogService.searchProductList(keyword.toLowerCase());
161       return "success";
162     }
163   }
164 
165   public String switchProductListPage() {
166     if ("next".equals(pageDirection)) {
167       productList.nextPage();
168     } else if ("previous".equals(pageDirection)) {
169       productList.previousPage();
170     }
171     return "success";
172   }
173 
174   public String viewProduct() {
175     if (productId != null) {
176       itemList = catalogService.getItemListByProduct(productId);
177       product = catalogService.getProduct(productId);
178     }
179     return "success";
180   }
181 
182   public String switchItemListPage() {
183     if ("next".equals(pageDirection)) {
184       itemList.nextPage();
185     } else if ("previous".equals(pageDirection)) {
186       itemList.previousPage();
187     }
188     return "success";
189   }
190 
191   public String viewItem() {
192     item = catalogService.getItem(itemId);
193     product = item.getProduct();
194     return "success";
195   }
196 
197   public void clear () {
198     keyword = null;
199     pageDirection = null;
200 
201     categoryId = null;
202     category = null;
203     categoryList = null;
204 
205     productId = null;
206     product = null;
207     productList = null;
208 
209     itemId = null;
210     item = null;
211     itemList = null;
212   }
213 
214 }