KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfox > petstore > action > ProductAction


1 /*
2  * JFox - The most lightweight Java EE Application Server!
3  * more details please visit http://www.huihoo.org/jfox or http://www.jfox.org.cn.
4  *
5  * JFox is licenced and re-distributable under GNU LGPL.
6  */

7 package org.jfox.petstore.action;
8
9 import java.util.List JavaDoc;
10 import javax.ejb.EJB JavaDoc;
11
12 import org.jfox.petstore.bo.ItemBO;
13 import org.jfox.petstore.bo.ProductBO;
14 import org.jfox.petstore.entity.Item;
15 import org.jfox.petstore.entity.Product;
16 import org.jfox.framework.annotation.Service;
17 import org.jfox.mvc.ActionSupport;
18 import org.jfox.mvc.Invocation;
19 import org.jfox.mvc.InvocationContext;
20 import org.jfox.mvc.PageContext;
21 import org.jfox.mvc.util.PagedList;
22 import org.jfox.mvc.annotation.ActionMethod;
23
24 /**
25  * @author <a HREF="mailto:jfox.young@gmail.com">Young Yang</a>
26  */

27 @Service(id = "product")
28 public class ProductAction extends ActionSupport {
29
30     @EJB JavaDoc
31     ItemBO itemBO;
32
33     @EJB JavaDoc
34     ProductBO productBO;
35
36     @ActionMethod(successView = "Product.vhtml", invocationClass = ProductInvoation.class)
37     public void doGetView(InvocationContext invocationContext) {
38         ProductInvoation invocation = (ProductInvoation)invocationContext.getInvocation();
39
40         Product product = productBO.getProduct(invocation.getProductId());
41
42         List JavaDoc<Item> items = itemBO.getItemListByProduct(invocation.getProductId());
43         PagedList<Item> itemPageList = new PagedList<Item>(items, 4);
44         for (int i = 0; i < invocation.getPage(); i++) {
45             itemPageList.nextPage();
46         }
47
48         int nextPage = invocation.getPage();
49         if (!itemPageList.isLastPage()) {
50             nextPage++;
51         }
52
53         int previousPage = invocation.getPage();
54         if (!itemPageList.isFirstPage()) {
55             previousPage--;
56         }
57
58         PageContext pageContext = invocationContext.getPageContext();
59         pageContext.setAttribute("product", product);
60         pageContext.setAttribute("productId", invocation.getProductId());
61         pageContext.setAttribute("itemPageList", itemPageList);
62         pageContext.setAttribute("previousPage", previousPage);
63         pageContext.setAttribute("nextPage", nextPage);
64
65     }
66
67     @ActionMethod(successView = "SearchProducts.vhtml", invocationClass = SearchProductInvocation.class)
68     public void doPostSearch(InvocationContext invocationContext) {
69         SearchProductInvocation invocation = (SearchProductInvocation)invocationContext.getInvocation();
70         String JavaDoc keyword = invocation.getKeyword();
71         String JavaDoc[] keywords = keyword.split(" ");
72         List JavaDoc<Product> products = productBO.searchProductList(keywords);
73         PagedList<Product> productPagedList = new PagedList<Product>(products,4);
74
75         for (int i = 0; i < invocation.getPage(); i++) {
76             productPagedList.nextPage();
77         }
78
79         int nextPage = invocation.getPage();
80         if (!productPagedList.isLastPage()) {
81             nextPage++;
82         }
83
84         int previousPage = invocation.getPage();
85         if (!productPagedList.isFirstPage()) {
86             previousPage--;
87         }
88
89         PageContext pageContext = invocationContext.getPageContext();
90         pageContext.setAttribute("productPageList", productPagedList);
91         pageContext.setAttribute("previousPage", previousPage);
92         pageContext.setAttribute("nextPage", nextPage);
93     }
94
95     @ActionMethod(successView = "SearchProducts.vhtml", invocationClass = SearchProductInvocation.class)
96     public void doGetSearch(InvocationContext invocationContext) {
97         doPostSearch(invocationContext);
98     }
99
100     public static class ProductInvoation extends Invocation {
101         private String JavaDoc productId;
102
103         private int page = 0;
104
105         public String JavaDoc getProductId() {
106             return productId;
107         }
108
109         public void setProductId(String JavaDoc productId) {
110             this.productId = productId;
111         }
112
113         public int getPage() {
114             return page;
115         }
116
117         public void setPage(int page) {
118             this.page = page;
119         }
120     }
121
122     public static class SearchProductInvocation extends Invocation {
123
124         private String JavaDoc keyword = "";
125
126         private int page = 0;
127
128         public String JavaDoc getKeyword() {
129             return keyword;
130         }
131
132         public void setKeyword(String JavaDoc keyword) {
133             this.keyword = keyword;
134         }
135
136         public int getPage() {
137             return page;
138         }
139
140         public void setPage(int page) {
141             this.page = page;
142         }
143     }
144
145     public static void main(String JavaDoc[] args) {
146
147     }
148 }
149
Popular Tags