001: /*
002: * JFox - The most lightweight Java EE Application Server!
003: * more details please visit http://www.huihoo.org/jfox or http://www.jfox.org.cn.
004: *
005: * JFox is licenced and re-distributable under GNU LGPL.
006: */
007: package org.jfox.petstore.action;
008:
009: import java.util.List;
010: import javax.ejb.EJB;
011:
012: import org.jfox.framework.annotation.Service;
013: import org.jfox.mvc.ActionContext;
014: import org.jfox.mvc.ActionSupport;
015: import org.jfox.mvc.Invocation;
016: import org.jfox.mvc.PageContext;
017: import org.jfox.mvc.SessionContext;
018: import org.jfox.mvc.annotation.ActionMethod;
019: import org.jfox.mvc.util.PagedList;
020: import org.jfox.petstore.bo.AccountBO;
021: import org.jfox.petstore.bo.CategoryBO;
022: import org.jfox.petstore.bo.ProductBO;
023: import org.jfox.petstore.entity.Category;
024: import org.jfox.petstore.entity.Product;
025:
026: /**
027: * @author <a href="mailto:jfox.young@gmail.com">Young Yang</a>
028: */
029: @Service(id="category")
030: public class CategoryAction extends ActionSupport {
031:
032: @EJB
033: AccountBO accountBO;
034:
035: @EJB(name="CategoryBOImpl")
036: CategoryBO categoryBO;
037:
038: @EJB
039: ProductBO productBO;
040:
041: public void postInject() {
042: super .postInject();
043: }
044:
045: /**
046: * index page
047: *
048: * @param actionContext invocationContext
049: * @throws Exception exception
050: */
051: @ActionMethod(name="view",successView="Category.vhtml",invocationClass=CategoryInvocation.class,httpMethod=ActionMethod.HttpMethod.GET)
052: public void doGetView(ActionContext actionContext) throws Exception {
053: CategoryInvocation invocation = (CategoryInvocation) actionContext
054: .getInvocation();
055: Category category = categoryBO.getCategory(invocation
056: .getCategoryId());
057:
058: PageContext pageContext = actionContext.getPageContext();
059: SessionContext sessionContext = actionContext
060: .getSessionContext();
061:
062: PagedList<Product> productPagedList;
063:
064: //product list cached by JPA Cache
065: // if (!sessionContext.containsAttribute("ProductPageList")) {
066: List<Product> products = productBO
067: .getProductsByCategory(invocation.getCategoryId());
068: productPagedList = new PagedList<Product>(products, 4);
069: sessionContext
070: .setAttribute("ProductPageList", productPagedList);
071: // }
072: // else {
073: // productPagedList = (PagedList<Product>)sessionContext.getAttribute("ProductPageList");
074: // }
075: for (int i = 0; i < invocation.getPage(); i++) {
076: productPagedList.nextPage();
077: }
078:
079: int nextPage = invocation.getPage();
080: if (!productPagedList.isLastPage()) {
081: nextPage++;
082: }
083:
084: int previousPage = invocation.getPage();
085: if (!productPagedList.isFirstPage()) {
086: previousPage--;
087: }
088:
089: /*
090: if ("next".equals(invocation.getPage())) {
091: productPagedList.nextPage();
092: }
093: else if ("previous".equals(invocation.getPage())) {
094: productPagedList.previousPage();
095: }
096: */
097:
098: // Account account = accountBO.getAccount();
099: // account.setFirstName("Yang Yong");
100: // sessionContext.setAttribute("account", account);
101: pageContext.setAttribute("account", sessionContext
102: .getAttribute("account"));
103: pageContext.setAttribute("category", category);
104: pageContext.setAttribute("categoryId", invocation
105: .getCategoryId());
106: pageContext.setAttribute("pageList", productPagedList);
107: pageContext.setAttribute("previousPage", previousPage);
108: pageContext.setAttribute("nextPage", nextPage);
109:
110: }
111:
112: public static class CategoryInvocation extends Invocation {
113:
114: private String categoryId = "BIRDS";
115:
116: private int page = 0;
117:
118: public String getCategoryId() {
119: return categoryId;
120: }
121:
122: public void setCategoryId(String categoryId) {
123: this .categoryId = categoryId;
124: }
125:
126: public int getPage() {
127: return page;
128: }
129:
130: public void setPage(int page) {
131: this .page = page;
132: }
133: }
134:
135: public static void main(String[] args) {
136:
137: }
138: }
|