⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 shoppingsession.java

📁 《J2EE企业级应用开发》一书的配套源代码
💻 JAVA
字号:
//Source file: H:\\book\\case\\com\\j2eeapp\\cdstore\\bean\\ShoppingSession.java

package com.j2eeapp.cdstore.bean;
import java.util.*;
import java.rmi.*;
import javax.naming.*;
import javax.ejb.*;
import com.j2eeapp.cdstore.util.JNDINames;
import com.j2eeapp.cdstore.vo.*;
import com.j2eeapp.cdstore.servicelovator.*;
import com.j2eeapp.cdstore.cart.*;
import com.j2eeapp.cdstore.signon.*;
import com.j2eeapp.cdstore.storage.*;
import com.j2eeapp.cdstore.order.*;

public class ShoppingSession 
{
   EJBServiceLocator sl;
   ShoppingFacade facade;
   ShoppingCart cart;
   String searchString=null ;
   boolean isLogin=false;
   Collection searchResults=null;
   public ShoppingSession() 
   {
      try {
      init();
      ShoppingFacadeHome facadeHome = (ShoppingFacadeHome) sl.getRemoteHome(JNDINames.SHOPPING_FACADE,ShoppingFacadeHome.class);
      facade = facadeHome.create();
      System.out.println("Instantiated sessionBean and Cart reference");
      cart=facade.getShoppingCart();
     }
     catch (Exception ex) {
      ex.printStackTrace();
     }
  }
  
    public boolean login(String user, String password) {
    try
    {
      SignonLocalHome signonHome=(SignonLocalHome)sl.getLocalHome(JNDINames.SIGNON);
      SignonLocal signon=signonHome.create();
      facade.setUserId(user);
      return isLogin=signon.authenticate(user,password);
    }
    catch(Exception ex) {
      return false;
    }
  }
  public void logOut() {
   

  }
  public boolean isLoggedIn() {
    return isLogin;
  }
  public String getUserId() {
    try
    {
      return facade.getUserId();
      
    }
    catch (Exception ex) {
      ex.printStackTrace();
      return null;
    }
  }
   
   public void addItem(String itemid) 
   {
    try {
      cart.addItem(itemid);
    }
    catch (Exception ex) {
      throw new RuntimeException(ex.toString());
    }
   }
   
    public void addItem(String itemid,int qty) 
   {
    try {
      cart.addItem(itemid,qty);
    }
    catch (Exception ex) {
      throw new RuntimeException(ex.toString());
    }
   }
   //增加一个用户
   public void addUser(String username,String password) throws ServiceLocatorException,javax.ejb.CreateException
   {
      SignonLocalHome signonHome=(SignonLocalHome)sl.getLocalHome(JNDINames.SIGNON);
      SignonLocal signon=signonHome.create();
      signon.createUser(username,password);
      isLogin=true;
   }
   
   public void updateItem(String itemId,int newQty) 
   {
    try {
      cart.updateItemQuantity(itemId,newQty);
    }
    catch (Exception ex) {
      throw new RuntimeException(ex.toString());
    }
   }
   
   public void removeItem(String itemid) 
   {
	   	 try {
	      cart.deleteItem(itemid);
    }
    catch (Exception ex) {
      throw new RuntimeException(ex.toString());
    }
    
   }
   
   public void setSearchString(String search) 
   {
   		this.searchString=search;
    
   }
   
   public String  getSearchString() 
   {
    	return searchString;
   }
   //结帐
   public String checkOut(ContactInfo contactInfo,ShippingInfo shippingInfo,BillingInfo billingInfo,CreditCard card,String cardPassword) throws RemoteException
   {
      try
      {
      	
	      String re=cart.checkOut(contactInfo,shippingInfo,billingInfo,card,cardPassword,facade.getUserId());
	      cart.empty();
	      return re;
	     }
	     catch(Exception e)
	     {
	     	System.out.println(e.getMessage());
	     	return null;
	     }
   }
   //获得所有category
   public Collection getCategory()throws FinderException,ServiceLocatorException
   {
   	 try
   	 {
   	 	return facade.getCategory();
   	 }
   	 catch(Exception e)
   	 {
   	 	return null;
   	 }
   }
   //按category获得产品信息
   public Collection getProductByCategory(String category)throws FinderException,ServiceLocatorException
   {
   	   ProductLocalHome productHome=(ProductLocalHome)sl.getLocalHome(JNDINames.PRODUCT);
   	   ItemLocalHome itemHome=(ItemLocalHome)sl.getLocalHome(JNDINames.ITEMLOCAL);
   	   String itemId;
   		Collection co=productHome.findProductByCategory(category);
   		Iterator it=co.iterator();
   		Collection re=new ArrayList();
   		while(it.hasNext())
   		{
   			ProductLocal product=(ProductLocal)it.next();
   			itemId=itemHome.findByProductId(product.getProductId()).getItemId();
   			re.add(new ProductDescription(product.getProductId(),product.getName(),product.getDescription(),itemId));
   			
   		}
   		return re;
   	}
   	//按产品id获得产品信息
   public Product getProductById(String productId)throws NamingException,FinderException
   {
   	   ProductLocalHome productHome=(ProductLocalHome)new InitialContext().lookup(JNDINames.PRODUCT);
   	   ProductLocal productLocal=productHome.findByPrimaryKey(productId);
   	   Product productVO=new Product(productLocal.getProductId(),productLocal.getCategory().getCateId(),
   	   			productLocal.getName(),productLocal.getDescription(),(Collection)productLocal.getMediaList());
   	   	return productVO;
   }   	
   	 	
      
   public void init() throws ServiceLocatorException
   {
     sl=new EJBServiceLocator();
   }
   public Item getItemInfo(String productId)throws NamingException,FinderException
   {
   		ItemLocalHome itemHome=(ItemLocalHome)new InitialContext().lookup(JNDINames.ITEMLOCAL);
   		return itemHome.findByProductId(productId).getData();
   	}
   	
   	public Collection getItems()throws RemoteException,ServiceLocatorException
   	{
   		try
   		{
   			return cart.getItems();
   		}
   		catch(Exception e)
   		{
   			return null;
   		}
   	
   	} 
   	public void empty()throws RemoteException
   	{
   		cart.empty();
   	}
   	public int getCount()throws RemoteException
   	{
   		return cart.getCount().intValue();
   	}
   	//获得购物车里的商品总金额
   	public double getSubTotal()throws RemoteException,ServiceLocatorException
   	{
   		try
   		{
   			return cart.getSubTotal();
   		}
   		catch(Exception e)
   		{
   			return 0.0;
   		}
   	}
   	//获得客户的联系信息
   	public ContactInfo getContactInfo()throws RemoteException
   	{
   		try
   		{
   			return facade.getCustomer().getAccount().getContactInfo().getData();
   		}
   		catch(Exception e)
   		{
   			return null;
   		}
   	}
   	//获得客户的信用卡信息
   	public  CreditCard getCreditCard()throws RemoteException
   	{
   		try
   		{
   			return facade.getCustomer().getAccount().getCreditCard().getData();
   		}
   		catch(Exception e)
   		{
   			return null;
   		}
   	}
   	//获得关于某个客户的AllUnPassedAndUnCanceled订单
   	public Collection getOrderStatus()throws RemoteException,ServiceLocatorException
   	{
   		try
   		{
   			OrderLocalHome orderHome=(OrderLocalHome)sl.getLocalHome(JNDINames.ORDER);
	   		Collection orders=orderHome.findAllUnPassedAndUnCanceled(facade.getUserId());
	   		Collection re=new ArrayList();
	   		OrderStatus temp=null;
	   		OrderLocal orderLocal=null;
	   		Iterator it=orders.iterator();
	   		while(it.hasNext())
	   		{
	   			orderLocal=(OrderLocal)it.next();
	   			temp=orderLocal.getOrderStatus().getData();
	   			temp.setOrderId(orderLocal.getOrderId());
	   			re.add(temp);
	   		}
	   		return re;
	   	}
	   	catch(Exception e)
	   	{
	   		return null;
	   	}
   } 
   //取消所有订单  		
   	public void cancelAllOrder()throws RemoteException
   	{
   		try
   		{
   			facade.cancelAllOrder();
   		}
   		catch(Exception e)
   		{
   			System.out.println(e.getMessage());
   		}
   	}
   	//取消某个 order
   	public void cancelOrder(String orderId)throws RemoteException
   	{
   		try
   		{
   			facade.cancelOrder(orderId);
   		}
   		catch(Exception e)
   		{
   			System.out.println(e.getMessage());
   		}
   	}
   	//获得某个order 的所有lineitem
   	public Collection getOrderLineItem(String orderId)
   	{
   		return facade.getAllItems(orderId);
   	}
   	public void search()throws FinderException,ServiceLocatorException
   	{
   	     
   		 ProductLocalHome productHome=(ProductLocalHome)sl.getLocalHome(JNDINames.PRODUCT);
   	     ItemLocalHome itemHome=(ItemLocalHome)sl.getLocalHome(JNDINames.ITEMLOCAL);
   	     String itemId;
   	      Collection co=productHome.findProductByName(this.getSearchString());
   		 Iterator it=co.iterator();
   		 Collection re=new ArrayList();
   	     while(it.hasNext())
   		 {
   			ProductLocal product=(ProductLocal)it.next();
   			itemId=itemHome.findByProductId(product.getProductId()).getItemId();
   			re.add(new ProductDescription(product.getProductId(),product.getName(),product.getDescription(),itemId));
   			
   		 }
   		 this.searchResults=re;   		
   	}
   	public Collection getSearchResults()
   	{
   		return this.searchResults;
   	}
   				
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -