use of org.springframework.cloud.cloudfoundry.com.fasterxml.jackson.core.JsonProcessingException in project ArTEMiS by ls1intum.
the class TutorParticipationService method validateTutorialExampleSubmission.
/**
* Validates the tutor example submission. If invalid, throw bad request exception with information which feedback are incorrect.
*/
private void validateTutorialExampleSubmission(ExampleSubmission tutorExampleSubmission) {
var latestResult = tutorExampleSubmission.getSubmission().getLatestResult();
if (latestResult == null) {
throw new BadRequestAlertException("The training does not contain an assessment", ENTITY_NAME, "invalid_assessment");
}
var tutorFeedback = latestResult.getFeedbacks();
var instructorFeedback = exampleSubmissionRepository.getFeedbackForExampleSubmission(tutorExampleSubmission.getId());
boolean equalFeedbackCount = instructorFeedback.size() == tutorFeedback.size();
var unreferencedInstructorFeedbackCount = instructorFeedback.stream().filter(feedback -> feedback.getType() == MANUAL_UNREFERENCED).toList().size();
var unreferencedTutorFeedback = tutorFeedback.stream().filter(feedback -> feedback.getType() == MANUAL_UNREFERENCED).toList();
// If invalid, get all incorrect feedback and send an array of the corresponding `FeedbackCorrectionError`s to the client.
var wrongFeedback = tutorFeedback.stream().flatMap(feedback -> {
// If current tutor feedback is unreferenced and there are already more than enough unreferenced feedback provided, mark this feedback as unnecessary.
var unreferencedTutorFeedbackCount = unreferencedTutorFeedback.indexOf(feedback) + 1;
var validationError = unreferencedTutorFeedbackCount > unreferencedInstructorFeedbackCount ? Optional.of(UNNECESSARY_FEEDBACK) : checkTutorFeedbackForErrors(feedback, instructorFeedback);
if (validationError.isEmpty()) {
return Stream.empty();
}
var objectWriter = new ObjectMapper().writer().withDefaultPrettyPrinter();
try {
// Build JSON string for the corresponding `FeedbackCorrectionError` object.
// TODO: I think we should let Spring automatically convert it to Json
var feedbackCorrectionErrorJSON = objectWriter.writeValueAsString(new FeedbackCorrectionError(feedback.getReference(), validationError.get()));
return Stream.of(feedbackCorrectionErrorJSON);
} catch (JsonProcessingException e) {
log.warn("JsonProcessingException in validateTutorialExampleSubmission: {}", e.getMessage());
return Stream.empty();
}
}).collect(Collectors.joining(","));
if (wrongFeedback.isBlank() && equalFeedbackCount) {
return;
}
// Pack this information into bad request exception.
throw new BadRequestAlertException("{\"errors\": [" + wrongFeedback + "]}", ENTITY_NAME, "invalid_assessment", true);
}
use of org.springframework.cloud.cloudfoundry.com.fasterxml.jackson.core.JsonProcessingException in project Artemis by ls1intum.
the class TutorParticipationService method validateTutorialExampleSubmission.
/**
* Validates the tutor example submission. If invalid, throw bad request exception with information which feedback are incorrect.
*/
private void validateTutorialExampleSubmission(ExampleSubmission tutorExampleSubmission) {
var latestResult = tutorExampleSubmission.getSubmission().getLatestResult();
if (latestResult == null) {
throw new BadRequestAlertException("The training does not contain an assessment", ENTITY_NAME, "invalid_assessment");
}
var tutorFeedback = latestResult.getFeedbacks();
var instructorFeedback = exampleSubmissionRepository.getFeedbackForExampleSubmission(tutorExampleSubmission.getId());
boolean equalFeedbackCount = instructorFeedback.size() == tutorFeedback.size();
var unreferencedInstructorFeedbackCount = instructorFeedback.stream().filter(feedback -> feedback.getType() == MANUAL_UNREFERENCED).toList().size();
var unreferencedTutorFeedback = tutorFeedback.stream().filter(feedback -> feedback.getType() == MANUAL_UNREFERENCED).toList();
// If invalid, get all incorrect feedback and send an array of the corresponding `FeedbackCorrectionError`s to the client.
var wrongFeedback = tutorFeedback.stream().flatMap(feedback -> {
// If current tutor feedback is unreferenced and there are already more than enough unreferenced feedback provided, mark this feedback as unnecessary.
var unreferencedTutorFeedbackCount = unreferencedTutorFeedback.indexOf(feedback) + 1;
var validationError = unreferencedTutorFeedbackCount > unreferencedInstructorFeedbackCount ? Optional.of(UNNECESSARY_FEEDBACK) : checkTutorFeedbackForErrors(feedback, instructorFeedback);
if (validationError.isEmpty()) {
return Stream.empty();
}
var objectWriter = new ObjectMapper().writer().withDefaultPrettyPrinter();
try {
// Build JSON string for the corresponding `FeedbackCorrectionError` object.
// TODO: I think we should let Spring automatically convert it to Json
var feedbackCorrectionErrorJSON = objectWriter.writeValueAsString(new FeedbackCorrectionError(feedback.getReference(), validationError.get()));
return Stream.of(feedbackCorrectionErrorJSON);
} catch (JsonProcessingException e) {
log.warn("JsonProcessingException in validateTutorialExampleSubmission: {}", e.getMessage());
return Stream.empty();
}
}).collect(Collectors.joining(","));
if (wrongFeedback.isBlank() && equalFeedbackCount) {
return;
}
// Pack this information into bad request exception.
throw new BadRequestAlertException("{\"errors\": [" + wrongFeedback + "]}", ENTITY_NAME, "invalid_assessment", true);
}
Aggregations