/*
* Copyright 2004-2005 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* $Header:$
*/
package shop;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.beehive.netui.pageflow.FormData;
import org.apache.beehive.netui.pageflow.Forward;
import org.apache.beehive.netui.pageflow.PageFlowController;
import org.apache.beehive.netui.pageflow.annotations.Jpf;
import org.apache.beehive.samples.petstore.controls.CatalogControl;
import org.apache.beehive.samples.petstore.controls.exceptions.InvalidIdentifierException;
import org.apache.beehive.samples.petstore.model.Cart;
import org.apache.beehive.samples.petstore.model.Category;
import org.apache.beehive.samples.petstore.model.Item;
import org.apache.beehive.samples.petstore.model.LineItem;
import org.apache.beehive.samples.petstore.model.Product;
import org.apache.beehive.controls.api.bean.Control;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
@Jpf.Controller(
forwards = {
@Jpf.Forward(name = "cart", path = "cart.jsp"),
@Jpf.Forward(name = "viewCart", path = "viewCart.do")
},
sharedFlowRefs={
@Jpf.SharedFlowRef(name="rootSharedFlow", type=webappRoot.SharedFlow.class)
},
simpleActions={
@Jpf.SimpleAction(name="checkout", path="/checkout/Controller.jpf")
}
)
public class Controller
extends PageFlowController {
@Control()
private CatalogControl _catalogControl;
@Jpf.SharedFlowField(name="rootSharedFlow")
private webappRoot.SharedFlow _sharedFlow = null;
/* todo: this should be shared for all users */
private Category[] _categories; // the list of categories
private Category _currentCategory; // the current category being viewed
private Product _currentProduct; // the current product being viewed
private Item _currentItem; // the current item being viewed
private CartForm _cartForm;
protected void onCreate() {
_cartForm = new CartForm();
_cartForm.setCart(_sharedFlow.getCart());
}
@Jpf.Action(
forwards = {
@Jpf.Forward(name = "success", path = "index.jsp",
actionOutputs = {
@Jpf.ActionOutput(name = "categories",
type = org.apache.beehive.samples.petstore.model.Category[].class,
required = true)
})
}
)
protected Forward begin() {
if(_categories == null)
_categories = _catalogControl.getCategoryList();
return new Forward("success", "categories", _categories);
}
@Jpf.Action(
forwards = {
@Jpf.Forward(name = "category", path = "category.jsp",
actionOutputs = {
@Jpf.ActionOutput(name = "category",
type = org.apache.beehive.samples.petstore.model.Category.class,
required = true),
@Jpf.ActionOutput(name = "products",
type = org.apache.beehive.samples.petstore.model.Product[].class,
required = false)
})
}
)
protected Forward viewCategory() throws InvalidIdentifierException {
String categoryId = getRequest().getParameter("catId");
if(categoryId == null || categoryId.equals(""))
throw new IllegalStateException("Found a null catId executing action viewCategory");
_currentCategory = _catalogControl.getCategory(categoryId);
Product[] productArray = _catalogControl.getProductListByCategory(categoryId);
Forward forward = new Forward("category");
forward.addActionOutput("products", productArray);
forward.addActionOutput("category", _currentCategory);
return forward;
}
@Jpf.Action(
forwards = {
@Jpf.Forward(name = "product", path = "product.jsp",
actionOutputs = {
@Jpf.ActionOutput(name = "product",
type = org.apache.beehive.samples.petstore.model.Product.class,
required = true),
@Jpf.ActionOutput(name = "items",
type = org.apache.beehive.samples.petstore.model.Item[].class,
required = true)
})
}
)
protected Forward viewProduct() throws InvalidIdentifierException {
String productId = getRequest().getParameter("productId");
if(productId == null || productId.equals(""))
throw new IllegalStateException("Found a null productId executing action viewProduct");
_currentProduct = _catalogControl.getProduct(productId);
Item[] items = _catalogControl.getItemListByProduct(productId);
Forward f = new Forward("product");
f.addActionOutput("product", _currentProduct);
f.addActionOutput("items", items);
return f;
}
@Jpf.Action(
forwards = {
@Jpf.Forward(name = "item", path = "item.jsp",
actionOutputs = {
@Jpf.ActionOutput(name = "product",
type = org.apache.beehive.samples.petstore.model.Product.class,
required = true),
@Jpf.ActionOutput(name = "item",
type = org.apache.beehive.samples.petstore.model.Item.class,
required = true)
})
}
)
protected Forward viewItem() throws InvalidIdentifierException {
String itemId = getRequest().getParameter("itemId");
if(itemId == null || itemId.equals(""))
throw new IllegalStateException("Found a null itemId executing action viewProduct");
_currentItem = _catalogControl.getItem(itemId);
_currentProduct = _catalogControl.getProduct(_currentItem.getProductId());
Forward f = new Forward("item");
f.addActionOutput("item", _currentItem);
f.addActionOutput("product", _currentProduct);
return f;
}
@Jpf.Action(useFormBean = "_cartForm",
forwards = {
@Jpf.Forward(name = "success",
path = "cart.jsp",
actionOutputs = {
@Jpf.ActionOutput(name = "myList",
type = org.apache.beehive.samples.petstore.model.Product[].class,
required = false)
}),
@Jpf.Forward(name="cart", path="cart.jsp")
})
protected Forward addItemToCart(CartForm form) throws InvalidIdentifierException {
Cart cart = form.getCart();
if (cart.containsItemId(form.getWorkingItemId()))
{
cart.incrementQuantityByItemId(form.getWorkingItemId());
}
else
{
Item toAddItem = _catalogControl.getItem(form.getWorkingItemId());
if (toAddItem != null)
{
//toAddItem.setProduct(_currentProduct);
cart.addItem(toAddItem);
}
/* todo: error handling path */
}
return new Forward("cart", "product", _currentProduct);
}
@Jpf.Action(useFormBean = "_cartForm", forwards={@Jpf.Forward(name="cart", path="cart.jsp")})
protected Forward viewCart(CartForm form) {
return new Forward("cart");
}
@Jpf.Action(useFormBean = "_cartForm",
forwards = {
@Jpf.Forward(name = "cart",
path = "cart.jsp",
actionOutputs = {
@Jpf.ActionOutput(name = "myList",
type = org.apache.beehive.samples.petstore.model.Product[].class,
required = false)
})
})
protected Forward updateCartQuantities(CartForm form) {
return new Forward("cart", "product", _currentProduct);
}
@Jpf.Action(useFormBean = "_cartForm",
forwards = {
@Jpf.Forward(name = "success",
path = "cart.jsp",
actionOutputs = {
@Jpf.ActionOutput(name = "myList",
type = org.apache.beehive.samples.petstore.model.Product[].class,
required = false)
}),
@Jpf.Forward(name="cart", path="cart.jsp")
})
protected Forward removeItemFromCart(CartForm form) {
if(_cartForm.getCart().containsItemId(form.getWorkingItemId()))
_cartForm.getCart().removeItemById(form.getWorkingItemId());
return new Forward("cart", "product", _currentProduct);
}
public static class CartForm
extends FormData {
private String _workingItemId;
private Cart _cart;
public void setCart(Cart cart) {
_cart = cart;
}
public Cart getCart() {
return _cart;
}
public void setWorkingItemId(String workingItemId) {
_workingItemId = workingItemId;
}
public String getWorkingItemId() {
return _workingItemId;
}
public void reset(ActionMapping mapping, HttpServletRequest request) {
super.reset(mapping, request);
_workingItemId = null;
}
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
List<LineItem> items = getCart().getLineItems();
if(items != null && items.size() > 0) {
for(int i = 0; i < items.size(); i++) {
errors.add("invalidQuantity" + i, new ActionError("order.error.invalidQuantity"));
}
}
return errors;
}
}
}