Example usage for org.springframework.data.mongodb.core MongoDataIntegrityViolationException getWriteResult

List of usage examples for org.springframework.data.mongodb.core MongoDataIntegrityViolationException getWriteResult

Introduction

In this page you can find the example usage for org.springframework.data.mongodb.core MongoDataIntegrityViolationException getWriteResult.

Prototype

public WriteResult getWriteResult() 

Source Link

Document

Returns the WriteResult that caused the exception.

Usage

From source file:se.inera.axel.shs.broker.messagestore.internal.MongoMessageLogService.java

private ShsMessageEntry saveShsMessageEntry(ShsMessageEntry entry) {
    if (entry.getLabel() == null)
        throw new IllegalArgumentException("Label must not be null");

    entry.setState(MessageState.NEW);//from   w  w w .ja v a  2s  .c  om
    entry.setStateTimeStamp(new Date());
    entry.setArrivalTimeStamp(entry.getStateTimeStamp());

    ShsMessageEntry newEntry;
    ShsLabel label = entry.getLabel();

    try {
        newEntry = messageLogRepository.save(entry);
    } catch (MongoDataIntegrityViolationException e) {
        if (e.getWriteResult().getCachedLastError().getException() instanceof MongoException.DuplicateKey) {
            throw new MessageAlreadyExistsException(label, new Date(0));
        } else {
            throw e;
        }
    } catch (DuplicateKeyException e) {
        throw new MessageAlreadyExistsException(label, new Date(0));
    }

    return newEntry;
}