Example usage for org.springframework.dao DataIntegrityViolationException getMostSpecificCause

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

Introduction

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

Prototype

public Throwable getMostSpecificCause() 

Source Link

Document

Retrieve the most specific cause of this exception, that is, either the innermost cause (root cause) or this exception itself.

Usage

From source file:com.gst.organisation.teller.service.TellerWritePlatformServiceJpaImpl.java

@Override
@Transactional/* www.  jav a 2s  .  c o  m*/
public CommandProcessingResult updateCashierAllocation(Long tellerId, Long cashierId, JsonCommand command) {
    try {
        final AppUser currentUser = this.context.authenticatedUser();

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

        final Long staffId = command.longValueOfParameterNamed("staffId");
        final Staff staff = this.staffRepository.findOne(staffId);
        if (staff == null) {
            throw new StaffNotFoundException(staffId);
        }

        final Cashier cashier = validateUserPriviledgeOnCashierAndRetrieve(currentUser, tellerId, cashierId);

        cashier.setStaff(staff);

        // TODO - check if staff office and teller office match

        final Map<String, Object> changes = cashier.update(command);

        if (!changes.isEmpty()) {
            this.cashierRepository.saveAndFlush(cashier);
        }

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

From source file:com.gst.organisation.teller.service.TellerWritePlatformServiceJpaImpl.java

@Override
public CommandProcessingResult allocateCashierToTeller(final Long tellerId, JsonCommand command) {
    try {//from ww w  .  ja v  a  2s .c om
        this.context.authenticatedUser();
        Long hourStartTime;
        Long minStartTime;
        Long hourEndTime;
        Long minEndTime;
        String startTime = " ";
        String endTime = " ";
        final Teller teller = this.tellerRepositoryWrapper.findOneWithNotFoundDetection(tellerId);
        final Office tellerOffice = teller.getOffice();

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

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

        final Staff staff = this.staffRepository.findOne(staffId);
        if (staff == null) {
            throw new StaffNotFoundException(staffId);
        }
        final Boolean isFullDay = command.booleanObjectValueOfParameterNamed("isFullDay");
        if (!isFullDay) {
            hourStartTime = command.longValueOfParameterNamed("hourStartTime");
            minStartTime = command.longValueOfParameterNamed("minStartTime");

            if (minStartTime == 0)
                startTime = hourStartTime.toString() + ":" + minStartTime.toString() + "0";
            else
                startTime = hourStartTime.toString() + ":" + minStartTime.toString();

            hourEndTime = command.longValueOfParameterNamed("hourEndTime");
            minEndTime = command.longValueOfParameterNamed("minEndTime");
            if (minEndTime == 0)
                endTime = hourEndTime.toString() + ":" + minEndTime.toString() + "0";
            else
                endTime = hourEndTime.toString() + ":" + minEndTime.toString();

        }

        final Cashier cashier = Cashier.fromJson(tellerOffice, teller, staff, startTime, endTime, command);

        this.cashierRepository.save(cashier);

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

From source file:com.gst.portfolio.shareaccounts.service.ShareAccountWritePlatformServiceJpaRepositoryImpl.java

@Override
public CommandProcessingResult activateShareAccount(Long accountId, JsonCommand jsonCommand) {

    try {/*  w ww  . j  ava2s.c o  m*/
        ShareAccount account = this.shareAccountRepository.findOneWithNotFoundDetection(accountId);
        Map<String, Object> changes = this.accountDataSerializer.validateAndActivate(jsonCommand, account);
        if (!changes.isEmpty()) {
            this.shareAccountRepository.save(account);
        }
        this.journalEntryWritePlatformService.createJournalEntriesForShares(
                populateJournalEntries(account, account.getChargeTransactions()));
        return new CommandProcessingResultBuilder() //
                .withCommandId(jsonCommand.commandId()) //
                .withEntityId(accountId) //
                .with(changes) //
                .build();
    } catch (DataIntegrityViolationException dve) {
        handleDataIntegrityIssues(jsonCommand, dve.getMostSpecificCause(), dve);
        return CommandProcessingResult.empty();
    }
}

From source file:com.gst.portfolio.shareaccounts.service.ShareAccountWritePlatformServiceJpaRepositoryImpl.java

@Override
public CommandProcessingResult createShareAccount(JsonCommand jsonCommand) {
    try {//from  w  w w.j  a  v a2s  . c  o m
        ShareAccount account = this.accountDataSerializer.validateAndCreate(jsonCommand);
        this.shareAccountRepository.save(account);
        generateAccountNumber(account);
        journalEntryWritePlatformService.createJournalEntriesForShares(
                populateJournalEntries(account, account.getPendingForApprovalSharePurchaseTransactions()));
        return new CommandProcessingResultBuilder() //
                .withCommandId(jsonCommand.commandId()) //
                .withEntityId(account.getId()) //
                .build();
    } catch (final DataIntegrityViolationException dve) {
        handleDataIntegrityIssues(jsonCommand, dve.getMostSpecificCause(), dve);
        return CommandProcessingResult.empty();
    } catch (final PersistenceException dve) {
        Throwable throwable = ExceptionUtils.getRootCause(dve.getCause());
        handleDataIntegrityIssues(jsonCommand, throwable, dve);
        return CommandProcessingResult.empty();
    }
}

From source file:com.gst.portfolio.shareaccounts.service.ShareAccountWritePlatformServiceJpaRepositoryImpl.java

@SuppressWarnings("unchecked")
@Override/*from   ww  w . j ava  2  s.c o m*/
public CommandProcessingResult rejectAdditionalShares(Long accountId, JsonCommand jsonCommand) {
    try {
        ShareAccount account = this.shareAccountRepository.findOneWithNotFoundDetection(accountId);
        Map<String, Object> changes = this.accountDataSerializer.validateAndRejectAddtionalShares(jsonCommand,
                account);
        if (!changes.isEmpty()) {
            this.shareAccountRepository.save(account);
            ArrayList<Long> transactionIds = (ArrayList<Long>) changes
                    .get(ShareAccountApiConstants.requestedshares_paramname);
            if (transactionIds != null) {
                Set<ShareAccountTransaction> transactions = new HashSet<>();
                for (Long id : transactionIds) {
                    ShareAccountTransaction transaction = account.retrievePurchasedShares(id);
                    transactions.add(transaction);
                }
                this.journalEntryWritePlatformService
                        .createJournalEntriesForShares(populateJournalEntries(account, transactions));
            }
        }
        return new CommandProcessingResultBuilder() //
                .withCommandId(jsonCommand.commandId()) //
                .withEntityId(accountId) //
                .with(changes) //
                .build();
    } catch (DataIntegrityViolationException dve) {
        handleDataIntegrityIssues(jsonCommand, dve.getMostSpecificCause(), dve);
        return CommandProcessingResult.empty();
    }
}

From source file:com.gst.portfolio.shareaccounts.service.ShareAccountWritePlatformServiceJpaRepositoryImpl.java

@SuppressWarnings("unchecked")
@Override//from  ww w.  j ava2 s.  co  m
public CommandProcessingResult updateShareAccount(Long accountId, JsonCommand jsonCommand) {
    try {
        Date transactionDate = DateUtils.getDateOfTenant();
        ShareAccount account = this.shareAccountRepository.findOneWithNotFoundDetection(accountId);
        Map<String, Object> changes = this.accountDataSerializer.validateAndUpdate(jsonCommand, account);
        if (!changes.isEmpty()) {
            this.shareAccountRepository.save(account);
        }
        // since we are reverting all journal entries we need to add journal
        // entries for application request
        if (changes.containsKey("reversalIds")) {
            ArrayList<Long> reversalIds = (ArrayList<Long>) changes.get("reversalIds");
            this.journalEntryWritePlatformService.revertShareAccountJournalEntries(reversalIds,
                    transactionDate);
            journalEntryWritePlatformService.createJournalEntriesForShares(
                    populateJournalEntries(account, account.getPendingForApprovalSharePurchaseTransactions()));
            changes.remove("reversalIds");
        }
        return new CommandProcessingResultBuilder() //
                .withCommandId(jsonCommand.commandId()) //
                .withEntityId(accountId) //
                .with(changes) //
                .build();
    } catch (DataIntegrityViolationException dve) {
        handleDataIntegrityIssues(jsonCommand, dve.getMostSpecificCause(), dve);
        return CommandProcessingResult.empty();
    } catch (final PersistenceException dve) {
        Throwable throwable = ExceptionUtils.getRootCause(dve.getCause());
        handleDataIntegrityIssues(jsonCommand, throwable, dve);
        return CommandProcessingResult.empty();
    }
}

From source file:com.gst.portfolio.shareaccounts.service.ShareAccountWritePlatformServiceJpaRepositoryImpl.java

@Override
public CommandProcessingResult rejectShareAccount(Long accountId, JsonCommand jsonCommand) {
    try {/*w  w  w. jav  a2s .  c o  m*/
        ShareAccount account = this.shareAccountRepository.findOneWithNotFoundDetection(accountId);
        Map<String, Object> changes = this.accountDataSerializer.validateAndReject(jsonCommand, account);
        if (!changes.isEmpty()) {
            this.shareAccountRepository.save(account);
            final String noteText = jsonCommand.stringValueOfParameterNamed("note");
            if (StringUtils.isNotBlank(noteText)) {
                final Note note = Note.shareNote(account, noteText);
                changes.put("note", noteText);
                this.noteRepository.save(note);
            }
        }
        Set<ShareAccountTransaction> transactions = account.getShareAccountTransactions();
        Set<ShareAccountTransaction> journalTransactions = new HashSet<>();
        for (ShareAccountTransaction transaction : transactions) {
            if (transaction.isActive() && !transaction.isChargeTransaction()) {
                journalTransactions.add(transaction);
            }
        }

        this.journalEntryWritePlatformService
                .createJournalEntriesForShares(populateJournalEntries(account, journalTransactions));
        return new CommandProcessingResultBuilder() //
                .withCommandId(jsonCommand.commandId()) //
                .withEntityId(accountId) //
                .with(changes) //
                .build();
    } catch (DataIntegrityViolationException dve) {
        handleDataIntegrityIssues(jsonCommand, dve.getMostSpecificCause(), dve);
        return CommandProcessingResult.empty();
    }
}

From source file:com.gst.portfolio.shareaccounts.service.ShareAccountWritePlatformServiceJpaRepositoryImpl.java

@Override
public CommandProcessingResult closeShareAccount(Long accountId, JsonCommand jsonCommand) {
    try {//  ww w . j  a v  a2  s .c  o m
        ShareAccount account = this.shareAccountRepository.findOneWithNotFoundDetection(accountId);
        Map<String, Object> changes = this.accountDataSerializer.validateAndClose(jsonCommand, account);
        if (!changes.isEmpty()) {
            this.shareAccountRepository.save(account);
            final String noteText = jsonCommand.stringValueOfParameterNamed("note");
            if (StringUtils.isNotBlank(noteText)) {
                final Note note = Note.shareNote(account, noteText);
                changes.put("note", noteText);
                this.noteRepository.save(note);
            }
            ShareAccountTransaction transaction = (ShareAccountTransaction) changes
                    .get(ShareAccountApiConstants.requestedshares_paramname);
            transaction = account.getShareAccountTransaction(transaction);
            Set<ShareAccountTransaction> transactions = new HashSet<>();
            transactions.add(transaction);
            this.journalEntryWritePlatformService
                    .createJournalEntriesForShares(populateJournalEntries(account, transactions));
            changes.clear();
            changes.put(ShareAccountApiConstants.requestedshares_paramname, transaction.getId());

        }
        return new CommandProcessingResultBuilder() //
                .withCommandId(jsonCommand.commandId()) //
                .withEntityId(accountId) //
                .with(changes) //
                .build();
    } catch (DataIntegrityViolationException dve) {
        handleDataIntegrityIssues(jsonCommand, dve.getMostSpecificCause(), dve);
        return CommandProcessingResult.empty();
    }
}

From source file:com.gst.portfolio.shareaccounts.service.ShareAccountWritePlatformServiceJpaRepositoryImpl.java

@Override
public CommandProcessingResult redeemShares(Long accountId, JsonCommand jsonCommand) {
    try {//from  ww  w  .  ja  v a 2  s.c  om
        ShareAccount account = this.shareAccountRepository.findOneWithNotFoundDetection(accountId);
        Map<String, Object> changes = this.accountDataSerializer.validateAndRedeemShares(jsonCommand, account);
        if (!changes.isEmpty()) {
            this.shareAccountRepository.save(account);
            ShareAccountTransaction transaction = (ShareAccountTransaction) changes
                    .get(ShareAccountApiConstants.requestedshares_paramname);
            // after saving, entity will have different object. So need to retrieve the entity object
            transaction = account.getShareAccountTransaction(transaction);
            Long redeemShares = transaction.getTotalShares();
            ShareProduct shareProduct = account.getShareProduct();
            //remove the redeem shares from total subscribed shares 
            shareProduct.removeSubscribedShares(redeemShares);
            this.shareProductRepository.save(shareProduct);

            Set<ShareAccountTransaction> transactions = new HashSet<>();
            transactions.add(transaction);
            this.journalEntryWritePlatformService
                    .createJournalEntriesForShares(populateJournalEntries(account, transactions));
            changes.clear();
            changes.put(ShareAccountApiConstants.requestedshares_paramname, transaction.getId());

        }
        return new CommandProcessingResultBuilder() //
                .withCommandId(jsonCommand.commandId()) //
                .withEntityId(accountId) //
                .with(changes) //
                .build();
    } catch (DataIntegrityViolationException dve) {
        handleDataIntegrityIssues(jsonCommand, dve.getMostSpecificCause(), dve);
        return CommandProcessingResult.empty();
    }
}

From source file:com.gst.portfolio.shareaccounts.service.ShareAccountWritePlatformServiceJpaRepositoryImpl.java

@Override
public CommandProcessingResult undoApproveShareAccount(Long accountId, JsonCommand jsonCommand) {
    try {/*  ww w  .  ja v  a  2  s . c  o m*/
        ShareAccount account = this.shareAccountRepository.findOneWithNotFoundDetection(accountId);
        Map<String, Object> changes = this.accountDataSerializer.validateAndUndoApprove(jsonCommand, account);
        if (!changes.isEmpty()) {
            this.shareAccountRepository.save(account);
            final String noteText = jsonCommand.stringValueOfParameterNamed("note");
            if (StringUtils.isNotBlank(noteText)) {
                final Note note = Note.shareNote(account, noteText);
                changes.put("note", noteText);
                this.noteRepository.save(note);
            }
        }

        Set<ShareAccountTransaction> transactions = account.getShareAccountTransactions();
        ArrayList<Long> journalEntryTransactions = new ArrayList<>();
        for (ShareAccountTransaction transaction : transactions) {
            if (transaction.isActive() && !transaction.isChargeTransaction()) {
                journalEntryTransactions.add(transaction.getId());
            }
        }
        Date transactionDate = DateUtils.getDateOfTenant();
        this.journalEntryWritePlatformService.revertShareAccountJournalEntries(journalEntryTransactions,
                transactionDate);
        journalEntryWritePlatformService.createJournalEntriesForShares(
                populateJournalEntries(account, account.getPendingForApprovalSharePurchaseTransactions()));
        return new CommandProcessingResultBuilder() //
                .withCommandId(jsonCommand.commandId()) //
                .withEntityId(accountId) //
                .with(changes) //
                .build();
    } catch (DataIntegrityViolationException dve) {
        handleDataIntegrityIssues(jsonCommand, dve.getMostSpecificCause(), dve);
        return CommandProcessingResult.empty();
    }
}