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

📄 itemejb.java

📁 《J2EE企业级应用开发》一书的配套源代码
💻 JAVA
字号:
package com.j2eeapp.cdstore.storage;

import java.rmi.RemoteException;
import javax.ejb.*;
import com.j2eeapp.cdstore.vo.Item;
import com.j2eeapp.cdstore.servicelovator.*;
import com.j2eeapp.cdstore.util.JNDINames;

public abstract class ItemEJB implements EntityBean
{
    /**
     * Attributes declaration
    */
    public EntityContext EJB_Context;
    EJBServiceLocator sl;
    
    /**
     * @J2EE_METHOD  --  ItemEJB
     */
    public ItemEJB    () throws ServiceLocatorException 
    { 
		 sl=new EJBServiceLocator();
    }
    
    /**
     * @J2EE_METHOD  --  ejbCreate
     * Matching method of the create() method of the bean's home interface. The container
     * invokes an ejbCreate method to create an entity object. It executes in the transaction
     * context determined by the transaction attribute of the matching create() method.
     * @throws javax.ejb.CreateException
     */
    public String ejbCreate    (Item item) throws CreateException 
    { 
		setItemId(item.getItemId());
		setAttr(item.getAttr());
		setImageLocation(item.getImageLocation());
		setUnitPrice(item.getUnitPrice());
		return null;
    }
    
    /**
     * @J2EE_METHOD  --  ejbPostCreate
     * Matching method of ejbCreate. The container invokes the matching ejbPostCreate method
     * on an instance after it invokes the ejbCreate method with the same arguments. It
     * executes in the same transaction context as that of the matching ejbCreate method.
     * @throws javax.ejb.CreateException
     */
    public void ejbPostCreate    (Item item) throws CreateException 
    { 
		setSupplierHelper(item);
		setProductHelper(item);		
    }
    
    /**
     * @J2EE_METHOD  --  ejbActivate
     * A container invokes this method when the instance is taken out of the pool of available
     * instances to become associated with a specific EJB object. This method transitions
     * the instance to the ready state. This method executes in an unspecified transaction
     * context.
     */
    public void ejbActivate    ()  
    { 

    }
    
    /**
     * @J2EE_METHOD  --  ejbPassivate
     * A container invokes this method on an instance before the instance becomes disassociated
     * with a specific EJB object. After this method completes, the container will place
     * the instance into the pool of available instances. This method executes in an unspecified
     * transaction context.
     */
    public void ejbPassivate    ()  
    { 

    }
    
    /**
     * @J2EE_METHOD  --  ejbLoad
     * A container invokes this method to instruct the instance to synchronize its state
     * by loading it from the underlying database. This method always executes in the transaction
     * context determined by the value of the transaction attribute in the deployment descriptor.
     */
    public void ejbLoad    ()  
    { 

    }
    
    /**
     * @J2EE_METHOD  --  ejbStore
     * A container invokes this method to instruct the instance to synchronize its state
     * by storing it to the underlying database. This method always executes in the transaction
     * context determined by the value of the transaction attribute in the deployment descriptor.
     */
    public void ejbStore    ()  
    { 

    }
    
    /**
     * @J2EE_METHOD  --  ejbRemove
     * A container invokes this method before it removes the EJB object that is currently
     * associated with the instance. It is invoked when a client invokes a remove operation
     * on the enterprise Bean's home or remote interface. It transitions the instance from
     * the ready state to the pool of available instances. It is called in the transaction
     * context of the remove operation.
     * @throws javax.ejb.RemoveException
     */
    public void ejbRemove    () throws RemoveException 
    { 

    }
    
    /**
     * @J2EE_METHOD  --  setEntityContext
     * Set the associated entity context. The container invokes this method on an instance
     * after the instance has been created. This method is called in an unspecified transaction
     * context. 
     */
    public void setEntityContext    (EntityContext ctx)  
    { 
		EJB_Context=ctx;
    }
    
    /**
     * @J2EE_METHOD  --  unsetEntityContext
     * Unset the associated entity context. The container calls this method before removing
     * the instance. This is the last method that the container invokes on the instance.
     * The Java garbage collector will  invoke the finalize() method on the instance. It
     * is called in an unspecified transaction context.
     */
    public void unsetEntityContext    ()  
    { 
		EJB_Context=null;
    }
    
    /**
     * @J2EE_METHOD  --  getItemId
     */
    public abstract String getItemId    (); 
    
    /**
     * @J2EE_METHOD  --  setItemId
     */
    public abstract void setItemId    (String newItemId); 
    
    /**
     * @J2EE_METHOD  --  getProduct
     */
    public abstract ProductLocal getProduct    (); 
    
    /**
     * @J2EE_METHOD  --  setProduct
     */
    public abstract void setProduct    (ProductLocal newProduct); 
    
    
    /**
     * @J2EE_METHOD  --  getUnitPrice
     */
    public abstract float getUnitPrice    (); 
    
    /**
     * @J2EE_METHOD  --  setUnitPrice
     */
    public abstract void setUnitPrice    (float newUnitPrice); 
    
    /**
     * @J2EE_METHOD  --  getSupplier
     */
    public abstract SupplierLocal getSupplier    (); 
    
    /**
     * @J2EE_METHOD  --  setSupplier
     */
    public abstract void setSupplier    (SupplierLocal newSupplier); 
    
        
    /**
     * @J2EE_METHOD  --  getAttr
     */
    public abstract String getAttr    (); 
    
    /**
     * @J2EE_METHOD  --  setAttr
     */
    public abstract void setAttr    (String newAttr); 
    
    /**
     * @J2EE_METHOD  --  getImageLocation
     */
    public abstract String getImageLocation     ();
    
    /**
     * @J2EE_METHOD  --  setImageLocation
     */
    public abstract void setImageLocation     (String imageLocation ) ;
    
    //helper method
    public void setSupplierHelper(Item item)
    {
    	try
		{
			
			SupplierLocalHome slh=(SupplierLocalHome)sl.getLocalHome(JNDINames.SUPPLIER);
			SupplierLocal sup=slh.findByPrimaryKey(item.getSupplierId());
			if(sup!=null)
			{
				setSupplier(sup);
			}
			else
			{
				sup=slh.create(item.getSupplierId());
				setSupplier(sup);
			}
		}
		catch(ServiceLocatorException e)
		{
			System.out.println("cant find service"+JNDINames.SUPPLIER);
		}
		catch(FinderException e)
		{
			System.out.println("cant find supplier"+item.getSupplierId());
		}
		catch(CreateException e)
		{
		}
	}
	private void setProductHelper(Item item)
	{
		try
		{
			
			ProductLocalHome slh=(ProductLocalHome)sl.getLocalHome(JNDINames.PRODUCT);
			ProductLocal sup=slh.findByPrimaryKey(item.getProductId());
			if(sup!=null)
			{
				setProduct(sup);
			}
			else
			{
				sup=slh.create(item.getProductId());
				setProduct(sup);
			}
		}
		catch(ServiceLocatorException e)
		{
			System.out.println("cant find service"+JNDINames.PRODUCT);
		}
		catch(FinderException e)
		{
			System.out.println("cant find supplier"+item.getProductId());
		}
		catch(CreateException e)
		{
		}
	}
    
    public Item getData()
    {
    	return new Item(getProduct().getProductId(),
    					"supplier",//getSupplier().getSupplierId(),
    					getItemId(),
    					getAttr(),
    					getUnitPrice(),
    					getProduct().getCategory().getCateId(),
    					getProduct().getName(),
    					getImageLocation());
    }
}

⌨️ 快捷键说明

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