/* * Copyright 2004 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 webappRoot; import org.apache.beehive.netui.pageflow.annotations.Jpf; import org.apache.beehive.netui.pageflow.*; import org.apache.beehive.samples.petstore.forms.ReturnToForm; import org.apache.beehive.samples.petstore.forms.SearchForm; import org.apache.beehive.samples.petstore.model.Account; import org.apache.beehive.samples.petstore.model.Cart; import org.apache.beehive.samples.petstore.model.Category; import org.apache.beehive.samples.petstore.model.Product; import org.apache.beehive.samples.petstore.controls.AccountControl; import org.apache.beehive.samples.petstore.controls.CatalogControl; import org.apache.beehive.samples.petstore.controls.exceptions.NoSuchAccountException; import org.apache.beehive.samples.petstore.controls.exceptions.InvalidIdentifierException; /** * todo: one issue with exposing properties on a SharedFlow / GlobalApp is that they * are then bindable by all web clients; there needs to be a flag in netui-config.xml * that prevents this from happening. */ @Jpf.Controller( catches={ @Jpf.Catch(method="handlePageFlowException", type=PageFlowException.class), @Jpf.Catch(method="handleException", type=Exception.class), @Jpf.Catch(method="handleNotLoggedInException", type=NotLoggedInException.class) }, simpleActions={ @Jpf.SimpleAction(name="signoff", path="/auth/logout.do"), @Jpf.SimpleAction(name="globalShowHelp", path="/help.jsp"), @Jpf.SimpleAction(name="globalViewCart", path="/shop/viewCart.do"), @Jpf.SimpleAction(name="globalViewCategory", path="/shop/viewCategory.do"), @Jpf.SimpleAction(name="globalEditAccount", path="/account/edit/viewUpdateAccount.do"), @Jpf.SimpleAction(name="globalViewProductById", path="/shop/viewProduct.do"), @Jpf.SimpleAction(name="globalViewCreateAccount", path="/account/create/Controller.jpf") } ) public class SharedFlow extends SharedFlowController { @org.apache.beehive.controls.api.bean.Control() private CatalogControl _catalogControl; @org.apache.beehive.controls.api.bean.Control() private AccountControl _accountControl; private Account _account = null; private Cart _cart = null; private Product[] _myList; private static String[] _categoryNames = null; protected void onCreate() { // todo: move to ServletContext so it's cached for all users Category[] categories = _catalogControl.getCategoryList(); _categoryNames = new String[categories.length]; for(int i = 0; i < _categoryNames.length; i++) { _categoryNames[i] = categories[i].getCatId(); } } /** * Utility used to ensure that the user is logged into the site; an exception is * thrown when the the user is not logged in. */ public void ensureLogin() throws NotLoggedInException { if (!isUserLoggedIn()) throw new NotLoggedInException("User not logged in", this); } public void handleLogin(String username) { Account account = _accountControl.getAccount(username); // @todo: need to fix the case where the account isn't found for this username if(account == null) return; // and set the account (this toggles the user's logged in status) setAccount(account); // set the user's favorites list if they are logged in if (account.isMyListOpt()) { Product[] favProducts = _catalogControl.getProductListByCategory(account.getFavCategory()); setMyList(favProducts); } } public void handleLogout() { setAccount(null); setCart(null); } public void handleCheckout() { setCart(null); } public void updateAccount(Account account) throws InvalidIdentifierException, NoSuchAccountException { // update account info in globalApp _account = account; // set the user's favorites list if they are logged in if (account.isMyListOpt()) setMyList(_catalogControl.getProductListByCategory(account.getFavCategory())); } public String[] getCategoryNames() { return _categoryNames; } /** * Determine if the user is logged into the website. */ public boolean isUserLoggedIn() { return _account == null ? false : true; } public Product[] getMyList() { return _myList; } public Cart getCart() { if(_cart == null) _cart = new Cart(); return _cart; } public Account getAccount() { return _account; } private void setAccount(Account account) { _account = account; } private void setCart(Cart cart) { _cart = cart; } private void setMyList(Product[] myList) { _myList = myList; } @Jpf.Action( forwards={@Jpf.Forward(name="help", path="/help.jsp")} ) public Forward showHelp() { return new Forward("help"); } /** * This action is used to drop into the nested login controller * from a user click on a page */ @Jpf.Action( forwards={@Jpf.Forward(name="auth", path="/auth/Controller.jpf")} ) public Forward signon() { ReturnToForm initForm = new ReturnToForm(false); return new Forward("auth", initForm); } /** * This action is used to drop into the nested login controller * en-route to another action */ @Jpf.Action( forwards={@Jpf.Forward(name="auth", path="/auth/Controller.jpf")} ) public Forward actionSignon() { ReturnToForm initForm = new ReturnToForm(true); return new Forward("auth", initForm); } @Jpf.Action( forwards={@Jpf.Forward(name="search", path="/search/Controller.jpf")} ) public Forward search(SearchForm form) { return new Forward("search"); } @Jpf.Action( forwards={ @Jpf.Forward( redirect=true, name="shop", path="/shop/Controller.jpf" ) } ) public Forward globalShop() { return new Forward("shop"); } /** * after login, depending on what the user was doing, we may want to return * to current page or previous action */ @Jpf.Action( forwards={ @Jpf.Forward(name="previousAction", navigateTo=Jpf.NavigateTo.previousAction), @Jpf.Forward(name="currentPage", navigateTo=Jpf.NavigateTo.currentPage) } ) public Forward loginDone(ReturnToForm initForm) { if (initForm.getReturnToPreviousAction()) return new Forward("previousAction"); else return new Forward("currentPage"); } @Jpf.ExceptionHandler( forwards={ @Jpf.Forward(name="errorPage", path="/error.jsp") } ) protected Forward handleException(Exception ex, String actionName, String message, Object form) { System.err.print("[" + getRequest().getContextPath() + "] "); System.err.println("Unhandled exception caught in SharedFlow.jpfs:"); ex.printStackTrace(); return new Forward("errorPage"); } @Jpf.ExceptionHandler( forwards={ @Jpf.Forward(name="login", path="actionSignon.do") } ) protected Forward handleNotLoggedInException(NotLoggedInException ex, String actionName, String message, Object form) { return new Forward("login"); } @Jpf.ExceptionHandler() public Forward handlePageFlowException(PageFlowException ex, String message, String action, Object form) throws java.io.IOException { ex.sendError(getRequest(), getResponse()); return null; } }