diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/data/Person.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/data/Person.java index e6299cb..6c1b907 100644 --- a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/data/Person.java +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/data/Person.java @@ -28,7 +28,7 @@ import javax.persistence.Table; @NamedQuery(name = Person.QUERY_FINDALL, query = "SELECT p FROM Person p") public class Person extends BaseEntity implements Serializable { - public final static String QUERY_FINDALL = "Person.findAll"; + public static final String QUERY_FINDALL = "Person.findAll"; private static final long serialVersionUID = -8361264400619997123L; @@ -305,6 +305,76 @@ public class Person extends BaseEntity implements Serializable { return startpaess; } + @Override + public int hashCode() { + final int prime = 31; + int result = super.hashCode(); + result = prime * result + ((birth == null) ? 0 : birth.hashCode()); + result = prime * result + ((groups == null) ? 0 : groups.hashCode()); + result = prime * result + ((password == null) ? 0 : password.hashCode()); + result = prime * result + ((prename == null) ? 0 : prename.hashCode()); + result = prime * result + ((surname == null) ? 0 : surname.hashCode()); + result = prime * result + ((username == null) ? 0 : username.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (!super.equals(obj)) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + Person other = (Person) obj; + if (birth == null) { + if (other.birth != null) { + return false; + } + } else if (!birth.equals(other.birth)) { + return false; + } + if (groups == null) { + if (other.groups != null) { + return false; + } + } else if (!groups.equals(other.groups)) { + return false; + } + if (password == null) { + if (other.password != null) { + return false; + } + } else if (!password.equals(other.password)) { + return false; + } + if (prename == null) { + if (other.prename != null) { + return false; + } + } else if (!prename.equals(other.prename)) { + return false; + } + if (surname == null) { + if (other.surname != null) { + return false; + } + } else if (!surname.equals(other.surname)) { + return false; + } + if (username == null) { + if (other.username != null) { + return false; + } + } else if (!username.equals(other.username)) { + return false; + } + return true; + } + @Override public String toString() { return "Person [id=" + getId() + ", prename=" + prename + ", surname=" + surname + "]"; diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/MainUi.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/MainUi.java index 415bff5..385897d 100644 --- a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/MainUi.java +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/MainUi.java @@ -56,10 +56,8 @@ public class MainUi extends UI { LOGGER.debug("Starting Vaadin UI with {}", getClass().getName()); - List persons = personDao.listAll(); - personGrid = new PersonGrid(groupDao); + personGrid = new PersonGrid(groupDao, personDao); personGrid.setId("main.person"); - personGrid.setItems(persons); personGrid.setCaption("Personen"); personGrid.onClosedFunction(() -> detailClosed()); personGrid.onPersonSelect(ev -> personSelectionChange(ev)); diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/PersonFilter.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/PersonFilter.java index 57908c1..c2e4a5c 100644 --- a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/PersonFilter.java +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/PersonFilter.java @@ -1,19 +1,32 @@ package de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components; -import java.util.Collections; +import java.util.ArrayList; +import java.util.Collection; import java.util.HashSet; +import java.util.List; import java.util.Set; +import java.util.stream.Collectors; +import com.vaadin.data.provider.DataChangeEvent; +import com.vaadin.data.provider.DataProviderListener; import com.vaadin.server.SerializablePredicate; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.dao.PersonDao; import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.GroupDef; import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Person; -public class PersonFilter implements SerializablePredicate { +public class PersonFilter implements SerializablePredicate, DataProviderListener { private static final long serialVersionUID = -8481035921020651601L; private Set selectedPersons = null; private Set selectedGroups = null; + private final List publishedList; + private final PersonDao personDao; + + public PersonFilter(PersonDao personDao) { + this.personDao = personDao; + publishedList = new ArrayList<>(personDao.listAll()); + } @Override public boolean test(Person t) { @@ -32,11 +45,23 @@ public class PersonFilter implements SerializablePredicate { private boolean personInGroup(Person t) { if (selectedGroups != null) { - return t.getGroups() != null && !Collections.disjoint(t.getGroups(), selectedGroups); + return t.getGroups() != null && haveCommonGroup(t.getGroups(), selectedGroups); } return true; } + private boolean haveCommonGroup(Set groups, Set selectedGroups2) { + + for (GroupDef g1 : groups) { + for (GroupDef g2 : selectedGroups2) { + if (g1.getId() == g2.getId()) { + return true; + } + } + } + return false; + } + private boolean personSelected(Person t) { if (selectedPersons != null) { if (selectedPersons.contains(t.getId()) == false) { @@ -61,4 +86,14 @@ public class PersonFilter implements SerializablePredicate { this.selectedGroups = selected; } + public Collection asCollection() { + return publishedList; + } + + @Override + public void onDataChange(DataChangeEvent event) { + publishedList.clear(); + publishedList.addAll(personDao.listAll().stream().filter(this).collect(Collectors.toList())); + } + } 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 f9efa47..0135971 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 @@ -2,25 +2,20 @@ package de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components; import java.time.format.DateTimeFormatter; import java.time.format.FormatStyle; -import java.util.ArrayList; import java.util.Arrays; -import java.util.Collection; -import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.function.Consumer; -import java.util.function.Predicate; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.vaadin.data.HasValue.ValueChangeEvent; -import com.vaadin.data.provider.ConfigurableFilterDataProvider; +import com.vaadin.data.provider.DataProvider; import com.vaadin.data.provider.ListDataProvider; import com.vaadin.event.selection.SelectionListener; import com.vaadin.event.selection.SingleSelectionEvent; import com.vaadin.icons.VaadinIcons; -import com.vaadin.server.SerializablePredicate; import com.vaadin.ui.Button; import com.vaadin.ui.CheckBox; import com.vaadin.ui.ComboBox; @@ -34,6 +29,7 @@ import com.vaadin.ui.VerticalLayout; import com.vaadin.ui.themes.ValoTheme; import de.kreth.vaadin.clubhelper.vaadinclubhelper.dao.GroupDao; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.dao.PersonDao; import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.ClubEvent; import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.GroupDef; import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Person; @@ -44,7 +40,7 @@ public class PersonGrid extends CustomComponent { private final transient Logger log = LoggerFactory.getLogger(getClass()); private final transient DateTimeFormatter birthFormat = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM); - private final ConfigurableFilterDataProvider> dataProvider; + private final ListDataProvider dataProvider; private final Grid grid; private final CheckBox checkIncluded; @@ -57,8 +53,9 @@ public class PersonGrid extends CustomComponent { private Set groupMemberFilter; private List allGroups; + private PersonFilter filter; - public PersonGrid(GroupDao groupDao) { + public PersonGrid(GroupDao groupDao, PersonDao personDao) { textTitle = new TextField(); textTitle.setId("event.title"); @@ -82,7 +79,10 @@ public class PersonGrid extends CustomComponent { HorizontalLayout filters = new HorizontalLayout(); filters.addComponents(checkIncluded, comboGroups); - dataProvider = new ListDataProvider(new ArrayList<>()).withConfigurableFilter(); + + filter = new PersonFilter(personDao); + dataProvider = DataProvider.ofCollection(filter.asCollection()); + dataProvider.addDataProviderListener(filter); grid = new Grid<>(); grid.setDataProvider(dataProvider); @@ -129,33 +129,14 @@ public class PersonGrid extends CustomComponent { } private void updateFilter() { - Predicate filter = p -> true; if (selectedOnlyFilter != null && selectedOnlyFilter.equals(Boolean.TRUE)) { - Set selected = grid.getSelectedItems(); - filter = p -> selected.contains(p); - } - if (groupMemberFilter != null && groupMemberFilter.isEmpty() == false) { - final Set groupIds = new HashSet<>(); - groupMemberFilter.forEach(gm -> { - groupIds.add(gm.getId()); - }); - - filter = filter.and(p -> { - Set personGroups = p.getPersongroups(); - for (GroupDef pg : personGroups) { - if (groupIds.contains(pg.getId())) { - return true; - } - } - return false; - }); + filter.setSelectedPersons(grid.getSelectedItems()); + } else { + filter.setSelectedPersons(null); } - setFilter(filter); - } + filter.setSelectedGroups(groupMemberFilter); - public void setFilter(Predicate filter) { - dataProvider.setFilter(p -> filter.test(p)); dataProvider.refreshAll(); } @@ -193,6 +174,9 @@ public class PersonGrid extends CustomComponent { private void onGroupSelected(SingleSelectionEvent ev) { groupMemberFilter = ev.getAllSelectedItems(); + if (groupMemberFilter.isEmpty()) { + groupMemberFilter = null; + } updateFilter(); } @@ -203,14 +187,6 @@ public class PersonGrid extends CustomComponent { textTitle.setValue(value); } - public void setItems(Collection items) { - grid.setItems(items); - } - - public void setItems(Person... items) { - grid.setItems(items); - } - public interface ClosedFunction { void closed(); } diff --git a/src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/PersonFilterTest.java b/src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/PersonFilterTest.java index 1a859ff..2cd8dee 100644 --- a/src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/PersonFilterTest.java +++ b/src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/PersonFilterTest.java @@ -3,6 +3,7 @@ package de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.when; import java.util.Arrays; import java.util.Collections; @@ -12,8 +13,11 @@ import java.util.Set; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; import de.kreth.vaadin.clubhelper.vaadinclubhelper.dao.AbstractDatabaseTest; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.dao.PersonDao; import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.GroupDef; import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Person; @@ -22,6 +26,8 @@ class PersonFilterTest { private PersonFilter filter; private List persons; private final List groups; + @Mock + private PersonDao dao; public PersonFilterTest() { GroupDef adminGroup = new GroupDef(); @@ -47,10 +53,12 @@ class PersonFilterTest { @BeforeEach void setUp() throws Exception { - filter = new PersonFilter(); - persons = AbstractDatabaseTest.createPersons(5); + MockitoAnnotations.initMocks(this); + filter = new PersonFilter(dao); + persons = Collections.unmodifiableList(AbstractDatabaseTest.createPersons(5)); assertEquals(5, persons.size()); + when(dao.listAll()).thenReturn(persons); for (int i = 0; i < groups.size(); i++) { persons.get(i).add(groups.get(i)); persons.get(0).add(groups.get(i)); // Person1 has all Rights. @@ -99,6 +107,11 @@ class PersonFilterTest { assertFalse(filter.test(persons.get(3))); assertFalse(filter.test(persons.get(4))); + selected.add(groups.get(3)); + assertTrue(filter.test(persons.get(3))); + assertFalse(filter.test(persons.get(1))); + assertFalse(filter.test(persons.get(4))); + } public void allPersonsAccepted() {