Skip to content
Advertisement

“Show deleted” checkbox not working as intended

I’ve implemented a soft delete behavior in my imaginary Video rental app, and now I am trying to implement a way to reactivate my “deleted” customers, but I can’t get my approach to work, surely something simple, but google did not let me find the answer, so here I am.

Here is an excerpt from my repo interface (JpaRepository):

  @Query("select m from Movie m where m.isDeleted = true")
  List<Movie> findAllIsDeleted();

  @Override
  @Query("select m from Movie m where m.isDeleted=false")
  List<Movie> findAll();

  @Modifying
  @Transactional
  @Query("update Movie m set m.isDeleted=true where id=?1")
  void softDelete(Long id);

In my service class I have:

  public List<Movie> findAllMovies(String filterText) {
    if (filterText == null || filterText.isEmpty()) {
      return movieRepository.findAll();
    } else {
      return movieRepository.search(filterText);
    }
  }

  public List<Movie> findAllDeletedMovies() {
    return movieRepository.findAllIsDeleted();
  }

And an excerpt from my listview class looks like:

...
  Checkbox showDeleted = new Checkbox("Show deleted movies", e -> {
    updateList();
    Notification.show(e.getValue().toString());
  });
...
  private void updateList() {
    if (showDeleted.getValue() == true) {
      grid.setItems(service.findAllDeletedMovies());
    }
    grid.setItems(service.findAllMovies(filterText.getValue()));
  }

But obviously there is something wrong in the listener part, or there is a silent “sure we want to help feature” that I am not aware of. Because the updateList function is not executed. What have I missed?

Advertisement

Answer

The problem lies in the implementation of your updateList method.

No matter if the value of the checkbox is true, at the end it always sets the items again that are returned by service::findAllMovies.

move the last statement into an else block and it should work.

private void updateList() {
    if (showDeleted.getValue()) { // btw, if(boolValue == true) is redundant. just do if(boolValue)
        grid.setItems(service.findAllDeletedMovies());
    } else {
        grid.setItems(service.findAllMovies(filterText.getValue()));
    }
}
7 People found this is helpful
Advertisement