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 b0b37f3
Show file tree
Hide file tree
Showing 39 changed files with 404 additions and 578 deletions.
9 changes: 5 additions & 4 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 @@ -176,7 +177,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 +267,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
4 changes: 2 additions & 2 deletions app/controllers/admin/StatisticsController.java
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,8 @@ public Result getReviewsByDate(String from, String to) throws IOException {
data[2] = e.getCourse().getCode();
data[3] = ISODateTimeFormat.dateTimeNoMillis().print(new DateTime(e.getCreated()));
data[4] = ISODateTimeFormat.dateTimeNoMillis().print(new DateTime(e.getGradedTime()));
data[5] = parse(
() -> String.format("%s %s", e.getGradedByUser().getFirstName(), e.getGradedByUser().getLastName())
data[5] = parse(() ->
String.format("%s %s", e.getGradedByUser().getFirstName(), e.getGradedByUser().getLastName())
);

data[6] = e.getCourse().getCredits() == null ? "" : Double.toString(e.getCourse().getCredits()); // custom credits?
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/assessment/ExamInspectionController.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ public Result deleteInspection(Long id) {
.getChildren()
.stream()
.filter(c -> c.hasState(Exam.State.REVIEW, Exam.State.STUDENT_STARTED, Exam.State.REVIEW_STARTED))
.forEach(
c -> c.getExamInspections().stream().filter(ei -> ei.getUser().equals(inspector)).forEach(Model::delete)
.forEach(c ->
c.getExamInspections().stream().filter(ei -> ei.getUser().equals(inspector)).forEach(Model::delete)
);
inspection.delete();
return ok();
Expand Down
15 changes: 7 additions & 8 deletions app/controllers/exam/ExamController.java
Original file line number Diff line number Diff line change
Expand Up @@ -355,11 +355,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,15 +427,15 @@ 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 &&
!user.hasPermission(Permission.Type.CAN_CREATE_BYOD_EXAM)
) {
return forbidden("i18n_access_forbidden");
}
Exam prototype = DB
.find(Exam.class) // TODO: check if all this fetching is necessary
Exam prototype = DB.find(Exam.class) // TODO: check if all this fetching is necessary
.fetch("creator", "id")
.fetch("examType", "id, type")
.fetch("examSections", "id, name, sequenceNumber")
Expand Down Expand Up @@ -535,7 +534,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
21 changes: 14 additions & 7 deletions app/controllers/examination/ExaminationController.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ private CompletionStage<Result> postProcessExisting(
return wrapAsPromise(forbidden());
}
ExamEnrolment enrolment = optionalEnrolment.get();
return getEnrolmentError( // allow state = initialized
return getEnrolmentError(
// allow state = initialized
enrolment,
request
).thenComposeAsync(
Expand Down Expand Up @@ -170,7 +171,8 @@ private CompletionStage<Result> createClone(
return wrapAsPromise(forbidden());
}
ExamEnrolment enrolment = optionalEnrolment.get();
return getEnrolmentError( // allow state = initialized
return getEnrolmentError(
// allow state = initialized
enrolment,
request
).thenComposeAsync(
Expand Down Expand Up @@ -302,7 +304,8 @@ public CompletionStage<Result> turnExam(String hash, Http.Request request) {
autoEvaluationHandler.autoEvaluate(exam);
}
return ok().withSession(session);
}));
})
);
}

@Authenticated
Expand Down Expand Up @@ -330,7 +333,8 @@ public CompletionStage<Result> abortExam(String hash, Http.Request request) {
} else {
return forbidden().withSession(session);
}
}));
})
);
}

@Authenticated
Expand All @@ -356,7 +360,8 @@ public CompletionStage<Result> answerEssay(String hash, Long questionId, Http.Re
question.setEssayAnswer(answer);
question.save();
return ok(answer);
}));
})
);
}

@Authenticated
Expand All @@ -377,7 +382,8 @@ public CompletionStage<Result> answerMultiChoice(String hash, Long qid, Http.Req
o.update();
});
return ok();
}));
})
);
}

@Authenticated
Expand All @@ -400,7 +406,8 @@ public CompletionStage<Result> answerClozeTest(String hash, Long questionId, Htt
answer.setAnswer(request.attrs().getOptional(Attrs.ESSAY_ANSWER).orElse(null));
answer.save();
return ok(answer, PathProperties.parse("(id, objectVersion, answer)"));
}));
})
);
}

private Optional<ExamParticipation> findParticipation(Exam exam, User user) {
Expand Down
11 changes: 5 additions & 6 deletions app/controllers/facility/AvailabilityController.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,11 @@ public Result getAvailability(Long roomId, String day) {
List<Interval> slotsForDate = dateTimeHandler
.getWorkingHoursForDate(window, room)
.stream()
.map(
oh ->
new Interval(
oh.getHours().getStart().minusMillis(oh.getTimezoneOffset()),
oh.getHours().getEnd().minusMillis(oh.getTimezoneOffset())
)
.map(oh ->
new Interval(
oh.getHours().getStart().minusMillis(oh.getTimezoneOffset()),
oh.getHours().getEnd().minusMillis(oh.getTimezoneOffset())
)
)
.map(this::round)
.flatMap(i -> toOneHourChunks(i).stream())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,10 @@ default Either<CompletionStage<Result>, LanguageInspection> findLanguageInspecti
@Override
default CompletionStage<Result> deleteExamAttachment(T id, Http.Request request) {
return findExternalExam(id, request)
.map(
ee ->
findExam(ee)
.map(e -> deleteExternalAttachment(e, ee, e, getUser(request)))
.getOrElseGet(Function.identity())
.map(ee ->
findExam(ee)
.map(e -> deleteExternalAttachment(e, ee, e, getUser(request)))
.getOrElseGet(Function.identity())
)
.getOrElseGet(Function.identity());
}
Expand All @@ -118,11 +117,10 @@ default CompletionStage<Result> addAttachmentToExam(Http.Request request) {
Http.MultipartFormData.FilePart<Files.TemporaryFile> filePart = mf.getFilePart();
final String id = mf.getForm().get("examId")[0];
return findExternalExam(parseId(id), request)
.map(
ee ->
findExam(ee)
.map(e -> uploadAttachment(filePart, ee, e, e, getUser(request)))
.getOrElseGet(Function.identity())
.map(ee ->
findExam(ee)
.map(e -> uploadAttachment(filePart, ee, e, e, getUser(request)))
.getOrElseGet(Function.identity())
)
.getOrElseGet(Function.identity());
}
Expand All @@ -146,16 +144,14 @@ default CompletionStage<Result> addAttachmentToQuestion(Http.Request request) {
final String id = mf.getForm().get("examId")[0];
final Long qid = Long.parseLong(mf.getForm().get("questionId")[0]);
return findExternalExam(parseId(id), request)
.map(
ee ->
findExam(ee)
.map(
e ->
findSectionQuestion(qid, e)
.map(sq -> uploadAttachment(filePart, ee, e, sq.getQuestion(), getUser(request)))
.getOrElseGet(Function.identity())
)
.getOrElseGet(Function.identity())
.map(ee ->
findExam(ee)
.map(e ->
findSectionQuestion(qid, e)
.map(sq -> uploadAttachment(filePart, ee, e, sq.getQuestion(), getUser(request)))
.getOrElseGet(Function.identity())
)
.getOrElseGet(Function.identity())
)
.getOrElseGet(Function.identity());
}
Expand All @@ -174,16 +170,14 @@ default CompletionStage<Result> downloadQuestionAttachment(T eid, Long qid, Http
@Restrict({ @Group("TEACHER"), @Group("ADMIN") })
default CompletionStage<Result> deleteQuestionAttachment(T eid, Long qid, Http.Request request) {
return findExternalExam(eid, request)
.map(
ee ->
findExam(ee)
.map(
e ->
findSectionQuestion(qid, e)
.map(sq -> deleteExternalAttachment(sq.getQuestion(), ee, e, getUser(request)))
.getOrElseGet(Function.identity())
)
.getOrElseGet(Function.identity())
.map(ee ->
findExam(ee)
.map(e ->
findSectionQuestion(qid, e)
.map(sq -> deleteExternalAttachment(sq.getQuestion(), ee, e, getUser(request)))
.getOrElseGet(Function.identity())
)
.getOrElseGet(Function.identity())
)
.getOrElseGet(Function.identity());
}
Expand All @@ -207,30 +201,29 @@ default CompletionStage<Result> addAttachmentToQuestionAnswer(Http.Request reque
}
return uploadAttachment(filePart, ee, e, sq.getEssayAnswer(), getUser(request));
})
.getOrElseGet(Function.identity()))
.getOrElseGet(Function.identity()))
.getOrElseGet(Function.identity())
)
.getOrElseGet(Function.identity())
)
.getOrElseGet(Function.identity());
}

@Authenticated
@Restrict({ @Group("ADMIN"), @Group("STUDENT") })
default CompletionStage<Result> deleteQuestionAnswerAttachment(Long qid, T eid, Http.Request request) {
return findExternalExam(eid, request)
.map(
ee ->
findExam(ee)
.map(
e ->
findSectionQuestion(qid, e)
.map(
sq ->
findEssayAnswerWithAttachment(sq)
.map(ea -> deleteExternalAttachment(ea, ee, e, getUser(request)))
.getOrElseGet(Function.identity())
)
.map(ee ->
findExam(ee)
.map(e ->
findSectionQuestion(qid, e)
.map(sq ->
findEssayAnswerWithAttachment(sq)
.map(ea -> deleteExternalAttachment(ea, ee, e, getUser(request)))
.getOrElseGet(Function.identity())
)
.getOrElseGet(Function.identity())
)
.getOrElseGet(Function.identity())
)
.getOrElseGet(Function.identity())
)
.getOrElseGet(Function.identity());
}
Expand Down Expand Up @@ -282,8 +275,10 @@ default CompletionStage<Result> addStatementAttachment(T id, Http.Request reques
}
return uploadAttachment(filePart, ee, e, li.getStatement(), user);
})
.getOrElseGet(Function.identity()))
.getOrElseGet(Function.identity()))
.getOrElseGet(Function.identity())
)
.getOrElseGet(Function.identity())
)
.getOrElseGet(Function.identity());
}

Expand All @@ -292,16 +287,14 @@ default CompletionStage<Result> addStatementAttachment(T id, Http.Request reques
@Override
default CompletionStage<Result> downloadStatementAttachment(T id, Http.Request request) {
return findExternalExam(id, request)
.map(
ee ->
findExam(ee)
.map(
e ->
findLanguageInspectionWithAttachment(e)
.map(li -> downloadExternalAttachment(li.getStatement().getAttachment()))
.getOrElseGet(Function.identity())
)
.getOrElseGet(Function.identity())
.map(ee ->
findExam(ee)
.map(e ->
findLanguageInspectionWithAttachment(e)
.map(li -> downloadExternalAttachment(li.getStatement().getAttachment()))
.getOrElseGet(Function.identity())
)
.getOrElseGet(Function.identity())
)
.getOrElseGet(Function.identity());
}
Expand All @@ -311,16 +304,14 @@ default CompletionStage<Result> downloadStatementAttachment(T id, Http.Request r
@Override
default CompletionStage<Result> deleteStatementAttachment(T id, Http.Request request) {
return findExternalExam(id, request)
.map(
ee ->
findExam(ee)
.map(
e ->
findLanguageInspectionWithAttachment(e)
.map(li -> deleteExternalAttachment(li.getStatement(), ee, e, getUser(request)))
.getOrElseGet(Function.identity())
)
.getOrElseGet(Function.identity())
.map(ee ->
findExam(ee)
.map(e ->
findLanguageInspectionWithAttachment(e)
.map(li -> deleteExternalAttachment(li.getStatement(), ee, e, getUser(request)))
.getOrElseGet(Function.identity())
)
.getOrElseGet(Function.identity())
)
.getOrElseGet(Function.identity());
}
Expand Down Expand Up @@ -445,8 +436,8 @@ default CompletionStage<Result> uploadAttachment(
if (StringUtils.isEmpty(externalId)) {
return request
.post(source)
.thenComposeAsync(
wsResponse -> createExternalAttachment(externalExam, exam, container, wsResponse, user)
.thenComposeAsync(wsResponse ->
createExternalAttachment(externalExam, exam, container, wsResponse, user)
);
}
return request
Expand Down
Loading

0 comments on commit b0b37f3

Please sign in to comment.