001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.foedeployer.ejb.o2mb;
023:
024: import java.sql.Date;
025: import java.util.Collection;
026: import java.util.HashSet;
027: import java.util.Set;
028: import java.util.Iterator;
029: import java.util.ArrayList;
030:
031: import java.rmi.RemoteException;
032:
033: import javax.ejb.SessionBean;
034: import javax.ejb.SessionContext;
035: import javax.ejb.EJBException;
036: import javax.ejb.CreateException;
037: import javax.ejb.FinderException;
038: import javax.ejb.RemoveException;
039:
040: import javax.naming.Context;
041: import javax.naming.InitialContext;
042: import javax.naming.NamingException;
043:
044: import javax.rmi.PortableRemoteObject;
045:
046: import org.apache.log4j.Category;
047:
048: /**
049: * Manager session bean.
050: *
051: * @ejb.bean
052: * type="Stateless"
053: * name="O2MBManager"
054: * jndi-name="O2MBManagerEJB.O2MBManagerHome"
055: * generate="true"
056: * view-type="remote"
057: *
058: * @ejb.ejb-ref
059: * ejb-name="Company"
060: * view-type="local"
061: * @ejb.ejb-ref
062: * ejb-name="Employee"
063: * view-type="local"
064: *
065: * @ejb.transaction type="Required"
066: */
067: public class O2MBManagerBean implements SessionBean {
068: // Attributes --------------------------------------------------
069: static Category log = Category.getInstance(O2MBManagerBean.class);
070:
071: static String COMPANY_NAME = "java:comp/env/ejb/Company";
072: static String EMPLOYEE_NAME = "java:comp/env/ejb/Employee";
073:
074: private CompanyLocalHome companyHome;
075: private EmployeeLocalHome employeeHome;
076:
077: // Business methods ---------------------------------------------
078: /**
079: * Creates a company
080: *
081: * @ejb.interface-method
082: */
083: public void createCompany(String companyName) {
084: try {
085: companyHome.create(companyName);
086: } catch (CreateException ce) {
087: throw new EJBException(ce);
088: }
089: }
090:
091: /**
092: * Creates an employee
093: *
094: * @ejb.interface-method
095: */
096: public void createEmployee(String employeeName) {
097: try {
098: employeeHome.create(employeeName);
099: } catch (CreateException ce) {
100: throw new EJBException(ce);
101: }
102: }
103:
104: /**
105: * Returns all the companies
106: *
107: * @ejb.interface-method
108: */
109: public Collection getEmployeesForCompany(String companyName) {
110: try {
111: CompanyLocal company = companyHome
112: .findByPrimaryKey(companyName);
113: Collection emps = new ArrayList();
114: for (Iterator iter = company.getEmployees().iterator(); iter
115: .hasNext();) {
116: EmployeeLocal employee = (EmployeeLocal) iter.next();
117: emps.add(employee.getName());
118: }
119: return emps;
120: } catch (FinderException fe) {
121: throw new EJBException(fe);
122: }
123: }
124:
125: /**
126: * Returns emaployee's company
127: *
128: * @ejb.interface-method
129: */
130: public String getCompanyForEmployee(String employeeName) {
131: try {
132: CompanyLocal company = employeeHome.findByPrimaryKey(
133: employeeName).getCompany();
134: if (company == null)
135: return null;
136: return company.getName();
137: } catch (FinderException fe) {
138: throw new EJBException(fe);
139: }
140: }
141:
142: /**
143: * Creates new emaployee and adds it to a company
144: *
145: * @ejb.interface-method
146: */
147: public void createEmployeeForCompany(String employeeName,
148: String companyName) {
149: try {
150: CompanyLocal company = companyHome
151: .findByPrimaryKey(companyName);
152: EmployeeLocal employee = employeeHome.create(employeeName);
153: company.getEmployees().add(employee);
154: } catch (Exception e) {
155: throw new EJBException(e);
156: }
157: }
158:
159: /**
160: * Sets a company for employee
161: *
162: * @ejb.interface-method
163: */
164: public void employ(String employeeName, String companyName) {
165: try {
166: CompanyLocal company = companyHome
167: .findByPrimaryKey(companyName);
168: EmployeeLocal employee = employeeHome
169: .findByPrimaryKey(employeeName);
170: employee.setCompany(company);
171: } catch (Exception e) {
172: throw new EJBException(e);
173: }
174: }
175:
176: /**
177: * Removes a company
178: *
179: * @ejb.interface-method
180: */
181: public void removeCompany(String companyName) {
182: try {
183: CompanyLocal company = companyHome
184: .findByPrimaryKey(companyName);
185: company.remove();
186: } catch (Exception e) {
187: throw new EJBException(e);
188: }
189: }
190:
191: /**
192: * Removes a company if it exists
193: *
194: * @ejb.interface-method
195: */
196: public void removeCompanyIfExists(String companyName) {
197: try {
198: CompanyLocal company = companyHome
199: .findByPrimaryKey(companyName);
200: company.remove();
201: } catch (Exception e) {
202: // yam-yam
203: }
204: }
205:
206: // SessionBean implementation -------------------------------------
207:
208: public void setSessionContext(SessionContext ctx) {
209: try {
210: Context ic = new InitialContext();
211:
212: companyHome = (CompanyLocalHome) ic.lookup(COMPANY_NAME);
213: employeeHome = (EmployeeLocalHome) ic.lookup(EMPLOYEE_NAME);
214: } catch (NamingException ne) {
215: throw new EJBException(ne);
216: }
217: }
218:
219: /**
220: * create method
221: *
222: * @ejb.create-method
223: */
224: public void ejbCreate() {
225: }
226:
227: public void ejbActivate() {
228: }
229:
230: public void ejbPassivate() {
231: }
232:
233: public void ejbRemove() {
234: }
235: }
|