From 3f6bc5098dedd2963845e77c95f469f85b152c41 Mon Sep 17 00:00:00 2001 From: anhefti Date: Thu, 24 Jun 2021 13:45:12 +0200 Subject: [PATCH] SEBSERV-58 point 6 --- .../weblayer/api/IndicatorController.java | 57 +++++++++++++++++++ src/main/resources/messages.properties | 2 + 2 files changed, 59 insertions(+) diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/IndicatorController.java b/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/IndicatorController.java index 2f3a28f3..1548f22c 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/IndicatorController.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/IndicatorController.java @@ -8,17 +8,25 @@ package ch.ethz.seb.sebserver.webservice.weblayer.api; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; + import org.mybatis.dynamic.sql.SqlTable; +import org.springframework.validation.FieldError; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import ch.ethz.seb.sebserver.gbl.api.API; +import ch.ethz.seb.sebserver.gbl.api.APIMessage; +import ch.ethz.seb.sebserver.gbl.api.APIMessage.APIMessageException; import ch.ethz.seb.sebserver.gbl.api.EntityType; import ch.ethz.seb.sebserver.gbl.api.POSTMapper; import ch.ethz.seb.sebserver.gbl.model.Domain; import ch.ethz.seb.sebserver.gbl.model.EntityProcessingReport; import ch.ethz.seb.sebserver.gbl.model.GrantEntity; import ch.ethz.seb.sebserver.gbl.model.exam.Indicator; +import ch.ethz.seb.sebserver.gbl.model.exam.Indicator.Threshold; import ch.ethz.seb.sebserver.gbl.profile.WebServiceProfile; import ch.ethz.seb.sebserver.gbl.util.Pair; import ch.ethz.seb.sebserver.gbl.util.Result; @@ -86,6 +94,26 @@ public class IndicatorController extends EntityController return Result.of(entity); } + @Override + protected Result validForCreate(final Indicator entity) { + final Result validForCreate = super.validForCreate(entity); + if (validForCreate.hasError()) { + return validForCreate; + } + + return checkThresholdConsistency(entity); + } + + @Override + protected Result validForSave(final Indicator entity) { + final Result validForSave = super.validForSave(entity); + if (validForSave.hasError()) { + return validForSave; + } + + return checkThresholdConsistency(entity); + } + @Override protected GrantEntity toGrantEntity(final Indicator entity) { if (entity == null) { @@ -128,4 +156,33 @@ public class IndicatorController extends EntityController } + private Result checkThresholdConsistency(final Indicator entity) { + if (entity != null) { + final List emptyThresholds = entity.thresholds.stream() + .filter(t -> t.getValue() == null) + .collect(Collectors.toList()); + + if (!emptyThresholds.isEmpty()) { + throw new APIMessageException(APIMessage.fieldValidationError( + new FieldError( + Domain.EXAM.TYPE_NAME, + Domain.EXAM.ATTR_SUPPORTER, + "indicator:thresholds:thresholdEmpty"))); + } + + final Set values = entity.thresholds.stream() + .map(t -> t.getValue()) + .collect(Collectors.toSet()); + + if (values.size() != entity.thresholds.size()) { + throw new APIMessageException(APIMessage.fieldValidationError( + new FieldError( + Domain.EXAM.TYPE_NAME, + Domain.EXAM.ATTR_SUPPORTER, + "indicator:thresholds:thresholdDuplicate"))); + } + } + return Result.of(entity); + } + } diff --git a/src/main/resources/messages.properties b/src/main/resources/messages.properties index fc67fbe3..246eb3a1 100644 --- a/src/main/resources/messages.properties +++ b/src/main/resources/messages.properties @@ -90,6 +90,8 @@ sebserver.form.validation.fieldError.exists=This name already exists. Please cho sebserver.form.validation.fieldError.email=Invalid mail address sebserver.form.validation.fieldError.serverNotAvailable=No service seems to be available within the given URL sebserver.form.validation.fieldError.url.invalid=Invalid URL. The given URL cannot be reached. +sebserver.form.validation.fieldError.thresholdDuplicate=There are duplicate threshold values. +sebserver.form.validation.fieldError.thresholdEmpty=There are empty threshold entries. sebserver.error.unexpected=Unexpected Error sebserver.page.message=Information sebserver.dialog.confirm.title=Confirmation