Skip to content

Commit bacdf43

Browse files
authored
Merge pull request #61 from MireiaTerri/main
Feat: Add basic field validations to domain entities
2 parents 91a47de + 624070e commit bacdf43

File tree

61 files changed

+2156
-1019
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+2156
-1019
lines changed

src/main/java/cat/udl/eps/softarch/fll/domain/Award.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,18 @@ public class Award extends UriEntity<Long> {
5050
@JsonIdentityReference(alwaysAsId = true)
5151
private Team winner;
5252

53+
public static Award create(String name, Edition edition, Team winner) {
54+
DomainValidation.requireNonBlank(name, "name");
55+
DomainValidation.requireNonNull(edition, "edition");
56+
DomainValidation.requireNonNull(winner, "winner");
57+
58+
Award award = new Award();
59+
award.name = name;
60+
award.edition = edition;
61+
award.winner = winner;
62+
return award;
63+
}
64+
5365
@Override
5466
public Long getId() {
5567
return this.id;

src/main/java/cat/udl/eps/softarch/fll/domain/Coach.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package cat.udl.eps.softarch.fll.domain;
22

3-
import java.util.HashSet;
4-
import java.util.Set;
53
import jakarta.persistence.Column;
64
import jakarta.persistence.Entity;
75
import jakarta.persistence.GeneratedValue;
@@ -13,6 +11,8 @@
1311
import lombok.Data;
1412
import lombok.EqualsAndHashCode;
1513
import lombok.ToString;
14+
import java.util.HashSet;
15+
import java.util.Set;
1616

1717
@Entity
1818
@Data
@@ -30,11 +30,21 @@ public class Coach extends UriEntity<Integer> {
3030
@NotBlank
3131
@Email
3232
@Column(unique = true)
33-
3433
private String emailAddress;
34+
3535
private String phoneNumber;
3636

3737
@ManyToMany(mappedBy = "trainedBy")
3838
@ToString.Exclude
3939
private Set<Team> teams = new HashSet<>();
40+
41+
public static Coach create(String name, String emailAddress) {
42+
DomainValidation.requireNonBlank(name, "name");
43+
DomainValidation.requireValidEmail(emailAddress, "emailAddress");
44+
45+
Coach coach = new Coach();
46+
coach.name = name;
47+
coach.emailAddress = emailAddress;
48+
return coach;
49+
}
4050
}
Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package cat.udl.eps.softarch.fll.domain;
22

3-
import java.util.ArrayList;
4-
import java.util.List;
53
import com.fasterxml.jackson.annotation.JsonManagedReference;
64
import jakarta.persistence.CascadeType;
75
import jakarta.persistence.Entity;
@@ -11,39 +9,56 @@
119
import jakarta.validation.constraints.Size;
1210
import lombok.EqualsAndHashCode;
1311
import lombok.Getter;
14-
import lombok.NoArgsConstructor;
15-
import lombok.Setter;
12+
import java.util.ArrayList;
13+
import java.util.List;
1614

1715
@Entity
1816
@Table(name = "competition_tables")
19-
@Getter
20-
@Setter
21-
@NoArgsConstructor
22-
@EqualsAndHashCode(callSuper = false, onlyExplicitlyIncluded = true)
2317
public class CompetitionTable extends UriEntity<String> {
2418

2519
@Id
2620
@EqualsAndHashCode.Include
2721
private String id;
28-
29-
@OneToMany(mappedBy = "competitionTable", cascade = {CascadeType.PERSIST, CascadeType.MERGE})
22+
@Getter
23+
@OneToMany(mappedBy = "competitionTable", cascade = CascadeType.ALL)
3024
@JsonManagedReference("table-matches")
31-
@Setter(lombok.AccessLevel.NONE)
3225
private List<Match> matches = new ArrayList<>();
33-
26+
@Getter
3427
@OneToMany(mappedBy = "supervisesTable")
3528
@Size(max = 3, message = "A table can have a maximum of 3 referees")
3629
@JsonManagedReference("table-referees")
37-
@Setter(lombok.AccessLevel.NONE)
3830
private List<Referee> referees = new ArrayList<>();
3931

32+
public static CompetitionTable create(String id) {
33+
DomainValidation.requireNonBlank(id, "id");
34+
CompetitionTable table = new CompetitionTable();
35+
table.id = id;
36+
return table;
37+
}
38+
39+
@Override
40+
public String getId() {
41+
return id;
42+
}
43+
44+
public void setId(String id) {
45+
this.id = id;
46+
}
47+
4048
public void setMatches(List<Match> matches) {
4149
new ArrayList<>(this.matches).forEach(this::removeMatch);
4250
if (matches != null) {
4351
matches.forEach(this::addMatch);
4452
}
4553
}
4654

55+
public void setReferees(List<Referee> referees) {
56+
new ArrayList<>(this.referees).forEach(this::removeReferee);
57+
if (referees != null) {
58+
referees.forEach(this::addReferee);
59+
}
60+
}
61+
4762
public void addMatch(Match match) {
4863
if (match == null) {
4964
return;
@@ -55,10 +70,11 @@ public void addMatch(Match match) {
5570

5671
CompetitionTable previousTable = match.getCompetitionTable();
5772
if (previousTable != null && previousTable != this) {
58-
previousTable.getMatches().removeIf(m -> m == match);
73+
previousTable.removeMatch(match);
74+
}
75+
if (!matches.contains(match)) {
76+
matches.add(match);
5977
}
60-
61-
this.matches.add(match);
6278
match.setCompetitionTable(this);
6379
}
6480

@@ -67,18 +83,8 @@ public void removeMatch(Match match) {
6783
return;
6884
}
6985

70-
if (this.matches.removeIf(m -> m == match)) {
71-
match.setCompetitionTable(null);
72-
}
73-
}
74-
75-
public void setReferees(List<Referee> referees) {
76-
if (referees == this.referees) {
77-
return;
78-
}
79-
List<Referee> incoming = (referees == null) ? List.of() : new ArrayList<>(referees);
80-
new ArrayList<>(this.referees).forEach(this::removeReferee);
81-
incoming.forEach(this::addReferee);
86+
matches.remove(match);
87+
match.setCompetitionTable(null);
8288
}
8389

8490
public void addReferee(Referee referee) {
@@ -96,10 +102,12 @@ public void addReferee(Referee referee) {
96102

97103
CompetitionTable previousTable = referee.getSupervisesTable();
98104
if (previousTable != null && previousTable != this) {
99-
previousTable.getReferees().removeIf(r -> r == referee);
105+
previousTable.removeReferee(referee);
100106
}
101-
102-
this.referees.add(referee);
107+
if (referees.contains(referee)) {
108+
return;
109+
}
110+
referees.add(referee);
103111
referee.setSupervisesTable(this);
104112
}
105113

@@ -108,8 +116,7 @@ public void removeReferee(Referee referee) {
108116
return;
109117
}
110118

111-
if (this.referees.removeIf(r -> r == referee)) {
112-
referee.setSupervisesTable(null);
113-
}
119+
referees.remove(referee);
120+
referee.setSupervisesTable(null);
114121
}
115122
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package cat.udl.eps.softarch.fll.domain;
2+
3+
import java.time.LocalDate;
4+
import java.util.regex.Pattern;
5+
6+
public final class DomainValidation {
7+
8+
private static final Pattern EMAIL_PATTERN =
9+
Pattern.compile("^[\\w\\-.]+@([\\w-]+\\.)+[\\w-]{2,}$");
10+
11+
private DomainValidation() {
12+
}
13+
14+
public static void requireNonBlank(String value, String fieldName) {
15+
if (value == null) {
16+
throw new DomainValidationException(fieldName + " must not be null");
17+
}
18+
if (value.isBlank()) {
19+
throw new DomainValidationException(fieldName + " must not be blank");
20+
}
21+
}
22+
23+
public static void requireValidEmail(String email, String fieldName) {
24+
requireNonBlank(email, fieldName);
25+
if (!EMAIL_PATTERN.matcher(email).matches()) {
26+
throw new DomainValidationException(fieldName + " must be a valid email address");
27+
}
28+
}
29+
30+
public static void requireNonNegative(Integer value, String fieldName) {
31+
if (value == null) {
32+
throw new DomainValidationException(fieldName + " must not be null");
33+
}
34+
if (value < 0) {
35+
throw new DomainValidationException(fieldName + " must not be negative");
36+
}
37+
}
38+
39+
public static void requireMin(Integer value, Integer minValue, String fieldName) {
40+
if (value == null) {
41+
throw new DomainValidationException(fieldName + " must not be null");
42+
}
43+
if (value < minValue) {
44+
throw new DomainValidationException(fieldName + " must not be less than " + minValue);
45+
}
46+
}
47+
48+
public static void requirePast(LocalDate value, String fieldName) {
49+
if (value == null) {
50+
throw new DomainValidationException(fieldName + " must not be null");
51+
}
52+
if (!value.isBefore(LocalDate.now())) {
53+
throw new DomainValidationException(fieldName + " must be in the past");
54+
}
55+
}
56+
57+
public static void requireLengthBetween(String value, Integer minLength, Integer maxLength, String fieldName) {
58+
if (value == null) {
59+
throw new DomainValidationException(fieldName + " must not be null");
60+
}
61+
if (value.length() < minLength) {
62+
throw new DomainValidationException(fieldName + " length must not be less than " + minLength);
63+
}
64+
if (value.length() > maxLength) {
65+
throw new DomainValidationException(fieldName + " length must not be more than " + maxLength);
66+
}
67+
}
68+
69+
public static void requireMaxLength(String value, Integer maxLength, String fieldName) {
70+
if (value == null) {
71+
throw new DomainValidationException(fieldName + " must not be null");
72+
}
73+
if (value.length() > maxLength) {
74+
throw new DomainValidationException(fieldName + " length must not be more than " + maxLength);
75+
}
76+
}
77+
78+
public static void requireNonNull(Object value, String fieldName) {
79+
if (value == null) {
80+
throw new DomainValidationException(fieldName + " must not be null");
81+
}
82+
}
83+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package cat.udl.eps.softarch.fll.domain;
2+
3+
public class DomainValidationException extends RuntimeException {
4+
5+
public DomainValidationException(String message) {
6+
super(message);
7+
}
8+
}

src/main/java/cat/udl/eps/softarch/fll/domain/Edition.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package cat.udl.eps.softarch.fll.domain;
22

3-
import java.util.HashSet;
4-
import java.util.Set;
53
import jakarta.persistence.Column;
64
import jakarta.persistence.Entity;
75
import jakarta.persistence.EnumType;
@@ -18,6 +16,8 @@
1816
import lombok.Data;
1917
import lombok.EqualsAndHashCode;
2018
import lombok.ToString;
19+
import java.util.HashSet;
20+
import java.util.Set;
2121

2222
@Entity
2323
@Data
@@ -48,13 +48,28 @@ public class Edition extends UriEntity<Long> {
4848

4949
@ManyToMany
5050
@JoinTable(
51-
name = "edition_teams",
52-
joinColumns = @JoinColumn(name = "edition_id"),
53-
inverseJoinColumns = @JoinColumn(name = "team_name"))
51+
name = "edition_teams",
52+
joinColumns = @JoinColumn(name = "edition_id"),
53+
inverseJoinColumns = @JoinColumn(name = "team_name"))
5454
@ToString.Exclude
5555
@EqualsAndHashCode.Exclude
5656
private Set<Team> teams = new HashSet<>();
5757

58+
protected Edition() {
59+
}
60+
61+
public static Edition create(Integer year, String venueName, String description) {
62+
DomainValidation.requireNonNull(year, "year");
63+
DomainValidation.requireNonBlank(venueName, "venueName");
64+
DomainValidation.requireNonBlank(description, "description");
65+
66+
Edition edition = new Edition();
67+
edition.year = year;
68+
edition.venueName = venueName;
69+
edition.description = description;
70+
return edition;
71+
}
72+
5873
public boolean hasReachedMaxTeams() {
5974
return teams.size() >= MAX_TEAMS;
6075
}
Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,42 @@
11
package cat.udl.eps.softarch.fll.domain;
22

3-
import java.util.HashSet;
4-
import java.util.Set;
53
import jakarta.persistence.Column;
64
import jakarta.persistence.Entity;
75
import jakarta.persistence.ManyToMany;
86
import jakarta.persistence.Table;
97
import jakarta.validation.constraints.NotBlank;
8+
import lombok.AccessLevel;
109
import lombok.EqualsAndHashCode;
1110
import lombok.Getter;
11+
import lombok.NoArgsConstructor;
1212
import lombok.Setter;
1313
import lombok.ToString;
14+
import java.util.HashSet;
15+
import java.util.Set;
1416

1517
@Entity
1618
@Table(name = "floaters")
1719
@Getter
1820
@Setter
21+
@NoArgsConstructor(access = AccessLevel.PROTECTED)
1922
@EqualsAndHashCode(callSuper = true, onlyExplicitlyIncluded = true)
2023
public class Floater extends Volunteer {
2124

22-
2325
@NotBlank(message = "Student code is mandatory")
2426
@Column(unique = true)
2527
private String studentCode;
2628

2729
@ManyToMany(mappedBy = "floaters")
2830
@ToString.Exclude
2931
private Set<Team> assistedTeams = new HashSet<>();
32+
33+
public static Floater create(String name, String emailAddress, String phoneNumber, String studentCode) {
34+
DomainValidation.requireNonBlank(studentCode, "studentCode");
35+
36+
Floater floater = new Floater();
37+
floater.initFields(name, emailAddress, phoneNumber);
38+
floater.studentCode = studentCode;
39+
return floater;
40+
}
3041
}
3142

0 commit comments

Comments
 (0)