Manual Filter working.

master
Markus Kreth 7 years ago
parent e68aa3f43d
commit 00b62e4167
  1. 72
      src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/data/Person.java
  2. 4
      src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/MainUi.java
  3. 41
      src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/PersonFilter.java
  4. 56
      src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/PersonGrid.java
  5. 17
      src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/components/PersonFilterTest.java

@ -28,7 +28,7 @@ import javax.persistence.Table;
@NamedQuery(name = Person.QUERY_FINDALL, query = "SELECT p FROM Person p") @NamedQuery(name = Person.QUERY_FINDALL, query = "SELECT p FROM Person p")
public class Person extends BaseEntity implements Serializable { 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; private static final long serialVersionUID = -8361264400619997123L;
@ -305,6 +305,76 @@ public class Person extends BaseEntity implements Serializable {
return startpaess; 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 @Override
public String toString() { public String toString() {
return "Person [id=" + getId() + ", prename=" + prename + ", surname=" + surname + "]"; return "Person [id=" + getId() + ", prename=" + prename + ", surname=" + surname + "]";

@ -56,10 +56,8 @@ public class MainUi extends UI {
LOGGER.debug("Starting Vaadin UI with {}", getClass().getName()); LOGGER.debug("Starting Vaadin UI with {}", getClass().getName());
List<Person> persons = personDao.listAll(); personGrid = new PersonGrid(groupDao, personDao);
personGrid = new PersonGrid(groupDao);
personGrid.setId("main.person"); personGrid.setId("main.person");
personGrid.setItems(persons);
personGrid.setCaption("Personen"); personGrid.setCaption("Personen");
personGrid.onClosedFunction(() -> detailClosed()); personGrid.onClosedFunction(() -> detailClosed());
personGrid.onPersonSelect(ev -> personSelectionChange(ev)); personGrid.onPersonSelect(ev -> personSelectionChange(ev));

@ -1,19 +1,32 @@
package de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components; 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.HashSet;
import java.util.List;
import java.util.Set; 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 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.GroupDef;
import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Person; import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Person;
public class PersonFilter implements SerializablePredicate<Person> { public class PersonFilter implements SerializablePredicate<Person>, DataProviderListener<Person> {
private static final long serialVersionUID = -8481035921020651601L; private static final long serialVersionUID = -8481035921020651601L;
private Set<Integer> selectedPersons = null; private Set<Integer> selectedPersons = null;
private Set<GroupDef> selectedGroups = null; private Set<GroupDef> selectedGroups = null;
private final List<Person> publishedList;
private final PersonDao personDao;
public PersonFilter(PersonDao personDao) {
this.personDao = personDao;
publishedList = new ArrayList<>(personDao.listAll());
}
@Override @Override
public boolean test(Person t) { public boolean test(Person t) {
@ -32,11 +45,23 @@ public class PersonFilter implements SerializablePredicate<Person> {
private boolean personInGroup(Person t) { private boolean personInGroup(Person t) {
if (selectedGroups != null) { if (selectedGroups != null) {
return t.getGroups() != null && !Collections.disjoint(t.getGroups(), selectedGroups); return t.getGroups() != null && haveCommonGroup(t.getGroups(), selectedGroups);
} }
return true; return true;
} }
private boolean haveCommonGroup(Set<GroupDef> groups, Set<GroupDef> selectedGroups2) {
for (GroupDef g1 : groups) {
for (GroupDef g2 : selectedGroups2) {
if (g1.getId() == g2.getId()) {
return true;
}
}
}
return false;
}
private boolean personSelected(Person t) { private boolean personSelected(Person t) {
if (selectedPersons != null) { if (selectedPersons != null) {
if (selectedPersons.contains(t.getId()) == false) { if (selectedPersons.contains(t.getId()) == false) {
@ -61,4 +86,14 @@ public class PersonFilter implements SerializablePredicate<Person> {
this.selectedGroups = selected; this.selectedGroups = selected;
} }
public Collection<Person> asCollection() {
return publishedList;
}
@Override
public void onDataChange(DataChangeEvent<Person> event) {
publishedList.clear();
publishedList.addAll(personDao.listAll().stream().filter(this).collect(Collectors.toList()));
}
} }

@ -2,25 +2,20 @@ package de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle; import java.time.format.FormatStyle;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.Predicate;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import com.vaadin.data.HasValue.ValueChangeEvent; 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.data.provider.ListDataProvider;
import com.vaadin.event.selection.SelectionListener; import com.vaadin.event.selection.SelectionListener;
import com.vaadin.event.selection.SingleSelectionEvent; import com.vaadin.event.selection.SingleSelectionEvent;
import com.vaadin.icons.VaadinIcons; import com.vaadin.icons.VaadinIcons;
import com.vaadin.server.SerializablePredicate;
import com.vaadin.ui.Button; import com.vaadin.ui.Button;
import com.vaadin.ui.CheckBox; import com.vaadin.ui.CheckBox;
import com.vaadin.ui.ComboBox; import com.vaadin.ui.ComboBox;
@ -34,6 +29,7 @@ import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.themes.ValoTheme; import com.vaadin.ui.themes.ValoTheme;
import de.kreth.vaadin.clubhelper.vaadinclubhelper.dao.GroupDao; 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.ClubEvent;
import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.GroupDef; import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.GroupDef;
import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Person; 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 Logger log = LoggerFactory.getLogger(getClass());
private final transient DateTimeFormatter birthFormat = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM); private final transient DateTimeFormatter birthFormat = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM);
private final ConfigurableFilterDataProvider<Person, Void, SerializablePredicate<Person>> dataProvider; private final ListDataProvider<Person> dataProvider;
private final Grid<Person> grid; private final Grid<Person> grid;
private final CheckBox checkIncluded; private final CheckBox checkIncluded;
@ -57,8 +53,9 @@ public class PersonGrid extends CustomComponent {
private Set<GroupDef> groupMemberFilter; private Set<GroupDef> groupMemberFilter;
private List<GroupDef> allGroups; private List<GroupDef> allGroups;
private PersonFilter filter;
public PersonGrid(GroupDao groupDao) { public PersonGrid(GroupDao groupDao, PersonDao personDao) {
textTitle = new TextField(); textTitle = new TextField();
textTitle.setId("event.title"); textTitle.setId("event.title");
@ -82,7 +79,10 @@ 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();
filter = new PersonFilter(personDao);
dataProvider = DataProvider.ofCollection(filter.asCollection());
dataProvider.addDataProviderListener(filter);
grid = new Grid<>(); grid = new Grid<>();
grid.setDataProvider(dataProvider); grid.setDataProvider(dataProvider);
@ -129,33 +129,14 @@ public class PersonGrid extends CustomComponent {
} }
private void updateFilter() { private void updateFilter() {
Predicate<Person> filter = p -> true;
if (selectedOnlyFilter != null && selectedOnlyFilter.equals(Boolean.TRUE)) { if (selectedOnlyFilter != null && selectedOnlyFilter.equals(Boolean.TRUE)) {
Set<Person> selected = grid.getSelectedItems(); filter.setSelectedPersons(grid.getSelectedItems());
filter = p -> selected.contains(p); } else {
} filter.setSelectedPersons(null);
if (groupMemberFilter != null && groupMemberFilter.isEmpty() == false) {
final Set<Integer> groupIds = new HashSet<>();
groupMemberFilter.forEach(gm -> {
groupIds.add(gm.getId());
});
filter = filter.and(p -> {
Set<GroupDef> personGroups = p.getPersongroups();
for (GroupDef pg : personGroups) {
if (groupIds.contains(pg.getId())) {
return true;
}
}
return false;
});
} }
setFilter(filter); filter.setSelectedGroups(groupMemberFilter);
}
public void setFilter(Predicate<Person> filter) {
dataProvider.setFilter(p -> filter.test(p));
dataProvider.refreshAll(); dataProvider.refreshAll();
} }
@ -193,6 +174,9 @@ public class PersonGrid extends CustomComponent {
private void onGroupSelected(SingleSelectionEvent<GroupDef> ev) { private void onGroupSelected(SingleSelectionEvent<GroupDef> ev) {
groupMemberFilter = ev.getAllSelectedItems(); groupMemberFilter = ev.getAllSelectedItems();
if (groupMemberFilter.isEmpty()) {
groupMemberFilter = null;
}
updateFilter(); updateFilter();
} }
@ -203,14 +187,6 @@ public class PersonGrid extends CustomComponent {
textTitle.setValue(value); textTitle.setValue(value);
} }
public void setItems(Collection<Person> items) {
grid.setItems(items);
}
public void setItems(Person... items) {
grid.setItems(items);
}
public interface ClosedFunction { public interface ClosedFunction {
void closed(); void closed();
} }

@ -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.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.when;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
@ -12,8 +13,11 @@ import java.util.Set;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; 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.AbstractDatabaseTest;
import de.kreth.vaadin.clubhelper.vaadinclubhelper.dao.PersonDao;
import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.GroupDef; import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.GroupDef;
import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Person; import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Person;
@ -22,6 +26,8 @@ class PersonFilterTest {
private PersonFilter filter; private PersonFilter filter;
private List<Person> persons; private List<Person> persons;
private final List<GroupDef> groups; private final List<GroupDef> groups;
@Mock
private PersonDao dao;
public PersonFilterTest() { public PersonFilterTest() {
GroupDef adminGroup = new GroupDef(); GroupDef adminGroup = new GroupDef();
@ -47,10 +53,12 @@ class PersonFilterTest {
@BeforeEach @BeforeEach
void setUp() throws Exception { void setUp() throws Exception {
filter = new PersonFilter(); MockitoAnnotations.initMocks(this);
persons = AbstractDatabaseTest.createPersons(5); filter = new PersonFilter(dao);
persons = Collections.unmodifiableList(AbstractDatabaseTest.createPersons(5));
assertEquals(5, persons.size()); assertEquals(5, persons.size());
when(dao.listAll()).thenReturn(persons);
for (int i = 0; i < groups.size(); i++) { for (int i = 0; i < groups.size(); i++) {
persons.get(i).add(groups.get(i)); persons.get(i).add(groups.get(i));
persons.get(0).add(groups.get(i)); // Person1 has all Rights. 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(3)));
assertFalse(filter.test(persons.get(4))); 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() { public void allPersonsAccepted() {

Loading…
Cancel
Save