-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompetitionTable.java
More file actions
105 lines (87 loc) · 2.62 KB
/
CompetitionTable.java
File metadata and controls
105 lines (87 loc) · 2.62 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
package cat.udl.eps.softarch.fll.domain;
import java.util.ArrayList;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import jakarta.validation.constraints.Size;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Entity
@Table(name = "competition_tables")
@Getter
@Setter
@NoArgsConstructor
@EqualsAndHashCode(callSuper = false, onlyExplicitlyIncluded = true)
public class CompetitionTable extends UriEntity<String> {
@Id
@EqualsAndHashCode.Include
private String id;
@OneToMany(mappedBy = "competitionTable", cascade = {CascadeType.PERSIST, CascadeType.MERGE})
@JsonManagedReference("table-matches")
@Setter(lombok.AccessLevel.NONE)
private List<Match> matches = new ArrayList<>();
@OneToMany(mappedBy = "supervisesTable")
@Size(max = 3, message = "A table can have a maximum of 3 referees")
@JsonManagedReference("table-referees")
@Setter(lombok.AccessLevel.NONE)
private List<Referee> referees = new ArrayList<>();
public void setMatches(List<Match> matches) {
new ArrayList<>(this.matches).forEach(this::removeMatch);
if (matches != null) {
matches.forEach(this::addMatch);
}
}
public void addMatch(Match match) {
if (match == null || matches.contains(match)) {
return;
}
CompetitionTable previousTable = match.getCompetitionTable();
if (previousTable != null && previousTable != this) {
previousTable.removeMatch(match);
}
matches.add(match);
match.setCompetitionTable(this);
}
public void removeMatch(Match match) {
if (match == null) {
return;
}
if (matches.remove(match)) {
match.setCompetitionTable(null);
}
}
public void setReferees(List<Referee> referees) {
new ArrayList<>(this.referees).forEach(this::removeReferee);
if (referees != null) {
referees.forEach(this::addReferee);
}
}
public void addReferee(Referee referee) {
if (referee == null || referees.contains(referee)) {
return;
}
if (referees.size() >= 3) {
throw new IllegalStateException("A table can have a maximum of 3 referees");
}
CompetitionTable previousTable = referee.getSupervisesTable();
if (previousTable != null && previousTable != this) {
previousTable.removeReferee(referee);
}
referees.add(referee);
referee.setSupervisesTable(this);
}
public void removeReferee(Referee referee) {
if (referee == null) {
return;
}
if (referees.remove(referee)) {
referee.setSupervisesTable(null);
}
}
}