Listeners: What are delete listeners and how to work with them?

IDocumentDeleteListener interface which needs to be implemented if a user wants to add a custom logic or an action when a delete operation is executed. The interface contains only one method that is invoked before the delete request is sent to the server:

public interface IDocumentDeleteListener {

  /**
   * Invoked before the delete request is sent to the server.
   * @param key The key.
   * @param entityInstance The entity instance.
   * @param metadata The metadata.
   */
  public void beforeDelete(String key, Object entityInstance, RavenJObject metadata);
}

Example

To prevent anyone from deleting documents we can create preventDeleteListener with can be implemented as follows:

public static class PreventDeleteListener implements IDocumentDeleteListener {
  @Override
  public void beforeDelete(String key, Object entityInstance, RavenJObject metadata) {
    throw new NotImplementedException();
  }
}