From b99f287f8cb624cd4f559c506581a6fee080e280 Mon Sep 17 00:00:00 2001 From: Alex Prudencio Date: Mon, 5 Aug 2024 19:42:27 -0400 Subject: [PATCH] fixes --- .../model/Assessment.java | 7 ++ .../service/AssessmentService.java | 71 ++++++++++++------- .../views/EvaluationView.java | 50 ++++++++----- src/main/resources/application.properties | 7 +- 4 files changed, 90 insertions(+), 45 deletions(-) diff --git a/src/main/java/com/primefactorsolutions/model/Assessment.java b/src/main/java/com/primefactorsolutions/model/Assessment.java index c682bbf..5d665fd 100644 --- a/src/main/java/com/primefactorsolutions/model/Assessment.java +++ b/src/main/java/com/primefactorsolutions/model/Assessment.java @@ -54,6 +54,13 @@ public class Assessment extends BaseEntity { .isPresent(); } + public boolean isStarted() { + return assessmentEvents.stream().filter(e -> e.getStatus() == AssessmentStatus.STARTED) + .map(AssessmentEvent::getTimestamp) + .findFirst() + .isPresent(); + } + public boolean isFirst(Submission currSubmission) { return getQuestions().indexOf(currSubmission.getQuestion()) == 0; } diff --git a/src/main/java/com/primefactorsolutions/service/AssessmentService.java b/src/main/java/com/primefactorsolutions/service/AssessmentService.java index f92bd9c..2752327 100644 --- a/src/main/java/com/primefactorsolutions/service/AssessmentService.java +++ b/src/main/java/com/primefactorsolutions/service/AssessmentService.java @@ -35,34 +35,45 @@ public class AssessmentService { public Assessment startAssessment(final UUID id) { final Assessment assessment = assessmentRepository.findById(id).get(); - final Optional started = assessment.getAssessmentEvents().stream() - .filter(e -> e.getStatus() == AssessmentStatus.STARTED) - .findFirst(); - - if (started.isPresent()) { + if (assessment.isStarted()) { return assessment; } assessment.getAssessmentEvents().add(new AssessmentEvent(Instant.now(), AssessmentStatus.STARTED, assessment)); - assessmentRepository.save(assessment); - return assessment; + return assessmentRepository.save(assessment); } - public Submission getNextSubmission(final Assessment assessment, final Submission currSubmission) { - if (currSubmission == null) { + public Submission getNextSubmission(final UUID assessmentId, final UUID currSubmissionId) { + final Assessment assessment = assessmentRepository.findById(assessmentId).get(); + + Assessment saved; + if (currSubmissionId == null) { if (assessment.isCompleted()) { return null; } final Question firstQuestion = assessment.getQuestions().stream().findFirst().get(); - final Submission submissionToReturn = new Submission(firstQuestion, firstQuestion.getContent(), Map.of(), SubmissionStatus.FAIL, assessment); - assessment.getSubmissions().add(submissionToReturn); - assessmentRepository.save(assessment); + final Optional submissionToReturn = assessment.getSubmissions().stream() + .filter(s -> s.getQuestion().equals(firstQuestion)) + .findFirst(); - return submissionToReturn; + if (submissionToReturn.isEmpty()) { + final Submission result = new Submission(firstQuestion, firstQuestion.getContent(), Map.of(), SubmissionStatus.FAIL, assessment); + assessment.getSubmissions().add(result); + } + + saved = assessmentRepository.save(assessment); + final Optional submissionToReturn2 = saved.getSubmissions().stream() + .filter(s -> s.getQuestion().equals(firstQuestion)) + .findFirst(); + + return submissionToReturn2.get(); } + final Submission currSubmission = assessment.getSubmissions().stream() + .filter(s -> s.getId().equals(currSubmissionId)) + .findFirst().get(); final Question currQuestion = currSubmission.getQuestion(); int idx = assessment.getQuestions().indexOf(currQuestion); @@ -76,25 +87,26 @@ public class AssessmentService { .filter(s -> s.getQuestion().equals(nextQuestion)) .findFirst(); - final Submission result; if (submissionToReturn.isEmpty()) { - result = new Submission(nextQuestion, nextQuestion.getContent(), Map.of(), SubmissionStatus.FAIL, assessment); + final Submission result = new Submission(nextQuestion, nextQuestion.getContent(), Map.of(), SubmissionStatus.FAIL, assessment); assessment.getSubmissions().add(result); - } else { - result = submissionToReturn.get(); } - assessmentRepository.save(assessment); + saved = assessmentRepository.save(assessment); + final Optional submissionToReturn2 = saved.getSubmissions().stream() + .filter(s -> s.getQuestion().equals(nextQuestion)) + .findFirst(); - return result; + return submissionToReturn2.get(); } - public Submission getPrevSubmission(final Assessment assessment, final Submission currSubmission) { + public Submission getPrevSubmission(final UUID assessmentId, final Submission currSubmission) { if (currSubmission == null) { return null; } final Question currQuestion = currSubmission.getQuestion(); + Assessment assessment = assessmentRepository.findById(assessmentId).get(); int idx = assessment.getQuestions().indexOf(currQuestion); if (idx == 0) { @@ -103,15 +115,12 @@ public class AssessmentService { final Question prevQuestion = assessment.getQuestions().get(idx - 1); - assessmentRepository.save(assessment); - - return assessment.getSubmissions().stream() .filter(s -> s.getQuestion().equals(prevQuestion)) .findFirst().orElseThrow(() -> new IllegalStateException("submission invalid")); } - public Assessment completeAssessment(final UUID id, final Submission submission) { + public Assessment completeAssessment(final UUID id) { Assessment assessment = assessmentRepository.findById(id).get(); Optional completed = assessment.getAssessmentEvents().stream() .filter(e -> e.getStatus() == AssessmentStatus.COMPLETED) @@ -122,8 +131,18 @@ public class AssessmentService { } assessment.getAssessmentEvents().add(new AssessmentEvent(Instant.now(), AssessmentStatus.COMPLETED, assessment)); - assessmentRepository.save(assessment); + Assessment saved = assessmentRepository.save(assessment); - return assessment; + return saved; + } + + public void saveSubmission(UUID id, Submission currSubmission) { + Assessment assessment = assessmentRepository.findById(id).get(); + final Submission submission = assessment.getSubmissions().stream() + .filter(s -> s.getId().equals(currSubmission.getId())) + .findFirst().get(); + + submission.setResponse(currSubmission.getResponse()); + Assessment saved = assessmentRepository.save(assessment); } } diff --git a/src/main/java/com/primefactorsolutions/views/EvaluationView.java b/src/main/java/com/primefactorsolutions/views/EvaluationView.java index b66ba4f..afbe08c 100644 --- a/src/main/java/com/primefactorsolutions/views/EvaluationView.java +++ b/src/main/java/com/primefactorsolutions/views/EvaluationView.java @@ -70,6 +70,7 @@ public class EvaluationView extends Main implements HasUrlParameter { Button start = null; MenuItem prev = null; MenuItem next = null; + MenuItem reset = null; Section sidebar = null; SimpleTimer timer = null; @@ -126,8 +127,9 @@ public class EvaluationView extends Main implements HasUrlParameter { final Button saveButton = new Button("Guardar y Siguiente", e -> { this.currSubmission.setResponse(this.questionEditor.getValue()); - goToNext(); + this.assessmentService.saveSubmission(assessment.getId(), this.currSubmission); dialog.close(); + goToNext(); }); saveButton.addThemeVariants(ButtonVariant.LUMO_PRIMARY); final Button closeButton = new Button("Cerrar", e -> dialog.close()); @@ -151,7 +153,9 @@ public class EvaluationView extends Main implements HasUrlParameter { final Button completeButton = new Button("Terminar", e -> { completeDialog.close(); - this.assessment = assessmentService.completeAssessment(assessment.getId(), currSubmission); + this.currSubmission.setResponse(this.questionEditor.getValue()); + this.assessmentService.saveSubmission(assessment.getId(), this.currSubmission); + this.assessment = assessmentService.completeAssessment(assessment.getId()); goToCompleted(); updateUI(); }); @@ -182,13 +186,19 @@ public class EvaluationView extends Main implements HasUrlParameter { prev = navMenuBar.addItem("Anterior pregunta", (ComponentEventListener>) menuItemClickEvent -> { System.out.println(">>> prev"); this.currSubmission.setResponse(this.questionEditor.getValue()); - this.currSubmission = this.assessmentService.getPrevSubmission(assessment, this.currSubmission); + this.assessmentService.saveSubmission(assessment.getId(), this.currSubmission); + this.currSubmission = this.assessmentService.getPrevSubmission(assessment.getId(), this.currSubmission); updateUI(); }); next = navMenuBar.addItem("Siguiente pregunta", (ComponentEventListener>) menuItemClickEvent -> { this.currSubmission.setResponse(this.questionEditor.getValue()); + this.assessmentService.saveSubmission(assessment.getId(), this.currSubmission); goToNext(); }); + reset = navMenuBar.addItem("Reiniciar pregunta (deshacer cambios)", (ComponentEventListener>) menuItemClickEvent -> { + this.currSubmission.setResponse(this.currSubmission.getQuestion().getContent()); + this.questionEditor.setValue(this.currSubmission.getQuestion().getContent()); + }); final Div menuBar = new Div(); menuBar.add(runMenuBar, navMenuBar); @@ -228,8 +238,7 @@ public class EvaluationView extends Main implements HasUrlParameter { this.assessment = this.assessmentService.startAssessment(this.assessment.getId()); if (tf.getValue().trim().equalsIgnoreCase(this.assessment.getAppUser().getEmail())) { - this.currSubmission = this.assessmentService.getNextSubmission(assessment, this.currSubmission); - updateUI(); + this.getUI().get().getPage().reload(); } else { Notification notification = new Notification(); notification.addThemeVariants(NotificationVariant.LUMO_ERROR); @@ -291,8 +300,14 @@ public class EvaluationView extends Main implements HasUrlParameter { private void goToNext() { System.out.println(">>> next"); - this.currSubmission = this.assessmentService.getNextSubmission(assessment, this.currSubmission); - updateUI(); + Submission found = this.assessmentService.getNextSubmission(assessment.getId(), this.currSubmission.getId()); + + if (found == null) { + this.completeDialog.open(); + } else { + this.currSubmission = found; + updateUI(); + } } private void initSidebar() { @@ -311,7 +326,9 @@ public class EvaluationView extends Main implements HasUrlParameter { timer.setMinutes(true); timer.addTimerEndEvent((ComponentEventListener) timerEndedEvent -> { Notification.show("Tiempo completado.", 5, Notification.Position.TOP_CENTER); - this.assessment = assessmentService.completeAssessment(assessment.getId(), currSubmission); + this.currSubmission.setResponse(this.questionEditor.getValue()); + this.assessmentService.saveSubmission(assessment.getId(), this.currSubmission); + this.assessment = assessmentService.completeAssessment(assessment.getId()); goToCompleted(); updateUI(); }); @@ -328,18 +345,19 @@ public class EvaluationView extends Main implements HasUrlParameter { } private void updateUI() { - if (currSubmission == null) { + if (assessment == null || !assessment.isStarted()) { editorSection.setVisible(false); startSection.setVisible(true); sidebar.setVisible(false); } else { - questionEditor.setValue(this.currSubmission.getResponse()); + if (currSubmission != null) { + questionEditor.setValue(this.currSubmission.getResponse()); + } + editorSection.setVisible(true); startSection.setVisible(false); sidebar.setVisible(true); - } - if (this.assessment != null) { prev.setEnabled(currSubmission != null && !assessment.isFirst(currSubmission)); next.setEnabled(currSubmission == null || !assessment.isLast(currSubmission)); @@ -354,7 +372,7 @@ public class EvaluationView extends Main implements HasUrlParameter { if (dl.getChildren().collect(Collectors.toList()).isEmpty()) { dl.add( createItem("Candidato:", assessment.getAppUser().getEmail()), - createItem("Hora comienzo:", Optional.ofNullable(assessment.getStartingTime()) + createItem("Hora de inicio:", Optional.ofNullable(assessment.getStartingTime()) .map(t -> ZonedDateTime.ofInstant(t, ZoneId.of("GMT-4")).format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)) .orElse("N/A")) ); @@ -401,9 +419,9 @@ public class EvaluationView extends Main implements HasUrlParameter { goToCompleted(); } - this.currSubmission = this.assessment.getSubmissions().isEmpty() - ? null - : this.assessmentService.getNextSubmission(assessment, null); + this.currSubmission = this.assessment.isStarted() + ? this.assessmentService.getNextSubmission(assessment.getId(), null) + : null; updateUI(); } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index f9480c4..220e85f 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -9,14 +9,15 @@ vaadin.launch-browser=true vaadin.allowed-packages = com.vaadin,org.vaadin,com.primefactorsolutions,com.hilerio.ace,com.flowingcode.vaadin -# spring.datasource.url=jdbc:h2:mem:testdb +spring.datasource.url=jdbc:h2:mem:testdb +#spring.datasource.url=jdbc:h2:file:./db +#spring.datasource.url=jdbc:h2:file:/var/opt/pfs/db spring.jpa.hibernate.ddl-auto=create-drop spring.sql.init.mode=always -#spring.datasource.url=jdbc:h2:file:./db -spring.datasource.url=jdbc:h2:file:/var/opt/pfs/db spring.datasource.driverClassName=org.h2.Driver spring.datasource.username=sa spring.datasource.password=sa +spring.h2.console.settings.web-allow-others=true spring.jpa.database-platform=org.hibernate.dialect.H2Dialect spring.jpa.defer-datasource-initialization = true spring.h2.console.enabled=true