TournamentBean.java
9.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*
* Copyright Codecrew Ry
*
* All rights reserved.
*
* This license applies to any software containing a notice placed by the
* copyright holder. Such software is herein referred to as the Software.
* This license covers modification, distribution and use of the Software.
*
* Any distribution and use in source and binary forms, with or without
* modification is not permitted without explicit written permission from the
* copyright owner.
*
* A non-exclusive royalty-free right is granted to the copyright owner of the
* Software to use, modify and distribute all modifications to the Software in
* future versions of the Software.
*
*/
package fi.codecrew.moya.beans;
import fi.codecrew.moya.enums.TournamentStatus;
import fi.codecrew.moya.enums.TournamentTeamMemberRole;
import fi.codecrew.moya.enums.apps.TournamentPermission;
import fi.codecrew.moya.facade.*;
import fi.codecrew.moya.model.*;
import javax.annotation.security.RolesAllowed;
import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import java.util.Date;
import java.util.List;
/**
* Session Bean implementation class TournamentBean
*/
@Stateless
@LocalBean
public class TournamentBean implements TournamentBeanLocal {
@EJB
private TournamentRuleFacade tournamentRuleFacade;
@EJB
private TournamentGameFacade tournamentGameFacade;
@EJB
private TournamentFacade tournamentFacade;
@EJB
private TournamentParticipantFacade tournamentParticipantFacade;
@EJB
private EventUserFacade eventUserFacade;
@EJB
private EventBean eventBean;
@EJB
private PermissionBean permissionBean;
@EJB
private TournamentTeamMemberFacade tournamentTeamMemberFacade;
@EJB
private UserBean userBean;
/**
* Default constructor.
*/
public TournamentBean() {
// TODO Auto-generated constructor stub
}
@Override
@RolesAllowed(TournamentPermission.S_VIEW)
public List<TournamentRule> getRulesByGame(TournamentGame tg) {
return tournamentRuleFacade.getRulesByGame(tg);
}
@Override
public boolean canUserParticipate(Tournament tournament) {
if(!permissionBean.hasPermission(TournamentPermission.PARTICIPATE))
return false;
if(tournament == null) {
return false;
}
// no roles specified, open for everyone
if(tournament.getOpenForRoles() == null || tournament.getOpenForRoles().size() == 0) {
return true;
}
List<Role> currentUserRoles = userBean.findUsersRoles(permissionBean.getCurrentUser());
for (Role r : tournament.getOpenForRoles()) {
if (currentUserRoles.contains(r)) {
return true;
}
}
return false;
}
@Override
@RolesAllowed(TournamentPermission.S_VIEW)
public List<TournamentGame> getGames() {
return tournamentGameFacade.getGames();
}
@Override
@RolesAllowed(TournamentPermission.S_MANAGE_ALL)
public TournamentGame createGame(TournamentGame tg) {
tournamentGameFacade.create(tg);
return tg;
}
@Override
@RolesAllowed(TournamentPermission.S_MANAGE_ALL)
public TournamentRule createRule(TournamentRule tr) {
tournamentRuleFacade.create(tr);
return tr;
}
@Override
@RolesAllowed(TournamentPermission.S_VIEW)
public TournamentGame findGame(Integer id) {
return tournamentGameFacade.find(id);
}
@Override
@RolesAllowed(TournamentPermission.S_VIEW)
public TournamentRule findRule(Integer id) {
return tournamentRuleFacade.find(id);
}
@Override
@RolesAllowed(TournamentPermission.S_MANAGE_ALL)
public void createTournament(Tournament tournament) throws Exception {
// Assert correct event
if (eventBean.getCurrentEvent().equals(tournament.getLanEvent()))
tournamentFacade.create(tournament);
else
throw new Exception("tournament.invalid_event");
}
@Override
@RolesAllowed(TournamentPermission.S_MANAGE_ALL)
public void updateTournamentRules(TournamentRule tr) throws Exception {
// Assert correct event
if (eventBean.getCurrentEvent().equals(tr.getTournamentGame().getLanEvent())) {
tournamentRuleFacade.merge(tr);
} else {
throw new Exception("tournament.invalid_event");
}
}
@Override
@RolesAllowed(TournamentPermission.S_VIEW)
public List<Tournament> getTournamentsInStatus(TournamentStatus status, boolean useTimeConstraints, boolean invertMatch) {
if (useTimeConstraints)
if (!invertMatch)
return tournamentFacade.getTournamentsInStatusWithParticipationTimeIn(status, eventBean.getCurrentEvent());
else
return tournamentFacade.getTournamentsInStatusWithParticipationTimeNotIn(status, eventBean.getCurrentEvent());
else
return tournamentFacade.getTournamentsInStatus(status, eventBean.getCurrentEvent());
}
@Override
@RolesAllowed(TournamentPermission.S_VIEW)
public List<Tournament> getActiveTournaments() {
return tournamentFacade.getTournamentsNotInStatus(TournamentStatus.COMPLETED, eventBean.getCurrentEvent());
}
@Override
@RolesAllowed(TournamentPermission.S_VIEW)
public Tournament getTournamentById(Integer tournamentId) {
return tournamentFacade.find(tournamentId);
}
@Override
@RolesAllowed(TournamentPermission.S_MANAGE_ALL)
public void editTournament(Tournament tournament) {
tournamentFacade.merge(tournament);
}
@Override
@RolesAllowed(TournamentPermission.S_MANAGE_ALL)
public void deleteTournament(Tournament tournament) {
tournament = tournamentFacade.merge(tournament);
tournamentFacade.remove(tournament);
}
@Override
@RolesAllowed(TournamentPermission.S_PARTICIPATE)
public void createParticipation(TournamentParticipant tournamentParticipant) throws Exception {
Tournament t = tournamentFacade.find(tournamentParticipant.getTournament().getId());
Date currentTime = new Date();
// Assert registration time is correct
if (t.getRegistrationOpensAt() != null && t.getRegistrationClosesAt() != null && currentTime.after(t.getRegistrationOpensAt()) && currentTime.before(t.getRegistrationClosesAt())) {
// Assert participant size is smaller than max
if (t.getParticipants().size() < t.getMaxParticipants()) {
TournamentTeamMember capt = null;
for (TournamentTeamMember ttm : tournamentParticipant.getTeamMembers())
if (ttm.getRole() == TournamentTeamMemberRole.CAPTAIN)
capt = ttm;
// Assert team has a captain
if (capt != null) {
// Assert team has the correct number of players for a match
if (tournamentParticipant.getTeamMembers().size() >= tournamentParticipant.getTournament().getPlayersPerMatch()) {
tournamentParticipantFacade.create(tournamentParticipant);
t.getParticipants().add(tournamentParticipant);
} else {
throw new Exception("tournament.not_enough_players");
}
} else {
throw new Exception("tournament.no_captain");
}
} else {
throw new Exception("tournament.participation_full");
}
} else {
throw new Exception("tournament.not_within_participation_time");
}
}
@Override
@RolesAllowed(TournamentPermission.S_VIEW)
public boolean hasParticipations(EventUser currentUser, Tournament tournament) {
for (TournamentParticipant tp : tournament.getParticipants()) {
for (TournamentTeamMember tm : tp.getTeamMembers()) {
EventUser eu = tm.getEventUser();
if (eu.equals(currentUser)) {
return true;
}
}
}
return false;
}
@Override
@RolesAllowed(TournamentPermission.S_VIEW)
public List<TournamentParticipant> findOwnParticipations() {
EventUser currentUser = permissionBean.getCurrentUser();
return tournamentParticipantFacade.findByParticipator(currentUser);
}
@Override
@RolesAllowed(TournamentPermission.S_VIEW)
public EventUser findAvailablePlayerForTournamentByLogin(Tournament t, String login) throws Exception {
EventUser u = eventUserFacade.findByLogin(login.toLowerCase().trim());
if (u != null) {
if (!hasParticipations(u, t))
return u;
else
throw new Exception("tournaments.participation_already_exists");
} else {
throw new Exception("tournaments.participated_user_not_found");
}
}
@Override
@RolesAllowed(TournamentPermission.S_REMOVE_OWN_PARTICIPATION)
public void deleteParticipationById(Integer tpid) throws Exception {
TournamentParticipant tp = tournamentParticipantFacade.find(tpid);
EventUser executor = permissionBean.getCurrentUser();
// Assert we have permission to remove participant
if(tp.getParticipator().equals(executor)) {
// own participation, can we delete?
if(!permissionBean.hasPermission(TournamentPermission.REMOVE_OWN_PARTICIPATION)) {
throw new Exception("tournaments.can_only_remove_own_participations");
}
} else {
if(!permissionBean.hasPermission(TournamentPermission.MANAGE_ALL)) {
throw new Exception("tournaments.cannot_remove_others_participations");
}
}
// Assert that tournament has not closed participation
if (!permissionBean.hasPermission(TournamentPermission.MANAGE_ALL) && !tp.getTournament().getRegistrationClosesAt().after(new Date())) {
throw new Exception("tournaments.cannot_remove_participation_from_closed_tournament");
}
// All ok, do nukage
for (TournamentTeamMember ttm : tp.getTeamMembers()) {
tournamentTeamMemberFacade.remove(ttm);
}
// Let's not leave anything in cache :)
tp.getTournament().getParticipants().remove(tp);
tournamentParticipantFacade.remove(tp);
}
}