KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > foedeployer > ejb > o2mb > O2MBManagerBean


1 /*
2 * JBoss, the OpenSource EJB server
3 *
4 * Distributable under LGPL license.
5 * See terms of license at gnu.org.
6 */

7 package org.jboss.test.foedeployer.ejb.o2mb;
8
9
10 import java.sql.Date JavaDoc;
11 import java.util.Collection JavaDoc;
12 import java.util.HashSet JavaDoc;
13 import java.util.Set JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.ArrayList JavaDoc;
16
17 import java.rmi.RemoteException JavaDoc;
18
19 import javax.ejb.SessionBean JavaDoc;
20 import javax.ejb.SessionContext JavaDoc;
21 import javax.ejb.EJBException JavaDoc;
22 import javax.ejb.CreateException JavaDoc;
23 import javax.ejb.FinderException JavaDoc;
24 import javax.ejb.RemoveException JavaDoc;
25
26 import javax.naming.Context JavaDoc;
27 import javax.naming.InitialContext JavaDoc;
28 import javax.naming.NamingException JavaDoc;
29
30 import javax.rmi.PortableRemoteObject JavaDoc;
31
32 import org.apache.log4j.Category;
33
34
35 /**
36  * Manager session bean.
37  *
38  * @ejb.bean
39  * type="Stateless"
40  * name="O2MBManager"
41  * jndi-name="O2MBManagerEJB.O2MBManagerHome"
42  * generate="true"
43  * view-type="remote"
44  *
45  * @ejb.ejb-ref
46  * ejb-name="Company"
47  * view-type="local"
48  * @ejb.ejb-ref
49  * ejb-name="Employee"
50  * view-type="local"
51  *
52  * @ejb.transaction type="Required"
53  */

54 public class O2MBManagerBean
55    implements SessionBean JavaDoc
56 {
57    // Attributes --------------------------------------------------
58
static Category log = Category.getInstance( O2MBManagerBean.class );
59
60    static String JavaDoc COMPANY_NAME = "java:comp/env/ejb/Company";
61    static String JavaDoc EMPLOYEE_NAME = "java:comp/env/ejb/Employee";
62
63    private CompanyLocalHome companyHome;
64    private EmployeeLocalHome employeeHome;
65
66    // Business methods ---------------------------------------------
67
/**
68     * Creates a company
69     *
70     * @ejb.interface-method
71     */

72    public void createCompany( String JavaDoc companyName )
73    {
74       try
75       {
76          companyHome.create( companyName );
77       }
78       catch( CreateException JavaDoc ce )
79       {
80          throw new EJBException JavaDoc( ce );
81       }
82    }
83
84    /**
85     * Creates an employee
86     *
87     * @ejb.interface-method
88     */

89    public void createEmployee( String JavaDoc employeeName )
90    {
91       try
92       {
93          employeeHome.create( employeeName );
94       }
95       catch( CreateException JavaDoc ce )
96       {
97          throw new EJBException JavaDoc( ce );
98       }
99    }
100
101    /**
102     * Returns all the companies
103     *
104     * @ejb.interface-method
105     */

106    public Collection JavaDoc getEmployeesForCompany( String JavaDoc companyName )
107    {
108       try
109       {
110          CompanyLocal company = companyHome.findByPrimaryKey( companyName );
111          Collection JavaDoc emps = new ArrayList JavaDoc();
112          for(Iterator JavaDoc iter=company.getEmployees().iterator(); iter.hasNext();)
113          {
114             EmployeeLocal employee = (EmployeeLocal) iter.next();
115             emps.add( employee.getName() );
116          }
117          return emps;
118       }
119       catch( FinderException JavaDoc fe )
120       {
121         throw new EJBException JavaDoc( fe );
122       }
123    }
124
125    /**
126     * Returns emaployee's company
127     *
128     * @ejb.interface-method
129     */

130    public String JavaDoc getCompanyForEmployee( String JavaDoc employeeName )
131    {
132       try
133       {
134          CompanyLocal company = employeeHome.findByPrimaryKey( employeeName ).
135                                    getCompany();
136          if( company == null ) return null;
137          return company.getName();
138       }
139       catch( FinderException JavaDoc fe )
140       {
141         throw new EJBException JavaDoc( fe );
142       }
143    }
144
145    /**
146     * Creates new emaployee and adds it to a company
147     *
148     * @ejb.interface-method
149     */

150    public void createEmployeeForCompany(
151       String JavaDoc employeeName, String JavaDoc companyName )
152    {
153       try
154       {
155          CompanyLocal company = companyHome.findByPrimaryKey( companyName );
156          EmployeeLocal employee = employeeHome.create( employeeName );
157          company.getEmployees().add( employee );
158       }
159       catch( Exception JavaDoc e )
160       {
161         throw new EJBException JavaDoc( e );
162       }
163    }
164
165    /**
166     * Sets a company for employee
167     *
168     * @ejb.interface-method
169     */

170    public void employ( String JavaDoc employeeName, String JavaDoc companyName )
171    {
172       try
173       {
174          CompanyLocal company = companyHome.findByPrimaryKey( companyName );
175          EmployeeLocal employee = employeeHome.findByPrimaryKey( employeeName );
176          employee.setCompany( company );
177       }
178       catch( Exception JavaDoc e )
179       {
180         throw new EJBException JavaDoc( e );
181       }
182    }
183
184    /**
185     * Removes a company
186     *
187     * @ejb.interface-method
188     */

189    public void removeCompany( String JavaDoc companyName )
190    {
191       try
192       {
193          CompanyLocal company = companyHome.findByPrimaryKey( companyName );
194          company.remove();
195       }
196       catch( Exception JavaDoc e )
197       {
198         throw new EJBException JavaDoc( e );
199       }
200    }
201
202    /**
203     * Removes a company if it exists
204     *
205     * @ejb.interface-method
206     */

207    public void removeCompanyIfExists( String JavaDoc companyName )
208    {
209       try
210       {
211          CompanyLocal company = companyHome.findByPrimaryKey( companyName );
212          company.remove();
213       }
214       catch( Exception JavaDoc e )
215       {
216          // yam-yam
217
}
218    }
219
220
221    // SessionBean implementation -------------------------------------
222

223    public void setSessionContext(SessionContext JavaDoc ctx)
224    {
225       try
226       {
227          Context JavaDoc ic = new InitialContext JavaDoc();
228        
229          companyHome = (CompanyLocalHome)ic.lookup(COMPANY_NAME);
230          employeeHome = (EmployeeLocalHome)ic.lookup(EMPLOYEE_NAME);
231       }
232       catch(NamingException JavaDoc ne)
233       {
234          throw new EJBException JavaDoc(ne);
235       }
236    }
237
238    /**
239     * create method
240     *
241     * @ejb.create-method
242     */

243    public void ejbCreate() { }
244    public void ejbActivate() { }
245    public void ejbPassivate() { }
246    public void ejbRemove() { }
247 }
248
Popular Tags