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:org.mifosplatform.infrastructure.configuration.service.GlobalConfigurationWritePlatformServiceJpaRepositoryImpl.java

private void handleDataIntegrityIssues(final JsonCommand command, final DataIntegrityViolationException dve) {

    final Throwable realCause = dve.getMostSpecificCause();
    if (realCause.getMessage().contains("name_config")) {
        final String username = command.stringValueOfParameterNamed(ConfigurationConstants.NAME);
        final StringBuilder defaultMessageBuilder = new StringBuilder("Name with").append(username)
                .append(" already exists.");
        throw new PlatformDataIntegrityException("error.msg.smtp.duplicate.name",
                defaultMessageBuilder.toString(), "username", username);
    }// ww  w  .  j av a 2 s.  c  om
    //    logger.error(dve.getMessage(), dve);
    throw new PlatformDataIntegrityException("error.msg.globalConfiguration.unknown.data.integrity.issue",
            "Unknown data integrity issue with resource: " + realCause.getMessage());
}

From source file:org.mifosplatform.infrastructure.dataqueries.service.ReadWriteNonCoreDataServiceImpl.java

@Transactional
@Override//from  w ww  . j a  v  a  2s.c o m
public void registerDatatable(final String dataTableName, final String applicationTableName) {

    validateAppTable(applicationTableName);
    assertDataTableExists(dataTableName);

    final String registerDatatableSql = "insert into x_registered_table (registered_table_name, application_table_name) values ('"
            + dataTableName + "', '" + applicationTableName + "')";

    final String createPermission = "'CREATE_" + dataTableName + "'";
    final String createPermissionChecker = "'CREATE_" + dataTableName + "_CHECKER'";
    final String readPermission = "'READ_" + dataTableName + "'";
    final String updatePermission = "'UPDATE_" + dataTableName + "'";
    final String updatePermissionChecker = "'UPDATE_" + dataTableName + "_CHECKER'";
    final String deletePermission = "'DELETE_" + dataTableName + "'";
    final String deletePermissionChecker = "'DELETE_" + dataTableName + "_CHECKER'";

    final String permissionsSql = "insert into m_permission (grouping, code, action_name, entity_name, can_maker_checker) values "
            + "('datatable', " + createPermission + ", 'CREATE', '" + dataTableName + "', true),"
            + "('datatable', " + createPermissionChecker + ", 'CREATE', '" + dataTableName + "', false),"
            + "('datatable', " + readPermission + ", 'READ', '" + dataTableName + "', false),"
            + "('datatable', " + updatePermission + ", 'UPDATE', '" + dataTableName + "', true),"
            + "('datatable', " + updatePermissionChecker + ", 'UPDATE', '" + dataTableName + "', false),"
            + "('datatable', " + deletePermission + ", 'DELETE', '" + dataTableName + "', true),"
            + "('datatable', " + deletePermissionChecker + ", 'DELETE', '" + dataTableName + "', false)";

    try {
        final String[] sqlArray = { registerDatatableSql, permissionsSql };
        this.jdbcTemplate.batchUpdate(sqlArray);

    }
    /***
     * Strangely, a Hibernate contraint violation exception is thrown
     ****/
    catch (final ConstraintViolationException cve) {
        final Throwable realCause = cve.getCause();
        // even if duplicate is only due to permission duplicate, okay to
        // show duplicate datatable error msg
        if (realCause.getMessage().contains("Duplicate entry")) {
            throw new PlatformDataIntegrityException("error.msg.datatable.registered",
                    "Datatable `" + dataTableName + "` is already registered against an application table.",
                    "dataTableName", dataTableName);
        }
    } catch (final DataIntegrityViolationException dve) {
        final Throwable realCause = dve.getMostSpecificCause();
        // even if duplicate is only due to permission duplicate, okay to
        // show duplicate datatable error msg
        if (realCause.getMessage().contains("Duplicate entry")) {
            throw new PlatformDataIntegrityException("error.msg.datatable.registered",
                    "Datatable `" + dataTableName + "` is already registered against an application table.",
                    "dataTableName", dataTableName);
        }
        logAsErrorUnexpectedDataIntegrityException(dve);
        throw new PlatformDataIntegrityException("error.msg.unknown.data.integrity.issue",
                "Unknown data integrity issue with resource.");
    }

}

From source file:org.mifosplatform.organisation.dsa.service.DsaWritePlatformServiceJpaRepositoryImpl.java

private void handleStaffDataIntegrityIssues(final JsonCommand command,
        final DataIntegrityViolationException dve) {
    final Throwable realCause = dve.getMostSpecificCause();

    if (realCause.getMessage().contains("display_name")) {
        final String lastname = command.stringValueOfParameterNamed("lastname");
        String displayName = lastname;
        if (!StringUtils.isBlank(displayName)) {
            final String firstname = command.stringValueOfParameterNamed("firstname");
            displayName = lastname + ", " + firstname;
        }/* w  w  w  .ja v  a2s.c  om*/
        throw new PlatformDataIntegrityException("error.msg.staff.duplicate.displayName",
                "A Dsa with the given display name '" + displayName + "' already exists", "displayName",
                displayName);
    }

    logger.error(dve.getMessage(), dve);
    throw new PlatformDataIntegrityException("error.msg.staff.unknown.data.integrity.issue",
            "Unknown data integrity issue with resource: " + realCause.getMessage());
}

From source file:org.mifosplatform.portfolio.group.service.GroupWritePlatformServiceJpaRepositoryImpl.java

private void handleGroupDataIntegrityIssues(final JsonCommand command,
        final DataIntegrityViolationException dve, GroupLevel groupLevel) {

    final Throwable realCause = dve.getMostSpecificCause();
    String errorMessageForUser = null;
    String errorMessageForMachine = null;

    if (realCause.getMessage().contains("external_id")) {

        errorMessageForUser = groupLevel.getLevelName() + " with externalId {0} already exists";
        errorMessageForMachine = "error.msg." + groupLevel.getLevelName().toLowerCase()
                + ".duplicate.externalId";
        throw new PlatformDataIntegrityException(errorMessageForMachine, errorMessageForUser, "externalId",
                command.stringValueOfParameterNamed("externalId"));

    } else if (realCause.getMessage().contains("name")) {

        errorMessageForUser = groupLevel.getLevelName() + " with name {0} already exists";
        errorMessageForMachine = "error.msg." + groupLevel.getLevelName().toLowerCase() + ".duplicate.name";
        throw new PlatformDataIntegrityException(errorMessageForMachine, errorMessageForUser, "name",
                command.stringValueOfParameterNamed("name"));

    }//ww  w.j a v  a2s .co m

    logger.error(dve.getMessage(), dve);
    throw new PlatformDataIntegrityException("error.msg.group.unknown.data.integrity.issue",
            "Unknown data integrity issue with resource.");
}

From source file:org.mifosplatform.useradministration.service.AppUserWritePlatformServiceJpaRepositoryImpl.java

private void handleDataIntegrityIssues(final JsonCommand command, final DataIntegrityViolationException dve) {

    final Throwable realCause = dve.getMostSpecificCause();
    if (realCause.getMessage().contains("username_org")) {
        final String username = command.stringValueOfParameterNamed("username");
        final StringBuilder defaultMessageBuilder = new StringBuilder("User with username ").append(username)
                .append(" already exists.");
        throw new PlatformDataIntegrityException("error.msg.user.duplicate.username",
                defaultMessageBuilder.toString(), "username", username);
    }//w w w.  jav  a  2 s . c om

    logger.error(dve.getMessage(), dve);
    throw new PlatformDataIntegrityException("error.msg.unknown.data.integrity.issue",
            "Unknown data integrity issue with resource.");
}