📄 statemachine.java
字号:
package apusic.myshop.control.ejb;import java.rmi.RemoteException;import java.util.ArrayList;import java.util.AbstractList;import java.util.Collection;import java.util.Iterator;import java.util.HashMap;import javax.rmi.PortableRemoteObject;import javax.naming.Context;import javax.naming.InitialContext;import javax.naming.NamingException;import javax.ejb.CreateException;import javax.ejb.DuplicateKeyException;import javax.ejb.EJBException;import javax.ejb.FinderException;import apusic.myshop.control.event.BaseEvent;import apusic.myshop.control.event.CartEvent;import apusic.myshop.control.event.LoginEvent;import apusic.myshop.control.event.LogoutEvent;import apusic.myshop.control.event.CustomerEvent;import apusic.myshop.control.event.DBLoginEvent;import apusic.myshop.control.ejb.ShoppingClientControllerEJB;import apusic.myshop.control.EventException;import apusic.myshop.util.Debug;import apusic.myshop.cart.ejb.Cart;import apusic.myshop.util.SecurityAdapter;import apusic.myshop.customer.ejb.DuplicateAccountException;import apusic.myshop.util.JNDINames;import apusic.myshop.customer.ejb.CustomerHome;import apusic.myshop.customer.ejb.Customer;import apusic.myshop.util.EJBUtil;import apusic.myshop.control.LoginFailedException;import apusic.myshop.control.event.OrderEvent;import apusic.myshop.order.ejb.Order;import apusic.myshop.inventory.ejb.InventoryHome;import apusic.myshop.inventory.ejb.Inventory;import apusic.myshop.cart.model.CartItem;import apusic.myshop.cart.model.CartModel;import apusic.myshop.order.model.LineItem;import apusic.myshop.order.ejb.OrderHome;import apusic.myshop.util.JNDINames;public class StateMachine implements java.io.Serializable { private ShoppingClientControllerEJB sccejb; private ModelUpdateManager mum; private HashMap orderTable; public StateMachine(ShoppingClientControllerEJB sccejb) { this.sccejb = sccejb; this.mum = new ModelUpdateManager(); } public Collection handleEvent(BaseEvent be) throws RemoteException, EventException { Debug.println("StateMachine: received event= " + be); if (be instanceof CartEvent) { handleCartEvent((CartEvent)be); } else if (be instanceof CustomerEvent) { handleCustomerEvent((CustomerEvent)be); } else if (be instanceof DBLoginEvent) { Debug.println("<<<<<finding user..."); handleDBLoginEvent((DBLoginEvent)be); } else if (be instanceof LoginEvent) { Debug.println("<<<<<login user..."); // Nothing needs to be done. so ignore. }else if (be instanceof LogoutEvent) { // do noting. EJB is removed at the web tier by the RequestProcessor } else if (be instanceof OrderEvent) { handleOrderEvent((OrderEvent)be); } return (mum.getUpdatedModels(be)); } private void handleDBLoginEvent(DBLoginEvent dle) throws RemoteException, LoginFailedException { Debug.println("<<<<<finding user..."); try { CustomerHome home = EJBUtil.getCustomerHome(); Customer customer = home.findByUserId(dle.getUserId()); String password = customer.getPassword(); if (!password.equals(dle.getPassword())) { throw new LoginFailedException("密码不正确!"); } }catch(FinderException fe) { Debug.print("<<<<<can't find user..."); throw new LoginFailedException("你的账号还未注册!"); } } private void handleCustomerEvent(CustomerEvent ce) throws RemoteException, DuplicateAccountException { // currently handles only AccountEvent.CREATE_ACCOUNT event. switch (ce.getActionType()) { case CustomerEvent.CREATE_CUSTOMER: { try { SecurityAdapter securityAdapter = null; String securityAdapterClassName = null; try{ InitialContext ic = new InitialContext(); securityAdapterClassName = (String) ic.lookup(JNDINames.SECURITY_ADAPTER_CLASSNAME); } catch (NamingException ex) { Debug.println("StateMachine caught: " + ex); // ignore.. we are working around it below. } // use j2ee user manager if not specified if (securityAdapterClassName == null){ Debug.println("Security Adapter not specified in deployment descriptor: using default J2ee Security Adapter"); securityAdapterClassName = "apusic.myshop.util.ApusicSecurityAdapter"; } Debug.println("StateMachine: Security Adapter class name = " + securityAdapterClassName); try{ securityAdapter = (SecurityAdapter)Class.forName(securityAdapterClassName).newInstance(); if (securityAdapter != null) securityAdapter.addUser("default", "gold", ce.getUserId(), ce.getPassword()); } catch (java.lang.InstantiationException ie){ Debug.println("StateMachine caught: " + ie); } catch (java.lang.IllegalAccessException il){ Debug.println("StateMachine caught: " + il); } catch (java.lang.ClassNotFoundException e){ Debug.println("StateMachine caught: " + e); } CustomerHome home = EJBUtil.getCustomerHome(); home.create( ce.getUserId(), ce.getPassword(), ce.getName(), ce.getSex(), ce.getCompany(), ce.getCid(), ce.getAddress(), ce.getProvince(), ce.getCity(), ce.getZip(), ce.getPhone(), ce.getEmail(), ce.getRegDate() ); } catch (DuplicateKeyException dke) { throw new DuplicateAccountException("Customer Exists"); } catch (CreateException cre) { throw new EJBException(cre); } } break; default: Debug.print("Error: not implemented yet"); break; } } private void handleCartEvent(CartEvent ce) throws RemoteException { Cart cart = sccejb.getCart(); switch (ce.getActionType()) { case CartEvent.ADD_ITEM:{ Collection itemIds = ce.getItemIds(); Iterator it = itemIds.iterator(); while (it.hasNext()){ cart.addItem((String)it.next()); } } break; case CartEvent.DELETE_ITEM: { Collection itemIds = ce.getItemIds(); Iterator it = itemIds.iterator(); while (it.hasNext()){ cart.deleteItem((String)it.next()); } } break; case CartEvent.UPDATE_ITEM :{ Collection itemIds = ce.getItemIds(); Iterator it = itemIds.iterator(); while (it.hasNext()){ String itemId = (String)it.next(); int quantity = ce.getItemQty(itemId); // change the quanty or delete the item if the item quantity is less than or equal to 0 if (quantity > 0){ cart.updateItemQty(itemId, quantity); } else { cart.deleteItem(itemId); } } } break; } } public int getOrderId(int requestId) { if (orderTable != null) { if (orderTable.containsKey(requestId + "")) { return Integer.parseInt((String)orderTable.get(requestId + "")); } } return -1; } private void handleOrderEvent(OrderEvent oe) throws RemoteException { switch (oe.getActionType()) { case OrderEvent.CREATE_ORDER: createOrder(oe); break; default: Debug.print("Error: Not implemented yet!"); break; } } private Order createOrder(OrderEvent oe) throws RemoteException { Cart cart = sccejb.getCart(); Order order = null; try { InventoryHome inventHome = EJBUtil.getInventoryHome(); Iterator ci = ((CartModel)cart.getDetails()).getItems(); ArrayList lineItems = new ArrayList(); int lineNo = 0; double total = 0; while (ci.hasNext()) { lineNo++; CartItem cartItem = (CartItem) ci.next(); LineItem li = new LineItem(cartItem.getItemId(), cartItem.getQuantity(), cartItem.getUnitCost(), lineNo); lineItems.add(li); total += (cartItem.getUnitCost() * (double) cartItem.getQuantity()); } /* for (Iterator it = lineItems.iterator(); it.hasNext();){ LineItem LI = (LineItem)it.next(); Inventory inventRef = inventHome.findByProductId(LI.getItemNo()); inventRef.updateQty(LI.getQty()); } */ OrderHome home = EJBUtil.getOrderHome(); order = home.create(oe.getUserId(), oe.getShipName(), oe.getShipAddr(), oe.getShipProvince(), oe.getShipCity(), oe.getShipZip(), oe.getShipCountry(), oe.getBillName(), oe.getBillAddr(), oe.getBillProvince(), oe.getBillCity(), oe.getBillZip(), oe.getBillCountry(), oe.getCreditCard(), total, lineItems); Debug.println("Created the order"); Debug.println("StateMachine;OrderId= " + order.getDetails().getOrderId()); // put the requestId and the orderId in a table to match up later if (orderTable == null) orderTable = new HashMap(); orderTable.put(oe.getRequestId() + "", order.getDetails().getOrderId() +""); // empty shopping cart cart.empty(); /* if (JNDIUtil.sendConfirmationMail()) { // send order confirmation mail. Mailer mailer = EJBUtil.createMailerEJB(); mailer.sendOrderConfirmationMail(order.getDetails().getOrderId()); } */ } catch (DuplicateKeyException dke) { // How can another order have the same id ? throw new EJBException(dke); } catch (CreateException ce) { ce.printStackTrace(); throw new EJBException(ce); /* } catch (FinderException fe) { throw new EJBException(fe); */ } return order; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -