KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > league > ejb > session > LeagueFacadeBean


1 package league.ejb.session;
2 import java.math.BigDecimal JavaDoc;
3 import java.sql.Timestamp JavaDoc;
4 import league.ejb.*;
5 import league.ejb.entity.*;
6 import java.util.*;
7 import league.util.League;
8 import league.util.Match;
9 import league.util.Result;
10 import league.util.Round;
11
12 /**
13  * This is the bean class for the LeagueFacadeBean enterprise bean.
14  * Created Mar 9, 2005 1:40:25 PM
15  * @author honza
16  */

17 public class LeagueFacadeBean implements javax.ejb.SessionBean JavaDoc, league.ejb.session.LeagueFacadeRemoteBusiness {
18     private javax.ejb.SessionContext JavaDoc context;
19     private league.ejb.entity.LeagueLocalHome league=null;
20     private league.ejb.entity.RoundLocalHome round=null;
21     private league.ejb.entity.GroupsLocalHome groups=null;
22     private league.ejb.entity.TeamLocalHome team=null;
23     private league.ejb.entity.PlayerLocalHome player=null;
24     private league.ejb.entity.PlaceInGroupLocalHome place=null;
25     private league.ejb.entity.MatchLocalHome match=null;
26     private league.ejb.entity.ResultLocalHome result=null;
27     
28     
29     // <editor-fold defaultstate="collapsed" desc="EJB infrastructure methods. Click the + sign on the left to edit the code.">
30
// TODO Add code to acquire and use other enterprise resources (DataSource, JMS, enterprise bean, Web services)
31
// TODO Add business methods
32
/**
33      * @see javax.ejb.SessionBean#setSessionContext(javax.ejb.SessionContext)
34      */

35     public void setSessionContext(javax.ejb.SessionContext JavaDoc aContext) {
36         context = aContext;
37     }
38     
39     /**
40      * @see javax.ejb.SessionBean#ejbActivate()
41      */

42     public void ejbActivate() {
43         
44     }
45     
46     /**
47      * @see javax.ejb.SessionBean#ejbPassivate()
48      */

49     public void ejbPassivate() {
50         
51     }
52     
53     /**
54      * @see javax.ejb.SessionBean#ejbRemove()
55      */

56     public void ejbRemove() {
57         
58     }
59     // </editor-fold>
60

61     /**
62      * See section 7.10.3 of the EJB 2.0 specification
63      * See section 7.11.3 of the EJB 2.1 specification
64      */

65     public void ejbCreate() {
66         // TODO implement ejbCreate if necessary, acquire resources
67
// This method has access to the JNDI context so resource aquisition
68
// spanning all methods can be performed here such as home interfaces
69
// and data sources.
70
league=lookupLeague();
71         round=lookupRound();
72         groups=lookupGroups();
73         team=lookupTeam();
74         player=lookupPlayer();
75         place=lookupPlaceInGroup();
76         match=lookupMatch();
77         result=lookupResult();
78     }
79     
80     private league.ejb.entity.LeagueLocalHome lookupLeague() {
81         try {
82             return (league.ejb.entity.LeagueLocalHome) CachingServiceLocator.getInstance().getLocalHome("java:comp/env/ejb/League");
83         } catch(javax.naming.NamingException JavaDoc ne) {
84             java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,"exception caught" ,ne);
85             throw new RuntimeException JavaDoc(ne);
86         }
87     }
88     
89     private league.ejb.entity.RoundLocalHome lookupRound() {
90         try {
91             return (league.ejb.entity.RoundLocalHome) CachingServiceLocator.getInstance().getLocalHome("java:comp/env/ejb/Round");
92         } catch(javax.naming.NamingException JavaDoc ne) {
93             java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,"exception caught" ,ne);
94             throw new RuntimeException JavaDoc(ne);
95         }
96     }
97     
98     private league.ejb.entity.GroupsLocalHome lookupGroups() {
99         try {
100             return (league.ejb.entity.GroupsLocalHome) CachingServiceLocator.getInstance().getLocalHome("java:comp/env/ejb/Groups");
101         } catch(javax.naming.NamingException JavaDoc ne) {
102             java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,"exception caught" ,ne);
103             throw new RuntimeException JavaDoc(ne);
104         }
105     }
106     
107     private league.ejb.entity.TeamLocalHome lookupTeam() {
108         try {
109             return (league.ejb.entity.TeamLocalHome) CachingServiceLocator.getInstance().getLocalHome("java:comp/env/ejb/Team");
110         } catch(javax.naming.NamingException JavaDoc ne) {
111             java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,"exception caught" ,ne);
112             throw new RuntimeException JavaDoc(ne);
113         }
114     }
115     
116     private league.ejb.entity.PlayerLocalHome lookupPlayer() {
117         try {
118             return (league.ejb.entity.PlayerLocalHome) CachingServiceLocator.getInstance().getLocalHome("java:comp/env/ejb/Player");
119         } catch(javax.naming.NamingException JavaDoc ne) {
120             java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,"exception caught" ,ne);
121             throw new RuntimeException JavaDoc(ne);
122         }
123     }
124     
125     private league.ejb.entity.PlaceInGroupLocalHome lookupPlaceInGroup() {
126         try {
127             return (league.ejb.entity.PlaceInGroupLocalHome) CachingServiceLocator.getInstance().getLocalHome("java:comp/env/ejb/PlaceInGroup");
128         } catch(javax.naming.NamingException JavaDoc ne) {
129             java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,"exception caught" ,ne);
130             throw new RuntimeException JavaDoc(ne);
131         }
132     }
133     
134     private league.ejb.entity.MatchLocalHome lookupMatch() {
135         try {
136             return (league.ejb.entity.MatchLocalHome) CachingServiceLocator.getInstance().getLocalHome("java:comp/env/ejb/Match");
137         } catch(javax.naming.NamingException JavaDoc ne) {
138             java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,"exception caught" ,ne);
139             throw new RuntimeException JavaDoc(ne);
140         }
141     }
142     
143     private league.ejb.entity.ResultLocalHome lookupResult() {
144         try {
145             return (league.ejb.entity.ResultLocalHome) CachingServiceLocator.getInstance().getLocalHome("java:comp/env/ejb/Result");
146         } catch(javax.naming.NamingException JavaDoc ne) {
147             java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,"exception caught" ,ne);
148             throw new RuntimeException JavaDoc(ne);
149         }
150     }
151     
152     public league.util.League createLeague(java.lang.String JavaDoc name, java.util.Date JavaDoc startDate, java.util.Date JavaDoc endDate, java.lang.String JavaDoc note) {
153         try {
154             league.ejb.entity.LeagueLocal ll=league.create(name, startDate, endDate, note);
155             return new league.util.League(new Integer JavaDoc(ll.getId().intValue()), name, startDate, endDate, note);
156         } catch (javax.ejb.CreateException JavaDoc ce) {
157             java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,"exception caught" ,ce);
158             throw new RuntimeException JavaDoc(ce);
159         }
160     }
161     
162     public java.util.Collection JavaDoc findAllLeagues() {
163         try {
164             ArrayList al=new ArrayList();
165             Iterator it=league.findAll().iterator();
166             while (it.hasNext()) {
167                 league.ejb.entity.LeagueLocal ll=(league.ejb.entity.LeagueLocal) it.next();
168                 al.add(new league.util.League(new Integer JavaDoc(ll.getId().intValue()), ll.getName(), ll.getStartDate(), ll.getEndDate(), ll.getNote()));
169             }
170             return al;
171         } catch (javax.ejb.FinderException JavaDoc fe) {
172             java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,"exception caught" ,fe);
173             throw new RuntimeException JavaDoc(fe);
174         }
175     }
176     
177     private league.ejb.entity.RoundLocal createEmptyRound(java.lang.String JavaDoc name, java.util.Date JavaDoc startDate, league.ejb.entity.LeagueLocal ll) {
178         try {
179             java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,"createEmptyRound() new round name : "+name);
180             return round.create(name, new java.sql.Timestamp JavaDoc(startDate.getTime()), ll);
181         } catch(javax.ejb.CreateException JavaDoc fe) {
182             java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,"exception caught" ,fe);
183             throw new RuntimeException JavaDoc(fe);
184         }
185     }
186     
187     public void createRound(java.lang.Integer JavaDoc leagueId, java.lang.String JavaDoc name, java.util.Date JavaDoc startDate) {
188         league.ejb.entity.LeagueLocal ll=null;
189         league.ejb.entity.RoundLocal rl=null;
190         if (leagueId==null) throw new RuntimeException JavaDoc("unknown league when creating round");
191         try {
192             ll=league.findByPrimaryKey(new java.math.BigDecimal JavaDoc(leagueId.intValue()));
193             
194         } catch (javax.ejb.FinderException JavaDoc fe) {
195             java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,"exception caught" ,fe);
196             throw new RuntimeException JavaDoc(fe);
197         }
198         // try {
199
// rl=getActiveRound(ll);
200
/*
201             Collection groupsCol=groups.findByRound(rl); //rl.getGroups();
202             Vector groups=new Vector(groupsCol.size());
203             Iterator it=rl.getGroups().iterator();
204             Hashtable ht=new Hashtable();
205             while (it.hasNext()) {
206                 league.ejb.entity.GroupsLocal gl=(league.ejb.entity.GroupsLocal) it.next();
207                 ht.put(new Integer(gl.getPlace().intValue()), gl);
208              
209             }
210             for (int i=1; i<=ht.size(); i++) {
211                 GroupsLocal gl=(GroupsLocal)ht.get(new Integer(i));
212              
213              
214              
215                 Collection placesCol=place.findByGroupId(gl.getId()); //gl.getPlaceInGroup();
216                 Vector teams=new Vector(placesCol.size());
217                 Iterator it1=placesCol.iterator();
218                 Hashtable teamsInGroup=new Hashtable();
219                 while (it1.hasNext()) {
220                     league.ejb.entity.PlaceInGroupLocal pl=(league.ejb.entity.PlaceInGroupLocal) it1.next();
221                     teamsInGroup.put(pl.getTeam().getTeam(pl.getTeam()));
222                 }
223              
224              
225             }
226              */

227         
228         
229         //Date d=round.getLastStartDate();
230
//if (d!=null) rl=round.findByStartDate(new java.sql.Timestamp(d.getTime()));
231
// } catch (javax.ejb.FinderException fe) {
232

233         //}
234
if (rl==null) rl=createEmptyRound(name, startDate, ll);
235         java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,"new round name: "+rl.getName());
236     }
237     
238     public java.util.Collection JavaDoc getPlayersInRound(league.ejb.entity.RoundLocal rl) {
239         //TODO implement getPlayersInRound
240
ArrayList al=new ArrayList();
241         Iterator it=rl.getGroups().iterator();
242         while (it.hasNext()) {
243             league.ejb.entity.GroupsLocal gl=(league.ejb.entity.GroupsLocal) it.next();
244             PlaceInGroupLocal pll=(PlaceInGroupLocal) gl.getPlaceInGroup();
245             al.add(pll.getTeam().getName());
246         }
247         
248         return null;
249     }
250     
251     public void setTeamStartPoints(Integer JavaDoc teamId, Integer JavaDoc groupId, Integer JavaDoc startPoints) {
252         try {
253             PlaceInGroupPK pk=new PlaceInGroupPK();
254             pk.groupId=new BigDecimal JavaDoc(groupId.intValue());
255             pk.teamId=new BigDecimal JavaDoc(teamId.intValue());
256             PlaceInGroupLocal pl=place.findByPrimaryKey(pk);
257             pl.setStartPoints(startPoints);
258         } catch (javax.ejb.FinderException JavaDoc fe) {
259             java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,"exception caught" ,fe);
260             throw new RuntimeException JavaDoc(fe);
261         }
262     }
263     
264     public java.lang.Integer JavaDoc createPlayer(java.lang.String JavaDoc name, java.lang.String JavaDoc note, java.lang.String JavaDoc userId) {
265         try {
266             league.ejb.entity.PlayerLocal pl=player.create(name, note, userId);
267             return new Integer JavaDoc(pl.getId().intValue());
268         } catch (javax.ejb.CreateException JavaDoc ce) {
269             java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,"exception caught" ,ce);
270             throw new RuntimeException JavaDoc(ce);
271         }
272     }
273     
274     public league.util.Player getPlayer(league.ejb.entity.PlayerLocal pl) {
275         league.util.Player player=new league.util.Player(new Integer JavaDoc(pl.getId().intValue()), pl.getName(), pl.getUserid(), pl.getNote());
276         return player;
277     }
278     
279     private PlaceInGroupLocal getActualPlace(TeamLocal tl, RoundLocal rl) {
280         //RoundLocal rl=getActiveRound(ll);
281
Iterator it=tl.getPlaceInGroup().iterator();
282         
283         while (it.hasNext()) {
284             PlaceInGroupLocal plg = (PlaceInGroupLocal) it.next();
285             if (rl.equals(plg.getGroups().getRoundId())) return plg;
286         }
287         return null;
288     }
289     
290     public league.util.Team getTeam(league.ejb.entity.TeamLocal tl, RoundLocal rl) {
291         Iterator it=tl.getPlayerId().iterator();
292         ArrayList al=new ArrayList();
293         //RoundLocal activeRl=getActiveRound(ll);
294
while (it.hasNext()) {
295             league.ejb.entity.PlayerLocal pl=(league.ejb.entity.PlayerLocal) it.next();
296             al.add(getPlayer(pl));
297         }
298         
299         PlaceInGroupLocal plg=getActualPlace(tl, rl);
300         
301         Iterator resIter =tl.getResult().iterator();
302         int points=0;
303         if (plg!=null) {
304             Integer JavaDoc i=plg.getStartPoints();
305             if (i!=null) points=i.intValue();
306         }
307         Integer JavaDoc startPoints=new Integer JavaDoc(points);
308         System.out.println("startPoints "+startPoints.toString());
309         while (resIter.hasNext()) {
310             ResultLocal resl=(ResultLocal) resIter.next();
311             if (rl.equals(resl.getMatch().getRound())) points += resl.getPoints().intValue();
312         }
313         league.util.Team team=new league.util.Team(new Integer JavaDoc(tl.getId().intValue()), tl.getName(), al, new Integer JavaDoc(points), startPoints);
314         System.out.println("startPoints "+team.getStartPoints().toString());
315         return team;
316     }
317     
318     public league.util.Group getGroup(league.ejb.entity.GroupsLocal gl) {
319         try {
320             Collection placesCol=place.findByGroupId(gl.getId()); //gl.getPlaceInGroup();
321
Vector teams=new Vector(placesCol.size());
322             Iterator it=placesCol.iterator();
323             while (it.hasNext()) {
324                 league.ejb.entity.PlaceInGroupLocal pl=(league.ejb.entity.PlaceInGroupLocal) it.next();
325                 //int p=pl.getPlace().intValue();
326
teams.add(getTeam(pl.getTeam(), gl.getRoundId()));
327             }
328             return new league.util.Group(new Integer JavaDoc(gl.getId().intValue()), gl.getName(), teams);
329         } catch (javax.ejb.FinderException JavaDoc fe) {
330             java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,"exception caught" ,fe);
331             throw new RuntimeException JavaDoc(fe);
332         }
333     }
334     
335     public league.util.Round getRound(Integer JavaDoc id) {
336         
337         return getRound(findRound(id));
338         
339     }
340     
341     private league.ejb.entity.TeamLocal findTeam(Integer JavaDoc id) {
342         try {
343             return team.findByPrimaryKey(new java.math.BigDecimal JavaDoc(id.intValue()));
344         } catch (javax.ejb.FinderException JavaDoc fe) {
345             java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,"exception caught" ,fe);
346             throw new RuntimeException JavaDoc(fe);
347         }
348     }
349     private league.ejb.entity.RoundLocal findRound(Integer JavaDoc id) {
350         try {
351             return round.findByPrimaryKey(new java.math.BigDecimal JavaDoc(id.intValue()));
352         } catch (javax.ejb.FinderException JavaDoc fe) {
353             java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,"exception caught" ,fe);
354             throw new RuntimeException JavaDoc(fe);
355         }
356     }
357     
358     private league.ejb.entity.MatchLocal findMatch(Integer JavaDoc id) {
359         try {
360             return match.findByPrimaryKey(new java.math.BigDecimal JavaDoc(id.intValue()));
361         } catch (javax.ejb.FinderException JavaDoc fe) {
362             java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,"exception caught" ,fe);
363             throw new RuntimeException JavaDoc(fe);
364         }
365     }
366     
367     private league.ejb.entity.ResultLocal findResult(ResultPK pk) {
368         try {
369             return result.findByPrimaryKey(pk);
370         } catch (javax.ejb.FinderException JavaDoc fe) {
371             java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,"exception caught" ,fe);
372             throw new RuntimeException JavaDoc(fe);
373         }
374     }
375     
376     private league.ejb.entity.LeagueLocal findLeague(Integer JavaDoc id) {
377         try {
378             return league.findByPrimaryKey(new java.math.BigDecimal JavaDoc(id.intValue()));
379         } catch (javax.ejb.FinderException JavaDoc fe) {
380             java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,"exception caught" ,fe);
381             throw new RuntimeException JavaDoc(fe);
382         }
383     }
384     
385     private league.ejb.entity.GroupsLocal findGroups(Integer JavaDoc id) {
386         try {
387             return groups.findByPrimaryKey(new java.math.BigDecimal JavaDoc(id.intValue()));
388         } catch (javax.ejb.FinderException JavaDoc fe) {
389             java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,"exception caught" ,fe);
390             throw new RuntimeException JavaDoc(fe);
391         }
392     }
393     
394     public league.util.Round getRound(league.ejb.entity.RoundLocal rl) {
395         try {
396             Collection groupsCol=groups.findByRound(rl); //rl.getGroups();
397
Vector groups=new Vector(groupsCol.size());
398             Iterator it=rl.getGroups().iterator();
399             Hashtable ht=new Hashtable();
400             while (it.hasNext()) {
401                 league.ejb.entity.GroupsLocal gl=(league.ejb.entity.GroupsLocal) it.next();
402                 ht.put(new Integer JavaDoc(gl.getPlace().intValue()), getGroup(gl));
403                 
404             }
405             for (int i=1; i<=ht.size(); i++) {
406                 groups.add(ht.get(new Integer JavaDoc(i)));
407             }
408             return new league.util.Round(new Integer JavaDoc(rl.getId().intValue()), rl.getName(), rl.getStartDate(), groups);
409         } catch (javax.ejb.FinderException JavaDoc fe) {
410             java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,"exception caught" ,fe);
411             throw new RuntimeException JavaDoc(fe);
412         }
413     }
414     
415     public Collection getMatches(Integer JavaDoc roundId) {
416         RoundLocal rl=findRound(roundId);
417         ArrayList al=new ArrayList();
418         Iterator it=rl.getMatches().iterator();
419         while (it.hasNext()) {
420             MatchLocal ml=(MatchLocal) it.next();
421             Match m=new Match(new Integer JavaDoc(ml.getId().intValue()), new Date(ml.getDatePlayed().getTime()));
422             Iterator resIter=ml.getResult().iterator();
423             while (resIter.hasNext()) {
424                 ResultLocal resl=(ResultLocal) resIter.next();
425                 m.addResult(new Result(m.getId(), new Integer JavaDoc(resl.getTeam().getId().intValue()), new Integer JavaDoc(resl.getScore().intValue()), new Integer JavaDoc(resl.getPoints().intValue())));
426             }
427             al.add(m);
428         }
429         return al;
430     }
431     
432     public Match getMatchById(Integer JavaDoc matchId) {
433         MatchLocal ml=findMatch(matchId);
434         Match m=new Match(new Integer JavaDoc(ml.getId().intValue()), new Date(ml.getDatePlayed().getTime()));
435         Iterator resIter=ml.getResult().iterator();
436         while (resIter.hasNext()) {
437             ResultLocal resl=(ResultLocal) resIter.next();
438             m.addResult(new Result(m.getId(), new Integer JavaDoc(resl.getTeam().getId().intValue()), new Integer JavaDoc(resl.getScore().intValue()), new Integer JavaDoc(resl.getPoints().intValue())));
439         }
440         
441         return m;
442     }
443     
444     public java.lang.Integer JavaDoc createTeam(java.lang.String JavaDoc name, Integer JavaDoc leagueId) {
445         try {
446             LeagueLocal ll=league.findByPrimaryKey(new java.math.BigDecimal JavaDoc(leagueId.intValue()));
447             league.ejb.entity.TeamLocal tl=team.create(name, ll);
448             return new Integer JavaDoc(tl.getId().intValue());
449         } catch (javax.ejb.CreateException JavaDoc ce) {
450             java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,"exception caught" ,ce);
451             throw new RuntimeException JavaDoc(ce);
452         } catch (javax.ejb.FinderException JavaDoc fe) {
453             java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,"exception caught" ,fe);
454             throw new RuntimeException JavaDoc(fe);
455         }
456     }
457     
458     public void addPlayerToTeam(java.lang.Integer JavaDoc playerId, java.lang.Integer JavaDoc teamId) {
459         try {
460             league.ejb.entity.TeamLocal tl=team.findByPrimaryKey(new java.math.BigDecimal JavaDoc(teamId.intValue()));
461             league.ejb.entity.PlayerLocal pl=player.findByPrimaryKey(new java.math.BigDecimal JavaDoc(playerId.intValue()));
462             ArrayList al=new ArrayList(tl.getPlayerId());
463             al.add(pl);
464             tl.setPlayerId(al);
465         } catch (javax.ejb.FinderException JavaDoc fe) {
466             java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,"exception caught" ,fe);
467             throw new RuntimeException JavaDoc(fe);
468         }
469     }
470     
471     public Collection getTeamsInLeague(Integer JavaDoc id) {
472         try {
473             return getTeamsInLeague(league.findByPrimaryKey(new java.math.BigDecimal JavaDoc(id.intValue())));
474         } catch (javax.ejb.FinderException JavaDoc fe) {
475             java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,"exception caught" ,fe);
476             throw new RuntimeException JavaDoc(fe);
477         }
478     }
479     
480     public Collection getTeamsInLeague(league.ejb.entity.LeagueLocal ll) {
481         Iterator it=ll.getTeams().iterator();
482         ArrayList al=new ArrayList();
483         while (it.hasNext()) {
484             TeamLocal tl=(TeamLocal) it.next();
485             al.add(getTeam(tl, getActiveRound(ll)));
486         }
487         return al;
488     }
489     
490     private league.ejb.entity.GroupsLocal createGroup(String JavaDoc name, Integer JavaDoc place, league.ejb.entity.RoundLocal rl) {
491         try {
492             return groups.create(name, new BigDecimal JavaDoc(place.intValue()), rl);
493         } catch (javax.ejb.CreateException JavaDoc ce) {
494             java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,"exception caught" ,ce);
495             throw new RuntimeException JavaDoc(ce);
496         }
497     }
498     
499     public java.util.Collection JavaDoc getRounds(Integer JavaDoc leagueId) {
500         try {
501             LeagueLocal ll=findLeague(leagueId);
502             Iterator it=round.findByLeague(ll).iterator();
503             ArrayList al=new ArrayList();
504             while (it.hasNext()) {
505                 league.ejb.entity.RoundLocal rl=(league.ejb.entity.RoundLocal) it.next();
506                 al.add(getRound(rl));
507             }
508             return al;
509         } catch (javax.ejb.FinderException JavaDoc fe) {
510             java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,"exception caught" ,fe);
511             throw new RuntimeException JavaDoc(fe);
512         }
513         
514     }
515     
516     /*
517      *
518      * vrati Collection vsech lig
519      *
520      */

521     public java.util.Collection JavaDoc getLeagues() {
522         try {
523             Iterator it=league.findAll().iterator();
524             ArrayList al=new ArrayList();
525             while (it.hasNext()) {
526                 league.ejb.entity.LeagueLocal ll=(league.ejb.entity.LeagueLocal) it.next();
527                 al.add(new league.util.League(new Integer JavaDoc(ll.getId().intValue()), ll.getName(), ll.getStartDate(), ll.getEndDate(), ll.getNote()));
528             }
529             return al;
530         } catch (javax.ejb.FinderException JavaDoc fe) {
531             java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,"exception caught" ,fe);
532             throw new RuntimeException JavaDoc(fe);
533         }
534         
535     }
536     
537     
538     public League getLeagueById(Integer JavaDoc leagueId) {
539         LeagueLocal ll=findLeague(leagueId);
540         return new league.util.League(new Integer JavaDoc(ll.getId().intValue()), ll.getName(), ll.getStartDate(), ll.getEndDate(), ll.getNote());
541     }
542     
543     
544    /* public java.util.Collection getRounds(league.ejb.entity.LeagueLocal ll) {
545         try {
546             Iterator it=round.findByLeague(ll).iterator();
547             ArrayList al=new ArrayList();
548             while (it.hasNext()) {
549                 league.ejb.entity.RoundLocal rl=(league.ejb.entity.RoundLocal) it.next();
550                 al.add(new league.util.Round(new Integer(rl.getId().intValue()), rl.getName(), rl.getStartDate(), null));
551             }
552             return al;
553         } catch (javax.ejb.FinderException fe) {
554             java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,"exception caught" ,fe);
555             throw new RuntimeException(fe);
556         }
557     
558     }
559     */

560     public Round getActiveRound(Integer JavaDoc leagueId) {
561         try {
562             LeagueLocal ll=league.findByPrimaryKey(new java.math.BigDecimal JavaDoc(leagueId.intValue()));
563             league.ejb.entity.RoundLocal rl=getActiveRound(ll);
564             if (rl==null) return null;
565             return getRound(rl);
566         } catch (javax.ejb.FinderException JavaDoc fe) {
567             java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,"exception caught" ,fe);
568             throw new RuntimeException JavaDoc(fe);
569         }
570     }
571     
572     public Integer JavaDoc createMatch(Integer JavaDoc roundId, Hashtable results) {
573         if (results.size()!=2) return null;
574         try {
575             
576             Enumeration e=results.keys();
577             RoundLocal rl=findRound(roundId);
578             MatchLocal ml=match.create(new java.sql.Timestamp JavaDoc(Calendar.getInstance().getTimeInMillis()));
579             ml.setRound(rl);
580             
581             int a=0;
582             int b=0;
583             if (results.containsValue(new Integer JavaDoc(3))) {
584                 if (results.containsValue(new Integer JavaDoc(0))) {a=7;b=1;}
585                 if (results.containsValue(new Integer JavaDoc(1))) {a=6;b=2;}
586                 if (results.containsValue(new Integer JavaDoc(2))) {a=5;b=3;}
587             } else
588                 if (results.containsValue(new Integer JavaDoc(-1))) {
589                 a=0;b=4;
590                 }
591             while (e.hasMoreElements()) {
592                 Integer JavaDoc teamId=(Integer JavaDoc) e.nextElement();
593                 Integer JavaDoc score=(Integer JavaDoc) results.get(teamId);
594                 int p=0;
595                 if (score.intValue()==-1) p=-1;
596                 else if (score.intValue()==3) p=a;
597                 else p=b;
598                 result.create(new BigDecimal JavaDoc(score.intValue()), new BigDecimal JavaDoc(p), teamId, new Integer JavaDoc(ml.getId().intValue()));
599             }
600             return new Integer JavaDoc(ml.getId().intValue());
601         } catch (javax.ejb.CreateException JavaDoc ce) {
602             java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,"exception caught" ,ce);
603             throw new RuntimeException JavaDoc(ce);
604         }
605         
606     }
607     
608     public void updateMatch(Match m) {
609         MatchLocal ml=findMatch(m.getId());
610         Iterator it=m.getResults().iterator();
611         while (it.hasNext()) {
612             Result r=(Result) it.next();
613             ResultPK pk=new ResultPK();
614             pk.matchId=ml.getId();
615             pk.teamId =new BigDecimal JavaDoc(r.getTeamId().intValue());
616             ResultLocal rl=findResult(pk);
617             rl.setScore(new BigDecimal JavaDoc(r.getScore().intValue()));
618             rl.setPoints(new BigDecimal JavaDoc(r.getPoints().intValue()));
619         }
620         
621     }
622  /*
623     public Hashtable getResults(Integer roundId) {
624         return getResults(findRound(roundId));
625     }*/

626  /*
627     public Hashtable getResults(RoundLocal rl) {
628         Iterator it=rl.getGroups().iterator();
629         while (it.hasNext()) {
630             GroupsLocal gl=(GroupsLocal) it.next();
631             Iterator placeIter=place.findByGroupId(gl.getId()).iterator();
632             while (placeIter.hasNext()) {
633                 PlaceInGoupLocal pll=(PlaceInGoupLocal)it.next();
634               // pll.get
635   
636             }
637         }
638         result.findByTeamId(teamId);
639         return null;
640     }
641   */

642     private league.ejb.entity.RoundLocal getActiveRound(league.ejb.entity.LeagueLocal ll) {
643         try {
644             Date last=round.getLastStartDate();
645             if (last==null) return null;
646             return round.findByStartDate(new Timestamp JavaDoc(last.getTime()));
647         } catch (javax.ejb.FinderException JavaDoc fe) {
648             java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,"exception caught" ,fe);
649             throw new RuntimeException JavaDoc(fe);
650         }
651     }
652     
653     private void addTeamToGroup(GroupsLocal gl, TeamLocal tl) {
654         try {
655             
656             Collection col=gl.getPlaceInGroup();
657             place.create(gl, tl, new Integer JavaDoc(col.size()+1));
658             /*if (col.isEmpty()) {
659                 ArrayList al=new ArrayList();
660             }
661             ArrayList al=new ArrayList();
662             al.add(place.create(gl, tl, new Integer(al.size()+2)));*/

663             //gl.setPlaceInGroup(al);
664

665         } catch (javax.ejb.CreateException JavaDoc ce) {
666             java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,"exception caught" ,ce);
667             throw new RuntimeException JavaDoc(ce);
668         }
669         
670     }
671     public String JavaDoc testGroups() {
672         try {
673             LeagueLocal ll=league.findByPrimaryKey(new java.math.BigDecimal JavaDoc(0));
674             RoundLocal rl=getActiveRound(ll);
675             //Iterator it=ll.getTeams().iterator();
676
//while (it.hasNext()) {
677
int[] p= {1,0,3,6,2,4,7,9,5,8,11,13,10,15,14,17,12,16,18,21,19,23,20,25,22,26,24};
678             for (int i=0;i<=26;i++) {
679                 
680                 
681                 TeamLocal tl=team.findByPrimaryKey(new java.math.BigDecimal JavaDoc(p[i]));
682                 // addTeamToLastPlace(rl, tl);
683
}
684             
685         } catch (Exception JavaDoc e) {
686             e.printStackTrace();
687         }
688         return "abcd";
689     }
690     public void addTeamToLastPlace(Integer JavaDoc roundId, Integer JavaDoc teamId) {
691         RoundLocal rl=findRound(roundId);
692         TeamLocal tl=findTeam(teamId);
693         addTeamToLastPlace(rl, tl);
694     }
695     private void addTeamToLastPlace(RoundLocal rl, TeamLocal tl) {
696         try {
697             Collection groupsCol=rl.getGroups();
698             int groupsCount=groupsCol.size();
699             if (groupsCol.isEmpty()) {
700                 GroupsLocal gl=groups.create("group 1", new BigDecimal JavaDoc(1), rl);
701                 addTeamToGroup(gl, tl);
702                 return;
703             }
704             //Vector groups=new Vector(groupsCol.size());
705
league.ejb.entity.GroupsLocal lastGroupLocal=null;
706             int maxGroupId=0;
707             Iterator it=groupsCol.iterator();
708             int test=0;
709             int i=0;
710             while (it.hasNext()) {
711                 test++;
712                 league.ejb.entity.GroupsLocal gl=(league.ejb.entity.GroupsLocal) it.next();
713                 i=gl.getPlace().intValue();
714                 if (i>maxGroupId) { lastGroupLocal=gl;maxGroupId=i; }
715                 //groups.add(gl.getPlace().intValue()-1, gl);
716
}
717             //lastGroupLocal=(league.ejb.entity.GroupsLocal) groups.lastElement();
718
//System.out.println("max group place "+String.valueOf(maxGroupId)+"/last group place "+String.valueOf(lastGroupLocal.getPlace().intValue())+" /group size "+String.valueOf(lastGroupLocal.getPlaceInGroup().size())+" test "+String.valueOf(test));
719
if (lastGroupLocal.getPlaceInGroup().size()<4) addTeamToGroup(lastGroupLocal, tl);
720             else {
721                 groupsCount++;
722                 lastGroupLocal=this.groups.create("group "+String.valueOf(groupsCount), new BigDecimal JavaDoc(groupsCount), rl);
723                 addTeamToGroup(lastGroupLocal, tl);
724             }
725         } catch (javax.ejb.CreateException JavaDoc ce) {
726             java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,"exception caught" ,ce);
727             throw new RuntimeException JavaDoc(ce);
728         }
729         
730     }
731 }
732
Popular Tags