1 7 package org.jboss.test.foedeployer.ejb.o2mb; 8 9 10 import java.sql.Date ; 11 import java.util.Collection ; 12 import java.util.HashSet ; 13 import java.util.Set ; 14 import java.util.Iterator ; 15 import java.util.ArrayList ; 16 17 import java.rmi.RemoteException ; 18 19 import javax.ejb.SessionBean ; 20 import javax.ejb.SessionContext ; 21 import javax.ejb.EJBException ; 22 import javax.ejb.CreateException ; 23 import javax.ejb.FinderException ; 24 import javax.ejb.RemoveException ; 25 26 import javax.naming.Context ; 27 import javax.naming.InitialContext ; 28 import javax.naming.NamingException ; 29 30 import javax.rmi.PortableRemoteObject ; 31 32 import org.apache.log4j.Category; 33 34 35 54 public class O2MBManagerBean 55 implements SessionBean 56 { 57 static Category log = Category.getInstance( O2MBManagerBean.class ); 59 60 static String COMPANY_NAME = "java:comp/env/ejb/Company"; 61 static String EMPLOYEE_NAME = "java:comp/env/ejb/Employee"; 62 63 private CompanyLocalHome companyHome; 64 private EmployeeLocalHome employeeHome; 65 66 72 public void createCompany( String companyName ) 73 { 74 try 75 { 76 companyHome.create( companyName ); 77 } 78 catch( CreateException ce ) 79 { 80 throw new EJBException ( ce ); 81 } 82 } 83 84 89 public void createEmployee( String employeeName ) 90 { 91 try 92 { 93 employeeHome.create( employeeName ); 94 } 95 catch( CreateException ce ) 96 { 97 throw new EJBException ( ce ); 98 } 99 } 100 101 106 public Collection getEmployeesForCompany( String companyName ) 107 { 108 try 109 { 110 CompanyLocal company = companyHome.findByPrimaryKey( companyName ); 111 Collection emps = new ArrayList (); 112 for(Iterator 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 fe ) 120 { 121 throw new EJBException ( fe ); 122 } 123 } 124 125 130 public String getCompanyForEmployee( String 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 fe ) 140 { 141 throw new EJBException ( fe ); 142 } 143 } 144 145 150 public void createEmployeeForCompany( 151 String employeeName, String 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 e ) 160 { 161 throw new EJBException ( e ); 162 } 163 } 164 165 170 public void employ( String employeeName, String 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 e ) 179 { 180 throw new EJBException ( e ); 181 } 182 } 183 184 189 public void removeCompany( String companyName ) 190 { 191 try 192 { 193 CompanyLocal company = companyHome.findByPrimaryKey( companyName ); 194 company.remove(); 195 } 196 catch( Exception e ) 197 { 198 throw new EJBException ( e ); 199 } 200 } 201 202 207 public void removeCompanyIfExists( String companyName ) 208 { 209 try 210 { 211 CompanyLocal company = companyHome.findByPrimaryKey( companyName ); 212 company.remove(); 213 } 214 catch( Exception e ) 215 { 216 } 218 } 219 220 221 223 public void setSessionContext(SessionContext ctx) 224 { 225 try 226 { 227 Context ic = new InitialContext (); 228 229 companyHome = (CompanyLocalHome)ic.lookup(COMPANY_NAME); 230 employeeHome = (EmployeeLocalHome)ic.lookup(EMPLOYEE_NAME); 231 } 232 catch(NamingException ne) 233 { 234 throw new EJBException (ne); 235 } 236 } 237 238 243 public void ejbCreate() { } 244 public void ejbActivate() { } 245 public void ejbPassivate() { } 246 public void ejbRemove() { } 247 } 248 | Popular Tags |