truncate seconds

This commit is contained in:
2021-11-26 07:26:23 +01:00
parent 430d1c731d
commit 9ad604d012
4 changed files with 164 additions and 140 deletions
@@ -16,6 +16,7 @@ import com.google.common.collect.Lists;
import com.querydsl.core.BooleanBuilder;
import de.bstly.we.businesslogic.SystemPropertyManager;
import de.bstly.we.businesslogic.support.InstantHelper;
import de.bstly.we.partey.timeslot.businesslogic.TimeslotManager;
import de.bstly.we.partey.timeslot.model.QTimeslot;
import de.bstly.we.partey.timeslot.model.Timeslot;
@@ -65,65 +66,68 @@ public class TimeslotValidator implements Validator {
public void validateTime(Timeslot timeslot, Errors errors) {
if (timeslot.getStarts() == null) {
errors.rejectValue("starts", "REQUIRED");
return;
}
if (timeslot.getEnds() == null) {
errors.rejectValue("ends", "REQUIRED");
return;
}
if (timeslot.getStarts() != null && timeslot.getEnds() != null) {
if (timeslot.getStarts().isAfter(timeslot.getStarts())) {
errors.rejectValue("starts", "NOT_VALID");
errors.rejectValue("ends", "NOT_VALID");
} else {
BooleanBuilder timeQuery = new BooleanBuilder();
timeslot.setStarts(InstantHelper.truncate(timeslot.getStarts(), ChronoUnit.SECONDS));
timeslot.setEnds(InstantHelper.truncate(timeslot.getEnds(), ChronoUnit.SECONDS));
// same type
timeQuery.and(qTimeslot.type.eq(timeslot.getType()));
if (timeslot.getStarts().isAfter(timeslot.getStarts())) {
errors.rejectValue("starts", "NOT_VALID");
errors.rejectValue("ends", "NOT_VALID");
} else {
BooleanBuilder timeQuery = new BooleanBuilder();
switch (timeslot.getType()) {
case AUDIO:
case AUDIO_STREAM:
timeQuery.and(qTimeslot.type
.in(Lists.newArrayList(TimeslotType.AUDIO, TimeslotType.AUDIO_STREAM)));
break;
case VIDEO:
case VIDEO_STREAM:
timeQuery.and(qTimeslot.type
.in(Lists.newArrayList(TimeslotType.VIDEO, TimeslotType.VIDEO_STREAM)));
break;
}
// same type
timeQuery.and(qTimeslot.type.eq(timeslot.getType()));
// ends after start
timeQuery
.and(qTimeslot.ends.after(timeslot.getStarts().minus(
systemPropertyManager.getLong(TimeslotManager.TIMESLOT_TOLERANCE,
TimeslotManager.TIMESLOT_TOLERANCE_DEFAULT),
ChronoUnit.MINUTES)));
switch (timeslot.getType()) {
case AUDIO:
case AUDIO_STREAM:
timeQuery.and(qTimeslot.type
.in(Lists.newArrayList(TimeslotType.AUDIO, TimeslotType.AUDIO_STREAM)));
break;
case VIDEO:
case VIDEO_STREAM:
timeQuery.and(qTimeslot.type
.in(Lists.newArrayList(TimeslotType.VIDEO, TimeslotType.VIDEO_STREAM)));
break;
}
// starts before end
timeQuery
.and(qTimeslot.starts.before(timeslot.getEnds().plus(
systemPropertyManager.getLong(TimeslotManager.TIMESLOT_TOLERANCE,
TimeslotManager.TIMESLOT_TOLERANCE_DEFAULT),
ChronoUnit.MINUTES)));
// ends after start
timeQuery
.and(qTimeslot.ends.after(timeslot.getStarts()
.minus(systemPropertyManager.getLong(TimeslotManager.TIMESLOT_TOLERANCE,
TimeslotManager.TIMESLOT_TOLERANCE_DEFAULT),
ChronoUnit.MINUTES)));
if (timeslot.getId() != null && timeslotRepository.existsById(timeslot.getId())) {
timeQuery.and(qTimeslot.id.ne(timeslot.getId()));
}
// starts before end
timeQuery
.and(qTimeslot.starts.before(timeslot.getEnds()
.plus(systemPropertyManager.getLong(TimeslotManager.TIMESLOT_TOLERANCE,
TimeslotManager.TIMESLOT_TOLERANCE_DEFAULT),
ChronoUnit.MINUTES)));
if (timeslotRepository.exists(timeQuery.getValue())) {
errors.rejectValue("starts", "ALREADY_EXISTS");
errors.rejectValue("ends", "ALREADY_EXISTS");
} else if (((timeslot.getEnds().getEpochSecond()
- timeslot.getStarts().getEpochSecond()) / 60) > systemPropertyManager
.getLong(TimeslotManager.TIMESLOT_MINUTES,
TimeslotManager.TIMESLOT_MINUTES_DEFAULT)) {
errors.rejectValue("ends", "TOO_LONG",
String.valueOf(
systemPropertyManager.getLong(TimeslotManager.TIMESLOT_MINUTES,
TimeslotManager.TIMESLOT_MINUTES_DEFAULT)));
}
if (timeslot.getId() != null && timeslotRepository.existsById(timeslot.getId())) {
timeQuery.and(qTimeslot.id.ne(timeslot.getId()));
}
if (timeslotRepository.exists(timeQuery.getValue())) {
errors.rejectValue("starts", "ALREADY_EXISTS");
errors.rejectValue("ends", "ALREADY_EXISTS");
} else if (((timeslot.getEnds().getEpochSecond()
- timeslot.getStarts().getEpochSecond()) / 60) > systemPropertyManager.getLong(
TimeslotManager.TIMESLOT_MINUTES,
TimeslotManager.TIMESLOT_MINUTES_DEFAULT)) {
errors.rejectValue("ends", "TOO_LONG",
String.valueOf(
systemPropertyManager.getLong(TimeslotManager.TIMESLOT_MINUTES,
TimeslotManager.TIMESLOT_MINUTES_DEFAULT)));
}
}
}