From ab1b9b87318ec30c9e7a66838df46ead1d985f90 Mon Sep 17 00:00:00 2001 From: Markus Kreth Date: Wed, 23 Jan 2019 04:58:47 +0100 Subject: [PATCH] database storage simplified, Person Add incl. Validation. --- .../business/EventBusiness.java | 2 +- .../meldung/MeldungDmtWettkampfGenerator.java | 2 +- .../MeldungEinzelWettkampfGenerator.java | 3 +- .../vaadinclubhelper/dao/AbstractDaoImpl.java | 6 -- .../dao/ClubEventDaoImpl.java | 4 +- .../clubhelper/vaadinclubhelper/dao/IDao.java | 3 +- .../vaadinclubhelper/dao/PersonDaoImpl.java | 6 -- .../vaadinclubhelper/ui/PersonEditView.java | 19 ++++++- .../ui/components/PersonEditDetails.java | 55 +++++++++++++------ .../ui/components/PersonGrid.java | 5 ++ .../ui/components/PersonGroupValidator.java | 25 +++++++++ .../vaadinclubhelper/dao/PersonDaoTest.java | 2 +- 12 files changed, 96 insertions(+), 36 deletions(-) create mode 100644 src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/PersonGroupValidator.java diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/business/EventBusiness.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/business/EventBusiness.java index 27a5251..477d96b 100644 --- a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/business/EventBusiness.java +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/business/EventBusiness.java @@ -80,7 +80,7 @@ public class EventBusiness { public void storeAltersgruppe(Altersgruppe edited) { altersgruppeDao.save(edited); - clubEventDao.update(current); + clubEventDao.save(current); } public EventMeldung createMeldung() { diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/business/meldung/MeldungDmtWettkampfGenerator.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/business/meldung/MeldungDmtWettkampfGenerator.java index 848cd0c..fbf7fa3 100644 --- a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/business/meldung/MeldungDmtWettkampfGenerator.java +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/business/meldung/MeldungDmtWettkampfGenerator.java @@ -8,7 +8,7 @@ class MeldungDmtWettkampfGenerator extends AbstractMeldungGenerator { @Override public void personRepresentation(StringBuilder txt, Altersgruppe g, Person p) { txt.append("\n").append(p.getPrename()).append(" ").append(p.getSurname()).append("\t") - .append(p.getBirth().getYear()); + .append(p.getBirth().getYear()).append("\t").append(p.getStartpass().getStartpassNr()); } } diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/business/meldung/MeldungEinzelWettkampfGenerator.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/business/meldung/MeldungEinzelWettkampfGenerator.java index ad63ea9..4f57468 100644 --- a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/business/meldung/MeldungEinzelWettkampfGenerator.java +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/business/meldung/MeldungEinzelWettkampfGenerator.java @@ -8,7 +8,8 @@ class MeldungEinzelWettkampfGenerator extends AbstractMeldungGenerator { @Override public void personRepresentation(StringBuilder txt, Altersgruppe g, Person p) { txt.append("\n").append(p.getPrename()).append(" ").append(p.getSurname()).append("\t") - .append(p.getBirth().getYear()).append("\t").append(g.getPflicht().getName()); + .append(p.getBirth().getYear()).append("\t").append(g.getPflicht().getName()).append("\t") + .append(p.getStartpass().getStartpassNr()); } } diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/dao/AbstractDaoImpl.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/dao/AbstractDaoImpl.java index 6299fc4..ee6b543 100644 --- a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/dao/AbstractDaoImpl.java +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/dao/AbstractDaoImpl.java @@ -33,12 +33,6 @@ public abstract class AbstractDaoImpl implements IDao< } } - @Override - @Transactional - public T update(T obj) { - return entityManager.merge(obj); - } - @Override public T get(Object primaryKey) { return entityManager.find(entityClass, primaryKey); diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/dao/ClubEventDaoImpl.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/dao/ClubEventDaoImpl.java index 0d5142a..b929796 100644 --- a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/dao/ClubEventDaoImpl.java +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/dao/ClubEventDaoImpl.java @@ -24,14 +24,14 @@ public class ClubEventDaoImpl extends AbstractDaoImpl implements Club } @Override - public ClubEvent update(ClubEvent obj) { + public void save(ClubEvent obj) { CompetitionType competitionType = obj.getCompetitionType(); if (competitionType != null) { if (obj.getCompetitionType().getId() == null) { obj.getCompetitionType().setId(obj.getId()); } } - return super.update(obj); + super.save(obj); } @Override diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/dao/IDao.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/dao/IDao.java index 5629777..737a2e2 100644 --- a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/dao/IDao.java +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/dao/IDao.java @@ -5,7 +5,8 @@ import java.util.List; public interface IDao { void save(T obj); - T update(T obj); + List listAll(); + T get(Object primaryKey); } diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/dao/PersonDaoImpl.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/dao/PersonDaoImpl.java index 32ea027..8d9d4d1 100644 --- a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/dao/PersonDaoImpl.java +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/dao/PersonDaoImpl.java @@ -13,12 +13,6 @@ public class PersonDaoImpl extends AbstractDaoImpl implements PersonDao super(Person.class); } - @Override - public Person update(Person obj) { - - return super.update(obj); - } - @Override public Person findLoginUser(String username, String password) { TypedQuery query = entityManager.createNamedQuery(Person.QUERY_FINDLOGIN, Person.class); diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/PersonEditView.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/PersonEditView.java index fe14fc5..ceb1b13 100644 --- a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/PersonEditView.java +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/PersonEditView.java @@ -1,9 +1,13 @@ package de.kreth.vaadin.clubhelper.vaadinclubhelper.ui; +import java.util.ArrayList; +import java.util.HashSet; import java.util.Optional; import com.vaadin.event.selection.SelectionEvent; +import com.vaadin.ui.Button; import com.vaadin.ui.HorizontalLayout; +import com.vaadin.ui.VerticalLayout; import de.kreth.vaadin.clubhelper.vaadinclubhelper.dao.GroupDao; import de.kreth.vaadin.clubhelper.vaadinclubhelper.dao.PersonDao; @@ -11,7 +15,7 @@ import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Person; import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components.PersonEditDetails; import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components.PersonGrid; -public class PersonEditView extends HorizontalLayout implements NamedView { +public class PersonEditView extends VerticalLayout implements NamedView { public static final String VIEW_NAME = "PersonEditView"; private static final long serialVersionUID = 1770993670570422036L; @@ -33,6 +37,19 @@ public class PersonEditView extends HorizontalLayout implements NamedView { layout.addComponents(personGrid, personDetails); layout.setSizeFull(); addComponent(layout); + Button addPerson = new Button("Hinzufügen"); + addPerson.addClickListener(ev -> addPerson()); + addComponent(addPerson); + } + + private void addPerson() { + Person person = new Person(); + person.setGroups(new HashSet<>()); + person.setAdresses(new ArrayList<>()); + person.setEvents(new HashSet<>()); + person.setRelatives1(new ArrayList<>()); + personDetails.setBean(person); + personGrid.deselectAll(); } void selectedPerson(SelectionEvent p) { diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/PersonEditDetails.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/PersonEditDetails.java index 9e90dc4..ab7bfe0 100644 --- a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/PersonEditDetails.java +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/PersonEditDetails.java @@ -6,10 +6,13 @@ import java.util.function.Consumer; import org.vaadin.teemu.switchui.Switch; import com.vaadin.data.Binder; +import com.vaadin.data.BinderValidationStatus; +import com.vaadin.data.ValidationResult; import com.vaadin.ui.Button; import com.vaadin.ui.ComboBox; import com.vaadin.ui.DateField; import com.vaadin.ui.HorizontalLayout; +import com.vaadin.ui.Notification; import com.vaadin.ui.Panel; import com.vaadin.ui.TextField; import com.vaadin.ui.VerticalLayout; @@ -29,7 +32,7 @@ public class PersonEditDetails extends HorizontalLayout { private final Binder binder; private Consumer personChangeHandler; - private Person current; + private Button okButton; public PersonEditDetails(List groups, PersonDao dao) { this(groups, dao, true); @@ -47,8 +50,8 @@ public class PersonEditDetails extends HorizontalLayout { TextField textStartPass = new TextField("Startpass"); binder = new Binder<>(); - binder.forField(textPrename).bind(Person::getPrename, Person::setPrename); - binder.forField(textSureName).bind(Person::getSurname, Person::setSurname); + binder.forField(textPrename).asRequired().bind(Person::getPrename, Person::setPrename); + binder.forField(textSureName).asRequired().bind(Person::getSurname, Person::setSurname); binder.forField(birthday).bind(Person::getBirth, Person::setBirth); binder.forField(genderBox).bind(Person::getGender, Person::setGender); binder.forField(textStartPass).bind(p -> { @@ -77,25 +80,41 @@ public class PersonEditDetails extends HorizontalLayout { }); } + binder.withValidator(p -> (p.getGroups() != null && p.getGroups().isEmpty() == false), + "Mind. eine Gruppe muss gewählt sein!"); + Button close = new Button("Schließen"); if (showCloseButton) { close.addClickListener(ev -> closeWithoutSave(dao)); } else { close.setVisible(false); } - Button ok = new Button("Speichern"); - ok.addClickListener(ev -> { - if (binder.validate().isOk()) { - binder.writeBeanIfValid(current); - dao.update(current); + + okButton = new Button("Speichern"); + okButton.addClickListener(ev -> { + BinderValidationStatus validate = binder.validate(); + if (validate.isOk()) { + dao.save(binder.getBean()); if (personChangeHandler != null) { - personChangeHandler.accept(current); + personChangeHandler.accept(binder.getBean()); + } + } else { + List errors = validate.getBeanValidationErrors(); + StringBuilder msg = new StringBuilder(); + for (ValidationResult res : errors) { + if (res.isError()) { + if (msg.length() > 0) { + msg.append("\n"); + } + msg.append(res.getErrorMessage()); + } } + Notification.show("Fehler korrigieren", msg.toString(), Notification.Type.ERROR_MESSAGE); } }); - + okButton.setEnabled(false); VerticalLayout layout = new VerticalLayout(textPrename, textSureName, birthday, genderBox, textStartPass, close, - ok); + okButton); addComponents(layout, groupPanel); } @@ -105,8 +124,13 @@ public class PersonEditDetails extends HorizontalLayout { } public void setBean(Person person) { - this.current = person; - binder.readBean(person); + binder.setBean(person); + + if (person != null) { + okButton.setEnabled(true); + } else { + okButton.setEnabled(false); + } } private void closeWithoutSave(PersonDao dao) { @@ -117,11 +141,10 @@ public class PersonEditDetails extends HorizontalLayout { .saveDiscardCancel().setResultHandler(button -> { if (button == ConfirmDialog.Buttons.SAVE) { if (binder.validate().isOk()) { - binder.writeBeanIfValid(current); - dao.update(current); + dao.save(binder.getBean()); } } else if (button == ConfirmDialog.Buttons.DISCARD) { - binder.readBean(current); + binder.removeBean(); } }).build(); diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/PersonGrid.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/PersonGrid.java index 9b330f4..5373093 100644 --- a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/PersonGrid.java +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/PersonGrid.java @@ -112,6 +112,11 @@ public class PersonGrid extends VerticalLayout { public void setSelectionMode(SelectionMode selectionMode) { grid.setSelectionMode(selectionMode); currentSelectionMode = selectionMode; + if (selectionMode != SelectionMode.MULTI) { + checkIncluded.setVisible(false); + } else { + checkIncluded.setVisible(true); + } } private Layout setupFilterComponents() { diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/PersonGroupValidator.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/PersonGroupValidator.java new file mode 100644 index 0000000..6731a37 --- /dev/null +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/PersonGroupValidator.java @@ -0,0 +1,25 @@ +package de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components; + +import java.util.Set; + +import com.vaadin.data.ValidationResult; +import com.vaadin.data.Validator; +import com.vaadin.data.ValueContext; + +import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.GroupDef; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Person; + +public class PersonGroupValidator implements Validator { + + private static final long serialVersionUID = -941938281840937295L; + + @Override + public ValidationResult apply(Person value, ValueContext context) { + Set gr = value.getGroups(); + if (gr != null && gr.isEmpty() == false) { + return ValidationResult.ok(); + } + + return ValidationResult.error("Es müssen Gruppen gesetzt sein!"); + } +} diff --git a/src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/dao/PersonDaoTest.java b/src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/dao/PersonDaoTest.java index 7d7d569..a5fa71c 100644 --- a/src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/dao/PersonDaoTest.java +++ b/src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/dao/PersonDaoTest.java @@ -71,7 +71,7 @@ public class PersonDaoTest { person.setSurname("surname2"); person.setPrename("prename2"); - testDatabaseHelper.transactional(() -> personDao.update(person)); + testDatabaseHelper.transactional(() -> personDao.save(person)); List stored = entityManager.createNamedQuery(Person.QUERY_FINDALL, Person.class).getResultList(); assertEquals(1, stored.size());