From df78a6a2a458ce7ed38c4ffb8eca1b4ac7816e42 Mon Sep 17 00:00:00 2001 From: anhefti Date: Wed, 19 Jun 2024 12:58:17 +0200 Subject: [PATCH] SEBSERV-417 apply exam_data before SEB restriction --- .../exam/impl/ExamAdminServiceImpl.java | 5 +- .../exam/impl/ExamImportServiceImpl.java | 8 +- .../lms/SEBRestrictionService.java | 12 ++- .../lms/impl/SEBRestrictionServiceImpl.java | 73 ++++++++----------- .../api/ExamAdministrationController.java | 4 +- 5 files changed, 54 insertions(+), 48 deletions(-) diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/exam/impl/ExamAdminServiceImpl.java b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/exam/impl/ExamAdminServiceImpl.java index 00baddc5..d265220a 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/exam/impl/ExamAdminServiceImpl.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/exam/impl/ExamAdminServiceImpl.java @@ -247,7 +247,10 @@ public class ExamAdminServiceImpl implements ExamAdminService { @Override public Result applyQuitPassword(final Exam exam) { - return this.sebRestrictionService.applyQuitPassword(exam); + return this.examConfigurationValueService + .applyQuitPasswordToConfigs(exam.id, exam.quitPassword) + .onError(t -> log.error("Failed to quit password for Exam: {}", exam, t)) + .map(id -> exam); } @Override diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/exam/impl/ExamImportServiceImpl.java b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/exam/impl/ExamImportServiceImpl.java index ea787942..84ab209e 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/exam/impl/ExamImportServiceImpl.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/exam/impl/ExamImportServiceImpl.java @@ -27,6 +27,7 @@ import ch.ethz.seb.sebserver.gbl.util.Result; import ch.ethz.seb.sebserver.gbl.util.Utils; import ch.ethz.seb.sebserver.webservice.servicelayer.dao.AdditionalAttributesDAO; import ch.ethz.seb.sebserver.webservice.servicelayer.dao.ExamDAO; +import ch.ethz.seb.sebserver.webservice.servicelayer.exam.ExamAdminService; import ch.ethz.seb.sebserver.webservice.servicelayer.exam.ExamImportService; import ch.ethz.seb.sebserver.webservice.servicelayer.exam.ExamTemplateService; import ch.ethz.seb.sebserver.webservice.servicelayer.lms.LmsAPIService; @@ -50,7 +51,7 @@ public class ExamImportServiceImpl implements ExamImportService { private final ExamDAO examDAO; private final ExamTemplateService examTemplateService; - private final SEBRestrictionService sebRestrictionService; + private final ExamAdminService examAdminService; private final boolean appSignatureKeyEnabled; private final int defaultNumericalTrustThreshold; @@ -58,6 +59,7 @@ public class ExamImportServiceImpl implements ExamImportService { final ExamDAO examDAO, final ExamTemplateService examTemplateService, final SEBRestrictionService sebRestrictionService, + final ExamAdminService examAdminService, final AdditionalAttributesDAO additionalAttributesDAO, final LmsAPIService lmsAPIService, final @Value("${sebserver.webservice.api.admin.exam.app.signature.key.enabled:false}") boolean appSignatureKeyEnabled, @@ -65,7 +67,7 @@ public class ExamImportServiceImpl implements ExamImportService { this.examDAO = examDAO; this.examTemplateService = examTemplateService; - this.sebRestrictionService = sebRestrictionService; + this.examAdminService = examAdminService; this.additionalAttributesDAO = additionalAttributesDAO; this.lmsAPIService = lmsAPIService; this.appSignatureKeyEnabled = appSignatureKeyEnabled; @@ -99,7 +101,7 @@ public class ExamImportServiceImpl implements ExamImportService { }) .flatMap(this::applyAdditionalSEBRestrictions) .onError(error -> errors.add(APIMessage.ErrorMessage.EXAM_IMPORT_ERROR_AUTO_RESTRICTION.of(error))) - .flatMap(sebRestrictionService::applyQuitPassword) + .flatMap(examAdminService::applyQuitPassword) .onError(error -> errors.add(APIMessage.ErrorMessage.EXAM_IMPORT_ERROR_QUIT_PASSWORD.of(error))) .flatMap(examTemplateService::applyScreenProctoringSettingsForExam) .onError(error -> errors.add(APIMessage.ErrorMessage.EXAM_IMPORT_ERROR_SCREEN_PROCTORING_SETTINGS.of(error))); diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/SEBRestrictionService.java b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/SEBRestrictionService.java index 4521a286..8455a3cf 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/SEBRestrictionService.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/SEBRestrictionService.java @@ -52,6 +52,16 @@ public interface SEBRestrictionService { * @return Result refer to the Exam instance or to an error if happened */ Result applySEBClientRestriction(Exam exam); + default Result applySEBRestrictionIfExamRunning(final Exam exam) { + return Result.tryCatch(() -> { + if (exam.status != Exam.ExamStatus.RUNNING) { + return exam; + } + + return applySEBClientRestriction(exam).getOrThrow(); + }); + } + /** Release SEB Client restriction within the LMS API for a specified Exam. * * @param exam the Exam instance @@ -68,8 +78,6 @@ public interface SEBRestrictionService { * to the LMS */ boolean checkSebRestrictionSet(Exam exam); - Result applyQuitPassword(final Exam exam); - @EventListener void notifyLmsSetupChange(final LmsSetupChangeEvent event); diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/SEBRestrictionServiceImpl.java b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/SEBRestrictionServiceImpl.java index b26bd18d..e1273c3f 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/SEBRestrictionServiceImpl.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/SEBRestrictionServiceImpl.java @@ -8,8 +8,6 @@ package ch.ethz.seb.sebserver.webservice.servicelayer.lms.impl; -import static ch.ethz.seb.sebserver.webservice.datalayer.batis.mapper.LmsSetupRecordDynamicSqlSupport.lmsType; - import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -22,7 +20,6 @@ import java.util.stream.Collectors; import ch.ethz.seb.sebserver.gbl.model.Activatable; import ch.ethz.seb.sebserver.webservice.datalayer.batis.model.AdditionalAttributeRecord; -import ch.ethz.seb.sebserver.webservice.servicelayer.exam.ExamConfigurationValueService; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -62,20 +59,17 @@ public class SEBRestrictionServiceImpl implements SEBRestrictionService { private final LmsAPIService lmsAPIService; private final AdditionalAttributesDAO additionalAttributesDAO; private final ExamConfigService examConfigService; - private final ExamConfigurationValueService examConfigurationValueService; protected SEBRestrictionServiceImpl( final ExamDAO examDAO, final LmsAPIService lmsAPIService, final AdditionalAttributesDAO additionalAttributesDAO, - final ExamConfigService examConfigService, - final ExamConfigurationValueService examConfigurationValueService) { + final ExamConfigService examConfigService) { this.examDAO = examDAO; this.lmsAPIService = lmsAPIService; this.additionalAttributesDAO = additionalAttributesDAO; this.examConfigService = examConfigService; - this.examConfigurationValueService = examConfigurationValueService; } @Override @@ -101,13 +95,13 @@ public class SEBRestrictionServiceImpl implements SEBRestrictionService { return true; } - @Override - public Result applyQuitPassword(final Exam exam) { - return this.examConfigurationValueService - .applyQuitPasswordToConfigs(exam.id, exam.quitPassword) - .map(id -> applyQuitPasswordWithRestrictionIfNeeded(exam)) - .onError(t -> log.error("Failed to quit password for Exam: {}", exam, t)); - } +// @Override +// public Result applyQuitPassword(final Exam exam) { +// return this.examConfigurationValueService +// .applyQuitPasswordToConfigs(exam.id, exam.quitPassword) +// .map(id -> applyQuitPasswordWithRestrictionIfNeeded(exam)) +// .onError(t -> log.error("Failed to quit password for Exam: {}", exam, t)); +// } @Override public void notifyLmsSetupChange(final LmsSetupChangeEvent event) { @@ -122,11 +116,8 @@ public class SEBRestrictionServiceImpl implements SEBRestrictionService { examDAO.allActiveForLMSSetup(Arrays.asList(lmsSetup.id)) .getOrThrow() .forEach(exam -> { - try { - this.applySEBRestrictionIfExamRunning(exam); - } catch (final Exception e) { - log.warn("Failed to update SEB restriction for exam: {} error: {}", exam.name, e.getMessage()); - } + this.applySEBRestrictionIfExamRunning(exam) + .onError(error -> log.warn("Failed to update SEB restriction for exam: {} error: {}", exam.name, error.getMessage())); }); } else if (event.activation == Activatable.ActivationAction.DEACTIVATE) { releaseAllRestrictionsOf(lmsSetup) @@ -161,29 +152,29 @@ public class SEBRestrictionServiceImpl implements SEBRestrictionService { }); } - private Exam applyQuitPasswordWithRestrictionIfNeeded(final Exam exam) { - if (exam.status != Exam.ExamStatus.RUNNING) { - return exam; - } +// private Exam applyQuitPasswordWithRestrictionIfNeeded(final Exam exam) { +// if (exam.status != Exam.ExamStatus.RUNNING) { +// return exam; +// } +// +// final LmsSetup lmsSetup = getLmsAPIService().getLmsSetup(exam.lmsSetupId).getOrThrow(); +// if (!lmsSetup.lmsType.features.contains(Features.LMS_FULL_INTEGRATION)) { +// applySEBRestrictionIfExamRunning(exam); +// } +// +// return exam; +// } - final LmsSetup lmsSetup = getLmsAPIService().getLmsSetup(exam.lmsSetupId).getOrThrow(); - if (!lmsSetup.lmsType.features.contains(Features.LMS_FULL_INTEGRATION)) { - applySEBRestrictionIfExamRunning(exam); - } - - return exam; - } - - private Exam applySEBRestrictionIfExamRunning(final Exam exam) { - if (exam.status != Exam.ExamStatus.RUNNING) { - return exam; - } - - return this.applySEBClientRestriction(exam) - .flatMap(e -> this.examDAO.setSEBRestriction(e.id, true)) - .onError(t -> log.error("Failed to update SEB Client restriction for Exam: {}", exam, t)) - .getOrThrow(); - } +// private Exam applySEBRestrictionIfExamRunning(final Exam exam) { +// if (exam.status != Exam.ExamStatus.RUNNING) { +// return exam; +// } +// +// return this.applySEBClientRestriction(exam) +// .flatMap(e -> this.examDAO.setSEBRestriction(e.id, true)) +// .onError(t -> log.error("Failed to update SEB Client restriction for Exam: {}", exam, t)) +// .getOrThrow(); +// } @Override @Transactional diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/ExamAdministrationController.java b/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/ExamAdministrationController.java index 9ac4b36f..49a05154 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/ExamAdministrationController.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/ExamAdministrationController.java @@ -616,7 +616,9 @@ public class ExamAdministrationController extends EntityController { @Override protected Result notifyCreated(final Exam entity) { - return examImportService.applyExamImportInitialization(entity); + return examImportService.applyExamImportInitialization(entity) + .flatMap(this.fullLmsIntegrationService::applyExamDataToLMS) + .flatMap(this.sebRestrictionService::applySEBRestrictionIfExamRunning); } @Override