From eaa2219cb88c11ea9cba5d2ff5013c06a5f8a6aa Mon Sep 17 00:00:00 2001 From: Markus Kreth Date: Sat, 26 Jan 2019 03:44:03 +0100 Subject: [PATCH] Person Relation is shown. --- .../dao/ClubEventDaoImpl.java | 8 +- .../vaadinclubhelper/dao/PersonDao.java | 5 ++ .../vaadinclubhelper/dao/PersonDaoImpl.java | 31 +++++++ .../vaadinclubhelper/data/Relation.java | 79 +++++++++++++++++ .../vaadinclubhelper/data/Relative.java | 31 +++++-- .../ui/components/PersonEditDetails.java | 88 ++++++++++++++----- .../vaadin/clubhelper/HibernateHolder.java | 2 +- .../clubhelper/MysqlLocalConfiguration.java | 2 +- .../clubhelper/MysqlTestConfiguration.java | 2 +- .../dao/PersonDaoRelativeTest.java | 84 ++++++++++++++++++ .../ui/tests/TestConfiguration.java | 4 + 11 files changed, 300 insertions(+), 36 deletions(-) create mode 100644 src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/data/Relation.java create mode 100644 src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/dao/PersonDaoRelativeTest.java 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 487fa7b..664bf1d 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 @@ -55,6 +55,12 @@ public class ClubEventDaoImpl extends AbstractDaoImpl implements Club } } + executeQueries(event, added, removed); + current.addAll(added); + current.removeAll(removed); + } + + public void executeQueries(ClubEvent event, List added, List removed) { if (!added.isEmpty()) { Query insertQuery = entityManager.createNativeQuery( "INSERT INTO clubevent_has_person (clubevent_id, person_id) VALUES (:eventId,:personId)"); @@ -73,8 +79,6 @@ public class ClubEventDaoImpl extends AbstractDaoImpl implements Club deleteQuery.executeUpdate(); } } - current.addAll(added); - current.removeAll(removed); } @Override diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/dao/PersonDao.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/dao/PersonDao.java index 07be91e..01d5534 100644 --- a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/dao/PersonDao.java +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/dao/PersonDao.java @@ -1,8 +1,11 @@ package de.kreth.vaadin.clubhelper.vaadinclubhelper.dao; +import java.util.List; + import javax.persistence.NoResultException; import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Person; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Relation; public interface PersonDao extends IDao { @@ -15,4 +18,6 @@ public interface PersonDao extends IDao { * @throws NoResultException if no result is found. */ Person findLoginUser(String username, String password) throws NoResultException; + + List findRelationsFor(Person p); } 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 fd627e2..f724ac7 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 @@ -1,14 +1,18 @@ package de.kreth.vaadin.clubhelper.vaadinclubhelper.dao; +import java.util.ArrayList; import java.util.List; +import javax.persistence.Query; import javax.persistence.TypedQuery; import org.springframework.stereotype.Repository; +import org.springframework.transaction.annotation.Transactional; import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Contact; import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.EntityAccessor; import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Person; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Relation; import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Startpass; @Repository @@ -37,6 +41,7 @@ public class PersonDaoImpl extends AbstractDaoImpl implements PersonDao } } + @Transactional public void persistIfNew(EntityAccessor c) { if (c.hasValidId() == false) { entityManager.persist(c); @@ -51,4 +56,30 @@ public class PersonDaoImpl extends AbstractDaoImpl implements PersonDao return query.getSingleResult(); } + @Override + public List findRelationsFor(Person p) { + Query query = entityManager.createNativeQuery( + "SELECT id, person1, person2, TO_PERSON1_RELATION, TO_PERSON2_RELATION, changed, created, deleted FROM relative"); + + @SuppressWarnings("unchecked") + List result = query.getResultList(); + List relations = new ArrayList<>(); + for (Object[] r : result) { + Relation rel = toRelative(r, p.getId()); + if (rel != null) { + relations.add(rel); + } + } + return relations; + } + + private Relation toRelative(Object[] r, int ignoring) { + + if (r[1].equals(ignoring)) { + return new Relation(entityManager.find(Person.class, r[2]), r[3].toString()); + } else if (r[2].equals(ignoring)) { + return new Relation(entityManager.find(Person.class, r[1]), r[4].toString()); + } + return null; + } } diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/data/Relation.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/data/Relation.java new file mode 100644 index 0000000..614733d --- /dev/null +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/data/Relation.java @@ -0,0 +1,79 @@ +package de.kreth.vaadin.clubhelper.vaadinclubhelper.data; + +public class Relation { + + public enum RelationType { + RELATIONSHIP("Partner"), SIBLINGS("Geschwister"), PARENT("Elternteil"), CHILD("Kind"); + + private final String localized; + + private RelationType(String localized) { + this.localized = localized; + } + + public String getLocalized() { + return localized; + } + } + + private Person person; + private String relation; + + public Relation(Person person, String relation) { + super(); + this.person = person; + this.relation = relation; + } + + public Person getPerson() { + return person; + } + + public RelationType getRelation() { + return RelationType.valueOf(relation); + } + + @Override + public String toString() { + return "Relation [person=" + person + ", relation=" + relation + "]"; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((person == null) ? 0 : person.hashCode()); + result = prime * result + ((relation == null) ? 0 : relation.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + Relation other = (Relation) obj; + if (person == null) { + if (other.person != null) { + return false; + } + } else if (!person.equals(other.person)) { + return false; + } + if (relation == null) { + if (other.relation != null) { + return false; + } + } else if (!relation.equals(other.relation)) { + return false; + } + return true; + } + +} diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/data/Relative.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/data/Relative.java index 0b22d53..6645bdb 100644 --- a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/data/Relative.java +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/data/Relative.java @@ -11,33 +11,33 @@ import javax.persistence.ManyToOne; import javax.persistence.NamedQuery; import javax.persistence.Table; - /** * The persistent class for the relative database table. * */ @Entity -@Table(name="relative") +@Table(name = "relative") @Inheritance(strategy = InheritanceType.SINGLE_TABLE) -@NamedQuery(name="Relative.findAll", query="SELECT r FROM Relative r") +@NamedQuery(name = Relative.QUERY_FINDALL, query = "SELECT r FROM Relative r") public class Relative extends BaseEntity implements Serializable { + public static final String QUERY_FINDALL = "Relative.findAll"; private static final long serialVersionUID = -1331008393583211773L; - @Column(name="TO_PERSON1_RELATION") + @Column(name = "TO_PERSON1_RELATION") private String toPerson1Relation; - @Column(name="TO_PERSON2_RELATION") + @Column(name = "TO_PERSON2_RELATION") private String toPerson2Relation; - //bi-directional many-to-one association to Person + // bi-directional many-to-one association to Person @ManyToOne - @JoinColumn(name="person1") + @JoinColumn(name = "person1") private Person person1Bean; - //bi-directional many-to-one association to Person + // bi-directional many-to-one association to Person @ManyToOne - @JoinColumn(name="person2") + @JoinColumn(name = "person2") private Person person2Bean; public Relative() { @@ -75,6 +75,19 @@ public class Relative extends BaseEntity implements Serializable { this.person2Bean = person2Bean; } + public Relation getRelationTo(Person person) { + if (person == null) { + return null; + } + if (person.equals(person1Bean)) { + return new Relation(person2Bean, toPerson2Relation); + } else if (person.equals(person2Bean)) { + return new Relation(person1Bean, toPerson1Relation); + } else { + return null; + } + } + @Override public int hashCode() { final int prime = 31; 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 42b9027..468bf94 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 @@ -10,10 +10,11 @@ import com.vaadin.data.BinderValidationStatus; import com.vaadin.data.ValidationResult; import com.vaadin.ui.Button; import com.vaadin.ui.ComboBox; +import com.vaadin.ui.Component; import com.vaadin.ui.DateField; import com.vaadin.ui.HorizontalLayout; import com.vaadin.ui.Notification; -import com.vaadin.ui.Panel; +import com.vaadin.ui.TabSheet; import com.vaadin.ui.TextField; import com.vaadin.ui.VerticalLayout; @@ -21,6 +22,7 @@ import de.kreth.vaadin.clubhelper.vaadinclubhelper.dao.PersonDao; import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Gender; import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.GroupDef; import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Person; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Relation; import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Startpass; public class PersonEditDetails extends HorizontalLayout { @@ -30,9 +32,13 @@ public class PersonEditDetails extends HorizontalLayout { private final TextField textSureName; private final DateField birthday; + private final PersonDao dao; + private final Binder binder; private Consumer personChangeHandler; private Button okButton; + private VerticalLayout contactLayout; + private VerticalLayout relationshipLayout; public PersonEditDetails(List groups, PersonDao dao) { this(groups, dao, true); @@ -40,6 +46,8 @@ public class PersonEditDetails extends HorizontalLayout { public PersonEditDetails(List groups, PersonDao dao, boolean showCloseButton) { + this.dao = dao; + setCaption("Personendetails"); textPrename = new TextField("Vorname"); @@ -64,30 +72,12 @@ public class PersonEditDetails extends HorizontalLayout { return startpass.getStartpassNr(); }, (p, value) -> p.setStartpass(value)); - Panel groupPanel = new Panel("Gruppen"); - VerticalLayout glay = new VerticalLayout(); - groupPanel.setContent(glay); - - for (GroupDef g : groups) { - Switch sw = new Switch(g.getName()); - sw.setData(g); - glay.addComponent(sw); - - binder.forField(sw).bind(p -> p.getGroups().contains(g), (bean, fieldvalue) -> { - if (fieldvalue) { - bean.getGroups().add(g); - } else { - bean.getGroups().remove(g); - } - }); - } - 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)); + close.addClickListener(ev -> closeWithoutSave()); } else { close.setVisible(false); } @@ -117,8 +107,47 @@ public class PersonEditDetails extends HorizontalLayout { okButton.setEnabled(false); VerticalLayout layout = new VerticalLayout(textPrename, textSureName, birthday, genderBox, textStartPass, close, okButton); - addComponents(layout, groupPanel); + Component groupLayout = createGroupPanel(groups); + Component contactLayout = createContactLayout(); + Component relationshipLayout = createRelationshipLayout(); + TabSheet sheet = new TabSheet(); + sheet.addTab(groupLayout, "Gruppen"); + sheet.addTab(contactLayout, "Kontakte"); + sheet.addTab(relationshipLayout, "Angehörige"); + addComponents(layout, sheet); + + } + + private Component createRelationshipLayout() { + relationshipLayout = new VerticalLayout(); + return relationshipLayout; + } + + private Component createContactLayout() { + contactLayout = new VerticalLayout(); + + return contactLayout; + } + + public Component createGroupPanel(List groups) { + + VerticalLayout layout = new VerticalLayout(); + + for (GroupDef g : groups) { + Switch sw = new Switch(g.getName()); + sw.setData(g); + layout.addComponent(sw); + + binder.forField(sw).bind(p -> p.getGroups().contains(g), (bean, fieldvalue) -> { + if (fieldvalue) { + bean.getGroups().add(g); + } else { + bean.getGroups().remove(g); + } + }); + } + return layout; } public void setPersonChangeHandler(Consumer personChangeHandler) { @@ -130,12 +159,27 @@ public class PersonEditDetails extends HorizontalLayout { if (person != null) { okButton.setEnabled(true); + updateRelationshipBinding(); } else { okButton.setEnabled(false); + contactLayout.removeAllComponents(); + relationshipLayout.removeAllComponents(); + } + } + + private void updateRelationshipBinding() { + relationshipLayout.removeAllComponents(); + Person current = binder.getBean(); + List related = dao.findRelationsFor(current); + for (Relation relation : related) { + TextField textField = new TextField(relation.getRelation().getLocalized()); + textField.setValue(relation.getPerson().getPrename() + " " + relation.getPerson().getSurname()); + textField.setEnabled(false); + relationshipLayout.addComponent(textField); } } - private void closeWithoutSave(PersonDao dao) { + private void closeWithoutSave() { if (binder.hasChanges()) { ConfirmDialog dlg = ConfirmDialog.builder().setCaption("Ungespeicherte Änderungen") diff --git a/src/test/java/de/kreth/vaadin/clubhelper/HibernateHolder.java b/src/test/java/de/kreth/vaadin/clubhelper/HibernateHolder.java index bfe55a4..9e19bb6 100644 --- a/src/test/java/de/kreth/vaadin/clubhelper/HibernateHolder.java +++ b/src/test/java/de/kreth/vaadin/clubhelper/HibernateHolder.java @@ -12,7 +12,7 @@ public enum HibernateHolder { private HibernateHolder() { configuration = new Configuration(); - HibernateConfiguration config = new H2MemoryConfiguration(); + HibernateConfiguration config = new MysqlLocalConfiguration(); config.configure(configuration); } diff --git a/src/test/java/de/kreth/vaadin/clubhelper/MysqlLocalConfiguration.java b/src/test/java/de/kreth/vaadin/clubhelper/MysqlLocalConfiguration.java index ce4a4e6..3a6d863 100644 --- a/src/test/java/de/kreth/vaadin/clubhelper/MysqlLocalConfiguration.java +++ b/src/test/java/de/kreth/vaadin/clubhelper/MysqlLocalConfiguration.java @@ -22,7 +22,7 @@ public class MysqlLocalConfiguration extends AbstractHibernateConfiguration { } public String getUrl() { - return "jdbc:mysql://localhost/test?useUnicode=yes&characterEncoding=utf8"; + return "jdbc:mysql://localhost/clubhelper?useUnicode=yes&characterEncoding=utf8&serverTimezone=UTC&useSSL=FALSE"; } } diff --git a/src/test/java/de/kreth/vaadin/clubhelper/MysqlTestConfiguration.java b/src/test/java/de/kreth/vaadin/clubhelper/MysqlTestConfiguration.java index febc90a..ae8fc85 100644 --- a/src/test/java/de/kreth/vaadin/clubhelper/MysqlTestConfiguration.java +++ b/src/test/java/de/kreth/vaadin/clubhelper/MysqlTestConfiguration.java @@ -4,7 +4,7 @@ public class MysqlTestConfiguration extends MysqlLocalConfiguration { @Override public String getUrl() { - return "jdbc:mysql://localhost/clubhelper?useUnicode=yes&characterEncoding=utf8"; + return "jdbc:mysql://localhost/test?useUnicode=yes&characterEncoding=utf8&serverTimezone=UTC&useSSL=FALSE"; } @Override diff --git a/src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/dao/PersonDaoRelativeTest.java b/src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/dao/PersonDaoRelativeTest.java new file mode 100644 index 0000000..834c663 --- /dev/null +++ b/src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/dao/PersonDaoRelativeTest.java @@ -0,0 +1,84 @@ +package de.kreth.vaadin.clubhelper.vaadinclubhelper.dao; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; + +import java.io.IOException; +import java.sql.ResultSet; +import java.sql.Statement; +import java.util.List; + +import javax.persistence.EntityManager; + +import org.hibernate.Session; +import org.hibernate.SessionFactory; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.ContextConfiguration; + +import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Person; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.tests.TestConfiguration; + +@SpringBootTest +@ContextConfiguration(classes = TestConfiguration.class) +@Disabled +public class PersonDaoRelativeTest { + + @Autowired + private PersonDao personDao; + + @Autowired + private EntityManager entityManager; + + @Autowired + private TestDatabaseHelper testDatabaseHelper; + + @Autowired + private SessionFactory sessionFactory; + + private Person person1; + private Person person2; + + @BeforeEach + public void setUp() throws Exception { +// testDatabaseHelper.cleanDatabase(); +// List persons = TestPersonGenerator.generatePersonen(2); +// person1 = persons.get(0); +// person2 = persons.get(1); + + List resultList = entityManager.createNamedQuery(Person.QUERY_FINDALL, Person.class).getResultList(); + assertFalse(resultList.isEmpty()); + } + + @AfterEach + public void clearDatabase() throws IOException { +// testDatabaseHelper.cleanDatabase(); + } + + @Test + void testLoadPersonById() { + person1 = entityManager.find(Person.class, Integer.valueOf(1)); + assertNotNull(person1); + assertEquals(1, person1.getId()); + } + + @Test + void testStorePersonChildRelationship() { + Session session = sessionFactory.openSession(); + session.doWork(conn -> { + try (Statement stm = conn.createStatement()) { + ResultSet rs = stm.executeQuery( + "SELECT id, person1, person2, TO_PERSON1_RELATION, TO_PERSON2_RELATION, changed, created, deleted FROM relative where person1=1 OR person2=1 and deleted is null"); + while (rs.next()) { + System.out.println("id=" + rs.getInt("id") + ", person1=" + rs.getInt("person1") + ", person2=" + + rs.getInt("person2") + ", TO_PERSON1_RELATION=" + rs.getString("TO_PERSON1_RELATION")); + } + } + }); + } +} diff --git a/src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/tests/TestConfiguration.java b/src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/tests/TestConfiguration.java index 4ccde1b..dcf939a 100644 --- a/src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/tests/TestConfiguration.java +++ b/src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/tests/TestConfiguration.java @@ -31,4 +31,8 @@ public class TestConfiguration { return sessionFactory.openSession(); } + @Bean + public SessionFactory sessionFactory() { + return sessionFactory; + } }