diff --git a/view/src/main/java/se/su/dsv/scipro/grading/SendToExaminer.java b/view/src/main/java/se/su/dsv/scipro/grading/SendToExaminer.java index 020b225639..1b3a523f28 100644 --- a/view/src/main/java/se/su/dsv/scipro/grading/SendToExaminer.java +++ b/view/src/main/java/se/su/dsv/scipro/grading/SendToExaminer.java @@ -100,9 +100,7 @@ public class SendToExaminer extends GenericPanel<Project> { add(form); WebMarkupContainer sendButton = new WebMarkupContainer("send"); - if (confirmationMessage.getObject() != null) { - sendButton.add(new JavascriptEventConfirmation("click", confirmationMessage)); - } + sendButton.add(new JavascriptEventConfirmation("click", confirmationMessage)); form.add(sendButton); TextField<LocalDate> examinationDateField = new TextField<>("examinationDate", examinationDate, LocalDate.class); diff --git a/view/src/main/java/se/su/dsv/scipro/util/JavascriptEventConfirmation.java b/view/src/main/java/se/su/dsv/scipro/util/JavascriptEventConfirmation.java index 5d272b363a..86be385fa9 100755 --- a/view/src/main/java/se/su/dsv/scipro/util/JavascriptEventConfirmation.java +++ b/view/src/main/java/se/su/dsv/scipro/util/JavascriptEventConfirmation.java @@ -37,18 +37,20 @@ public class JavascriptEventConfirmation extends Behavior { public void renderHead(Component component, IHeaderResponse response) { super.renderHead(component, response); final String confirmationMsg = getConfirmationMsg(component); + if (confirmationMsg == null) return; + String confirmScript = "var conf = confirm('" + confirmationMsg.replaceAll("'", "\\\\'") + "'); " + "if (!conf) event.preventDefault();"; response.render(OnEventHeaderItem.forComponent(component, event, confirmScript)); } private String getConfirmationMsg(Component component) { - IModel<?> model = msgModel; - if (model instanceof IComponentAssignedModel) + IModel<String> model = msgModel; + if (model instanceof IComponentAssignedModel<String> icam) { - model = ((IComponentAssignedModel<?>)model).wrapOnAssignment(component); + model = icam.wrapOnAssignment(component); } - return String.valueOf(model.getObject()); + return model.getObject(); } @Override diff --git a/view/src/test/java/se/su/dsv/scipro/grading/SendToExaminerTest.java b/view/src/test/java/se/su/dsv/scipro/grading/SendToExaminerTest.java index a699f1e6ad..c7387f7d5f 100644 --- a/view/src/test/java/se/su/dsv/scipro/grading/SendToExaminerTest.java +++ b/view/src/test/java/se/su/dsv/scipro/grading/SendToExaminerTest.java @@ -18,6 +18,7 @@ import se.su.dsv.scipro.system.DegreeType; import se.su.dsv.scipro.system.ProjectType; import se.su.dsv.scipro.system.User; import se.su.dsv.scipro.util.Either; +import se.su.dsv.scipro.util.JavascriptEventConfirmation; import java.math.BigDecimal; import java.time.LocalDate; @@ -152,6 +153,55 @@ public class SendToExaminerTest extends SciProTest { verify(gradingService).reportGrade(TOKEN, project.getIdentifier(), biden.getIdentifier(), gw.id(), grade.name(), finalThesis.getUploadDate()); } + @Test + public void author_without_active_participations_done_should_generate_confirmation_on_sending_to_examiner() { + ProjectType bachelor = new ProjectType(DegreeType.BACHELOR, "Bachelor", "Bachelor"); + User obama = User.builder() + .firstName("Barack") + .lastName("Obama") + .emailAddress("obama@example.com") + .identifier(44) + .build(); + User biden = User.builder() + .firstName("Joe") + .lastName("Biden") + .emailAddress("joe@example.com") + .identifier(46) + .build(); + biden.setId(46L); + Project project = Project.builder() + .title("My project") + .projectType(bachelor) + .startDate(LocalDate.of(2023, Month.JANUARY, 6)) + .headSupervisor(obama) + .projectParticipants(Set.of(biden)) + .identifier(1888) + .build(); + project.setId(8L); + + Examination gw = new Examination( + 2, + new Name("Examensarbete", "Graduation work"), + "KX1E", + BigDecimal.valueOf(9), + List.of( + new Grade(Grade.Type.PASSING, "A"), + new Grade(Grade.Type.PASSING, "B"), + new Grade(Grade.Type.PASSING, "C"), + new Grade(Grade.Type.PASSING, "D"), + new Grade(Grade.Type.PASSING, "E"), + new Grade(Grade.Type.FAILING, "F"))); + + when(gradingService.getExaminations(any(), anyLong(), anyLong())) + .thenReturn(List.of(gw)); + when(gradingService.getResult(any(), anyLong(), anyLong(), anyLong())) + .thenReturn(Either.right(Optional.empty())); + + tester.startComponentInPage(new SendToExaminer("id", () -> project, () -> biden, () -> "please check active participations")); + + tester.assertBehavior(path("id", "form", "send"), JavascriptEventConfirmation.class); + } + private static Thesis daisyThesis() { Unit unit = new Unit(); unit.setId(1);