truncate seconds

This commit is contained in:
_Bastler 2021-11-26 07:26:23 +01:00
parent 430d1c731d
commit 9ad604d012
4 changed files with 164 additions and 140 deletions

View File

@ -6,6 +6,7 @@ package de.bstly.we.borrow.controller.validation;
import java.time.Duration;
import java.time.LocalTime;
import java.time.ZoneOffset;
import java.time.temporal.ChronoUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@ -21,6 +22,7 @@ import de.bstly.we.borrow.model.BorrowRequest;
import de.bstly.we.borrow.model.BorrowRequestStatus;
import de.bstly.we.borrow.model.QBorrowRequest;
import de.bstly.we.borrow.repository.BorrowRequestRepository;
import de.bstly.we.businesslogic.support.InstantHelper;
/**
* The Class BorrowRequestValidator.
@ -61,112 +63,114 @@ public class BorrowRequestValidator implements Validator {
return;
}
validateTime(borrowRequest, borrowItem, errors);
}
public void validateTime(BorrowRequest borrowRequest, BorrowItem borrowItem, Errors errors) {
if (borrowRequest.getStarts() == null) {
errors.rejectValue("starts", "REQUIRED");
return;
}
if (borrowRequest.getEnds() == null) {
errors.rejectValue("ends", "REQUIRED");
return;
}
borrowRequest
.setStarts(InstantHelper.truncate(borrowRequest.getStarts(), ChronoUnit.SECONDS));
borrowRequest.setEnds(InstantHelper.truncate(borrowRequest.getEnds(), ChronoUnit.SECONDS));
// expiry + start
if (borrowRequest.getEnds() != null && borrowRequest.getStarts() != null) {
if (borrowRequest.getStarts().isAfter(borrowRequest.getEnds())
|| borrowRequestRepository.exists(qBorrowRequest.item
.eq(borrowRequest.getItem())
// exlude self
.and(qBorrowRequest.id.ne(
borrowRequest.getId() == null ? -1L : borrowRequest.getId()))
// accepted
.and(qBorrowRequest.status.eq(BorrowRequestStatus.ACCEPTED))
// expires after start
.and(qBorrowRequest.ends.after(borrowRequest.getStarts()))
// start before expires
.and(qBorrowRequest.starts.before(borrowRequest.getEnds())))) {
errors.rejectValue("starts", "ALREADY_USED");
errors.rejectValue("ends", "ALREADY_USED");
if (borrowRequest.getStarts().isAfter(borrowRequest.getEnds())
|| borrowRequestRepository.exists(qBorrowRequest.item.eq(borrowRequest.getItem())
// exlude self
.and(qBorrowRequest.id
.ne(borrowRequest.getId() == null ? -1L : borrowRequest.getId()))
// accepted
.and(qBorrowRequest.status.eq(BorrowRequestStatus.ACCEPTED))
// expires after start
.and(qBorrowRequest.ends.after(borrowRequest.getStarts()))
// start before expires
.and(qBorrowRequest.starts.before(borrowRequest.getEnds())))) {
errors.rejectValue("starts", "ALREADY_USED");
errors.rejectValue("ends", "ALREADY_USED");
} else {
if (borrowItem.getMinDuration() != null && borrowItem.getMinDuration().compareTo(
Duration.between(borrowRequest.getEnds(), borrowRequest.getStarts())) > 0) {
errors.rejectValue("starts", "TOO_SHORT");
errors.rejectValue("ends", "TOO_SHORT");
} else if (borrowItem.getMaxDuration() != null
&& Duration.between(borrowRequest.getEnds(), borrowRequest.getStarts())
.compareTo(borrowItem.getMaxDuration()) > 0) {
errors.rejectValue("starts", "TOO_LONG");
errors.rejectValue("ends", "TOO_LONG");
} else {
if (borrowItem.getMinDuration() != null && borrowItem.getMinDuration().compareTo(
Duration.between(borrowRequest.getEnds(), borrowRequest.getStarts())) > 0) {
errors.rejectValue("starts", "TOO_SHORT");
errors.rejectValue("ends", "TOO_SHORT");
} else if (borrowItem.getMaxDuration() != null
&& Duration.between(borrowRequest.getEnds(), borrowRequest.getStarts())
.compareTo(borrowItem.getMaxDuration()) > 0) {
errors.rejectValue("starts", "TOO_LONG");
errors.rejectValue("ends", "TOO_LONG");
} else {
boolean validSlot = false;
boolean validStart = false;
boolean validEnd = false;
borrowItemManager.applySlots(borrowItem);
boolean validSlot = false;
boolean validStart = false;
boolean validEnd = false;
borrowItemManager.applySlots(borrowItem);
switch (borrowItem.getAvailability()) {
case ALWAYS:
validSlot = true;
break;
case MANUAL:
for (BorrowItemSlot borrowItemSlot : borrowItem.getSlots()) {
if (borrowItemSlot instanceof BorrowItemManualSlot) {
BorrowItemManualSlot borrowItemManualSlot = (BorrowItemManualSlot) borrowItemSlot;
if (borrowRequest.getStarts()
.compareTo(borrowItemManualSlot.getStart()) >= 0) {
validStart = true;
}
if (borrowRequest.getEnds()
.compareTo(borrowItemManualSlot.getEnd()) <= 0) {
validEnd = true;
}
if (validStart && validEnd) {
validSlot = true;
break;
}
switch (borrowItem.getAvailability()) {
case ALWAYS:
validSlot = true;
break;
case MANUAL:
for (BorrowItemSlot borrowItemSlot : borrowItem.getSlots()) {
if (borrowItemSlot instanceof BorrowItemManualSlot) {
BorrowItemManualSlot borrowItemManualSlot = (BorrowItemManualSlot) borrowItemSlot;
if (borrowRequest.getStarts()
.compareTo(borrowItemManualSlot.getStart()) >= 0) {
validStart = true;
}
if (borrowRequest.getEnds()
.compareTo(borrowItemManualSlot.getEnd()) <= 0) {
validEnd = true;
}
if (validStart && validEnd) {
validSlot = true;
break;
}
}
break;
case PERIOD:
for (BorrowItemSlot borrowItemSlot : borrowItem.getSlots()) {
if (borrowItemSlot instanceof BorrowItemPeriodSlot) {
BorrowItemPeriodSlot borrowItemPeriodSlot = (BorrowItemPeriodSlot) borrowItemSlot;
if (borrowRequest.getStarts().atZone(ZoneOffset.UTC).getDayOfWeek()
.compareTo(borrowItemPeriodSlot.getStartDay()) >= 0
&& LocalTime
.ofInstant(borrowRequest.getStarts(),
ZoneOffset.UTC)
.compareTo(
borrowItemPeriodSlot.getStartTime()) >= 0) {
validStart = true;
}
if (borrowRequest.getEnds().atZone(ZoneOffset.UTC).getDayOfWeek()
.compareTo(borrowItemPeriodSlot.getEndDay()) <= 0
&& LocalTime
.ofInstant(borrowRequest.getEnds(), ZoneOffset.UTC)
.compareTo(
borrowItemPeriodSlot.getEndTime()) <= 0) {
validEnd = true;
}
if (validStart && validEnd) {
validSlot = true;
break;
}
}
}
break;
default:
break;
}
break;
case PERIOD:
for (BorrowItemSlot borrowItemSlot : borrowItem.getSlots()) {
if (borrowItemSlot instanceof BorrowItemPeriodSlot) {
BorrowItemPeriodSlot borrowItemPeriodSlot = (BorrowItemPeriodSlot) borrowItemSlot;
if (borrowRequest.getStarts().atZone(ZoneOffset.UTC).getDayOfWeek()
.compareTo(borrowItemPeriodSlot.getStartDay()) >= 0
&& LocalTime
.ofInstant(borrowRequest.getStarts(), ZoneOffset.UTC)
.compareTo(borrowItemPeriodSlot.getStartTime()) >= 0) {
validStart = true;
}
if (borrowRequest.getEnds().atZone(ZoneOffset.UTC).getDayOfWeek()
.compareTo(borrowItemPeriodSlot.getEndDay()) <= 0
&& LocalTime.ofInstant(borrowRequest.getEnds(), ZoneOffset.UTC)
.compareTo(borrowItemPeriodSlot.getEndTime()) <= 0) {
validEnd = true;
}
if (validStart && validEnd) {
validSlot = true;
break;
}
}
}
break;
default:
break;
}
if (!validSlot) {
if (!validStart) {
errors.rejectValue("starts", "INVALID");
}
if (!validEnd) {
errors.rejectValue("ends", "INVALID");
}
if (!validSlot) {
if (!validStart) {
errors.rejectValue("starts", "INVALID");
}
if (!validEnd) {
errors.rejectValue("ends", "INVALID");
}
}
}
}
}
}

View File

@ -4,6 +4,7 @@
package de.bstly.we.jitsi.controller.validation;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@ -13,6 +14,7 @@ import org.springframework.validation.Validator;
import de.bstly.we.businesslogic.SystemPropertyManager;
import de.bstly.we.businesslogic.UserManager;
import de.bstly.we.businesslogic.support.InstantHelper;
import de.bstly.we.jitsi.model.JitsiRoom;
import de.bstly.we.jitsi.model.QJitsiRoom;
import de.bstly.we.jitsi.repository.JitsiRoomRepository;
@ -65,9 +67,9 @@ public class JitsiRoomValidator implements Validator {
/**
* Validate.
*
* @param owner the owner
* @param owner the owner
* @param roomName the room name
* @param errors the errors
* @param errors the errors
*/
public void validate(Long owner, String roomName, Errors errors) {
if (owner == null) {
@ -95,7 +97,7 @@ public class JitsiRoomValidator implements Validator {
* Validate room.
*
* @param roomName the room name
* @param errors the errors
* @param errors the errors
*/
public void validateRoom(String roomName, Errors errors) {
for (String systemRoomName : systemPropertyManager.get(RESERVED_JITSI_ROOMS, "")
@ -113,10 +115,24 @@ public class JitsiRoomValidator implements Validator {
* Validate expiry.
*
* @param jitsiRoom the jitsi room
* @param errors the errors
* @param errors the errors
*/
public void validateExpiry(JitsiRoom jitsiRoom, Errors errors) {
if (jitsiRoom.getStarts() != null) {
jitsiRoom.setStarts(InstantHelper.truncate(jitsiRoom.getStarts(), ChronoUnit.SECONDS));
}
if (jitsiRoom.getModerationStarts() != null) {
jitsiRoom.setModerationStarts(
InstantHelper.truncate(jitsiRoom.getModerationStarts(), ChronoUnit.SECONDS));
}
if (jitsiRoom.getExpires() != null) {
jitsiRoom
.setExpires(InstantHelper.truncate(jitsiRoom.getExpires(), ChronoUnit.SECONDS));
}
// no moderation start without start
if (jitsiRoom.getStarts() == null && jitsiRoom.getModerationStarts() != null) {
errors.rejectValue("moderationStarts", "NOT_VALID");

View File

@ -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)));
}
}
}

View File

@ -12,7 +12,7 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>11</java.version>
<revision>1.4.1-SNAPSHOT</revision>
<revision>1.4.2-SNAPSHOT</revision>
</properties>
<parent>