AdminBean.ejb Sample

This topic inludes the source code for the AdminBean.ejb Sample.

Sample Location

This sample is located in the following directory in your WebLogic Workshop installation:

BEA_HOME/weblogic81/samples/workshop/SamplesApp/EJBs/valueObject/

Sample Source Code


001 package valueObject;
002 
003 import finderMethods.*;
004 import java.rmi.RemoteException;
005 import java.util.ArrayList;
006 import java.util.Collection;
007 import java.util.Iterator;
008 import javax.ejb.*;
009 import javax.naming.InitialContext;
010 import javax.naming.NamingException;
011 import weblogic.ejb.*;
012 
013 /**
014  * @ejbgen:session default-transaction="Supports"
015  *   ejb-name = "Admin"
016  *
017  * @ejbgen:jndi-name
018  *   remote = "ejb.AdminRemoteHome"
019  *
020  * @ejbgen:file-generation remote-class = "true" remote-class-name = "Admin" remote-home = "true" remote-home-name = "AdminHome" local-class = "false" local-class-name = "AdminLocal" local-home = "false" local-home-name = "AdminLocalHome"
021  * @ejbgen:ejb-local-ref link="ManufacturerBean_F"
022  * @ejbgen:value-object reference="Value"
023  */
024 public class AdminBean
025   extends GenericSessionBean
026   implements SessionBean
027 {
028   private ManufacturerBean_FHome manuHome;
029   
030   public void ejbCreate() {
031      try{
032         javax.naming.Context ic = new InitialContext();
033         manuHome = (ManufacturerBean_FHome)ic.lookup("java:comp/env/ejb/ManufacturerBean_F");
034      }
035      catch(NamingException ne) {
036          throw new EJBException(ne);
037      }
038   }
039 
040 
041     /**
042      * This method receives a ManufacturerVO and uses the contained information
043      * to create a new manufacturer.
044      * @ejbgen:remote-method
045      */
046     public void addManufacturerVO(ManufacturerVO mVO)
047     {
048       try {
049          manuHome.create(mVO.getManufacturerId(),mVO.getManufacturername(), mVO.getUsManufacturer());
050       
051       catch (CreateException ce) {
052          throw new EJBException(ce);
053       }
054     }
055 
056     /**
057      * This method returns all knows manufacturers as a collection of manufacturerVOs.
058      * The information specifed in the CMR field items is not entered in the VO, because
059      * this information is not necessary in the page flow.
060      * @ejbgen:remote-method
061      */
062     public Collection getManufacturersVO()
063     {
064        ManufacturerBean_FLocal manufacturer;
065        ManufacturerVO mVO;
066        ArrayList items = new ArrayList();
067        
068        try{
069         Iterator manuIter = manuHome.findAll().iterator();
070         while (manuIter.hasNext()) {
071            manufacturer = (ManufacturerBean_FLocalmanuIter.next();
072            mVO = new ManufacturerVO(null,manufacturer.getManufacturerId(), manufacturer.getManufacturername(), manufacturer.getUsManufacturer());
073            items.add(mVO);
074         }    
075         return items;
076        }
077        catch(FinderException fe) {
078           throw new EJBException(fe);
079        }
080     }
081 
082     /**
083      * This method returns a ManufacturerVO with all fields specified for the manufacturer. 
084      * The related entity beans 'items' are in turn represented as a collection of
085      * value objects in ManufacturerVO. 
086      * @ejbgen:remote-method
087      */
088     public ManufacturerVO getManufacturer(Integer PK)
089     {
090        ManufacturerBean_FLocal manufacturer;
091        ManufacturerVO mVO;
092        Collection itemsVO;   
093        Iterator itemsIter;  
094        Items_F oneItem;  
095 
096        try {
097           manufacturer = manuHome.findByPrimaryKey(PK);
098           itemsIter = manufacturer.getItems().iterator();
099           itemsVO = new ArrayList();
100           while(itemsIter.hasNext()) {
101              oneItem = (Items_FitemsIter.next();
102              itemsVO.add(new ItemVO(oneItem.getItemname(), oneItem.getItemnumber(), null, oneItem.getPrice(), oneItem.getQuantityavailable()));
103           }
104           mVO = new ManufacturerVO(itemsVO,manufacturer.getManufacturerId(), manufacturer.getManufacturername(), manufacturer.getUsManufacturer());
105           return mVO;
106        }
107        catch(FinderException fe) {
108           throw new EJBException(fe);
109        }
110     }
111  }
112