This commit is contained in:
alex 2024-08-05 19:42:27 -04:00
parent 24ec12553a
commit b99f287f8c
4 changed files with 90 additions and 45 deletions

View File

@ -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;
}

View File

@ -35,34 +35,45 @@ public class AssessmentService {
public Assessment startAssessment(final UUID id) {
final Assessment assessment = assessmentRepository.findById(id).get();
final Optional<AssessmentEvent> 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<Submission> 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<Submission> 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<Submission> 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<AssessmentEvent> 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);
}
}

View File

@ -70,6 +70,7 @@ public class EvaluationView extends Main implements HasUrlParameter<String> {
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<String> {
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<String> {
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<String> {
prev = navMenuBar.addItem("Anterior pregunta", (ComponentEventListener<ClickEvent<MenuItem>>) 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<ClickEvent<MenuItem>>) menuItemClickEvent -> {
this.currSubmission.setResponse(this.questionEditor.getValue());
this.assessmentService.saveSubmission(assessment.getId(), this.currSubmission);
goToNext();
});
reset = navMenuBar.addItem("Reiniciar pregunta (deshacer cambios)", (ComponentEventListener<ClickEvent<MenuItem>>) 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<String> {
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,9 +300,15 @@ public class EvaluationView extends Main implements HasUrlParameter<String> {
private void goToNext() {
System.out.println(">>> next");
this.currSubmission = this.assessmentService.getNextSubmission(assessment, this.currSubmission);
Submission found = this.assessmentService.getNextSubmission(assessment.getId(), this.currSubmission.getId());
if (found == null) {
this.completeDialog.open();
} else {
this.currSubmission = found;
updateUI();
}
}
private void initSidebar() {
sidebar = new Section();
@ -311,7 +326,9 @@ public class EvaluationView extends Main implements HasUrlParameter<String> {
timer.setMinutes(true);
timer.addTimerEndEvent((ComponentEventListener<SimpleTimer.TimerEndedEvent>) 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<String> {
}
private void updateUI() {
if (currSubmission == null) {
if (assessment == null || !assessment.isStarted()) {
editorSection.setVisible(false);
startSection.setVisible(true);
sidebar.setVisible(false);
} else {
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<String> {
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<String> {
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();
}

View File

@ -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