Skip to content

Commit

Permalink
CSCEXAM-000 Config reader as scala
Browse files Browse the repository at this point in the history
  • Loading branch information
lupari committed Aug 12, 2024
1 parent e6dfb98 commit 2c297f9
Show file tree
Hide file tree
Showing 9 changed files with 316 additions and 471 deletions.
18 changes: 10 additions & 8 deletions app/controllers/admin/SettingsController.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import play.libs.ws.WSRequest;
import play.mvc.Http;
import play.mvc.Result;
import scala.jdk.javaapi.CollectionConverters;

public class SettingsController extends BaseController {

Expand Down Expand Up @@ -93,7 +94,8 @@ public CompletionStage<Result> getMaturityInstructions(String lang, Optional<Str
return wrapAsPromise(badRequest("Language not supported"));
}
if (hash.isPresent()) {
ExamEnrolment enrolment = DB.find(ExamEnrolment.class)
ExamEnrolment enrolment = DB
.find(ExamEnrolment.class)
.where()
.eq("externalExam.hash", hash.get())
.findOne();
Expand Down Expand Up @@ -176,7 +178,7 @@ public Result getMaxFilesize() {
public Result getExamDurations() {
ObjectNode node = Json.newObject();
ArrayNode durations = node.putArray("examDurations");
configReader.getExamDurations().forEach(durations::add);
configReader.getExamDurationsJava().forEach(durations::add);
return ok(Json.toJson(node));
}

Expand Down Expand Up @@ -266,16 +268,16 @@ public Result getConfig() {
node.put("hasCourseSearchIntegration", configReader.isCourseSearchActive());
node.put("anonymousReviewEnabled", configReader.isAnonymousReviewEnabled());
ObjectNode courseIntegrationUrls = Json.newObject();
configReader.getCourseIntegrationUrls().forEach(courseIntegrationUrls::put);
CollectionConverters.asJava(configReader.getCourseIntegrationUrls()).forEach(courseIntegrationUrls::put);
node.set("courseSearchIntegrationUrls", courseIntegrationUrls);

ArrayNode durations = Json.newArray();
configReader.getExamDurations().forEach(durations::add);
configReader.getExamDurationsJava().forEach(durations::add);
node.set("examDurations", durations);

ObjectNode roles = Json.newObject();
configReader
.getRoleMapping()
.getRoleMappingJava()
.forEach((k, v) -> {
ArrayNode role = Json.newArray();
v.forEach(role::add);
Expand Down Expand Up @@ -321,8 +323,8 @@ public Result getByodMaxParticipants() {
}

private URL parseExternalUrl(String reservationRef) throws MalformedURLException {
return URI.create(
configReader.getIopHost() + String.format("/api/enrolments/%s/instructions", reservationRef)
).toURL();
return URI
.create(configReader.getIopHost() + String.format("/api/enrolments/%s/instructions", reservationRef))
.toURL();
}
}
27 changes: 16 additions & 11 deletions app/controllers/exam/ExamController.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ public ExamController(
}

private static ExpressionList<Exam> createPrototypeQuery() {
return DB.find(Exam.class)
return DB
.find(Exam.class)
.fetch("course")
.fetch("creator")
.fetch("examOwners")
Expand Down Expand Up @@ -124,7 +125,8 @@ public Result getExams(Optional<String> filter, Http.Request request) {

@Restrict({ @Group("ADMIN") })
public Result listPrintouts() {
List<Exam> printouts = DB.find(Exam.class)
List<Exam> printouts = DB
.find(Exam.class)
.where()
.eq("executionType.type", ExamExecutionType.Type.PRINTOUT.toString())
.eq("state", Exam.State.PUBLISHED)
Expand Down Expand Up @@ -291,7 +293,8 @@ public Result getExamExecutionTypes() {
@Restrict({ @Group("TEACHER"), @Group("ADMIN") })
public Result getExamPreview(Long id, Http.Request request) {
User user = request.attrs().get(Attrs.AUTHENTICATED_USER);
Exam exam = DB.find(Exam.class)
Exam exam = DB
.find(Exam.class)
.fetch("course")
.fetch("executionType")
.fetch("examinationDates")
Expand Down Expand Up @@ -355,11 +358,10 @@ public Result updateExam(Long id, Http.Request request) {
if (exam.isOwnedOrCreatedBy(user) || user.hasRole(Role.Name.ADMIN)) {
return examUpdater
.updateTemporalFieldsAndValidate(exam, user, request)
.orElseGet(
() ->
examUpdater
.updateStateAndValidate(exam, user, request)
.orElseGet(() -> handleExamUpdate(exam, user, request))
.orElseGet(() ->
examUpdater
.updateStateAndValidate(exam, user, request)
.orElseGet(() -> handleExamUpdate(exam, user, request))
);
} else {
return forbidden("i18n_error_access_forbidden");
Expand Down Expand Up @@ -428,6 +430,7 @@ public Result updateExamLanguage(Long eid, String code, Http.Request request) {
@Restrict({ @Group("TEACHER"), @Group("ADMIN") })
public Result copyExam(Long id, Http.Request request) {
User user = request.attrs().get(Attrs.AUTHENTICATED_USER);

String examinationType = formFactory.form().bindFromRequest(request).get("examinationType");
if (
Exam.Implementation.valueOf(examinationType) != Exam.Implementation.AQUARIUM &&
Expand Down Expand Up @@ -493,7 +496,8 @@ public Result copyExam(Long id, Http.Request request) {
public Result createExamDraft(Http.Request request) {
String executionType = request.body().asJson().get("executionType").asText();
String implementation = request.body().asJson().get("implementation").asText();
ExamExecutionType examExecutionType = DB.find(ExamExecutionType.class)
ExamExecutionType examExecutionType = DB
.find(ExamExecutionType.class)
.where()
.eq("type", executionType)
.findOne();
Expand Down Expand Up @@ -535,7 +539,7 @@ public Result createExamDraft(Http.Request request) {
exam.setPeriodStart(start);
exam.setPeriodEnd(start.plusDays(1));
}
exam.setDuration(configReader.getExamDurations().getFirst());
exam.setDuration(configReader.getExamDurationsJava().getFirst());
if (configReader.isCourseGradeScaleOverridable()) {
exam.setGradeScale(DB.find(GradeScale.class).findList().getFirst());
}
Expand Down Expand Up @@ -586,7 +590,8 @@ public Result updateCourse(Long eid, Long cid, Http.Request request) {
}

private static Query<Exam> prototypeQuery() {
return DB.find(Exam.class)
return DB
.find(Exam.class)
.fetch("course")
.fetch("course.organisation")
.fetch("course.gradeScale")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// SPDX-FileCopyrightText: 2024 The members of the EXAM Consortium
// SPDX-FileCopyrightText: 2024. The members of the EXAM Consortium
//
// SPDX-License-Identifier: EUPL-1.2

Expand Down Expand Up @@ -53,7 +52,8 @@ public class CollaborativeExamController extends CollaborationController {
private EmailComposer composer;

private Exam prepareDraft(User user) {
ExamExecutionType examExecutionType = DB.find(ExamExecutionType.class)
ExamExecutionType examExecutionType = DB
.find(ExamExecutionType.class)
.where()
.eq("type", ExamExecutionType.Type.PUBLIC.toString())
.findOne();
Expand All @@ -79,7 +79,7 @@ private Exam prepareDraft(User user) {
DateTime start = DateTime.now().withTimeAtStartOfDay();
exam.setPeriodStart(start);
exam.setPeriodEnd(start.plusDays(1));
exam.setDuration(configReader.getExamDurations().getFirst()); // check
exam.setDuration(configReader.getExamDurationsJava().getFirst()); // check
exam.setGradeScale(DB.find(GradeScale.class).findList().getFirst()); // check

exam.setTrialCount(1);
Expand Down Expand Up @@ -117,17 +117,19 @@ private CompletionStage<Result> getExam(Long id, Consumer<Exam> postProcessor, U
String homeOrg = configReader.getHomeOrganisationRef();
return findCollaborativeExam(id)
.map(ce ->
downloadExam(ce).thenApplyAsync(result -> {
if (result.isEmpty()) {
return notFound("i18n_error_exam_not_found");
}
Exam exam = result.get();
if (!isAuthorizedToView(exam, user, homeOrg)) {
return notFound("i18n_error_exam_not_found");
}
postProcessor.accept(exam);
return ok(serialize(exam));
}))
downloadExam(ce)
.thenApplyAsync(result -> {
if (result.isEmpty()) {
return notFound("i18n_error_exam_not_found");
}
Exam exam = result.get();
if (!isAuthorizedToView(exam, user, homeOrg)) {
return notFound("i18n_error_exam_not_found");
}
postProcessor.accept(exam);
return ok(serialize(exam));
})
)
.getOrElseGet(Function.identity());
}

Expand Down Expand Up @@ -203,52 +205,55 @@ public CompletionStage<Result> updateExam(Long id, Http.Request request) {
return findCollaborativeExam(id)
.map(ce -> {
User user = request.attrs().get(Attrs.AUTHENTICATED_USER);
return downloadExam(ce).thenComposeAsync(result -> {
if (result.isPresent()) {
Exam exam = result.get();
if (isAuthorizedToView(exam, user, homeOrg)) {
Exam.State previousState = exam.getState();
Optional<Result> error = Stream.of(
examUpdater.updateTemporalFieldsAndValidate(exam, user, request),
examUpdater.updateStateAndValidate(exam, user, request)
)
.filter(Optional::isPresent)
.map(Optional::get)
.findFirst();
if (error.isPresent()) {
return wrapAsPromise(error.get());
}
Exam.State nextState = exam.getState();
boolean isPrePublication =
previousState != Exam.State.PRE_PUBLISHED && nextState == Exam.State.PRE_PUBLISHED;
examUpdater.update(exam, request, user.getLoginRole());
return uploadExam(ce, exam, user).thenApplyAsync(result2 -> {
if (result2.status() == OK && isPrePublication) {
Set<String> receivers = exam
.getExamOwners()
.stream()
.map(User::getEmail)
.collect(Collectors.toSet());
as
.scheduler()
.scheduleOnce(
Duration.create(1, TimeUnit.SECONDS),
() ->
composer.composeCollaborativeExamAnnouncement(
CollectionConverters.asScala(receivers).toSet(),
user,
exam
),
as.dispatcher()
);
return downloadExam(ce)
.thenComposeAsync(result -> {
if (result.isPresent()) {
Exam exam = result.get();
if (isAuthorizedToView(exam, user, homeOrg)) {
Exam.State previousState = exam.getState();
Optional<Result> error = Stream
.of(
examUpdater.updateTemporalFieldsAndValidate(exam, user, request),
examUpdater.updateStateAndValidate(exam, user, request)
)
.filter(Optional::isPresent)
.map(Optional::get)
.findFirst();
if (error.isPresent()) {
return wrapAsPromise(error.get());
}
return result2;
});
Exam.State nextState = exam.getState();
boolean isPrePublication =
previousState != Exam.State.PRE_PUBLISHED && nextState == Exam.State.PRE_PUBLISHED;
examUpdater.update(exam, request, user.getLoginRole());
return uploadExam(ce, exam, user)
.thenApplyAsync(result2 -> {
if (result2.status() == OK && isPrePublication) {
Set<String> receivers = exam
.getExamOwners()
.stream()
.map(User::getEmail)
.collect(Collectors.toSet());
as
.scheduler()
.scheduleOnce(
Duration.create(1, TimeUnit.SECONDS),
() ->
composer.composeCollaborativeExamAnnouncement(
CollectionConverters.asScala(receivers).toSet(),
user,
exam
),
as.dispatcher()
);
}
return result2;
});
}
return wrapAsPromise(forbidden("i18n_error_access_forbidden"));
}
return wrapAsPromise(forbidden("i18n_error_access_forbidden"));
}
return wrapAsPromise(notFound());
});
return wrapAsPromise(notFound());
});
})
.getOrElseGet(Function.identity());
}
Expand All @@ -259,14 +264,15 @@ public CompletionStage<Result> updateLanguage(Long id, String code, Http.Request
return findCollaborativeExam(id)
.map(ce -> {
User user = request.attrs().get(Attrs.AUTHENTICATED_USER);
return downloadExam(ce).thenComposeAsync(result -> {
if (result.isPresent()) {
Exam exam = result.get();
Optional<Result> error = examUpdater.updateLanguage(exam, code, user);
return error.isPresent() ? wrapAsPromise(error.get()) : uploadExam(ce, exam, user);
}
return wrapAsPromise(notFound());
});
return downloadExam(ce)
.thenComposeAsync(result -> {
if (result.isPresent()) {
Exam exam = result.get();
Optional<Result> error = examUpdater.updateLanguage(exam, code, user);
return error.isPresent() ? wrapAsPromise(error.get()) : uploadExam(ce, exam, user);
}
return wrapAsPromise(notFound());
});
})
.getOrElseGet(Function.identity());
}
Expand All @@ -278,15 +284,16 @@ public CompletionStage<Result> addOwner(Long id, Http.Request request) {
return findCollaborativeExam(id)
.map(ce -> {
User user = request.attrs().get(Attrs.AUTHENTICATED_USER);
return downloadExam(ce).thenComposeAsync(result -> {
if (result.isPresent()) {
Exam exam = result.get();
User owner = createOwner(request.attrs().get(Attrs.EMAIL));
exam.getExamOwners().add(owner);
return uploadExam(ce, exam, user, owner, null);
}
return wrapAsPromise(notFound());
});
return downloadExam(ce)
.thenComposeAsync(result -> {
if (result.isPresent()) {
Exam exam = result.get();
User owner = createOwner(request.attrs().get(Attrs.EMAIL));
exam.getExamOwners().add(owner);
return uploadExam(ce, exam, user, owner, null);
}
return wrapAsPromise(notFound());
});
})
.getOrElseGet(Function.identity());
}
Expand All @@ -297,16 +304,17 @@ public CompletionStage<Result> removeOwner(Long id, Long oid, Http.Request reque
return findCollaborativeExam(id)
.map(ce -> {
User user = request.attrs().get(Attrs.AUTHENTICATED_USER);
return downloadExam(ce).thenComposeAsync(result -> {
if (result.isPresent()) {
Exam exam = result.get();
User owner = new User();
owner.setId(oid);
exam.getExamOwners().remove(owner);
return uploadExam(ce, exam, user);
}
return wrapAsPromise(notFound());
});
return downloadExam(ce)
.thenComposeAsync(result -> {
if (result.isPresent()) {
Exam exam = result.get();
User owner = new User();
owner.setId(oid);
exam.getExamOwners().remove(owner);
return uploadExam(ce, exam, user);
}
return wrapAsPromise(notFound());
});
})
.getOrElseGet(Function.identity());
}
Expand Down
Loading

0 comments on commit 2c297f9

Please sign in to comment.