PersonGrid refactoring: Filter extraction started.

master
Markus Kreth 7 years ago
parent 4843869e6f
commit a8c886d40e
  1. 56
      src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/PersonFilter.java
  2. 15
      src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/PersonGrid.java
  3. 69
      src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/PersonFilterTest.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<Person> {
private static final long serialVersionUID = -8481035921020651601L;
private Set<Integer> selectedPersons = null;
private Set<GroupDef> 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<Person> selected) {
if (selected == null) {
selectedPersons = null;
return;
}
selectedPersons = new HashSet<>();
for (Person p : selected) {
selectedPersons.add(p.getId());
}
}
}

@ -83,6 +83,7 @@ public class PersonGrid extends CustomComponent {
HorizontalLayout filters = new HorizontalLayout(); HorizontalLayout filters = new HorizontalLayout();
filters.addComponents(checkIncluded, comboGroups); filters.addComponents(checkIncluded, comboGroups);
dataProvider = new ListDataProvider<Person>(new ArrayList<>()).withConfigurableFilter(); dataProvider = new ListDataProvider<Person>(new ArrayList<>()).withConfigurableFilter();
grid = new Grid<>(); grid = new Grid<>();
grid.setDataProvider(dataProvider); grid.setDataProvider(dataProvider);
grid.setId("person.grid"); grid.setId("person.grid");
@ -140,15 +141,13 @@ public class PersonGrid extends CustomComponent {
}); });
filter = filter.and(p -> { filter = filter.and(p -> {
{ List<GroupDef> personGroups = p.getPersongroups();
List<GroupDef> personGroups = p.getPersongroups(); for (GroupDef pg : personGroups) {
for (GroupDef pg : personGroups) { if (groupIds.contains(pg.getId())) {
if (groupIds.contains(pg.getId())) { return true;
return true;
}
} }
return false;
} }
return false;
}); });
} }
@ -157,7 +156,7 @@ public class PersonGrid extends CustomComponent {
public void setFilter(Predicate<Person> filter) { public void setFilter(Predicate<Person> filter) {
dataProvider.setFilter(p -> filter.test(p)); dataProvider.setFilter(p -> filter.test(p));
grid.getDataProvider().refreshAll(); dataProvider.refreshAll();
} }
public void onPersonSelect(SelectionListener<Person> listener) throws UnsupportedOperationException { public void onPersonSelect(SelectionListener<Person> listener) throws UnsupportedOperationException {

@ -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<Person> 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<Person> 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));
}
}
}
Loading…
Cancel
Save