Example usage for org.springframework.dao IncorrectResultSizeDataAccessException getActualSize

List of usage examples for org.springframework.dao IncorrectResultSizeDataAccessException getActualSize

Introduction

In this page you can find the example usage for org.springframework.dao IncorrectResultSizeDataAccessException getActualSize.

Prototype

public int getActualSize() 

Source Link

Document

Return the actual result size (or -1 if unknown).

Usage

From source file:org.ohmage.query.impl.UserQueries.java

@Override
public String getEmailAddress(final String username) throws DataAccessException {

    String sql = "SELECT email_address FROM user WHERE username = ?";

    try {/*from www. ja  v a 2s. co  m*/
        return getJdbcTemplate().queryForObject(sql, new Object[] { username }, String.class);
    } catch (org.springframework.dao.IncorrectResultSizeDataAccessException e) {
        // If the user doesn't exist, return null.
        if (e.getActualSize() == 0) {
            return null;
        }

        throw new DataAccessException("Multiple users have the same username: " + username, e);
    } catch (org.springframework.dao.DataAccessException e) {
        throw new DataAccessException("Error executing '" + sql + "' with parameter: " + username, e);
    }
}

From source file:net.sourceforge.vulcan.spring.jdbc.JdbcBuildOutcomeStore.java

/**
 * Finds the most recent build outcome with the specified column value.
 * This is a helper method for loadMostRecentBuildOutcomeByWorkDir
 * and loadMostRecentBuildOutcomeByTagName
 *
 * @param projectName    Name of project
 * @param queryColumn    SQL column to compare
 * @param value          Value to locate
 * @param scopeToProject If true, limit result to project name.  If false, return most recent
 *                       build from any project that matches criteria.
 *//*from  w  w w . j  a  v a  2s .co  m*/
private ProjectStatusDto findAndLoadBuildOutcome(String projectName, String queryColumn, String value,
        boolean scopeToProject) throws StoreException {
    String sql = "select uuid from builds o inner join " + "(select max(id) as max_id from builds where "
            + queryColumn + "=?";
    Object[] params;

    if (scopeToProject) {
        sql += " and project_id=(select id from project_names where name=?)";
        params = new Object[] { value, projectName };
    } else {
        params = new Object[] { value };
    }

    sql += ") i on o.id = i.max_id";

    final String result;

    try {
        result = (String) jdbcTemplate.queryForObject(sql, params, String.class);
    } catch (IncorrectResultSizeDataAccessException e) {
        if (e.getActualSize() != 0) {
            // This is a bug.
            throw e;
        }
        // This just means "no results found."
        return null;
    }

    return loadBuildOutcome(UUID.fromString(result));
}

From source file:org.acegisecurity.ldap.search.FilterBasedLdapUserSearch.java

/**
 * Return the LdapUserDetails containing the user's information
 *
 * @param username the username to search for.
 *
 * @return An LdapUserDetails object containing the details of the located user's directory entry
 *
 * @throws UsernameNotFoundException if no matching entry is found.
 *//*from   w  ww. j av  a2 s  . com*/
public LdapUserDetails searchForUser(String username) {
    if (logger.isDebugEnabled()) {
        logger.debug("Searching for user '" + username + "', with user search " + this.toString());
    }

    LdapTemplate template = new LdapTemplate(initialDirContextFactory);

    template.setSearchControls(searchControls);

    try {
        LdapUserDetailsImpl.Essence user = (LdapUserDetailsImpl.Essence) template
                .searchForSingleEntry(searchBase, searchFilter, new String[] { username }, userDetailsMapper);
        user.setUsername(username);

        return user.createUserDetails();
    } catch (IncorrectResultSizeDataAccessException notFound) {
        if (notFound.getActualSize() == 0) {
            throw new UsernameNotFoundException("User " + username + " not found in directory.");
        }
        // Search should never return multiple results if properly configured, so just rethrow
        throw notFound;
    }
}

From source file:org.jasig.cas.adaptors.jdbc.QueryAndEncodeDatabaseAuthenticationHandler.java

@Override
protected final HandlerResult authenticateUsernamePasswordInternal(
        final UsernamePasswordCredential transformedCredential)
        throws GeneralSecurityException, PreventedException {
    final String username = getPrincipalNameTransformer().transform(transformedCredential.getUsername());
    final String encodedPsw = this.getPasswordEncoder().encode(transformedCredential.getPassword());

    try {/*from  www .j a v a 2 s .com*/
        final Map<String, Object> values = getJdbcTemplate().queryForMap(this.sql, username);
        final String digestedPassword = digestEncodedPassword(encodedPsw, values);

        if (!values.get(this.passwordFieldName).equals(digestedPassword)) {
            throw new FailedLoginException("Password does not match value on record.");
        }
        return createHandlerResult(transformedCredential, this.principalFactory.createPrincipal(username),
                null);

    } catch (final IncorrectResultSizeDataAccessException e) {
        if (e.getActualSize() == 0) {
            throw new AccountNotFoundException(username + " not found with SQL query");
        } else {
            throw new FailedLoginException("Multiple records found for " + username);
        }
    } catch (final DataAccessException e) {
        throw new PreventedException("SQL exception while executing query for " + username, e);
    }

}

From source file:org.springframework.security.ldap.search.FilterBasedLdapUserSearch.java

/**
 * Return the LdapUserDetails containing the user's information
 *
 * @param username the username to search for.
 *
 * @return An LdapUserDetails object containing the details of the located user's
 * directory entry/*from   ww w.  j a v  a2  s.c  om*/
 *
 * @throws UsernameNotFoundException if no matching entry is found.
 */
@Override
public DirContextOperations searchForUser(String username) {
    if (logger.isDebugEnabled()) {
        logger.debug("Searching for user '" + username + "', with user search " + this);
    }

    SpringSecurityLdapTemplate template = new SpringSecurityLdapTemplate(contextSource);

    template.setSearchControls(searchControls);

    try {

        return template.searchForSingleEntry(searchBase, searchFilter, new String[] { username });

    } catch (IncorrectResultSizeDataAccessException notFound) {
        if (notFound.getActualSize() == 0) {
            throw new UsernameNotFoundException("User " + username + " not found in directory.");
        }
        // Search should never return multiple results if properly configured, so just
        // rethrow
        throw notFound;
    }
}