From 0b00995aa79fc8a43ecfaf09c22638da227d748e Mon Sep 17 00:00:00 2001
From: anhefti
Date: Mon, 17 May 2021 19:26:26 +0200
Subject: [PATCH] simplified LMS API
---
.../webservice/servicelayer/dao/ExamDAO.java | 2 -
.../dao/impl/ExamConfigurationMapDAOImpl.java | 2 +-
.../servicelayer/dao/impl/ExamDAOImpl.java | 47 ++-----------
.../servicelayer/lms/LmsAPITemplate.java | 39 +----------
.../lms/impl/AbstractCachedCourseAccess.java | 8 ---
.../lms/impl/AbstractCourseAccess.java | 23 -------
.../lms/impl/edx/OpenEdxCourseAccess.java | 66 ++++++++++++------
.../lms/impl/edx/OpenEdxLmsAPITemplate.java | 40 ++++-------
.../lms/impl/mockup/MockupLmsAPITemplate.java | 21 +++---
.../lms/impl/moodle/MoodleCourseAccess.java | 67 ++++++++++---------
.../lms/impl/moodle/MoodleLmsAPITemplate.java | 28 ++------
.../session/impl/ExamSessionServiceImpl.java | 2 +-
.../api/ExamAdministrationController.java | 2 +-
13 files changed, 117 insertions(+), 230 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 4b76abec..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
@@ -37,8 +37,6 @@ public interface ExamDAO extends ActivatableEntityDAO, BulkActionSup
* happened */
Result examGrantEntityByClientConnection(Long connectionId);
- Result getWithQuizDataFromCache(Long id);
-
/** Get all active Exams for a given institution.
*
* @param institutionId the identifier of the institution
diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/dao/impl/ExamConfigurationMapDAOImpl.java b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/dao/impl/ExamConfigurationMapDAOImpl.java
index 59a919fd..358ed409 100644
--- a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/dao/impl/ExamConfigurationMapDAOImpl.java
+++ b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/dao/impl/ExamConfigurationMapDAOImpl.java
@@ -392,7 +392,7 @@ public class ExamConfigurationMapDAOImpl implements ExamConfigurationMapDAO {
final String status = config.getStatus();
final Exam exam = this.examDAO
- .getWithQuizDataFromCache(record.getExamId())
+ .byPK(record.getExamId())
.getOr(null);
return new ExamConfigurationMap(
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 1e6920c5..e1525283 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
@@ -119,13 +119,6 @@ public class ExamDAOImpl implements ExamDAO {
.map(record -> toDomainModel(record, null, null).getOrThrow());
}
- @Override
- @Transactional(readOnly = true)
- public Result getWithQuizDataFromCache(final Long id) {
- return recordById(id)
- .flatMap(this::toDomainModelFromCache);
- }
-
@Override
@Transactional(readOnly = true)
public Result> all(final Long institutionId, final Boolean active) {
@@ -169,7 +162,6 @@ public class ExamDAOImpl implements ExamDAO {
return Result.tryCatch(() -> {
- final boolean cached = filterMap.getBoolean(Exam.FILTER_CACHED_QUIZZES);
final String name = filterMap.getQuizName();
final DateTime from = filterMap.getExamFromTime();
final Predicate quizDataFilter = exam -> {
@@ -236,7 +228,7 @@ public class ExamDAOImpl implements ExamDAO {
.build()
.execute();
- return this.toDomainModel(records, cached)
+ return this.toDomainModel(records)
.getOrThrow()
.stream()
.filter(quizDataFilter.and(predicate))
@@ -768,17 +760,6 @@ public class ExamDAOImpl implements ExamDAO {
exam.getDescription());
}
- private Result toDomainModelFromCache(final ExamRecord record) {
-
- return this.lmsAPIService
- .getLmsAPITemplate(record.getLmsSetupId())
- .flatMap(template -> this.toDomainModel(
- record,
- template.getQuizFromCache(record.getExternalId())
- .getOrThrow(),
- null));
- }
-
private Result toDomainModel(final ExamRecord record) {
return toDomainModel(
record.getLmsSetupId(),
@@ -787,12 +768,6 @@ public class ExamDAOImpl implements ExamDAO {
}
private Result> toDomainModel(final Collection records) {
- return toDomainModel(records, false);
- }
-
- private Result> toDomainModel(
- final Collection records,
- final boolean cached) {
return Result.tryCatch(() -> {
@@ -807,8 +782,7 @@ public class ExamDAOImpl implements ExamDAO {
.stream()
.flatMap(entry -> toDomainModel(
entry.getKey(),
- entry.getValue(),
- cached)
+ entry.getValue())
.onError(error -> log.error(
"Failed to get quizzes from LMS Setup: {}",
entry.getKey(), error))
@@ -822,14 +796,6 @@ public class ExamDAOImpl implements ExamDAO {
final Long lmsSetupId,
final Collection records) {
- return toDomainModel(lmsSetupId, records, false);
- }
-
- private Result> toDomainModel(
- final Long lmsSetupId,
- final Collection records,
- final boolean cached) {
-
return Result.tryCatch(() -> {
// map records
@@ -840,7 +806,7 @@ public class ExamDAOImpl implements ExamDAO {
// get and map quizzes
final Map quizzes = this.lmsAPIService
.getLmsAPITemplate(lmsSetupId)
- .map(template -> getQuizzesFromLMS(template, recordMapping.keySet(), cached))
+ .map(template -> getQuizzesFromLMS(template, recordMapping.keySet()))
.onError(error -> log.error("Failed to get quizzes for exams: ", error))
.getOr(Collections.emptyList())
.stream()
@@ -894,13 +860,10 @@ public class ExamDAOImpl implements ExamDAO {
private Collection> getQuizzesFromLMS(
final LmsAPITemplate template,
- final Set ids,
- final boolean cached) {
+ final Set ids) {
try {
- return (cached)
- ? template.getQuizzesFromCache(ids)
- : template.getQuizzes(ids);
+ return template.getQuizzes(ids);
} catch (final Exception e) {
log.error("Unexpected error while using LmsAPITemplate to get quizzes: ", e);
return Collections.emptyList();
diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/LmsAPITemplate.java b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/LmsAPITemplate.java
index 2a40847e..80b397b7 100644
--- a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/LmsAPITemplate.java
+++ b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/LmsAPITemplate.java
@@ -8,15 +8,10 @@
package ch.ethz.seb.sebserver.webservice.servicelayer.lms;
-import java.util.Arrays;
import java.util.Collection;
-import java.util.HashSet;
import java.util.List;
import java.util.Set;
-import org.apache.commons.lang3.StringUtils;
-
-import ch.ethz.seb.sebserver.gbl.api.EntityType;
import ch.ethz.seb.sebserver.gbl.async.MemoizingCircuitBreaker;
import ch.ethz.seb.sebserver.gbl.model.exam.Chapters;
import ch.ethz.seb.sebserver.gbl.model.exam.Exam;
@@ -27,7 +22,6 @@ import ch.ethz.seb.sebserver.gbl.model.institution.LmsSetupTestResult;
import ch.ethz.seb.sebserver.gbl.model.user.ExamineeAccountDetails;
import ch.ethz.seb.sebserver.gbl.util.Result;
import ch.ethz.seb.sebserver.webservice.servicelayer.dao.FilterMap;
-import ch.ethz.seb.sebserver.webservice.servicelayer.dao.ResourceNotFoundException;
import ch.ethz.seb.sebserver.webservice.servicelayer.lms.impl.AbstractCourseAccess;
/** Defines an LMS API access template to build SEB Server LMS integration.
@@ -135,41 +129,10 @@ public interface LmsAPITemplate {
/** Get the quiz data with specified identifier.
*
- * Default implementation: Uses {@link #getQuizzes(Set ids) } and returns the first matching or an error.
*
* @param id the quiz data identifier
* @return Result refer to the quiz data or to an error when happened */
- default Result getQuiz(final String id) {
- if (StringUtils.isBlank(id)) {
- return Result.ofError(new RuntimeException("missing model id"));
- }
-
- return getQuizzes(new HashSet<>(Arrays.asList(id)))
- .stream()
- .findFirst()
- .orElse(Result.ofError(new ResourceNotFoundException(EntityType.EXAM, id)));
- }
-
- /** Get all {@link QuizData } for the set of {@link QuizData }-identifiers (ids) from the LMS defined within the
- * underling LmsSetup, in a collection of Results.
- *
- * If there is caching involved this function shall try to get the data from the cache first.
- *
- * NOTE: This function depends on the specific LMS implementation and on whether caching the quiz data
- * makes sense or not. Following strategy is recommended:
- * Looks first in the cache if the whole set of {@link QuizData } can be get from the cache.
- * If all quizzes are cached, returns all from cache.
- * If one or more quiz is not in the cache, requests all quizzes from the API and refreshes the cache
- *
- * @param ids the Set of Quiz identifiers to get the {@link QuizData } for
- * @return Collection of all {@link QuizData } from the given id set */
- Collection> getQuizzesFromCache(Set ids);
-
- /** Get a particular quiz data from cache if available. If not, tries to get it from the LMS.
- *
- * @param id the quiz identifier, external identifier of the exam.
- * @return Result refer to the {@link QuizData } or to an error when happended */
- Result getQuizFromCache(String id);
+ Result getQuiz(final String id);
/** Clears the underling caches if there are some for a particular implementation. */
void clearCache();
diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/AbstractCachedCourseAccess.java b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/AbstractCachedCourseAccess.java
index 864ddf5b..f9eb1431 100644
--- a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/AbstractCachedCourseAccess.java
+++ b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/AbstractCachedCourseAccess.java
@@ -9,8 +9,6 @@
package ch.ethz.seb.sebserver.webservice.servicelayer.lms.impl;
import java.util.Collection;
-import java.util.Set;
-import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -21,7 +19,6 @@ import org.springframework.core.env.Environment;
import ch.ethz.seb.sebserver.gbl.Constants;
import ch.ethz.seb.sebserver.gbl.async.AsyncService;
import ch.ethz.seb.sebserver.gbl.model.exam.QuizData;
-import ch.ethz.seb.sebserver.gbl.util.Result;
/** This implements an overall short time cache for QuizData objects for all implementing
* instances. It uses EH-Cache with a short time to live about 1 - 2 minutes.
@@ -97,11 +94,6 @@ public abstract class AbstractCachedCourseAccess extends AbstractCourseAccess {
this.cache.evict(createCacheKey);
}
- @Override
- public Result>> getQuizzesFromCache(final Set ids) {
- return Result.of(ids.stream().map(this::getQuizFromCache).collect(Collectors.toList()));
- }
-
/** Get the LMS setup identifier that is wrapped within the implementing template.
* This is used to create the cache Key.
*
diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/AbstractCourseAccess.java b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/AbstractCourseAccess.java
index 0b0afa2c..1f095800 100644
--- a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/AbstractCourseAccess.java
+++ b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/AbstractCourseAccess.java
@@ -8,7 +8,6 @@
package ch.ethz.seb.sebserver.webservice.servicelayer.lms.impl;
-import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;
@@ -135,28 +134,6 @@ public abstract class AbstractCourseAccess {
Collections.emptyMap());
}
- /** This abstraction has no cache implementation and therefore this returns a Result
- * with an "No cache supported error.
- *
- * To implement and use caching, this must be overridden and implemented
- *
- * @param id The identifier of the QuizData to get from cache
- * @return Result with an "No cache supported error */
- public Result getQuizFromCache(final String id) {
- return Result.ofRuntimeError("No cache supported");
- }
-
- /** This abstraction has no cache implementation and therefore this returns a Result
- * with an "No cache supported error.
- *
- * To implement and use caching, this must be overridden and implemented
- *
- * @param ids Collection of quiz data identifier to get from the cache
- * @return Result with an "No cache supported error */
- public Result>> getQuizzesFromCache(final Set ids) {
- return Result.ofRuntimeError("No cache supported");
- }
-
/** Provides a supplier for the quiz data request to use within the circuit breaker */
protected abstract Supplier> quizzesSupplier(final Set ids);
diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/edx/OpenEdxCourseAccess.java b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/edx/OpenEdxCourseAccess.java
index 521b3db3..40bb11cd 100644
--- a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/edx/OpenEdxCourseAccess.java
+++ b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/edx/OpenEdxCourseAccess.java
@@ -12,9 +12,12 @@ import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
+import java.util.Collections;
import java.util.HashMap;
+import java.util.HashSet;
import java.util.List;
import java.util.Map;
+import java.util.NoSuchElementException;
import java.util.Set;
import java.util.function.Supplier;
import java.util.stream.Collectors;
@@ -195,29 +198,52 @@ final class OpenEdxCourseAccess extends AbstractCachedCourseAccess {
.getOrThrow();
}
- @Override
- public Result getQuizFromCache(final String id) {
- return Result.tryCatch(() -> {
+ public Collection> getQuizzesFromCache(final Set ids) {
+ final HashSet leftIds = new HashSet<>(ids);
+ final Collection> result = new ArrayList<>();
+ ids.stream()
+ .map(this::getQuizFromCache)
+ .forEach(q -> {
+ if (q != null) {
+ leftIds.remove(q.id);
+ result.add(Result.of(q));
+ }
+ });
- // first try to get it from short time cache
- QuizData quizData = super.getFromCache(id);
- if (quizData != null) {
- return quizData;
- }
+ if (!leftIds.isEmpty()) {
+ super.quizzesRequest.protectedRun(this.quizzesSupplier(leftIds))
+ .onError(error -> log.error("Failed to get quizzes by ids: ", error))
+ .getOrElse(() -> Collections.emptyList())
+ .stream()
+ .forEach(q -> {
+ leftIds.remove(q.id);
+ result.add(Result.of(q));
+ });
+ }
- // Otherwise get one course from LMS and cache
- final LmsSetup lmsSetup = getApiTemplateDataSupplier().getLmsSetup();
- final String externalStartURI = getExternalLMSServerAddress(lmsSetup);
- quizData = quizDataOf(
- lmsSetup,
- this.getOneCourse(id, this.restTemplate, id),
- externalStartURI);
+ if (!leftIds.isEmpty()) {
+ leftIds.forEach(q -> result.add(Result.ofError(new NoSuchElementException())));
+ }
- if (quizData != null) {
- super.putToCache(quizData);
- }
- return quizData;
- });
+ return result;
+ }
+
+ public QuizData getQuizFromCache(final String id) {
+ return super.getFromCache(id);
+ }
+
+ public QuizData getQuizFromLMS(final String id) {
+ final LmsSetup lmsSetup = getApiTemplateDataSupplier().getLmsSetup();
+ final String externalStartURI = getExternalLMSServerAddress(lmsSetup);
+ final QuizData quizData = quizDataOf(
+ lmsSetup,
+ this.getOneCourse(id, this.restTemplate, id),
+ externalStartURI);
+
+ if (quizData != null) {
+ super.putToCache(quizData);
+ }
+ return quizData;
}
@Override
diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/edx/OpenEdxLmsAPITemplate.java b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/edx/OpenEdxLmsAPITemplate.java
index 7a1c068e..7713e792 100644
--- a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/edx/OpenEdxLmsAPITemplate.java
+++ b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/edx/OpenEdxLmsAPITemplate.java
@@ -10,9 +10,7 @@ package ch.ethz.seb.sebserver.webservice.servicelayer.lms.impl.edx;
import java.util.Collection;
import java.util.List;
-import java.util.Map;
import java.util.Set;
-import java.util.function.Function;
import java.util.stream.Collectors;
import org.slf4j.Logger;
@@ -82,33 +80,21 @@ final class OpenEdxLmsAPITemplate implements LmsAPITemplate {
.collect(Collectors.toList()));
}
+ @Override
+ public Result getQuiz(final String id) {
+ return Result.tryCatch(() -> {
+ final QuizData quizFromCache = this.openEdxCourseAccess.getQuizFromCache(id);
+ if (quizFromCache != null) {
+ return quizFromCache;
+ }
+
+ return this.openEdxCourseAccess.getQuizFromLMS(id);
+ });
+ }
+
@Override
public Collection> getQuizzes(final Set ids) {
- final Map mapping = this.openEdxCourseAccess
- .quizzesSupplier(ids)
- .get()
- .stream()
- .collect(Collectors.toMap(qd -> qd.id, Function.identity()));
-
- return ids.stream()
- .map(id -> {
- final QuizData data = mapping.get(id);
- return (data == null) ? Result. ofRuntimeError("Missing id: " + id) : Result.of(data);
- })
- .collect(Collectors.toList());
- }
-
- @Override
- public Result getQuizFromCache(final String id) {
- return this.openEdxCourseAccess
- .getQuizFromCache(id)
- .orElse(() -> getQuiz(id));
- }
-
- @Override
- public Collection> getQuizzesFromCache(final Set ids) {
- return this.openEdxCourseAccess.getQuizzesFromCache(ids)
- .getOrElse(() -> getQuizzes(ids));
+ return this.openEdxCourseAccess.getQuizzesFromCache(ids);
}
@Override
diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/mockup/MockupLmsAPITemplate.java b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/mockup/MockupLmsAPITemplate.java
index 150d1c04..4ca961e5 100644
--- a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/mockup/MockupLmsAPITemplate.java
+++ b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/mockup/MockupLmsAPITemplate.java
@@ -9,9 +9,7 @@
package ch.ethz.seb.sebserver.webservice.servicelayer.lms.impl.mockup;
import java.util.ArrayList;
-import java.util.Arrays;
import java.util.Collection;
-import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
@@ -164,6 +162,15 @@ public class MockupLmsAPITemplate implements LmsAPITemplate {
});
}
+ @Override
+ public Result getQuiz(final String id) {
+ return Result.of(this.mockups
+ .stream()
+ .filter(q -> id.equals(q.id))
+ .findFirst()
+ .get());
+ }
+
@Override
public Collection> getQuizzes(final Set ids) {
if (!authenticate()) {
@@ -178,16 +185,6 @@ public class MockupLmsAPITemplate implements LmsAPITemplate {
.collect(Collectors.toList());
}
- @Override
- public Collection> getQuizzesFromCache(final Set ids) {
- return getQuizzes(ids);
- }
-
- @Override
- public Result getQuizFromCache(final String id) {
- return getQuizzes(new HashSet<>(Arrays.asList(id))).iterator().next();
- }
-
@Override
public void clearCache() {
diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/moodle/MoodleCourseAccess.java b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/moodle/MoodleCourseAccess.java
index 62ac7d84..3b5e328c 100644
--- a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/moodle/MoodleCourseAccess.java
+++ b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/moodle/MoodleCourseAccess.java
@@ -20,6 +20,7 @@ import java.util.function.Function;
import java.util.function.Supplier;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
+import java.util.stream.Stream;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
@@ -213,7 +214,6 @@ public class MoodleCourseAccess extends AbstractCourseAccess {
return LmsSetupTestResult.ofOkay(LmsType.MOODLE);
}
- @Override
public Result getQuizFromCache(final String id) {
return Result.tryCatch(() -> {
@@ -245,43 +245,48 @@ public class MoodleCourseAccess extends AbstractCourseAccess {
}
}
- throw new RuntimeException("No quiz found in cache");
+ // get from LMS
+ final Set ids = Stream.of(id).collect(Collectors.toSet());
+ return super.quizzesRequest
+ .protectedRun(quizzesSupplier(ids))
+ .getOrThrow()
+ .get(0);
});
}
- @Override
- public Result>> getQuizzesFromCache(final Set ids) {
- return Result.tryCatch(() -> {
- final List cached = getCached();
- final List available = (cached != null)
- ? cached
- : Collections.emptyList();
+ public Collection> getQuizzesFromCache(final Set ids) {
+ final List cached = getCached();
+ final List available = (cached != null)
+ ? cached
+ : Collections.emptyList();
- final Map quizMapping = available
+ final Map quizMapping = available
+ .stream()
+ .collect(Collectors.toMap(q -> q.id, Function.identity()));
+
+ if (!quizMapping.keySet().containsAll(ids)) {
+
+ final Map collect = super.quizzesRequest
+ .protectedRun(quizzesSupplier(ids))
+ .onError(error -> log.error("Failed to get quizzes by ids: ", error))
+ .getOrElse(() -> Collections.emptyList())
.stream()
- .collect(Collectors.toMap(q -> q.id, Function.identity()));
-
- if (!quizMapping.keySet().containsAll(ids)) {
-
- final Map collect = quizzesSupplier(ids).get()
- .stream()
- .collect(Collectors.toMap(qd -> qd.id, Function.identity()));
- if (collect != null) {
- quizMapping.clear();
- quizMapping.putAll(collect);
- }
+ .collect(Collectors.toMap(qd -> qd.id, Function.identity()));
+ if (collect != null) {
+ quizMapping.clear();
+ quizMapping.putAll(collect);
}
+ }
- return ids
- .stream()
- .map(id -> {
- final QuizData q = quizMapping.get(id);
- return (q == null)
- ? Result. ofError(new NoSuchElementException("Quiz with id: " + id))
- : Result.of(q);
- })
- .collect(Collectors.toList());
- });
+ return ids
+ .stream()
+ .map(id -> {
+ final QuizData q = quizMapping.get(id);
+ return (q == null)
+ ? Result. ofError(new NoSuchElementException("Quiz with id: " + id))
+ : Result.of(q);
+ })
+ .collect(Collectors.toList());
}
@Override
diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/moodle/MoodleLmsAPITemplate.java b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/moodle/MoodleLmsAPITemplate.java
index f4079e82..72086e70 100644
--- a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/moodle/MoodleLmsAPITemplate.java
+++ b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/moodle/MoodleLmsAPITemplate.java
@@ -10,9 +10,7 @@ package ch.ethz.seb.sebserver.webservice.servicelayer.lms.impl.moodle;
import java.util.Collection;
import java.util.List;
-import java.util.Map;
import java.util.Set;
-import java.util.function.Function;
import java.util.stream.Collectors;
import org.slf4j.Logger;
@@ -95,25 +93,13 @@ public class MoodleLmsAPITemplate implements LmsAPITemplate {
}
@Override
- public Collection> getQuizzes(final Set ids) {
- final Map mapping = this.moodleCourseAccess
- .quizzesSupplier(ids)
- .get()
- .stream()
- .collect(Collectors.toMap(qd -> qd.id, Function.identity()));
-
- return ids.stream()
- .map(id -> {
- final QuizData data = mapping.get(id);
- return (data == null) ? Result. ofRuntimeError("Missing id: " + id) : Result.of(data);
- })
- .collect(Collectors.toList());
+ public Result getQuiz(final String id) {
+ return this.moodleCourseAccess.getQuizFromCache(id);
}
@Override
- public Result getQuizFromCache(final String id) {
- return this.moodleCourseAccess.getQuizFromCache(id)
- .orElse(() -> getQuiz(id));
+ public Collection> getQuizzes(final Set ids) {
+ return this.moodleCourseAccess.getQuizzesFromCache(ids);
}
@Override
@@ -121,12 +107,6 @@ public class MoodleLmsAPITemplate implements LmsAPITemplate {
this.moodleCourseAccess.clearCache();
}
- @Override
- public Collection> getQuizzesFromCache(final Set ids) {
- return this.moodleCourseAccess.getQuizzesFromCache(ids)
- .getOrElse(() -> getQuizzes(ids));
- }
-
@Override
public Result getCourseChapters(final String courseId) {
return Result.tryCatch(() -> this.moodleCourseAccess
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 1860b58d..7b613ed7 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
@@ -124,7 +124,7 @@ public class ExamSessionServiceImpl implements ExamSessionService {
final Collection result = new ArrayList<>();
final Exam exam = this.examDAO
- .getWithQuizDataFromCache(examId)
+ .byPK(examId)
.getOrThrow();
// check lms connection
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 6efd89c4..44b552bf 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
@@ -280,7 +280,7 @@ public class ExamAdministrationController extends EntityController {
checkReadPrivilege(institutionId);
return this.examDAO
- .getWithQuizDataFromCache(modelId)
+ .byPK(modelId)
.flatMap(this.examAdminService::isRestricted)
.getOrThrow();
}