diff --git a/src/main/java/com/primefactorsolutions/model/Assessment.java b/src/main/java/com/primefactorsolutions/model/Assessment.java index 5d665fd..78a9941 100644 --- a/src/main/java/com/primefactorsolutions/model/Assessment.java +++ b/src/main/java/com/primefactorsolutions/model/Assessment.java @@ -6,6 +6,7 @@ import lombok.EqualsAndHashCode; import lombok.NoArgsConstructor; import java.time.Instant; +import java.util.ArrayList; import java.util.List; import java.util.Optional; @@ -15,16 +16,16 @@ import java.util.Optional; @EqualsAndHashCode(callSuper = true) public class Assessment extends BaseEntity { @OneToMany(fetch = FetchType.EAGER) - private List questions; + private List questions = new ArrayList<>(); @OneToMany(fetch = FetchType.EAGER, mappedBy = "assessment", cascade = {CascadeType.ALL}) - private List submissions; + private List submissions = new ArrayList<>(); @ManyToOne private AppUser appUser; @OneToMany(fetch = FetchType.EAGER, mappedBy = "assessment", cascade = {CascadeType.ALL}) - private List assessmentEvents = List.of(); + private List assessmentEvents = new ArrayList<>(); public Submission getCurrentSubmission() { return submissions.getLast(); diff --git a/src/main/java/com/primefactorsolutions/model/Question.java b/src/main/java/com/primefactorsolutions/model/Question.java index 475713d..a6e295a 100644 --- a/src/main/java/com/primefactorsolutions/model/Question.java +++ b/src/main/java/com/primefactorsolutions/model/Question.java @@ -12,5 +12,7 @@ import lombok.NoArgsConstructor; @NoArgsConstructor @EqualsAndHashCode(callSuper = true) public class Question extends BaseEntity{ + private String title; + private String description; private String content; } diff --git a/src/main/java/com/primefactorsolutions/repositories/QuestionRepository.java b/src/main/java/com/primefactorsolutions/repositories/QuestionRepository.java new file mode 100644 index 0000000..363637f --- /dev/null +++ b/src/main/java/com/primefactorsolutions/repositories/QuestionRepository.java @@ -0,0 +1,9 @@ +package com.primefactorsolutions.repositories; + +import com.primefactorsolutions.model.Question; +import org.springframework.data.jpa.repository.JpaRepository; + +import java.util.UUID; + +public interface QuestionRepository extends JpaRepository { +} diff --git a/src/main/java/com/primefactorsolutions/service/AssessmentService.java b/src/main/java/com/primefactorsolutions/service/AssessmentService.java index 2752327..b40dbab 100644 --- a/src/main/java/com/primefactorsolutions/service/AssessmentService.java +++ b/src/main/java/com/primefactorsolutions/service/AssessmentService.java @@ -145,4 +145,8 @@ public class AssessmentService { submission.setResponse(currSubmission.getResponse()); Assessment saved = assessmentRepository.save(assessment); } + + public Assessment saveAssessment(final Assessment assessment) { + return assessmentRepository.save(assessment); + } } diff --git a/src/main/java/com/primefactorsolutions/service/QuestionService.java b/src/main/java/com/primefactorsolutions/service/QuestionService.java new file mode 100644 index 0000000..e2e6006 --- /dev/null +++ b/src/main/java/com/primefactorsolutions/service/QuestionService.java @@ -0,0 +1,18 @@ +package com.primefactorsolutions.service; + +import com.primefactorsolutions.model.Question; +import com.primefactorsolutions.repositories.QuestionRepository; +import lombok.Data; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +@Data +public class QuestionService { + private final QuestionRepository questionRepository; + + public List getQuestions() { + return questionRepository.findAll(); + } +} diff --git a/src/main/java/com/primefactorsolutions/views/AssessmentView.java b/src/main/java/com/primefactorsolutions/views/AssessmentView.java index 3f927e1..cddcfa3 100644 --- a/src/main/java/com/primefactorsolutions/views/AssessmentView.java +++ b/src/main/java/com/primefactorsolutions/views/AssessmentView.java @@ -1,9 +1,15 @@ package com.primefactorsolutions.views; +import com.primefactorsolutions.model.AppUser; import com.primefactorsolutions.model.Assessment; +import com.primefactorsolutions.model.Question; import com.primefactorsolutions.service.AssessmentService; +import com.primefactorsolutions.service.QuestionService; +import com.primefactorsolutions.service.UserService; import com.vaadin.flow.component.Component; -import com.vaadin.flow.component.textfield.TextField; +import com.vaadin.flow.component.ItemLabelGenerator; +import com.vaadin.flow.component.combobox.ComboBox; +import com.vaadin.flow.component.dependency.Uses; import com.vaadin.flow.router.BeforeEvent; import com.vaadin.flow.router.HasUrlParameter; import com.vaadin.flow.router.PageTitle; @@ -11,6 +17,7 @@ import com.vaadin.flow.router.Route; import com.vaadin.flow.spring.annotation.SpringComponent; import org.apache.commons.lang3.StringUtils; import org.springframework.context.annotation.Scope; +import org.vaadin.firitin.fields.SubListSelector; import org.vaadin.firitin.form.BeanValidationForm; import java.util.List; @@ -20,32 +27,43 @@ import java.util.UUID; @Scope("prototype") @PageTitle("Assessments") @Route(value = "/assessments", layout = MainLayout.class) +@Uses(ComboBox.class) public class AssessmentView extends BeanValidationForm implements HasUrlParameter { private final AssessmentService assessmentService; - private final TextField appUserName = new TextField(); + private ComboBox appUser = null; + private SubListSelector questions = null; - public AssessmentView(AssessmentService assessmentService) { + public AssessmentView(AssessmentService assessmentService, QuestionService questionService, UserService userService) { super(Assessment.class); - setEntityWithEnabledSave(new Assessment()); + this.assessmentService = assessmentService; - setSavedHandler((SavedHandler) assessment -> { - System.out.println(assessment); - }); - } + appUser = new ComboBox<>("AppUser", userService.getUsers()); + appUser.setItemLabelGenerator((ItemLabelGenerator) AppUser::getEmail); + appUser.setReadOnly(false); + questions = new SubListSelector<>(Question.class); + questions.setItemLabelGenerator((ItemLabelGenerator) Question::getTitle); + questions.setReadOnly(false); + questions.setAvailableOptions(questionService.getQuestions()); + + setSavedHandler((SavedHandler) this.assessmentService::saveAssessment); + } @Override public void setParameter(final BeforeEvent beforeEvent, final String s) { if (StringUtils.isNotBlank(s) && !"new".equals(s)) { var assessment = assessmentService.getAssessment(UUID.fromString(s)); + setEntityWithEnabledSave(assessment); + } else { + setEntityWithEnabledSave(new Assessment()); } } @Override protected List getFormComponents() { - return List.of(appUserName); + return List.of(appUser, questions); } } diff --git a/src/main/java/com/primefactorsolutions/views/AssessmentsListView.java b/src/main/java/com/primefactorsolutions/views/AssessmentsListView.java index 5856050..b1caa18 100644 --- a/src/main/java/com/primefactorsolutions/views/AssessmentsListView.java +++ b/src/main/java/com/primefactorsolutions/views/AssessmentsListView.java @@ -23,7 +23,9 @@ public class AssessmentsListView extends Main { public AssessmentsListView(final AssessmentService assessmentService) { final VGrid grid = new VGrid<>(Assessment.class); - grid.setDataProvider(new DataProvider() { + grid.setColumns("id", "appUser.email"); + + grid.setDataProvider(new DataProvider<>() { @Override public boolean isInMemory() { return false; @@ -31,12 +33,16 @@ public class AssessmentsListView extends Main { @Override public int size(Query query) { - return 0; + return assessmentService.getAssessments().size(); } @Override public Stream fetch(Query query) { - return null; + int limit = query.getLimit(); + int pagerSize = query.getPageSize(); + int page = query.getPage(); + + return assessmentService.getAssessments().stream(); } @Override diff --git a/src/main/java/com/primefactorsolutions/views/UserView.java b/src/main/java/com/primefactorsolutions/views/UserView.java index e9e49ed..8d9afef 100644 --- a/src/main/java/com/primefactorsolutions/views/UserView.java +++ b/src/main/java/com/primefactorsolutions/views/UserView.java @@ -23,29 +23,30 @@ import java.util.UUID; public class UserView extends BeanValidationForm implements HasUrlParameter { private final UserService userService; - private final TextField appUserName = new TextField(); + private TextField email = null; - public UserView(UserService userService) { + public UserView(final UserService userService) { super(AppUser.class); - setEntityWithEnabledSave(new AppUser("foo@bar.com", List.of())); this.userService = userService; + email = new TextField(); + email.setLabel("Email"); - setSavedHandler((SavedHandler) appUser -> { - System.out.println(appUser); - }); + setSavedHandler((SavedHandler) userService::createOrUpdate); } @Override public void setParameter(final BeforeEvent beforeEvent, final String s) { if (StringUtils.isNotBlank(s) && !"new".equals(s)) { - var assessment = userService.getUser(UUID.fromString(s)); - + var user = userService.getUser(UUID.fromString(s)); + setEntityWithEnabledSave(user); + } else { + setEntityWithEnabledSave(new AppUser()); } } @Override protected List getFormComponents() { - return List.of(appUserName); + return List.of(email); } } diff --git a/src/main/java/com/primefactorsolutions/views/UsersListView.java b/src/main/java/com/primefactorsolutions/views/UsersListView.java index 1c86f0b..561f30d 100644 --- a/src/main/java/com/primefactorsolutions/views/UsersListView.java +++ b/src/main/java/com/primefactorsolutions/views/UsersListView.java @@ -1,6 +1,7 @@ package com.primefactorsolutions.views; import com.primefactorsolutions.model.AppUser; +import com.primefactorsolutions.service.UserService; import com.vaadin.flow.component.html.Main; import com.vaadin.flow.data.provider.DataProvider; import com.vaadin.flow.data.provider.DataProviderListener; @@ -20,11 +21,14 @@ import java.util.stream.Stream; @PageTitle("Users") @Route(value = "/users", layout = MainLayout.class) public class UsersListView extends Main { + final UserService userService; - public UsersListView() { + public UsersListView(final UserService userService) { + this.userService = userService; final VGrid grid = new VGrid<>(AppUser.class); + grid.setColumns("id", "email"); - grid.setDataProvider(new DataProvider() { + grid.setDataProvider(new DataProvider<>() { @Override public boolean isInMemory() { return false; @@ -32,7 +36,7 @@ public class UsersListView extends Main { @Override public int size(Query query) { - return 1; + return userService.getUsers().size(); } @Override @@ -40,7 +44,7 @@ public class UsersListView extends Main { int limit = query.getLimit(); int pagerSize = query.getPageSize(); int page = query.getPage(); - return Stream.of(new AppUser("foo@bar.com", List.of())); + return userService.getUsers().stream(); } @Override diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index ff2f3b5..49a38cb 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -6,11 +6,11 @@ spring.mustache.check-template-location = false vaadin.launch-browser=true # To improve the performance during development. # For more information https://vaadin.com/docs/latest/integrations/spring/configuration#special-configuration-parameters -vaadin.allowed-packages = com.vaadin,org.vaadin,com.primefactorsolutions,com.hilerio.ace,com.flowingcode.vaadin +vaadin.allowed-packages = com.vaadin,org.vaadin,com.primefactorsolutions,com.hilerio.ace,com.flowingcode.vaadin,org.vaadin.firitin -#spring.datasource.url=jdbc:h2:mem:testdb -#spring.datasource.url=jdbc:h2:file:./db +# 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=embedded diff --git a/src/main/resources/data.sql b/src/main/resources/data.sql index 41ddc80..d5d963a 100644 --- a/src/main/resources/data.sql +++ b/src/main/resources/data.sql @@ -1,7 +1,7 @@ insert into app_user (id, version, email) values ('23471ab3-f639-4d2b-9541-7227f4ea7ee6', 1, 'foo@bar.com'); -insert into question (id, version, content) values ('a7e00ff8-da41-4624-b31c-1b13c3f2e3ae', 1, 'foo bar'); -insert into question (id, version, content) values ('8a4b213c-ca81-4c38-b56d-d7028c2dde88', 1, 'foo buzz'); +insert into question (id, version, content, title, description) values ('a7e00ff8-da41-4624-b31c-1b13c3f2e3ae', 1, 'foo bar', 'q1', 'lorem ipsum'); +insert into question (id, version, content, title, description) values ('8a4b213c-ca81-4c38-b56d-d7028c2dde88', 1, 'foo buzz', 'q2', 'lorem ipsum'); insert into assessment(id, version, app_user_id) values ('46b153f4-23fd-462f-8430-fbe67b83caab', 1, '23471ab3-f639-4d2b-9541-7227f4ea7ee6');