1

I am using @RepositoryRestResource for Mongo Repositories. I want to do some pre-save check and if those checks are not meeting the requirements, I want to abandon the save operation.

I tried :

    @HandleBeforeSave
    public void handleBeforeSave(CallLog callLog)
            throws InternalServerException {

    CallLog log = callRepository.findOne(callLog.getConferenceId());
    if (log != null
            && (callLog.getStatus().equals(Constants.CALL_IN_PROGRESS) || callLog
                    .getStatus().equals(Constants.CALL_DROPPED))) {
        if (callLog.getStatus().equals(Constants.CALL_DROPPED)) {
            User user = userRepository.findOne(callLog.getReceiverId());
            user.setStatus(Constants.USER_STATUS_IDLE);
            userRepository.save(user);
        }

        throw new InternalServerException(
                "This call has already been received");
    } else {
        User user = userRepository.findOne(callLog.getReceiverId());
        user.setStatus(Constants.USER_STATUS_BUSY);
        userRepository.save(user);
    }

}

But throwing exception, does not actually abandon the save call. Is there any other way to do it?

leeor
  • 17,041
  • 6
  • 34
  • 60
RickDavis
  • 2,276
  • 6
  • 25
  • 31

1 Answers1

0

You can use a RestController to intercept the request.

https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/annotation/RestController.html

Nestor Ledon
  • 1,925
  • 2
  • 23
  • 36