001: package roster;
002:
003: import java.util.ArrayList;
004: import java.util.Collection;
005: import java.util.Iterator;
006: import java.util.logging.Level;
007: import java.util.logging.Logger;
008: import javax.ejb.*;
009: import javax.naming.Context;
010: import javax.naming.InitialContext;
011: import javax.naming.NamingException;
012: import team.*;
013: import util.Debug;
014: import util.LeagueDetails;
015: import util.PlayerDetails;
016: import util.TeamDetails;
017:
018: /**
019: * This is the bean class for the RosterBean enterprise bean.
020: * Created Mar 23, 2005 1:49:15 PM
021: * @author honza
022: */
023: public class RosterBean implements SessionBean, RosterLocalBusiness,
024: RosterRemoteBusiness {
025: private SessionContext context;
026: private PlayerLocalHome playerHome = null;
027: private TeamLocalHome teamHome = null;
028: private LeagueLocalHome leagueHome = null;
029:
030: // <editor-fold defaultstate="collapsed" desc="EJB infrastructure methods. Click the + sign on the left to edit the code.">
031: // TODO Add code to acquire and use other enterprise resources (DataSource, JMS, enterprise bean, Web services)
032: // TODO Add business methods
033: /**
034: * @see javax.ejb.SessionBean#setSessionContext(javax.ejb.SessionContext)
035: */
036: public void setSessionContext(SessionContext aContext) {
037: context = aContext;
038: }
039:
040: public void ejbCreate() throws CreateException {
041: Debug.print("RosterBean ejbCreate");
042:
043: playerHome = lookupPlayerBean();
044: teamHome = lookupTeamBean();
045: leagueHome = lookupLeagueBean();
046:
047: }
048:
049: public void ejbActivate() {
050: Debug.print("RosterBean ejbActivate");
051:
052: playerHome = lookupPlayerBean();
053: teamHome = lookupTeamBean();
054: leagueHome = lookupLeagueBean();
055:
056: }
057:
058: public void ejbPassivate() {
059: playerHome = null;
060: teamHome = null;
061: leagueHome = null;
062: }
063:
064: /**
065: * @see javax.ejb.SessionBean#ejbRemove()
066: */
067: public void ejbRemove() {
068:
069: }
070:
071: // </editor-fold>
072:
073: /**
074: * See section 7.10.3 of the EJB 2.0 specification
075: * See section 7.11.3 of the EJB 2.1 specification
076: */
077:
078: // Player business methods
079: public ArrayList testFinder(String parm1, String parm2, String parm3) {
080: Debug.print("RosterBean testFinder");
081:
082: Collection players = null;
083:
084: try {
085: players = playerHome.findByTest(parm1, parm2, parm3);
086: } catch (Exception ex) {
087: throw new EJBException(ex.getMessage());
088: }
089:
090: return copyPlayersToDetails(players);
091: }
092:
093: public void createPlayer(PlayerDetails details) {
094: Debug.print("RosterBean createPlayer");
095:
096: try {
097: PlayerLocal player = playerHome.create(details.getId(),
098: details.getName(), details.getPosition(),
099: new Double(details.getSalary()));
100: } catch (Exception ex) {
101: throw new EJBException(ex.getMessage());
102: }
103: }
104:
105: public void addPlayer(String playerId, String teamId) {
106: Debug.print("RosterBean addPlayer");
107:
108: try {
109: TeamLocal team = teamHome.findByPrimaryKey(teamId);
110: PlayerLocal player = playerHome.findByPrimaryKey(playerId);
111:
112: team.addPlayer(player);
113: } catch (Exception ex) {
114: throw new EJBException(ex.getMessage());
115: }
116: }
117:
118: public void removePlayer(String playerId) {
119: Debug.print("RosterBean removePlayer");
120:
121: try {
122: PlayerLocal player = playerHome.findByPrimaryKey(playerId);
123:
124: player.remove();
125: } catch (Exception ex) {
126: throw new EJBException(ex.getMessage());
127: }
128: }
129:
130: public void dropPlayer(String playerId, String teamId) {
131: Debug.print("RosterBean dropPlayer");
132:
133: try {
134: PlayerLocal player = playerHome.findByPrimaryKey(playerId);
135: TeamLocal team = teamHome.findByPrimaryKey(teamId);
136:
137: team.dropPlayer(player);
138: } catch (Exception ex) {
139: throw new EJBException(ex.getMessage());
140: }
141: }
142:
143: public PlayerDetails getPlayer(String playerId) {
144: Debug.print("RosterBean getPlayer");
145:
146: PlayerDetails playerDetails = null;
147:
148: try {
149: PlayerLocal player = playerHome.findByPrimaryKey(playerId);
150:
151: playerDetails = new PlayerDetails(playerId, player
152: .getName(), player.getPosition(), player
153: .getSalary().doubleValue());
154: } catch (Exception ex) {
155: throw new EJBException(ex.getMessage());
156: }
157:
158: return playerDetails;
159: }
160:
161: // getPlayer
162:
163: public ArrayList getPlayersOfTeam(String teamId) {
164: Debug.print("RosterBean getPlayersOfTeam");
165:
166: Collection players = null;
167:
168: try {
169: TeamLocal team = teamHome.findByPrimaryKey(teamId);
170:
171: players = team.getPlayers();
172: } catch (Exception ex) {
173: throw new EJBException(ex.getMessage());
174: }
175:
176: return copyPlayersToDetails(players);
177: }
178:
179: // getPlayersOfTeam
180:
181: public ArrayList getPlayersOfTeamCopy(String teamId) {
182: Debug.print("RosterBean getPlayersOfTeamCopy");
183:
184: ArrayList playersList = null;
185:
186: try {
187: TeamLocal team = teamHome.findByPrimaryKey(teamId);
188:
189: playersList = team.getCopyOfPlayers();
190: } catch (Exception ex) {
191: throw new EJBException(ex.getMessage());
192: }
193:
194: return playersList;
195: }
196:
197: // getPlayersOfTeamCopy
198:
199: public ArrayList getTeamsOfLeague(String leagueId) {
200: Debug.print("RosterBean getTeamsOfLeague");
201:
202: ArrayList detailsList = new ArrayList();
203: Collection teams = null;
204:
205: try {
206: LeagueLocal league = leagueHome.findByPrimaryKey(leagueId);
207:
208: teams = league.getTeams();
209: } catch (Exception ex) {
210: throw new EJBException(ex.getMessage());
211: }
212:
213: Iterator i = teams.iterator();
214:
215: while (i.hasNext()) {
216: TeamLocal team = (TeamLocal) i.next();
217: TeamDetails details = new TeamDetails(team.getTeamId(),
218: team.getName(), team.getCity());
219:
220: detailsList.add(details);
221: }
222:
223: return detailsList;
224: }
225:
226: // getTeamsOfLeague
227:
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:
242: // getPlayersByPosition
243:
244: public ArrayList getPlayersByHigherSalary(String name) {
245: Debug.print("RosterBean getPlayersByByHigherSalary");
246:
247: Collection players = null;
248:
249: try {
250: players = playerHome.findByHigherSalary(name);
251: } catch (Exception ex) {
252: throw new EJBException(ex.getMessage());
253: }
254:
255: return copyPlayersToDetails(players);
256: }
257:
258: // getPlayersByHigherSalary
259:
260: public ArrayList getPlayersBySalaryRange(double low, double high) {
261: Debug.print("RosterBean getPlayersBySalaryRange");
262:
263: Collection players = null;
264:
265: try {
266: players = playerHome.findBySalaryRange(low, high);
267: } catch (Exception ex) {
268: throw new EJBException(ex.getMessage());
269: }
270:
271: return copyPlayersToDetails(players);
272: }
273:
274: // getPlayersBySalaryRange
275:
276: public ArrayList getPlayersByLeagueId(String leagueId) {
277: Debug.print("RosterBean getPlayersByLeagueId");
278:
279: Collection players = null;
280:
281: try {
282: LeagueLocal league = leagueHome.findByPrimaryKey(leagueId);
283:
284: players = playerHome.findByLeague(league);
285: } catch (Exception ex) {
286: throw new EJBException(ex.getMessage());
287: }
288:
289: return copyPlayersToDetails(players);
290: }
291:
292: // getPlayersByLeagueId
293:
294: public ArrayList getPlayersBySport(String sport) {
295: Debug.print("RosterBean getPlayersBySport");
296:
297: Collection players = null;
298:
299: try {
300: players = playerHome.findBySport(sport);
301: } catch (Exception ex) {
302: throw new EJBException(ex.getMessage());
303: }
304:
305: return copyPlayersToDetails(players);
306: }
307:
308: // getPlayersBySport
309:
310: public ArrayList getPlayersByCity(String city) {
311: Debug.print("RosterBean getPlayersByCity");
312:
313: Collection players = null;
314:
315: try {
316: players = playerHome.findByCity(city);
317: } catch (Exception ex) {
318: throw new EJBException(ex.getMessage());
319: }
320:
321: return copyPlayersToDetails(players);
322: }
323:
324: // getPlayersByCity
325:
326: public ArrayList getAllPlayers() {
327: Debug.print("RosterBean getAllPlayers");
328:
329: Collection players = null;
330:
331: try {
332: players = playerHome.findAll();
333: } catch (Exception ex) {
334: throw new EJBException(ex.getMessage());
335: }
336:
337: return copyPlayersToDetails(players);
338: }
339:
340: // getAllPlayers
341:
342: public ArrayList getPlayersNotOnTeam() {
343: Debug.print("RosterBean getPlayersNotOnTeam");
344:
345: Collection players = null;
346:
347: try {
348: players = playerHome.findNotOnTeam();
349: } catch (Exception ex) {
350: throw new EJBException(ex.getMessage());
351: }
352:
353: return copyPlayersToDetails(players);
354: }
355:
356: // getPlayersNotOnTeam
357:
358: public ArrayList getPlayersByPositionAndName(String position,
359: String name) {
360: Debug.print("RosterBean getPlayersByPositionAndName");
361:
362: Collection players = null;
363:
364: try {
365: players = playerHome.findByPositionAndName(position, name);
366: } catch (Exception ex) {
367: throw new EJBException(ex.getMessage());
368: }
369:
370: return copyPlayersToDetails(players);
371: }
372:
373: // getPlayersByPositionAndName
374:
375: public ArrayList getLeaguesOfPlayer(String playerId) {
376: Debug.print("RosterBean getLeaguesOfPlayer");
377:
378: ArrayList detailsList = new ArrayList();
379: Collection leagues = null;
380:
381: try {
382: PlayerLocal player = playerHome.findByPrimaryKey(playerId);
383:
384: leagues = player.getLeagues();
385: } catch (Exception ex) {
386: throw new EJBException(ex.getMessage());
387: }
388:
389: Iterator i = leagues.iterator();
390:
391: while (i.hasNext()) {
392: LeagueLocal league = (LeagueLocal) i.next();
393: LeagueDetails details = new LeagueDetails(league
394: .getLeagueId(), league.getName(), league.getSport());
395:
396: detailsList.add(details);
397: }
398:
399: return detailsList;
400: }
401:
402: // getLeaguesOfPlayer
403:
404: public ArrayList getSportsOfPlayer(String playerId) {
405: Debug.print("RosterBean getSportsOfPlayer");
406:
407: ArrayList sportsList = new ArrayList();
408: Collection sports = null;
409:
410: try {
411: PlayerLocal player = playerHome.findByPrimaryKey(playerId);
412:
413: sports = player.getSports();
414: } catch (Exception ex) {
415: throw new EJBException(ex.getMessage());
416: }
417:
418: Iterator i = sports.iterator();
419:
420: while (i.hasNext()) {
421: String sport = (String) i.next();
422:
423: sportsList.add(sport);
424: }
425:
426: return sportsList;
427: }
428:
429: // getSportsOfPlayer
430:
431: // Team business methods
432: public void createTeamInLeague(TeamDetails details, String leagueId) {
433: Debug.print("RosterBean createTeamInLeague");
434:
435: try {
436: LeagueLocal league = leagueHome.findByPrimaryKey(leagueId);
437: TeamLocal team = teamHome.create(details.getId(), details
438: .getName(), details.getCity());
439:
440: league.addTeam(team);
441: } catch (Exception ex) {
442: throw new EJBException(ex.getMessage());
443: }
444: }
445:
446: public void removeTeam(String teamId) {
447: Debug.print("RosterBean removeTeam");
448:
449: try {
450: TeamLocal team = teamHome.findByPrimaryKey(teamId);
451:
452: team.remove();
453: } catch (Exception ex) {
454: throw new EJBException(ex.getMessage());
455: }
456: }
457:
458: public TeamDetails getTeam(String teamId) {
459: Debug.print("RosterBean getTeam");
460:
461: TeamDetails teamDetails = null;
462:
463: try {
464: TeamLocal team = teamHome.findByPrimaryKey(teamId);
465:
466: teamDetails = new TeamDetails(teamId, team.getName(), team
467: .getCity());
468: } catch (Exception ex) {
469: throw new EJBException(ex.getMessage());
470: }
471:
472: return teamDetails;
473: }
474:
475: // League business methods
476: public void createLeague(LeagueDetails details) {
477: Debug.print("RosterBean createLeague");
478:
479: try {
480: LeagueLocal league = leagueHome.create(details.getId(),
481: details.getName(), details.getSport());
482: } catch (Exception ex) {
483: throw new EJBException(ex.getMessage());
484: }
485: }
486:
487: public void removeLeague(String leagueId) {
488: Debug.print("RosterBean removeLeague");
489:
490: try {
491: LeagueLocal league = leagueHome.findByPrimaryKey(leagueId);
492:
493: league.remove();
494: } catch (Exception ex) {
495: throw new EJBException(ex.getMessage());
496: }
497: }
498:
499: public LeagueDetails getLeague(String leagueId) {
500: Debug.print("RosterBean getLeague");
501:
502: LeagueDetails leagueDetails = null;
503:
504: try {
505: LeagueLocal league = leagueHome.findByPrimaryKey(leagueId);
506:
507: leagueDetails = new LeagueDetails(leagueId, league
508: .getName(), league.getSport());
509: } catch (Exception ex) {
510: throw new EJBException(ex.getMessage());
511: }
512:
513: return leagueDetails;
514: }
515:
516: private ArrayList copyPlayersToDetails(Collection players) {
517: ArrayList detailsList = new ArrayList();
518: Iterator i = players.iterator();
519:
520: while (i.hasNext()) {
521: PlayerLocal player = (PlayerLocal) i.next();
522: PlayerDetails details = new PlayerDetails(player
523: .getPlayerId(), player.getName(), player
524: .getPosition(), player.getSalary().doubleValue());
525:
526: detailsList.add(details);
527: }
528:
529: return detailsList;
530: }
531:
532: // Lookup methods for entity beans
533:
534: private LeagueLocalHome lookupLeagueBean() {
535: try {
536: Context c = new InitialContext();
537: LeagueLocalHome rv = (LeagueLocalHome) c
538: .lookup("java:comp/env/ejb/LeagueBean");
539: return rv;
540: } catch (NamingException ne) {
541: Logger.getLogger(getClass().getName()).log(Level.SEVERE,
542: "exception caught", ne);
543: throw new RuntimeException(ne);
544: }
545: }
546:
547: private PlayerLocalHome lookupPlayerBean() {
548: try {
549: Context c = new InitialContext();
550: PlayerLocalHome rv = (PlayerLocalHome) c
551: .lookup("java:comp/env/ejb/PlayerBean");
552: return rv;
553: } catch (javax.naming.NamingException ne) {
554: Logger.getLogger(getClass().getName()).log(Level.SEVERE,
555: "exception caught", ne);
556: throw new RuntimeException(ne);
557: }
558: }
559:
560: private TeamLocalHome lookupTeamBean() {
561: try {
562: javax.naming.Context c = new javax.naming.InitialContext();
563: TeamLocalHome rv = (TeamLocalHome) c
564: .lookup("java:comp/env/ejb/TeamBean");
565: return rv;
566: } catch (javax.naming.NamingException ne) {
567: Logger.getLogger(getClass().getName()).log(Level.SEVERE,
568: "exception caught", ne);
569: throw new RuntimeException(ne);
570: }
571: }
572:
573: }
|