📄 shoppingfacadeejb.java
字号:
package com.j2eeapp.cdstore.cart;
import com.j2eeapp.cdstore.customer.*;
import com.j2eeapp.cdstore.servicelocator.*;
import com.j2eeapp.cdstore.util.*;
import com.j2eeapp.cdstore.storage.*;
import com.j2eeapp.cdstore.vo.*;
import com.j2eeapp.cdstore.order.*;
import java.rmi.RemoteException;
import javax.ejb.*;
import java.util.*;
public class ShoppingFacadeEJB implements SessionBean
{
/**
* Attributes declaration
*/
public SessionContext EJB_Context = null;
private String userId;
private ShoppingCart cart;
private CustomerLocal customer;
EJBServiceLocator sl;
/**
* @J2EE_METHOD -- ShoppingFacadeEJB
*/
public ShoppingFacadeEJB () throws ServiceLocatorException
{
sl=new EJBServiceLocator();
}
/**
* @J2EE_METHOD -- ejbCreate
* Called by the container to create a session bean instance. Its parameters typically
* contain the information the client uses to customize the bean instance for its use.
* It requires a matching pair in the bean class and its home interface.
*/
public void ejbCreate ()
{
}
/**
* @J2EE_METHOD -- ejbRemove
* A container invokes this method before it ends the life of the session object. This
* happens as a result of a client's invoking a remove operation, or when a container
* decides to terminate the session object after a timeout. This method is called with
* no transaction context.
*/
public void ejbRemove ()
{
}
/**
* @J2EE_METHOD -- ejbActivate
* The activate method is called when the instance is activated from its 'passive' state.
* The instance should acquire any resource that it has released earlier in the ejbPassivate()
* method. This method is called with no transaction context.
*/
public void ejbActivate ()
{
}
/**
* @J2EE_METHOD -- ejbPassivate
* The passivate method is called before the instance enters the 'passive' state. The
* instance should release any resources that it can re-acquire later in the ejbActivate()
* method. After the passivate method completes, the instance must be in a state that
* allows the container to use the Java Serialization protocol to externalize and store
* away the instance's state. This method is called with no transaction context.
*/
public void ejbPassivate ()
{
}
/**
* @J2EE_METHOD -- setSessionContext
* Set the associated session context. The container calls this method after the instance
* creation. The enterprise Bean instance should store the reference to the context
* object in an instance variable. This method is called with no transaction context.
*/
public void setSessionContext (SessionContext sc)
{
this.EJB_Context=sc;
}
/**
* @J2EE_METHOD -- setUserId
*/
public void setUserId (String userId)
{
this.userId = userId;
}
/**
* @J2EE_METHOD -- getUserId
*/
public String getUserId ()
{
return userId;
}
/**
* @J2EE_METHOD -- getShoppingCart
*/
public ShoppingCart getShoppingCart ()
{
if (cart == null) {
try {
EJBServiceLocator sl = new EJBServiceLocator();
ShoppingCartHome home =(ShoppingCartHome)sl.getRemoteHome(JNDINames.SHOPPING_CART,ShoppingCartHome.class);
cart = home.create();
} catch (javax.ejb.CreateException cx) {
System.out.println("ShoppingClientFacade: failed to create cart: caught " + cx);
} catch (ServiceLocatorException slx) {
System.out.println("ShoppingClientFacade: failed to look up name of cart: caught " + slx);
}
catch(RemoteException e)
{
System.out.println(e.getMessage());
}
}
return cart;
}
/**
* @J2EE_METHOD -- getCustomer
*/
public CustomerLocal getCustomer ()
{
if (userId == null) {
System.out.println("ShoppingClientFacade: failed to look up name of customer: userId is not set" );
}
try {
EJBServiceLocator sl = new EJBServiceLocator();
CustomerLocalHome home =(CustomerLocalHome)sl.getLocalHome(JNDINames.CUSTOMER);
customer = home.findByPrimaryKey(userId);
} catch (ServiceLocatorException slx) {
System.out.println("ShoppingClientFacade: failed to look up name of customer: caught " + slx);
}
catch(FinderException e)
{
System.out.println("can't find customer"+userId+e.getMessage());
}
return customer;
}
/**
* @J2EE_METHOD -- createCustomer
*/
public CustomerLocal createCustomer ()
{
try {
EJBServiceLocator sl = new EJBServiceLocator();
CustomerLocalHome home =(CustomerLocalHome)sl.getLocalHome(JNDINames.CUSTOMER);
customer = home.create(userId);
this.userId = userId;
} catch (javax.ejb.CreateException ce) {
System.out.println("ShoppingClientFacade: failed to create customer: caught " + ce);
} catch (ServiceLocatorException slx) {
System.out.println("ShoppingClientFacade: failed to look up name of customer: caught " + slx);
}
return customer;
}
public Collection getCategory()throws javax.ejb.FinderException,ServiceLocatorException
{
CategoryLocalHome cateHome= (CategoryLocalHome) sl.getLocalHome(JNDINames.CATEGORY);
Collection c=cateHome.findAllCategorys();
Iterator it=c.iterator();
Collection cate=new ArrayList();
while(it.hasNext())
{
CategoryLocal category=(CategoryLocal)it.next();
cate.add(new Category(category.getCateId(),category.getName(),category.getDescription()));
}
return cate;
}
public Collection getProductByCategory(String category)throws javax.ejb.FinderException,ServiceLocatorException
{
ProductLocalHome productHome=(ProductLocalHome)sl.getLocalHome(JNDINames.PRODUCT);
Collection co=productHome.findProductByCategory(category);
Iterator it=co.iterator();
Collection re=new ArrayList();
while(it.hasNext())
{
ProductLocal product=(ProductLocal)it.next();
re.add(new ProductDescription(product.getProductId(),product.getName(),product.getDescription(),""));
}
return re;
}
public void cancelAllOrder()throws FinderException,ServiceLocatorException
{
try
{
OrderLocalHome orderHome=(OrderLocalHome)sl.getLocalHome(JNDINames.ORDER);
Collection orders=orderHome.findAllUnPassedAndUnCanceled(getUserId());
OrderLocal orderLocal;
Iterator it=orders.iterator();
while(it.hasNext())
{
orderLocal=(OrderLocal)it.next();
orderLocal.getOrderStatus().setStatus(OrderStatusLocalHome.ORDER_STATUS_CANCELED);
}
}
catch(Exception e)
{
}
}
public void cancelOrder(String orderId)throws FinderException,ServiceLocatorException
{
try
{
OrderLocalHome orderHome=(OrderLocalHome)sl.getLocalHome(JNDINames.ORDER);
OrderLocal orderLocal=orderHome.findByPrimaryKey(orderId);
orderLocal.getOrderStatus().setStatus(OrderStatusLocalHome.ORDER_STATUS_CANCELED);
}
catch(Exception e)
{
}
}
public Collection getAllItems(String orderId)
{
try
{
OrderLocalHome orderHome=(OrderLocalHome)sl.getLocalHome(JNDINames.ORDER);
OrderLocal orderLocal=orderHome.findByPrimaryKey(orderId);
return orderLocal.getAllItems();
}
catch(Exception e)
{
System.out.println(e.getMessage());
return null;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -