From 2a5a2437597f0fe2bea6724b365018a31a9b5e74 Mon Sep 17 00:00:00 2001 From: anhefti Date: Mon, 5 Oct 2020 16:53:59 +0200 Subject: [PATCH] fixed running exam cache --- .../webservice/servicelayer/dao/ExamDAO.java | 16 +++++++++------- .../servicelayer/dao/impl/ExamDAOImpl.java | 13 ++----------- .../session/impl/ExamSessionServiceImpl.java | 6 +++++- .../weblayer/api/ClientEventController.java | 2 +- 4 files changed, 17 insertions(+), 20 deletions(-) diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/dao/ExamDAO.java b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/dao/ExamDAO.java index fd6d1391..f651b48f 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/dao/ExamDAO.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/dao/ExamDAO.java @@ -24,11 +24,19 @@ public interface ExamDAO extends ActivatableEntityDAO, BulkActionSup /** Get a GrantEntity for the exam of specified id (PK) * This is actually a Exam instance but with no course data loaded. - * + * * @param id The id of the exam (PK) * @return Result referring to the GrantEntity of the exam or to an error when happened */ Result examGrantEntityByPK(final Long id); + /** Get an Exam GrantEntity by a given ClientConnection id. + * This is actually a Exam instance but with no course data loaded. + * + * @param connectionId the connection identifier + * @return a Result containing the Exam GrantEntity by a given ClientConnection id or refer to an error if + * happened */ + Result examGrantEntityByClientConnection(Long connectionId); + /** Get all active Exams for a given institution. * * @param institutionId the identifier of the institution @@ -56,12 +64,6 @@ public interface ExamDAO extends ActivatableEntityDAO, BulkActionSup key = "#exam.id") Result save(Exam exam); - /** Get an Exam by a given ClientConnection id. - * - * @param connectionId the connection identifier - * @return a Result containing the Exam by a given ClientConnection id or refer to an error if happened */ - Result byClientConnection(Long connectionId); - /** Use this to get identifiers of all exams in a specified state for a specified institution. * * @param institutionId the institution identifier. May be null for all institutions diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/dao/impl/ExamDAOImpl.java b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/dao/impl/ExamDAOImpl.java index 1a433549..b10ee3cb 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/dao/impl/ExamDAOImpl.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/dao/impl/ExamDAOImpl.java @@ -92,12 +92,11 @@ public class ExamDAOImpl implements ExamDAO { @Override @Transactional(readOnly = true) - public Result byClientConnection(final Long connectionId) { + public Result examGrantEntityByClientConnection(final Long connectionId) { return Result.tryCatch(() -> this.clientConnectionRecordMapper .selectByPrimaryKey(connectionId)) .flatMap(ccRecord -> recordById(ccRecord.getExamId())) - .flatMap(this::toDomainModelCached) - .onError(TransactionHandler::rollback); + .map(record -> toDomainModel(record, null).getOrThrow()); } @Override @@ -671,14 +670,6 @@ public class ExamDAOImpl implements ExamDAO { }); } - private Result toDomainModelCached(final ExamRecord record) { - return Result.tryCatch(() -> this.lmsAPIService - .getLmsAPITemplate(record.getLmsSetupId()) - .getOrThrow()) - .flatMap(template -> template.getQuizFromCache(record.getExternalId())) - .flatMap(quizData -> this.toDomainModel(record, quizData)); - } - private Result toDomainModel(final ExamRecord record) { return toDomainModel( record.getLmsSetupId(), diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/session/impl/ExamSessionServiceImpl.java b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/session/impl/ExamSessionServiceImpl.java index 66babe9c..afebcd43 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/session/impl/ExamSessionServiceImpl.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/session/impl/ExamSessionServiceImpl.java @@ -239,9 +239,13 @@ public class ExamSessionServiceImpl implements ExamSessionService { .putIfAbsent(Exam.FILTER_ATTR_ACTIVE, Constants.TRUE_STRING) .putIfAbsent(Exam.FILTER_ATTR_STATUS, ExamStatus.RUNNING.name()); + // NOTE: we evict the exam from the cache (if present) to ensure user is seeing always the current state of the Exam return this.examDAO.allMatching(filterMap, predicate) .map(col -> col.stream() - .map(exam -> this.examSessionCacheService.getRunningExam(exam.id)) + .map(exam -> { + this.examSessionCacheService.evict(exam); + return this.examSessionCacheService.getRunningExam(exam.id); + }) .filter(Objects::nonNull) .collect(Collectors.toList())); } diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/ClientEventController.java b/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/ClientEventController.java index 705534c5..a29378c2 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/ClientEventController.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/ClientEventController.java @@ -126,7 +126,7 @@ public class ClientEventController extends ReadonlyEntityController