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 new file mode 100644 index 0000000..66d7f92 --- /dev/null +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/PersonFilter.java @@ -0,0 +1,56 @@ +package de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components; + +import java.util.HashSet; +import java.util.Set; + +import com.vaadin.server.SerializablePredicate; + +import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.GroupDef; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Person; + +public class PersonFilter implements SerializablePredicate { + + private static final long serialVersionUID = -8481035921020651601L; + private Set selectedPersons = null; + private Set selectedGroups = null; + + @Override + public boolean test(Person t) { + if (selectedGroups == null && selectedPersons == null) { + return true; + } + + if (personSelected(t) == false) { + return false; + } +// if (personInGroup(t) == false) { +// return false; +// } + return true; + } + + private boolean personInGroup(Person t) { + return selectedGroups != null; + } + + private boolean personSelected(Person t) { + if (selectedPersons != null) { + if (selectedPersons.contains(t.getId()) == false) { + return false; + } + } + return true; + } + + public void setSelected(Set selected) { + if (selected == null) { + selectedPersons = null; + return; + } + selectedPersons = new HashSet<>(); + for (Person p : selected) { + selectedPersons.add(p.getId()); + } + } + +} 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 2ae1872..6157cff 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 @@ -83,6 +83,7 @@ public class PersonGrid extends CustomComponent { HorizontalLayout filters = new HorizontalLayout(); filters.addComponents(checkIncluded, comboGroups); dataProvider = new ListDataProvider(new ArrayList<>()).withConfigurableFilter(); + grid = new Grid<>(); grid.setDataProvider(dataProvider); grid.setId("person.grid"); @@ -140,15 +141,13 @@ public class PersonGrid extends CustomComponent { }); filter = filter.and(p -> { - { - List personGroups = p.getPersongroups(); - for (GroupDef pg : personGroups) { - if (groupIds.contains(pg.getId())) { - return true; - } + List personGroups = p.getPersongroups(); + for (GroupDef pg : personGroups) { + if (groupIds.contains(pg.getId())) { + return true; } - return false; } + return false; }); } @@ -157,7 +156,7 @@ public class PersonGrid extends CustomComponent { public void setFilter(Predicate filter) { dataProvider.setFilter(p -> filter.test(p)); - grid.getDataProvider().refreshAll(); + dataProvider.refreshAll(); } public void onPersonSelect(SelectionListener listener) throws UnsupportedOperationException { 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 new file mode 100644 index 0000000..51b119b --- /dev/null +++ b/src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/PersonFilterTest.java @@ -0,0 +1,69 @@ +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 java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import de.kreth.vaadin.clubhelper.vaadinclubhelper.dao.AbstractDatabaseTest; +import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Person; + +class PersonFilterTest { + + private PersonFilter filter; + private List persons; + + @BeforeEach + void setUp() throws Exception { + filter = new PersonFilter(); + persons = AbstractDatabaseTest.createPersons(5); + assertEquals(5, persons.size()); + } + + @Test + void testNoFilterSet() { + allAccepted(); + + assertTrue(filter.test(null)); + } + + @Test + void testSelectionFilter() { + Set selected = new HashSet<>(persons.subList(2, 4)); + filter.setSelected(selected); + assertFalse(filter.test(persons.get(0))); + assertFalse(filter.test(persons.get(1))); + assertFalse(filter.test(persons.get(4))); + assertTrue(filter.test(persons.get(2))); + assertTrue(filter.test(persons.get(3))); + + filter.setSelected(null); + testNoFilterSet(); + + filter.setSelected(Collections.emptySet()); + allFiltered(); + + filter.setSelected(new HashSet<>(persons)); + allAccepted(); + } + + public void allAccepted() { + for (Person p : persons) { + assertTrue(filter.test(p)); + } + } + + public void allFiltered() { + for (Person p : persons) { + assertFalse(filter.test(p)); + } + } + +}