KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > roster > RosterBean


1 package roster;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.Collection JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.logging.Level JavaDoc;
7 import java.util.logging.Logger JavaDoc;
8 import javax.ejb.*;
9 import javax.naming.Context JavaDoc;
10 import javax.naming.InitialContext JavaDoc;
11 import javax.naming.NamingException JavaDoc;
12 import team.*;
13 import util.Debug;
14 import util.LeagueDetails;
15 import util.PlayerDetails;
16 import util.TeamDetails;
17
18 /**
19  * This is the bean class for the RosterBean enterprise bean.
20  * Created Mar 23, 2005 1:49:15 PM
21  * @author honza
22  */

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     // <editor-fold defaultstate="collapsed" desc="EJB infrastructure methods. Click the + sign on the left to edit the code.">
31
// TODO Add code to acquire and use other enterprise resources (DataSource, JMS, enterprise bean, Web services)
32
// TODO Add business methods
33
/**
34      * @see javax.ejb.SessionBean#setSessionContext(javax.ejb.SessionContext)
35      */

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     /**
69      * @see javax.ejb.SessionBean#ejbRemove()
70      */

71     public void ejbRemove() {
72         
73     }
74     // </editor-fold>
75

76     /**
77      * See section 7.10.3 of the EJB 2.0 specification
78      * See section 7.11.3 of the EJB 2.1 specification
79      */

80  
81     
82     
83        // Player business methods
84
public ArrayList JavaDoc testFinder(String JavaDoc parm1, String JavaDoc parm2, String JavaDoc parm3) {
85         Debug.print("RosterBean testFinder");
86
87         Collection JavaDoc players = null;
88
89         try {
90             players = playerHome.findByTest(parm1, parm2, parm3);
91         } catch (Exception JavaDoc 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 JavaDoc(details.getSalary()));
104         } catch (Exception JavaDoc ex) {
105             throw new EJBException(ex.getMessage());
106         }
107     }
108
109     public void addPlayer(String JavaDoc playerId, String JavaDoc 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 JavaDoc ex) {
118             throw new EJBException(ex.getMessage());
119         }
120     }
121
122     public void removePlayer(String JavaDoc playerId) {
123         Debug.print("RosterBean removePlayer");
124
125         try {
126             PlayerLocal player = playerHome.findByPrimaryKey(playerId);
127
128             player.remove();
129         } catch (Exception JavaDoc ex) {
130             throw new EJBException(ex.getMessage());
131         }
132     }
133
134     public void dropPlayer(String JavaDoc playerId, String JavaDoc 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 JavaDoc ex) {
143             throw new EJBException(ex.getMessage());
144         }
145     }
146
147     public PlayerDetails getPlayer(String JavaDoc 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 JavaDoc ex) {
159             throw new EJBException(ex.getMessage());
160         }
161
162         return playerDetails;
163     }
164      // getPlayer
165

166     public ArrayList JavaDoc getPlayersOfTeam(String JavaDoc teamId) {
167         Debug.print("RosterBean getPlayersOfTeam");
168
169         Collection JavaDoc players = null;
170
171         try {
172             TeamLocal team = teamHome.findByPrimaryKey(teamId);
173
174             players = team.getPlayers();
175         } catch (Exception JavaDoc ex) {
176             throw new EJBException(ex.getMessage());
177         }
178
179         return copyPlayersToDetails(players);
180     }
181      // getPlayersOfTeam
182

183     public ArrayList JavaDoc getPlayersOfTeamCopy(String JavaDoc teamId) {
184         Debug.print("RosterBean getPlayersOfTeamCopy");
185
186         ArrayList JavaDoc playersList = null;
187
188         try {
189             TeamLocal team = teamHome.findByPrimaryKey(teamId);
190
191             playersList = team.getCopyOfPlayers();
192         } catch (Exception JavaDoc ex) {
193             throw new EJBException(ex.getMessage());
194         }
195
196         return playersList;
197     }
198      // getPlayersOfTeamCopy
199

200     public ArrayList JavaDoc getTeamsOfLeague(String JavaDoc leagueId) {
201         Debug.print("RosterBean getTeamsOfLeague");
202
203         ArrayList JavaDoc detailsList = new ArrayList JavaDoc();
204         Collection JavaDoc teams = null;
205
206         try {
207             LeagueLocal league = leagueHome.findByPrimaryKey(leagueId);
208
209             teams = league.getTeams();
210         } catch (Exception JavaDoc ex) {
211             throw new EJBException(ex.getMessage());
212         }
213
214         Iterator JavaDoc 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      // getTeamsOfLeague
227

228     public ArrayList JavaDoc getPlayersByPosition(String JavaDoc position) {
229         Debug.print("RosterBean getPlayersByPosition");
230
231         Collection JavaDoc players = null;
232
233         try {
234             players = playerHome.findByPosition(position);
235         } catch (Exception JavaDoc ex) {
236             throw new EJBException(ex.getMessage());
237         }
238
239         return copyPlayersToDetails(players);
240     }
241      // getPlayersByPosition
242

243     public ArrayList JavaDoc getPlayersByHigherSalary(String JavaDoc name) {
244         Debug.print("RosterBean getPlayersByByHigherSalary");
245
246         Collection JavaDoc players = null;
247
248         try {
249             players = playerHome.findByHigherSalary(name);
250         } catch (Exception JavaDoc ex) {
251             throw new EJBException(ex.getMessage());
252         }
253
254         return copyPlayersToDetails(players);
255     }
256      // getPlayersByHigherSalary
257

258     public ArrayList JavaDoc getPlayersBySalaryRange(double low, double high) {
259         Debug.print("RosterBean getPlayersBySalaryRange");
260
261         Collection JavaDoc players = null;
262
263         try {
264             players = playerHome.findBySalaryRange(low, high);
265         } catch (Exception JavaDoc ex) {
266             throw new EJBException(ex.getMessage());
267         }
268
269         return copyPlayersToDetails(players);
270     }
271      // getPlayersBySalaryRange
272

273     public ArrayList JavaDoc getPlayersByLeagueId(String JavaDoc leagueId) {
274         Debug.print("RosterBean getPlayersByLeagueId");
275
276         Collection JavaDoc players = null;
277
278         try {
279             LeagueLocal league = leagueHome.findByPrimaryKey(leagueId);
280
281             players = playerHome.findByLeague(league);
282         } catch (Exception JavaDoc ex) {
283             throw new EJBException(ex.getMessage());
284         }
285
286         return copyPlayersToDetails(players);
287     }
288      // getPlayersByLeagueId
289

290     public ArrayList JavaDoc getPlayersBySport(String JavaDoc sport) {
291         Debug.print("RosterBean getPlayersBySport");
292
293         Collection JavaDoc players = null;
294
295         try {
296             players = playerHome.findBySport(sport);
297         } catch (Exception JavaDoc ex) {
298             throw new EJBException(ex.getMessage());
299         }
300
301         return copyPlayersToDetails(players);
302     }
303      // getPlayersBySport
304

305     public ArrayList JavaDoc getPlayersByCity(String JavaDoc city) {
306         Debug.print("RosterBean getPlayersByCity");
307
308         Collection JavaDoc players = null;
309
310         try {
311             players = playerHome.findByCity(city);
312         } catch (Exception JavaDoc ex) {
313             throw new EJBException(ex.getMessage());
314         }
315
316         return copyPlayersToDetails(players);
317     }
318      // getPlayersByCity
319

320     public ArrayList JavaDoc getAllPlayers() {
321         Debug.print("RosterBean getAllPlayers");
322
323         Collection JavaDoc players = null;
324
325         try {
326             players = playerHome.findAll();
327         } catch (Exception JavaDoc ex) {
328             throw new EJBException(ex.getMessage());
329         }
330
331         return copyPlayersToDetails(players);
332     }
333      // getAllPlayers
334

335     public ArrayList JavaDoc getPlayersNotOnTeam() {
336         Debug.print("RosterBean getPlayersNotOnTeam");
337
338         Collection JavaDoc players = null;
339
340         try {
341             players = playerHome.findNotOnTeam();
342         } catch (Exception JavaDoc ex) {
343             throw new EJBException(ex.getMessage());
344         }
345
346         return copyPlayersToDetails(players);
347     }
348      // getPlayersNotOnTeam
349

350     public ArrayList JavaDoc getPlayersByPositionAndName(String JavaDoc position, String JavaDoc name) {
351         Debug.print("RosterBean getPlayersByPositionAndName");
352
353         Collection JavaDoc players = null;
354
355         try {
356             players = playerHome.findByPositionAndName(position, name);
357         } catch (Exception JavaDoc ex) {
358             throw new EJBException(ex.getMessage());
359         }
360
361         return copyPlayersToDetails(players);
362     }
363      // getPlayersByPositionAndName
364

365     public ArrayList JavaDoc getLeaguesOfPlayer(String JavaDoc playerId) {
366         Debug.print("RosterBean getLeaguesOfPlayer");
367
368         ArrayList JavaDoc detailsList = new ArrayList JavaDoc();
369         Collection JavaDoc leagues = null;
370
371         try {
372             PlayerLocal player = playerHome.findByPrimaryKey(playerId);
373
374             leagues = player.getLeagues();
375         } catch (Exception JavaDoc ex) {
376             throw new EJBException(ex.getMessage());
377         }
378
379         Iterator JavaDoc 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      // getLeaguesOfPlayer
393

394     public ArrayList JavaDoc getSportsOfPlayer(String JavaDoc playerId) {
395         Debug.print("RosterBean getSportsOfPlayer");
396
397         ArrayList JavaDoc sportsList = new ArrayList JavaDoc();
398         Collection JavaDoc sports = null;
399
400         try {
401             PlayerLocal player = playerHome.findByPrimaryKey(playerId);
402
403             sports = player.getSports();
404         } catch (Exception JavaDoc ex) {
405             throw new EJBException(ex.getMessage());
406         }
407
408         Iterator JavaDoc i = sports.iterator();
409
410         while (i.hasNext()) {
411             String JavaDoc sport = (String JavaDoc) i.next();
412
413             sportsList.add(sport);
414         }
415
416         return sportsList;
417     }
418      // getSportsOfPlayer
419

420     // Team business methods
421
public void createTeamInLeague(TeamDetails details, String JavaDoc 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 JavaDoc ex) {
430             throw new EJBException(ex.getMessage());
431         }
432     }
433
434     public void removeTeam(String JavaDoc teamId) {
435         Debug.print("RosterBean removeTeam");
436
437         try {
438             TeamLocal team = teamHome.findByPrimaryKey(teamId);
439
440             team.remove();
441         } catch (Exception JavaDoc ex) {
442             throw new EJBException(ex.getMessage());
443         }
444     }
445
446     public TeamDetails getTeam(String JavaDoc 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 JavaDoc ex) {
457             throw new EJBException(ex.getMessage());
458         }
459
460         return teamDetails;
461     }
462
463     // League business methods
464
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 JavaDoc ex) {
472             throw new EJBException(ex.getMessage());
473         }
474     }
475
476     public void removeLeague(String JavaDoc leagueId) {
477         Debug.print("RosterBean removeLeague");
478
479         try {
480             LeagueLocal league = leagueHome.findByPrimaryKey(leagueId);
481
482             league.remove();
483         } catch (Exception JavaDoc ex) {
484             throw new EJBException(ex.getMessage());
485         }
486     }
487
488     public LeagueDetails getLeague(String JavaDoc 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 JavaDoc ex) {
499             throw new EJBException(ex.getMessage());
500         }
501
502         return leagueDetails;
503     }
504
505  private ArrayList JavaDoc copyPlayersToDetails(Collection JavaDoc players) {
506         ArrayList JavaDoc detailsList = new ArrayList JavaDoc();
507         Iterator JavaDoc 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     // Lookup methods for entity beans
523

524     private LeagueLocalHome lookupLeagueBean() {
525         try {
526             Context JavaDoc c = new InitialContext JavaDoc();
527             LeagueLocalHome rv = (LeagueLocalHome) c.lookup("java:comp/env/ejb/LeagueBean");
528             return rv;
529         }
530         catch(NamingException JavaDoc ne) {
531             Logger.getLogger(getClass().getName()).log(Level.SEVERE,"exception caught" ,ne);
532             throw new RuntimeException JavaDoc(ne);
533         }
534     }
535
536     private PlayerLocalHome lookupPlayerBean() {
537         try {
538             Context JavaDoc c = new InitialContext JavaDoc();
539             PlayerLocalHome rv = (PlayerLocalHome) c.lookup("java:comp/env/ejb/PlayerBean");
540             return rv;
541         }
542         catch(javax.naming.NamingException JavaDoc ne) {
543             Logger.getLogger(getClass().getName()).log(Level.SEVERE,"exception caught" ,ne);
544             throw new RuntimeException JavaDoc(ne);
545         }
546     }
547
548     private TeamLocalHome lookupTeamBean() {
549         try {
550             javax.naming.Context JavaDoc c = new javax.naming.InitialContext JavaDoc();
551             TeamLocalHome rv = (TeamLocalHome) c.lookup("java:comp/env/ejb/TeamBean");
552             return rv;
553         }
554         catch(javax.naming.NamingException JavaDoc ne) {
555             Logger.getLogger(getClass().getName()).log(Level.SEVERE,"exception caught" ,ne);
556             throw new RuntimeException JavaDoc(ne);
557         }
558     }
559     
560 }
561
Popular Tags