I want to show the values of the ausstattung object as concatenated strings in the grid. Like (Beamer, Computer, Board) ausstattung is german for equipment. I have a many to many jpa relationsship between room and ausstattung. How can i render the data as String?
public class RoomDataProvider extends AbstractBackEndDataProvider<Room, CrudFilter> {
private final RoomService roomService;
private List<Room> rooms;
private Consumer<Long> sizeChangeListener;
public RoomDataProvider(RoomService roomService) {
this.roomService = roomService;
rooms = new ArrayList<>(roomService.findAll());
}
@Override
protected Stream<Room> fetchFromBackEnd(Query<Room, CrudFilter> query) {
int offset = query.getOffset();
int limit = query.getLimit();
Stream<Room> stream = rooms.stream();
if (query.getFilter().isPresent()) {
stream = stream.filter(predicate(query.getFilter().get()))
.sorted(comparator(query.getFilter().get()));
}
return stream.skip(offset).limit(limit);
}
@Override
protected int sizeInBackEnd(Query<Room, CrudFilter> query) {
long count = fetchFromBackEnd(query).count();
if (sizeChangeListener != null) {
sizeChangeListener.accept(count);
}
return (int) count;
}
void setSizeChangeListener(Consumer<Long> listener) {
sizeChangeListener = listener;
}
private static Predicate<Room> predicate(CrudFilter filter) {
return filter.getConstraints().entrySet().stream()
.map(constraint -> (Predicate<Room>) room -> {
try {
Object value = valueOf(constraint.getKey(), room);
return value != null && value.toString().toLowerCase()
.contains(constraint.getValue().toLowerCase());
} catch (Exception e) {
e.printStackTrace();
return false;
}
}).reduce(Predicate::and).orElse(e -> true);
}
private static Comparator<Room> comparator(CrudFilter filter) {
return filter.getSortOrders().entrySet().stream().map(sortClause -> {
try {
Comparator<Room> comparator = Comparator.comparing(
person -> (Comparable) valueOf(sortClause.getKey(),
person));
if (sortClause.getValue() == SortDirection.DESCENDING) {
comparator = comparator.reversed();
}
return comparator;
} catch (Exception ex) {
return (Comparator<Room>) (o1, o2) -> 0;
}
}).reduce(Comparator::thenComparing).orElse((o1, o2) -> 0);
}
private static Object valueOf(String fieldName, Room room) {
try {
Field field = Room.class.getDeclaredField(fieldName);
field.setAccessible(true);
return field.get(room);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
public void save(Room room) {
roomService.save(room);
rooms = roomService.findAll();
}
Optional<Room> find(String refNr) {
return Optional.of(roomService.findByRefNr(refNr));
}
public void delete(Room room) {
roomService.delete(room);
rooms = roomService.findAll();
}
}
@Route(value = "room-crud", layout = MainLayout.class)
@RouteAlias(value = "", layout = MainLayout.class)
@RolesAllowed("ADMIN")
@Uses(Icon.class)
@PageTitle("Räume")
public class RoomCrud extends Div {
private final AusstattungService ausstattungService;
private final RoomService roomService;
private Crud<Room> crud;
private String FACHBEREICH = "fachbereich";
private String POSITION = "position";
private String TYP = "typ";
private String CAPACITY = "capacity";
private String AUSSTATTUNG = "ausstattung";
private String REFNR = "refNr";
private String EDIT_COLUMN = "vaadin-crud-edit-column";
public RoomCrud(AusstattungService ausstattungService, RoomService roomService) {
this.ausstattungService = ausstattungService;
this.roomService = roomService;
crud = new Crud<>(Room.class, createEditor());
setupGrid();
setupDataProvider();
setupLanguage();
add(crud);
}
private CrudEditor<Room> createEditor() {
TextField refNr = new TextField("ReferenzBezeichnung");
IntegerField kapa = new IntegerField("Kapazität");
MultiSelectComboBox<Ausstattung> ausstattung = new MultiSelectComboBox<>("Ausstattung");
ausstattung.setItems(ausstattungService.findAll());
ausstattung.setItemLabelGenerator(Ausstattung::getBez);
ComboBox<String> typ = new ComboBox<>("Raumtyp");
typ.setItems(Arrays.stream(Raumtyp.values()).map(Raumtyp::getAnzeigeName).collect(Collectors.toList()));
ComboBox<String> fachbereich = new ComboBox<>("Fachbereich");
fachbereich.setRequired(true);
fachbereich.setItems(Arrays.stream(Fachbereich.values()).map(Fachbereich::getAnzeigeName).collect(Collectors.toList()));
TextField position = new TextField("Position");
FormLayout form = new FormLayout(refNr, kapa, ausstattung, typ, fachbereich, position);
Binder<Room> binder = new Binder<>(Room.class);
binder.forField(kapa).asRequired().bind(Room::getCapacity, Room::setCapacity);
binder.forField(ausstattung).bind(Room::getAusstattung, Room::setAusstattung);
binder.forField(typ).asRequired().bind(Room::getTyp, Room::setTyp);
binder.forField(fachbereich).asRequired().bind(Room::getFachbereich, Room::setFachbereich);
binder.forField(position).asRequired().bind(Room::getPosition, Room::setPosition);
binder.forField(refNr).asRequired()
.withValidator(refNrValue -> refNrValue.matches("^[a-zA-Z]{1}.{0,3}$"),
"ReferenzBezeichnung muss mit einem Buchstaben anfangen und darf maximal 4 Zeichen lang sein")
.bind(Room::getRefNr, Room::setRefNr);
return new BinderCrudEditor<>(binder, form);
}
private void setupGrid() {
Grid<Room> grid = crud.getGrid();
// Only show these columns (all columns shown by default):
// List<String> visibleColumns = Arrays.asList(FACHBEREICH, POSITION, TYP, CAPACITY, AUSSTATTUNG, EDIT_COLUMN);
// grid.getColumns().forEach(column -> {
// String key = column.getKey();
// if (!visibleColumns.contains(key)) {
// grid.removeColumn(column);
// }
// });
grid.removeColumn(grid.getColumnByKey("id"));
grid.getColumnByKey(CAPACITY).setHeader("Kapazität");
// Reorder the columns (alphabetical by default)
grid.setColumnOrder(grid.getColumnByKey(REFNR),
grid.getColumnByKey(FACHBEREICH),
grid.getColumnByKey(AUSSTATTUNG),
grid.getColumnByKey(TYP),
grid.getColumnByKey(CAPACITY),
grid.getColumnByKey(POSITION),
grid.getColumnByKey(EDIT_COLUMN));
}
private void setupDataProvider() {
RoomDataProvider dataProvider = new RoomDataProvider(roomService);
crud.setDataProvider(dataProvider);
crud.addDeleteListener(deleteEvent -> {
dataProvider.delete(deleteEvent.getItem());
dataProvider.refreshAll();
});
crud.addSaveListener(saveEvent -> {
dataProvider.save(saveEvent.getItem());
dataProvider.refreshAll();
});
}
private void setupLanguage() {
CrudI18n i18n = CrudI18n.createDefault();
i18n.setNewItem("Neuer Eintrag");
i18n.setEditItem("Bearbeiten");
i18n.setSaveItem("Speichern");
i18n.setCancel("Abbrechen");
i18n.setDeleteItem("Löschen");
i18n.setEditLabel("Bearbeiten");
crud.setI18n(i18n);
}
}
vaadin screenshot
I tried removing the column and adding a new column but then the filter and the sorting is lost. That is why i want to modify the existing column.
New contributor
MWeit is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.