View Javadoc
1   package ejava.projects.eleague.xml;
2   
3   import java.util.Calendar;
4   
5   import java.util.GregorianCalendar;
6   import java.util.Random;
7   
8   import ejava.projects.eleague.dto.Club;
9   import ejava.projects.eleague.dto.Contact;
10  import ejava.projects.eleague.dto.ContactRoleType;
11  import ejava.projects.eleague.dto.Contest;
12  import ejava.projects.eleague.dto.Division;
13  import ejava.projects.eleague.dto.ELeague;
14  import ejava.projects.eleague.dto.LeagueMetadata;
15  import ejava.projects.eleague.dto.Season;
16  import ejava.projects.eleague.dto.Team;
17  import ejava.projects.eleague.dto.TeamSeason;
18  import ejava.projects.eleague.dto.Venue;
19  
20  /**
21   * This class provides the ability to generate a sample set of League 
22   * DTOs and XML document. Of note, this is is to be used for initial 
23   * implementation testing only. It is not the official data set required
24   * for submission with the project.
25   */
26  public class SampleGen {
27      private int refid=0;
28      private long id=0;
29  
30      protected String nextRefId() { return "ref-" + Integer.toString(++refid); }
31      protected long nextId() { return ++id; }
32  
33      protected Club createClub(
34              ELeague league, String name, String rep, int teams) {
35          Club club = new Club();
36          club.setId(nextId());
37          club.setRefid(nextRefId());
38          club.setName(name);
39  
40          Contact clubRep = new Contact();
41          club.setContact(clubRep);
42          league.getContact().add(clubRep);
43          clubRep.setId(nextId());
44          clubRep.setRefid(nextRefId());
45          clubRep.setName("joe " + rep);
46          clubRep.setEMail(rep + "@" + name + ".org");
47          clubRep.setLogin(rep);
48          
49          Venue field1 = new Venue();
50          club.getVenue().add(field1);
51          field1.setId(nextId());
52          field1.setRefid(nextRefId());
53          field1.setName(name + " Park");
54          field1.setAdcPage("AA/13/5F");
55          field1.setDirections("get there and turn left");
56          field1.setStreet1("8830 " + name + " Blvd");
57          field1.setCity("Columbia");
58          field1.setState("MD");
59          field1.setZip("21045");
60          
61          for (int i=0; i<teams; i++) {
62              Team team = new Team();
63              club.getTeam().add(team);
64              team.setId(nextId());
65              team.setRefid(nextRefId());
66              team.setName(name + " team# " + i);
67  
68              ContactRoleType coach = new ContactRoleType();
69              team.getContactRole().add(coach);
70              coach.setRole("COACH");
71  
72              Contact coachContact = new Contact();
73              coach.setContact(coachContact);
74              league.getContact().add(coachContact);
75              coachContact.setRefid(nextRefId());
76              
77              if (i==0) {
78                  coach.setId(clubRep.getId());
79                  coachContact.setName(clubRep.getName());
80                  coachContact.setEMail(clubRep.getEMail());
81                  coachContact.setLogin(clubRep.getLogin());
82              }
83              else {
84                  coach.setId(nextId());
85                  coachContact.setName("coach #" + i);
86                  coachContact.setEMail("coach." + i + "@" + name + ".org");
87                  coachContact.setLogin(name + "_" + i);
88  
89                  ContactRoleType manager = new ContactRoleType();
90                  team.getContactRole().add(manager);
91                  Contact managerContact = new Contact();
92                  league.getContact().add(managerContact);
93                  manager.setContact(managerContact);
94                  manager.setId(nextId());
95                  manager.setRole("MANAGER");
96                  managerContact.setRefid(nextRefId());
97                  managerContact.setName("manage #" + i);
98                  managerContact.setEMail("manager." + i + "@" + name + ".org");
99                  managerContact.setLogin(name + "_m" + i);
100             }
101         }
102 
103         return club;
104     }
105     
106     protected Season createSeason(
107             ELeague league, int year) {
108         Season season = new Season();
109         season.setId(nextId());
110         season.setRefid(nextRefId());
111         season.setName("Spring " + Integer.toString(year));
112         
113         Calendar cal = new GregorianCalendar();
114         cal.set(2007, Calendar.MARCH, 01);
115         season.setStarts(cal.getTime());
116         cal.set(2007, Calendar.JUNE, 01);
117         season.setEnds(cal.getTime());
118         
119         String groups[] = { "U11", "U13", "U15"};
120         for (int g=0; g<groups.length; g++) {
121             String levels[] = new String[]{ "B", "A", "AA"};
122             for (int i=0; i<levels.length; i++) {
123                 Division division = new Division();
124                 season.getDivision().add(division);
125                 division.setId(nextId());
126                 division.setRefid(nextRefId());
127                 division.setGroup(groups[g]);
128                 division.setLevel(levels[i]);
129                 division.setRanking(g*10 + i);
130                 
131                 Contact coordinator = new Contact();
132                 league.getContact().add(coordinator);
133                 division.setContact(coordinator);
134                 coordinator.setId(nextId());
135                 coordinator.setRefid(nextRefId());
136                 coordinator.setName("Mr. " + groups[g] + "-" + levels[i]);
137                 coordinator.setLogin(groups[g] + levels[i]);                
138             }
139         }
140         
141         return season;
142     }
143     
144     protected void assignTeamsToDivision(Club club, Season season) {
145         int divIndex = 10000;
146         for (Team team : club.getTeam()) {
147             TeamSeason divPlay = new TeamSeason();
148             divPlay.setId(nextId());
149             divPlay.setRefid(nextRefId());
150             divPlay.setTeam(team);
151             if (++divIndex >= season.getDivision().size()) {
152                 divIndex = 0;    
153             }
154             Division division = season.getDivision().get(divIndex);
155             division.getTeamSeason().add(divPlay);
156         }
157         
158     }
159     
160     protected Venue getField(ELeague league, Team team) {
161         Venue field = null;
162         for (Club club : league.getClub()) {
163             for (Team clubTeam : club.getTeam()) {
164                 if (team.getId() == clubTeam.getId()) { 
165                     field = club.getVenue().iterator().next();
166                 }
167             }
168         }
169         return field;
170     }
171     
172     protected void scheduleSeason(ELeague league, Season season) {
173         for(Division division : season.getDivision()) {
174             int numTeams = division.getTeamSeason().size();
175             for (int i=0; i<numTeams; i++) {
176                 for (int j=numTeams-1; j>= 0; j--) {
177                     if (i!=j) {
178                         Contest contest = new Contest();
179                         season.getContest().add(contest);
180                         contest.setId(nextId());
181                         contest.setRefid(nextRefId());
182                         Calendar cal = new GregorianCalendar();
183                         cal.setTime(season.getStarts());
184                         int year = cal.get(Calendar.YEAR);
185                         cal.set(year,Calendar.MARCH,1+i,8+((j%12)*2),0, 0);
186                         contest.setStarts(cal.getTime());
187                         TeamSeason homeTeam = division.getTeamSeason().get(i);
188                         TeamSeason awayTeam = division.getTeamSeason().get(j);
189                         contest.setHomeTeam(homeTeam);
190                         contest.setAwayTeam(awayTeam);
191                         contest.setDuration(2*3600*1000);
192                         contest.setLocation(
193                                 getField(league, (Team)homeTeam.getTeam()));
194                         contest.setAwayScore(-1);
195                         contest.setHomeScore(-1);
196                     }
197                 }
198             }
199         }
200     }
201     
202     protected void reportScores(Season season) {
203         Random random = new Random();
204         for (Contest contest : season.getContest()) {
205             contest.setHomeScore(random.nextInt(5));
206             contest.setAwayScore(random.nextInt(5));
207         }
208     }
209     
210     protected LeagueMetadata createLeagueMetadata(ELeague league) {
211         LeagueMetadata md = new LeagueMetadata();
212         md.setName("Everyone Doit League");
213         
214         Contact coordinator = new Contact();
215         league.getContact().add(coordinator);
216         md.setContact(coordinator);
217         coordinator.setRefid(nextRefId());
218         coordinator.setId(nextId());
219         coordinator.setName("John Doe");
220         
221         return md;
222     }
223     
224     public ELeague createLeague() throws Exception {
225         ELeague league = new ELeague();
226         
227         LeagueMetadata md = createLeagueMetadata(league);
228         league.setLeagueMetadata(md);
229 
230         String club[] = new String[] {
231             "RockemSockem", "GoGetems", "RedRiders", "Senators", "Sailors",
232             "ChaChas", "Locos", "Greenhornets", "Predators", "Holsteins"
233         };
234         String rep[] = new String [] {
235           "smith", "jones", "klink", "schultz", "kotter",
236           "seaver", "sullivan", "marchabroda", "modell", "rozell"
237         };
238         for (int i=0; i<club.length; i++) {
239             league.getClub().add(createClub(league, club[i], rep[i], i+1));
240         }
241         
242         
243         for (int year : new int[] { 2005, 2006, 2007}) {
244             Season season = createSeason(league, year);
245             league.getSeason().add(season);
246             league.setCurrentSeason(season);
247             
248             for(Club c : league.getClub()) {
249                 assignTeamsToDivision(c, season);
250             }
251             
252             scheduleSeason(league, season);
253             reportScores(season);
254         }
255         Season season = createSeason(league, 2008);
256         league.getSeason().add(season);
257         league.setCurrentSeason(season);
258         
259         return league;     
260     }
261 }