KickJava   Java API By Example, From Geeks To Geeks.

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


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.AccountBO;
13 import org.jfox.petstore.bo.CategoryBO;
14 import org.jfox.petstore.bo.ProductBO;
15 import org.jfox.petstore.entity.Product;
16 import org.jfox.petstore.entity.Category;
17 import org.jfox.mvc.util.PagedList;
18 import org.jfox.framework.annotation.Service;
19 import org.jfox.mvc.ActionSupport;
20 import org.jfox.mvc.Invocation;
21 import org.jfox.mvc.InvocationContext;
22 import org.jfox.mvc.PageContext;
23 import org.jfox.mvc.SessionContext;
24 import org.jfox.mvc.annotation.ActionMethod;
25
26 /**
27  * @author <a HREF="mailto:jfox.young@gmail.com">Young Yang</a>
28  */

29 @Service(id = "category")
30 public class CategoryAction extends ActionSupport {
31
32     @EJB JavaDoc
33     AccountBO accountBO;
34
35     @EJB JavaDoc(name = "CategoryBOImpl")
36     CategoryBO categoryBO;
37
38     @EJB JavaDoc
39     ProductBO productBO;
40     
41     public void postInject() {
42         super.postInject();
43     }
44
45     /**
46      * index page
47      *
48      * @param invocationContext invocationContext
49      * @throws Exception exception
50      */

51     @ActionMethod(successView = "Category.vhtml", invocationClass = CategoryInvocation.class)
52     public void doGetView(InvocationContext invocationContext) throws Exception JavaDoc {
53         CategoryInvocation invocation = (CategoryInvocation)invocationContext.getInvocation();
54         Category category = categoryBO.getCategory(invocation.getCategoryId());
55
56         PageContext pageContext = invocationContext.getPageContext();
57         SessionContext sessionContext = invocationContext.getSessionContext();
58
59         PagedList<Product> productPagedList;
60
61         //product list cached by JPA Cache
62
// if (!sessionContext.containsAttribute("ProductPageList")) {
63
List JavaDoc<Product> products = productBO.getProductsByCategory(invocation.getCategoryId());
64             productPagedList = new PagedList<Product>(products, 4);
65             sessionContext.setAttribute("ProductPageList", productPagedList);
66 // }
67
// else {
68
// productPagedList = (PagedList<Product>)sessionContext.getAttribute("ProductPageList");
69
// }
70
for(int i=0; i< invocation.getPage(); i++){
71             productPagedList.nextPage();
72         }
73
74         int nextPage = invocation.getPage();
75         if(!productPagedList.isLastPage()){
76             nextPage++;
77         }
78
79         int previousPage = invocation.getPage();
80         if(!productPagedList.isFirstPage()){
81             previousPage--;
82         }
83
84 /*
85         if ("next".equals(invocation.getPage())) {
86             productPagedList.nextPage();
87         }
88         else if ("previous".equals(invocation.getPage())) {
89             productPagedList.previousPage();
90         }
91 */

92
93 // Account account = accountBO.getAccount();
94
// account.setFirstName("Yang Yong");
95
// sessionContext.setAttribute("account", account);
96

97         pageContext.setAttribute("account", sessionContext.getAttribute("account"));
98         pageContext.setAttribute("category", category);
99         pageContext.setAttribute("categoryId", invocation.getCategoryId());
100         pageContext.setAttribute("pageList", productPagedList);
101         pageContext.setAttribute("previousPage", previousPage);
102         pageContext.setAttribute("nextPage", nextPage);
103
104     }
105
106     public static class CategoryInvocation extends Invocation {
107
108         private String JavaDoc categoryId = "BIRDS";
109
110         private int page = 0;
111
112         public String JavaDoc getCategoryId() {
113             return categoryId;
114         }
115
116         public void setCategoryId(String JavaDoc categoryId) {
117             this.categoryId = categoryId;
118         }
119
120
121         public int getPage() {
122             return page;
123         }
124
125         public void setPage(int page) {
126             this.page = page;
127         }
128     }
129
130     public static void main(String JavaDoc[] args) {
131
132     }
133 }
134
Popular Tags