-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCustomizeTimeBoxEntity.java
More file actions
205 lines (176 loc) · 6.63 KB
/
CustomizeTimeBoxEntity.java
File metadata and controls
205 lines (176 loc) · 6.63 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
package com.debatetimer.entity.customize;
import com.debatetimer.domain.customize.Bell;
import com.debatetimer.domain.customize.CustomizeBoxType;
import com.debatetimer.domain.customize.CustomizeTimeBox;
import com.debatetimer.domain.customize.NormalTimeBox;
import com.debatetimer.domain.customize.Stance;
import com.debatetimer.domain.customize.TimeBasedTimeBox;
import com.debatetimer.exception.custom.DTClientErrorException;
import com.debatetimer.exception.errorcode.ClientErrorCode;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import java.util.List;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
@Table(name = "customize_time_box")
@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class CustomizeTimeBoxEntity {
public static final int SPEECH_TYPE_MAX_LENGTH = 10;
public static final int TIME_MULTIPLIER = 2;
public static final int SPEAKER_MAX_LENGTH = 5;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotNull
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "table_id")
private CustomizeTableEntity customizeTable;
private int sequence;
@NotNull
@Enumerated(EnumType.STRING)
private Stance stance;
private Integer time;
private String speaker;
@NotBlank
private String speechType;
@NotNull
@Enumerated(value = EnumType.STRING)
private CustomizeBoxType boxType;
private Integer timePerTeam;
private Integer timePerSpeaking;
public CustomizeTimeBoxEntity(
CustomizeTableEntity customizeTableEntity,
int sequence,
Stance stance,
String speechType,
CustomizeBoxType boxType,
Integer time,
String speaker
) {
validateNotTimeBasedType(boxType);
validateSpeechType(speechType);
validateSpeaker(speaker);
validateSequence(sequence);
validateTime(time);
this.customizeTable = customizeTableEntity;
this.sequence = sequence;
this.stance = stance;
this.time = time;
this.speaker = initializeSpeaker(speaker);
this.speechType = speechType;
this.boxType = boxType;
}
public CustomizeTimeBoxEntity(
CustomizeTableEntity customizeTableEntity,
int sequence,
Stance stance,
String speechType,
CustomizeBoxType boxType,
Integer timePerTeam,
Integer timePerSpeaking,
String speaker
) {
validateTimeBasedTimes(timePerTeam, timePerSpeaking);
validateTimeBasedType(boxType);
validateSpeechType(speechType);
validateSpeaker(speaker);
validateSequence(sequence);
validateTime(convertToTime(timePerTeam));
this.sequence = sequence;
this.stance = stance;
this.time = convertToTime(timePerTeam);
this.speaker = initializeSpeaker(speaker);
this.customizeTable = customizeTableEntity;
this.speechType = speechType;
this.boxType = boxType;
this.timePerTeam = timePerTeam;
this.timePerSpeaking = timePerSpeaking;
}
public CustomizeTimeBoxEntity(CustomizeTableEntity customizeTable, CustomizeTimeBox timeBox, int sequence) {
this.customizeTable = customizeTable;
this.sequence = sequence;
this.stance = timeBox.getStance();
this.time = timeBox.getTime() == null ? timeBox.getTimePerTeam() * 2 : timeBox.getTime(); // TODO : Nullable 의논
this.speaker = timeBox.getSpeaker();
this.speechType = timeBox.getSpeechType();
this.boxType = timeBox.getBoxType();
this.timePerTeam = timeBox.getTimePerTeam();
this.timePerSpeaking = timeBox.getTimePerSpeaking();
}
private static int convertToTime(Integer timePerTeam) {
if (timePerTeam == null) {
throw new DTClientErrorException(ClientErrorCode.INVALID_TIME_BOX_FORMAT);
}
return timePerTeam * TIME_MULTIPLIER;
}
private String initializeSpeaker(String speaker) {
if (speaker == null || speaker.isBlank()) {
return null;
}
return speaker;
}
private void validateSpeaker(String speaker) {
if (speaker != null && speaker.length() > SPEAKER_MAX_LENGTH) {
throw new DTClientErrorException(ClientErrorCode.INVALID_TIME_BOX_SPEAKER_LENGTH);
}
}
private void validateSequence(int sequence) {
if (sequence <= 0) {
throw new DTClientErrorException(ClientErrorCode.INVALID_TIME_BOX_SEQUENCE);
}
}
private void validateTime(int time) {
if (time <= 0) {
throw new DTClientErrorException(ClientErrorCode.INVALID_TIME_BOX_TIME);
}
}
private void validateTime(Integer time) {
if (time == null || time <= 0) {
throw new DTClientErrorException(ClientErrorCode.INVALID_TIME_BOX_TIME);
}
}
private void validateTimeBasedTimes(Integer timePerTeam, Integer timePerSpeaking) {
validateTime(timePerTeam);
if (timePerSpeaking == null) {
return;
}
validateTime(timePerSpeaking);
if (timePerTeam < timePerSpeaking) {
throw new DTClientErrorException(ClientErrorCode.INVALID_TIME_BASED_TIME);
}
}
private void validateTimeBasedType(CustomizeBoxType boxType) {
if (boxType.isNotTimeBased()) {
throw new DTClientErrorException(ClientErrorCode.INVALID_TIME_BOX_FORMAT);
}
}
private void validateNotTimeBasedType(CustomizeBoxType boxType) {
if (boxType.isTimeBased()) {
throw new DTClientErrorException(ClientErrorCode.INVALID_TIME_BOX_FORMAT);
}
}
private void validateSpeechType(String speechType) {
if (speechType.length() > SPEECH_TYPE_MAX_LENGTH) {
throw new DTClientErrorException(ClientErrorCode.INVALID_TIME_BOX_SPEECH_TYPE_LENGTH);
}
}
public CustomizeTimeBox toDomain(List<Bell> bells) {
if (boxType.isTimeBased()) {
return new TimeBasedTimeBox(stance, speechType, speaker, timePerTeam, timePerSpeaking);
}
return new NormalTimeBox(stance, speechType, speaker, time, bells);
}
}