I have a column in my table where I populate item with the following code:
@Override
public void populateItem(Item<ICellPopulator<T>> item, String componentId, IModel<T> model) {
if (deleteEnabled(model)) {
item.add(new ConfirmationLink<>(componentId).onClick(target -> deleteRow(...)));
} else {
item.add(new Label(componentId));
}
}
So when user has enough grants to delete an entity, he can see the icon in the row, if doesn't he only sees an empty cell.
Where can I check for each model in the table that the user does not have permission to delete any entity and prevent the column from being rendered in the table?
Update
I would like to add some code to explain my case.
This is a very common example of using a table in my application.
public class CenterPanel extends SWPanel<CourierSearchQuery> {
public CenterPanel(String id) {
super(id, new Model<>(new CourierSearchQuery()));
}
@Override
protected void onInitialize() {
super.onInitialize();
List<IColumn<Courier, String>> columns = List.of(
new StyledColumn<Courier>("Ф.И.О.", "name.FIO", "name.FIO")
.setCreateComponentFunction((id, model) ->
new LinkPanel(id, model.map(n -> n.getName().getFIO()), model)),
new StyledColumn<Courier>("Статус", "state.title")
.setCssClass(CSS.width(100)),
new StyledColumn<Courier>("Телефон", "phone")
.setCssClass(CSS.width(100)),
new DeleteColumn<>() // <- this column should be hideable
);
add(new EntityDataTable<>("table", columns, getModel()));
}
}
There is an JPA entity (Courier), POJO parameter object (CourierSearchQuery) where I can set criteria for filtering data and EntityDataTable. The last one contains a dataprovider that can find a DAO for the entity, call a search method with filter parameters and create an iterator over it.
Also there is a DeleteColumn class. Under the hood it checks user's permissions for current entity and shows an ajax link.
I would like to mark it with the following interface and hide it inside a DataTable class (if needed):
public interface HideableColumn<T> {
boolean isCellVisible(T object);
}
To implement hiding I've write following:
private List<? extends IColumn<T, String>> allColumns;
@Override
protected void onConfigure() {
super.onConfigure();
if (allColumns == null) allColumns = new ArrayList<>(getColumns());
if (allColumns.stream().anyMatch(e -> e instanceof HideableColumn)) {
getColumns().clear();
Set<T> objects = new HashSet<>();
getDataProvider().iterator(getCurrentPage() * getItemsPerPage(), getItemsPerPage()).forEachRemaining(objects::add); // <-1
allColumns.stream()
.filter(e -> !(e instanceof HideableColumn) || objects.stream().anyMatch(((HideableColumn<T>) e)::isCellVisible))
.forEach(e -> getColumns().add(e)); // <-2
}
}
But this code has two problems:
1 - it makes the table calls dataProvider#iterator() twice
2 - it doesn't compile because of dataTable#getColumns() has wildcard in declaration