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.annotation.ActionMethod;
018: import org.jfox.mvc.util.PagedList;
019: import org.jfox.petstore.bo.ItemBO;
020: import org.jfox.petstore.bo.ProductBO;
021: import org.jfox.petstore.entity.Item;
022: import org.jfox.petstore.entity.Product;
023:
024: /**
025: * @author <a href="mailto:jfox.young@gmail.com">Young Yang</a>
026: */
027: @Service(id="product")
028: public class ProductAction extends ActionSupport {
029:
030: @EJB
031: ItemBO itemBO;
032:
033: @EJB
034: ProductBO productBO;
035:
036: @ActionMethod(name="view",successView="Product.vhtml",invocationClass=ProductInvoation.class,httpMethod=ActionMethod.HttpMethod.GET)
037: public void doGetView(ActionContext actionContext) {
038: ProductInvoation invocation = (ProductInvoation) actionContext
039: .getInvocation();
040:
041: Product product = productBO.getProduct(invocation
042: .getProductId());
043:
044: List<Item> items = itemBO.getItemListByProduct(invocation
045: .getProductId());
046: PagedList<Item> itemPageList = new PagedList<Item>(items, 4);
047: for (int i = 0; i < invocation.getPage(); i++) {
048: itemPageList.nextPage();
049: }
050:
051: int nextPage = invocation.getPage();
052: if (!itemPageList.isLastPage()) {
053: nextPage++;
054: }
055:
056: int previousPage = invocation.getPage();
057: if (!itemPageList.isFirstPage()) {
058: previousPage--;
059: }
060:
061: PageContext pageContext = actionContext.getPageContext();
062: pageContext.setAttribute("product", product);
063: pageContext
064: .setAttribute("productId", invocation.getProductId());
065: pageContext.setAttribute("itemPageList", itemPageList);
066: pageContext.setAttribute("previousPage", previousPage);
067: pageContext.setAttribute("nextPage", nextPage);
068:
069: }
070:
071: @ActionMethod(name="search",successView="SearchProducts.vhtml",invocationClass=SearchProductInvocation.class,httpMethod=ActionMethod.HttpMethod.POST)
072: public void doPostSearch(ActionContext actionContext) {
073: SearchProductInvocation invocation = (SearchProductInvocation) actionContext
074: .getInvocation();
075: String keyword = invocation.getKeyword();
076: String[] keywords = keyword.split(" ");
077: List<Product> products = productBO.searchProductList(keywords);
078: PagedList<Product> productPagedList = new PagedList<Product>(
079: products, 4);
080:
081: for (int i = 0; i < invocation.getPage(); i++) {
082: productPagedList.nextPage();
083: }
084:
085: int nextPage = invocation.getPage();
086: if (!productPagedList.isLastPage()) {
087: nextPage++;
088: }
089:
090: int previousPage = invocation.getPage();
091: if (!productPagedList.isFirstPage()) {
092: previousPage--;
093: }
094:
095: PageContext pageContext = actionContext.getPageContext();
096: pageContext.setAttribute("productPageList", productPagedList);
097: pageContext.setAttribute("previousPage", previousPage);
098: pageContext.setAttribute("nextPage", nextPage);
099: }
100:
101: @ActionMethod(name="search",successView="SearchProducts.vhtml",invocationClass=SearchProductInvocation.class,httpMethod=ActionMethod.HttpMethod.GET)
102: public void doGetSearch(ActionContext actionContext) {
103: doPostSearch(actionContext);
104: }
105:
106: public static class ProductInvoation extends Invocation {
107: private String productId;
108:
109: private int page = 0;
110:
111: public String getProductId() {
112: return productId;
113: }
114:
115: public void setProductId(String productId) {
116: this .productId = productId;
117: }
118:
119: public int getPage() {
120: return page;
121: }
122:
123: public void setPage(int page) {
124: this .page = page;
125: }
126: }
127:
128: public static class SearchProductInvocation extends Invocation {
129:
130: private String keyword = "";
131:
132: private int page = 0;
133:
134: public String getKeyword() {
135: return keyword;
136: }
137:
138: public void setKeyword(String keyword) {
139: this .keyword = keyword;
140: }
141:
142: public int getPage() {
143: return page;
144: }
145:
146: public void setPage(int page) {
147: this .page = page;
148: }
149: }
150:
151: public static void main(String[] args) {
152:
153: }
154: }
|