Example usage for org.springframework.dao DataIntegrityViolationException getCause

List of usage examples for org.springframework.dao DataIntegrityViolationException getCause

Introduction

In this page you can find the example usage for org.springframework.dao DataIntegrityViolationException getCause.

Prototype

public synchronized Throwable getCause() 

Source Link

Document

Returns the cause of this throwable or null if the cause is nonexistent or unknown.

Usage

From source file:org.openlmis.fulfillment.web.errorhandler.ServiceErrorHandling.java

/**
 * Handles data integrity violation exception.
 *
 * @param ex the data integrity exception
 * @return the user-oriented error message.
 *///  w  w  w. j  a v a  2 s. c  o m
@ExceptionHandler(DataIntegrityViolationException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ResponseEntity<Message.LocalizedMessage> handleDataIntegrityViolation(
        DataIntegrityViolationException ex) {
    if (ex.getCause() instanceof ConstraintViolationException) {
        ConstraintViolationException cause = (ConstraintViolationException) ex.getCause();
        String messageKey = CONSTRAINT_MAP.get(cause.getConstraintName());
        if (messageKey != null) {
            logger.error(CONSTRAINT_VIOLATION, ex);
            return new ResponseEntity<>(getLocalizedMessage(new Message(messageKey)), HttpStatus.BAD_REQUEST);
        } else {
            return new ResponseEntity<>(
                    logErrorAndRespond(CONSTRAINT_VIOLATION, MessageKeys.CONSTRAINT_VIOLATION, ex.getMessage()),
                    HttpStatus.BAD_REQUEST);
        }
    }

    return new ResponseEntity<>(
            logErrorAndRespond("Data integrity violation", DATA_INTEGRITY_VIOLATION, ex.getMessage()),
            CONFLICT);
}

From source file:be.bittich.quote.controller.impl.DefaultExceptionHandler.java

@RequestMapping(produces = APPLICATION_JSON)
@ExceptionHandler(DataIntegrityViolationException.class)
@ResponseStatus(value = HttpStatus.CONFLICT)
public @ResponseBody Map<String, Object> handleDataIntegrityViolationException(
        DataIntegrityViolationException ex) throws IOException {
    Map<String, Object> map = newHashMap();
    map.put("error", "Data Integrity Error");
    map.put("cause", ex.getCause().getLocalizedMessage());
    return map;/*  ww  w .jav  a2  s . c  om*/
}

From source file:com.gst.portfolio.floatingrates.service.FloatingRateWritePlatformServiceImpl.java

@Transactional
@Override/*from   w  w  w .ja v  a 2s  .  c o m*/
public CommandProcessingResult createFloatingRate(final JsonCommand command) {
    try {
        this.fromApiJsonDeserializer.validateForCreate(command.json());
        final AppUser currentUser = this.context.authenticatedUser();
        final FloatingRate newFloatingRate = FloatingRate.createNew(currentUser, command);
        this.floatingRateRepository.save(newFloatingRate);
        return new CommandProcessingResultBuilder() //
                .withCommandId(command.commandId()) //
                .withEntityId(newFloatingRate.getId()) //
                .build();
    } catch (final DataIntegrityViolationException dve) {
        handleDataIntegrityIssues(command, dve.getMostSpecificCause(), dve);
        return CommandProcessingResult.empty();
    } catch (final PersistenceException dve) {
        Throwable throwable = ExceptionUtils.getRootCause(dve.getCause());
        handleDataIntegrityIssues(command, throwable, dve);
        return CommandProcessingResult.empty();
    }
}

From source file:com.gst.portfolio.floatingrates.service.FloatingRateWritePlatformServiceImpl.java

@Transactional
@Override/*from w  w  w. ja va2s . com*/
public CommandProcessingResult updateFloatingRate(final JsonCommand command) {
    try {
        final FloatingRate floatingRateForUpdate = this.floatingRateRepository
                .findOneWithNotFoundDetection(command.entityId());
        this.fromApiJsonDeserializer.validateForUpdate(command.json(), floatingRateForUpdate);
        final AppUser currentUser = this.context.authenticatedUser();
        final Map<String, Object> changes = floatingRateForUpdate.update(command, currentUser);

        if (!changes.isEmpty()) {
            this.floatingRateRepository.save(floatingRateForUpdate);
        }

        return new CommandProcessingResultBuilder() //
                .withCommandId(command.commandId()) //
                .withEntityId(command.entityId()) //
                .with(changes) //
                .build();
    } catch (final DataIntegrityViolationException dve) {
        handleDataIntegrityIssues(command, dve.getMostSpecificCause(), dve);
        return CommandProcessingResult.empty();
    } catch (final PersistenceException dve) {
        Throwable throwable = ExceptionUtils.getRootCause(dve.getCause());
        handleDataIntegrityIssues(command, throwable, dve);
        return CommandProcessingResult.empty();
    }
}

From source file:com.gst.organisation.provisioning.service.ProvisioningCategoryWritePlatformServiceJpaRepositoryImpl.java

@Override
public CommandProcessingResult createProvisioningCateogry(JsonCommand command) {
    try {/*w w  w  .ja va 2  s.  com*/
        this.fromApiJsonDeserializer.validateForCreate(command.json());
        final ProvisioningCategory provisioningCategory = ProvisioningCategory.fromJson(command);
        this.provisioningCategoryRepository.save(provisioningCategory);
        return new CommandProcessingResultBuilder().withCommandId(command.commandId())
                .withEntityId(provisioningCategory.getId()).build();
    } catch (final DataIntegrityViolationException dve) {
        handleDataIntegrityIssues(command, dve.getMostSpecificCause(), dve);
        return CommandProcessingResult.empty();
    } catch (final PersistenceException dve) {
        Throwable throwable = ExceptionUtils.getRootCause(dve.getCause());
        handleDataIntegrityIssues(command, throwable, dve);
        return CommandProcessingResult.empty();
    }
}

From source file:com.gst.portfolio.fund.service.FundWritePlatformServiceJpaRepositoryImpl.java

@Transactional
@Override/*from   ww w . java 2  s  . c  o m*/
@CacheEvict(value = "funds", key = "T(com.gst.infrastructure.core.service.ThreadLocalContextUtil).getTenant().getTenantIdentifier().concat('fn')")
public CommandProcessingResult createFund(final JsonCommand command) {

    try {
        this.context.authenticatedUser();

        this.fromApiJsonDeserializer.validateForCreate(command.json());

        final Fund fund = Fund.fromJson(command);

        this.fundRepository.save(fund);

        return new CommandProcessingResultBuilder().withCommandId(command.commandId())
                .withEntityId(fund.getId()).build();
    } catch (final DataIntegrityViolationException dve) {
        handleFundDataIntegrityIssues(command, dve.getMostSpecificCause(), dve);
        return CommandProcessingResult.empty();
    } catch (final PersistenceException dve) {
        Throwable throwable = ExceptionUtils.getRootCause(dve.getCause());
        handleFundDataIntegrityIssues(command, throwable, dve);
        return CommandProcessingResult.empty();
    }
}

From source file:com.gst.organisation.provisioning.service.ProvisioningCategoryWritePlatformServiceJpaRepositoryImpl.java

@Override
public CommandProcessingResult updateProvisioningCategory(final Long categoryId, JsonCommand command) {
    try {//from  w  w w . ja  v a2s . co  m
        this.fromApiJsonDeserializer.validateForUpdate(command.json());
        final ProvisioningCategory provisioningCategoryForUpdate = this.provisioningCategoryRepository
                .findOne(categoryId);
        if (provisioningCategoryForUpdate == null) {
            throw new ProvisioningCategoryNotFoundException(categoryId);
        }
        final Map<String, Object> changes = provisioningCategoryForUpdate.update(command);
        if (!changes.isEmpty()) {
            this.provisioningCategoryRepository.save(provisioningCategoryForUpdate);
        }
        return new CommandProcessingResultBuilder().withCommandId(command.commandId()).withEntityId(categoryId)
                .with(changes).build();
    } catch (final DataIntegrityViolationException dve) {
        handleDataIntegrityIssues(command, dve.getMostSpecificCause(), dve);
        return CommandProcessingResult.empty();
    } catch (final PersistenceException dve) {
        Throwable throwable = ExceptionUtils.getRootCause(dve.getCause());
        handleDataIntegrityIssues(command, throwable, dve);
        return CommandProcessingResult.empty();
    }
}

From source file:com.gst.portfolio.fund.service.FundWritePlatformServiceJpaRepositoryImpl.java

@Transactional
@Override/*from w  w w. ja  v a2  s  .c  om*/
@CacheEvict(value = "funds", key = "T(com.gst.infrastructure.core.service.ThreadLocalContextUtil).getTenant().getTenantIdentifier().concat('fn')")
public CommandProcessingResult updateFund(final Long fundId, final JsonCommand command) {

    try {
        this.context.authenticatedUser();

        this.fromApiJsonDeserializer.validateForUpdate(command.json());

        final Fund fund = this.fundRepository.findOne(fundId);
        if (fund == null) {
            throw new FundNotFoundException(fundId);
        }

        final Map<String, Object> changes = fund.update(command);
        if (!changes.isEmpty()) {
            this.fundRepository.saveAndFlush(fund);
        }

        return new CommandProcessingResultBuilder().withCommandId(command.commandId())
                .withEntityId(fund.getId()).with(changes).build();
    } catch (final DataIntegrityViolationException dve) {
        handleFundDataIntegrityIssues(command, dve.getMostSpecificCause(), dve);
        return CommandProcessingResult.empty();
    } catch (final PersistenceException dve) {
        Throwable throwable = ExceptionUtils.getRootCause(dve.getCause());
        handleFundDataIntegrityIssues(command, throwable, dve);
        return CommandProcessingResult.empty();
    }
}

From source file:com.gst.organisation.staff.service.StaffWritePlatformServiceJpaRepositoryImpl.java

@Transactional
@Override//from   ww w .j av  a  2 s. c o m
public CommandProcessingResult createStaff(final JsonCommand command) {

    try {
        this.fromApiJsonDeserializer.validateForCreate(command.json());

        final Long officeId = command.longValueOfParameterNamed("officeId");

        final Office staffOffice = this.officeRepositoryWrapper.findOneWithNotFoundDetection(officeId);
        final Staff staff = Staff.fromJson(staffOffice, command);

        this.staffRepository.save(staff);

        return new CommandProcessingResultBuilder() //
                .withCommandId(command.commandId()) //
                .withEntityId(staff.getId()).withOfficeId(officeId) //
                .build();
    } catch (final DataIntegrityViolationException dve) {
        handleStaffDataIntegrityIssues(command, dve.getMostSpecificCause(), dve);
        return CommandProcessingResult.empty();
    } catch (final PersistenceException dve) {
        Throwable throwable = ExceptionUtils.getRootCause(dve.getCause());
        handleStaffDataIntegrityIssues(command, throwable, dve);
        return CommandProcessingResult.empty();
    }
}

From source file:com.gst.organisation.staff.service.StaffWritePlatformServiceJpaRepositoryImpl.java

@Transactional
@Override//from   w  w  w  .j  av a2 s  .  c  om
public CommandProcessingResult updateStaff(final Long staffId, final JsonCommand command) {

    try {
        this.fromApiJsonDeserializer.validateForUpdate(command.json(), staffId);

        final Staff staffForUpdate = this.staffRepository.findOne(staffId);
        if (staffForUpdate == null) {
            throw new StaffNotFoundException(staffId);
        }

        final Map<String, Object> changesOnly = staffForUpdate.update(command);

        if (changesOnly.containsKey("officeId")) {
            final Long officeId = (Long) changesOnly.get("officeId");
            final Office newOffice = this.officeRepositoryWrapper.findOneWithNotFoundDetection(officeId);
            staffForUpdate.changeOffice(newOffice);
        }

        if (!changesOnly.isEmpty()) {
            this.staffRepository.saveAndFlush(staffForUpdate);
        }

        return new CommandProcessingResultBuilder().withCommandId(command.commandId()).withEntityId(staffId)
                .withOfficeId(staffForUpdate.officeId()).with(changesOnly).build();
    } catch (final DataIntegrityViolationException dve) {
        handleStaffDataIntegrityIssues(command, dve.getMostSpecificCause(), dve);
        return CommandProcessingResult.empty();
    } catch (final PersistenceException dve) {
        Throwable throwable = ExceptionUtils.getRootCause(dve.getCause());
        handleStaffDataIntegrityIssues(command, throwable, dve);
        return CommandProcessingResult.empty();
    }
}