1 package roster; 2 3 import java.util.ArrayList ; 4 import java.util.Collection ; 5 import java.util.Iterator ; 6 import java.util.logging.Level ; 7 import java.util.logging.Logger ; 8 import javax.ejb.*; 9 import javax.naming.Context ; 10 import javax.naming.InitialContext ; 11 import javax.naming.NamingException ; 12 import team.*; 13 import util.Debug; 14 import util.LeagueDetails; 15 import util.PlayerDetails; 16 import util.TeamDetails; 17 18 23 public class RosterBean implements SessionBean, RosterLocalBusiness, RosterRemoteBusiness { 24 private SessionContext context; 25 private PlayerLocalHome playerHome = null; 26 private TeamLocalHome teamHome = null; 27 private LeagueLocalHome leagueHome = null; 28 29 30 36 public void setSessionContext(SessionContext aContext) { 37 context = aContext; 38 } 39 40 public void ejbCreate() throws CreateException { 41 Debug.print("RosterBean ejbCreate"); 42 43 44 playerHome = lookupPlayerBean(); 45 teamHome = lookupTeamBean(); 46 leagueHome = lookupLeagueBean(); 47 48 } 49 50 public void ejbActivate() { 51 Debug.print("RosterBean ejbActivate"); 52 53 54 playerHome = lookupPlayerBean(); 55 teamHome = lookupTeamBean(); 56 leagueHome = lookupLeagueBean(); 57 58 } 59 60 public void ejbPassivate() { 61 playerHome = null; 62 teamHome = null; 63 leagueHome = null; 64 } 65 66 67 68 71 public void ejbRemove() { 72 73 } 74 76 80 81 82 83 public ArrayList testFinder(String parm1, String parm2, String parm3) { 85 Debug.print("RosterBean testFinder"); 86 87 Collection players = null; 88 89 try { 90 players = playerHome.findByTest(parm1, parm2, parm3); 91 } catch (Exception ex) { 92 throw new EJBException(ex.getMessage()); 93 } 94 95 return copyPlayersToDetails(players); 96 } 97 98 public void createPlayer(PlayerDetails details) { 99 Debug.print("RosterBean createPlayer"); 100 101 try { 102 PlayerLocal player = playerHome.create(details.getId(), details.getName(), 103 details.getPosition(), new Double (details.getSalary())); 104 } catch (Exception ex) { 105 throw new EJBException(ex.getMessage()); 106 } 107 } 108 109 public void addPlayer(String playerId, String teamId) { 110 Debug.print("RosterBean addPlayer"); 111 112 try { 113 TeamLocal team = teamHome.findByPrimaryKey(teamId); 114 PlayerLocal player = playerHome.findByPrimaryKey(playerId); 115 116 team.addPlayer(player); 117 } catch (Exception ex) { 118 throw new EJBException(ex.getMessage()); 119 } 120 } 121 122 public void removePlayer(String playerId) { 123 Debug.print("RosterBean removePlayer"); 124 125 try { 126 PlayerLocal player = playerHome.findByPrimaryKey(playerId); 127 128 player.remove(); 129 } catch (Exception ex) { 130 throw new EJBException(ex.getMessage()); 131 } 132 } 133 134 public void dropPlayer(String playerId, String teamId) { 135 Debug.print("RosterBean dropPlayer"); 136 137 try { 138 PlayerLocal player = playerHome.findByPrimaryKey(playerId); 139 TeamLocal team = teamHome.findByPrimaryKey(teamId); 140 141 team.dropPlayer(player); 142 } catch (Exception ex) { 143 throw new EJBException(ex.getMessage()); 144 } 145 } 146 147 public PlayerDetails getPlayer(String playerId) { 148 Debug.print("RosterBean getPlayer"); 149 150 PlayerDetails playerDetails = null; 151 152 try { 153 PlayerLocal player = playerHome.findByPrimaryKey(playerId); 154 155 playerDetails = 156 new PlayerDetails(playerId, player.getName(), 157 player.getPosition(), player.getSalary().doubleValue()); 158 } catch (Exception ex) { 159 throw new EJBException(ex.getMessage()); 160 } 161 162 return playerDetails; 163 } 164 166 public ArrayList getPlayersOfTeam(String teamId) { 167 Debug.print("RosterBean getPlayersOfTeam"); 168 169 Collection players = null; 170 171 try { 172 TeamLocal team = teamHome.findByPrimaryKey(teamId); 173 174 players = team.getPlayers(); 175 } catch (Exception ex) { 176 throw new EJBException(ex.getMessage()); 177 } 178 179 return copyPlayersToDetails(players); 180 } 181 183 public ArrayList getPlayersOfTeamCopy(String teamId) { 184 Debug.print("RosterBean getPlayersOfTeamCopy"); 185 186 ArrayList playersList = null; 187 188 try { 189 TeamLocal team = teamHome.findByPrimaryKey(teamId); 190 191 playersList = team.getCopyOfPlayers(); 192 } catch (Exception ex) { 193 throw new EJBException(ex.getMessage()); 194 } 195 196 return playersList; 197 } 198 200 public ArrayList getTeamsOfLeague(String leagueId) { 201 Debug.print("RosterBean getTeamsOfLeague"); 202 203 ArrayList detailsList = new ArrayList (); 204 Collection teams = null; 205 206 try { 207 LeagueLocal league = leagueHome.findByPrimaryKey(leagueId); 208 209 teams = league.getTeams(); 210 } catch (Exception ex) { 211 throw new EJBException(ex.getMessage()); 212 } 213 214 Iterator i = teams.iterator(); 215 216 while (i.hasNext()) { 217 TeamLocal team = (TeamLocal) i.next(); 218 TeamDetails details = 219 new TeamDetails(team.getTeamId(), team.getName(), team.getCity()); 220 221 detailsList.add(details); 222 } 223 224 return detailsList; 225 } 226 228 public ArrayList getPlayersByPosition(String position) { 229 Debug.print("RosterBean getPlayersByPosition"); 230 231 Collection players = null; 232 233 try { 234 players = playerHome.findByPosition(position); 235 } catch (Exception ex) { 236 throw new EJBException(ex.getMessage()); 237 } 238 239 return copyPlayersToDetails(players); 240 } 241 243 public ArrayList getPlayersByHigherSalary(String name) { 244 Debug.print("RosterBean getPlayersByByHigherSalary"); 245 246 Collection players = null; 247 248 try { 249 players = playerHome.findByHigherSalary(name); 250 } catch (Exception ex) { 251 throw new EJBException(ex.getMessage()); 252 } 253 254 return copyPlayersToDetails(players); 255 } 256 258 public ArrayList getPlayersBySalaryRange(double low, double high) { 259 Debug.print("RosterBean getPlayersBySalaryRange"); 260 261 Collection players = null; 262 263 try { 264 players = playerHome.findBySalaryRange(low, high); 265 } catch (Exception ex) { 266 throw new EJBException(ex.getMessage()); 267 } 268 269 return copyPlayersToDetails(players); 270 } 271 273 public ArrayList getPlayersByLeagueId(String leagueId) { 274 Debug.print("RosterBean getPlayersByLeagueId"); 275 276 Collection players = null; 277 278 try { 279 LeagueLocal league = leagueHome.findByPrimaryKey(leagueId); 280 281 players = playerHome.findByLeague(league); 282 } catch (Exception ex) { 283 throw new EJBException(ex.getMessage()); 284 } 285 286 return copyPlayersToDetails(players); 287 } 288 290 public ArrayList getPlayersBySport(String sport) { 291 Debug.print("RosterBean getPlayersBySport"); 292 293 Collection players = null; 294 295 try { 296 players = playerHome.findBySport(sport); 297 } catch (Exception ex) { 298 throw new EJBException(ex.getMessage()); 299 } 300 301 return copyPlayersToDetails(players); 302 } 303 305 public ArrayList getPlayersByCity(String city) { 306 Debug.print("RosterBean getPlayersByCity"); 307 308 Collection players = null; 309 310 try { 311 players = playerHome.findByCity(city); 312 } catch (Exception ex) { 313 throw new EJBException(ex.getMessage()); 314 } 315 316 return copyPlayersToDetails(players); 317 } 318 320 public ArrayList getAllPlayers() { 321 Debug.print("RosterBean getAllPlayers"); 322 323 Collection players = null; 324 325 try { 326 players = playerHome.findAll(); 327 } catch (Exception ex) { 328 throw new EJBException(ex.getMessage()); 329 } 330 331 return copyPlayersToDetails(players); 332 } 333 335 public ArrayList getPlayersNotOnTeam() { 336 Debug.print("RosterBean getPlayersNotOnTeam"); 337 338 Collection players = null; 339 340 try { 341 players = playerHome.findNotOnTeam(); 342 } catch (Exception ex) { 343 throw new EJBException(ex.getMessage()); 344 } 345 346 return copyPlayersToDetails(players); 347 } 348 350 public ArrayList getPlayersByPositionAndName(String position, String name) { 351 Debug.print("RosterBean getPlayersByPositionAndName"); 352 353 Collection players = null; 354 355 try { 356 players = playerHome.findByPositionAndName(position, name); 357 } catch (Exception ex) { 358 throw new EJBException(ex.getMessage()); 359 } 360 361 return copyPlayersToDetails(players); 362 } 363 365 public ArrayList getLeaguesOfPlayer(String playerId) { 366 Debug.print("RosterBean getLeaguesOfPlayer"); 367 368 ArrayList detailsList = new ArrayList (); 369 Collection leagues = null; 370 371 try { 372 PlayerLocal player = playerHome.findByPrimaryKey(playerId); 373 374 leagues = player.getLeagues(); 375 } catch (Exception ex) { 376 throw new EJBException(ex.getMessage()); 377 } 378 379 Iterator i = leagues.iterator(); 380 381 while (i.hasNext()) { 382 LeagueLocal league = (LeagueLocal) i.next(); 383 LeagueDetails details = 384 new LeagueDetails(league.getLeagueId(), league.getName(), 385 league.getSport()); 386 387 detailsList.add(details); 388 } 389 390 return detailsList; 391 } 392 394 public ArrayList getSportsOfPlayer(String playerId) { 395 Debug.print("RosterBean getSportsOfPlayer"); 396 397 ArrayList sportsList = new ArrayList (); 398 Collection sports = null; 399 400 try { 401 PlayerLocal player = playerHome.findByPrimaryKey(playerId); 402 403 sports = player.getSports(); 404 } catch (Exception ex) { 405 throw new EJBException(ex.getMessage()); 406 } 407 408 Iterator i = sports.iterator(); 409 410 while (i.hasNext()) { 411 String sport = (String ) i.next(); 412 413 sportsList.add(sport); 414 } 415 416 return sportsList; 417 } 418 420 public void createTeamInLeague(TeamDetails details, String leagueId) { 422 Debug.print("RosterBean createTeamInLeague"); 423 424 try { 425 LeagueLocal league = leagueHome.findByPrimaryKey(leagueId); 426 TeamLocal team = teamHome.create(details.getId(), details.getName(), details.getCity()); 427 428 league.addTeam(team); 429 } catch (Exception ex) { 430 throw new EJBException(ex.getMessage()); 431 } 432 } 433 434 public void removeTeam(String teamId) { 435 Debug.print("RosterBean removeTeam"); 436 437 try { 438 TeamLocal team = teamHome.findByPrimaryKey(teamId); 439 440 team.remove(); 441 } catch (Exception ex) { 442 throw new EJBException(ex.getMessage()); 443 } 444 } 445 446 public TeamDetails getTeam(String teamId) { 447 Debug.print("RosterBean getTeam"); 448 449 TeamDetails teamDetails = null; 450 451 try { 452 TeamLocal team = teamHome.findByPrimaryKey(teamId); 453 454 teamDetails = 455 new TeamDetails(teamId, team.getName(), team.getCity()); 456 } catch (Exception ex) { 457 throw new EJBException(ex.getMessage()); 458 } 459 460 return teamDetails; 461 } 462 463 public void createLeague(LeagueDetails details) { 465 Debug.print("RosterBean createLeague"); 466 467 try { 468 LeagueLocal league = 469 leagueHome.create(details.getId(), details.getName(), 470 details.getSport()); 471 } catch (Exception ex) { 472 throw new EJBException(ex.getMessage()); 473 } 474 } 475 476 public void removeLeague(String leagueId) { 477 Debug.print("RosterBean removeLeague"); 478 479 try { 480 LeagueLocal league = leagueHome.findByPrimaryKey(leagueId); 481 482 league.remove(); 483 } catch (Exception ex) { 484 throw new EJBException(ex.getMessage()); 485 } 486 } 487 488 public LeagueDetails getLeague(String leagueId) { 489 Debug.print("RosterBean getLeague"); 490 491 LeagueDetails leagueDetails = null; 492 493 try { 494 LeagueLocal league = leagueHome.findByPrimaryKey(leagueId); 495 496 leagueDetails = 497 new LeagueDetails(leagueId, league.getName(), league.getSport()); 498 } catch (Exception ex) { 499 throw new EJBException(ex.getMessage()); 500 } 501 502 return leagueDetails; 503 } 504 505 private ArrayList copyPlayersToDetails(Collection players) { 506 ArrayList detailsList = new ArrayList (); 507 Iterator i = players.iterator(); 508 509 while (i.hasNext()) { 510 PlayerLocal player = (PlayerLocal) i.next(); 511 PlayerDetails details = 512 new PlayerDetails(player.getPlayerId(), player.getName(), 513 player.getPosition(), player.getSalary().doubleValue()); 514 515 detailsList.add(details); 516 } 517 518 return detailsList; 519 } 520 521 522 524 private LeagueLocalHome lookupLeagueBean() { 525 try { 526 Context c = new InitialContext (); 527 LeagueLocalHome rv = (LeagueLocalHome) c.lookup("java:comp/env/ejb/LeagueBean"); 528 return rv; 529 } 530 catch(NamingException ne) { 531 Logger.getLogger(getClass().getName()).log(Level.SEVERE,"exception caught" ,ne); 532 throw new RuntimeException (ne); 533 } 534 } 535 536 private PlayerLocalHome lookupPlayerBean() { 537 try { 538 Context c = new InitialContext (); 539 PlayerLocalHome rv = (PlayerLocalHome) c.lookup("java:comp/env/ejb/PlayerBean"); 540 return rv; 541 } 542 catch(javax.naming.NamingException ne) { 543 Logger.getLogger(getClass().getName()).log(Level.SEVERE,"exception caught" ,ne); 544 throw new RuntimeException (ne); 545 } 546 } 547 548 private TeamLocalHome lookupTeamBean() { 549 try { 550 javax.naming.Context c = new javax.naming.InitialContext (); 551 TeamLocalHome rv = (TeamLocalHome) c.lookup("java:comp/env/ejb/TeamBean"); 552 return rv; 553 } 554 catch(javax.naming.NamingException ne) { 555 Logger.getLogger(getClass().getName()).log(Level.SEVERE,"exception caught" ,ne); 556 throw new RuntimeException (ne); 557 } 558 } 559 560 } 561 | Popular Tags |