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.UserSurveyResponseQueries.java

/**
 * Return the username of the user that created this survey response.
 * /*from   w  w w. j av  a  2s  .  c  om*/
 * @param surveyResponseId The unique identifier for the survey response.
 * 
 * @return The username of the user that owns this survey response or null
 *          if the survey response doesn't exist.
 * 
 * @throws DataAccessException Thrown if there is an error.
 */
public String getSurveyResponseOwner(UUID surveyResponseId) throws DataAccessException {
    try {
        return getJdbcTemplate().queryForObject(SQL_GET_SURVEY_RESPONSE_OWNER,
                new Object[] { surveyResponseId.toString() }, String.class);
    } catch (org.springframework.dao.IncorrectResultSizeDataAccessException e) {
        if (e.getActualSize() > 1) {
            throw new DataAccessException("One survey response has more than one owner.", e);
        }

        return null;
    } catch (org.springframework.dao.DataAccessException e) {
        throw new DataAccessException("Error executing SQL '" + SQL_GET_SURVEY_RESPONSE_OWNER
                + "' with parameter: " + surveyResponseId, e);
    }
}

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

@Override
public URL getImageUrl(UUID imageId) throws DataAccessException {
    try {/*w  ww.  j ava 2s. c om*/
        return new URL(getJdbcTemplate().queryForObject(SQL_GET_IMAGE_URL, new Object[] { imageId.toString() },
                String.class));
    } catch (org.springframework.dao.IncorrectResultSizeDataAccessException e) {
        if (e.getActualSize() > 1) {
            throw new DataAccessException("Multiple images have the same unique identifier.", e);
        }

        return null;
    } catch (org.springframework.dao.DataAccessException e) {
        throw new DataAccessException(
                "Error executing SQL '" + SQL_GET_IMAGE_URL + "' with parameter: " + imageId, e);
    } catch (MalformedURLException e) {
        throw new DataAccessException("The URL was not a valid URL.", e);
    }
}

From source file:com.nortal.petit.core.SqlSupport.java

/**
 * Queries for a single string object./*from   w  ww.  ja v  a2 s.  c o  m*/
 * 
 * @return null when no result is provided, exception is thrown when more
 *         that 1 result is provided
 */
public <T> T getObject(String sql, RowMapper<T> rowMapper, Object... args) {
    try {
        return getJdbcTemplate().queryForObject(sql, rowMapper, args);
    } catch (IncorrectResultSizeDataAccessException e) {
        if (e.getActualSize() == 0) {
            return null;
        }
        // else
        throw e;
    }
}

From source file:com.nortal.petit.core.SqlSupport.java

/**
 * @return null when no result is provided, exception is thrown when more
 *         that 1 result is provided// ww  w. j  av a2  s. co m
 */
private <T> T getPrimitiveObject(String sql, Class<T> clazz, Object... args) {
    try {
        return getJdbcTemplate().queryForObject(sql, clazz, args);
    } catch (IncorrectResultSizeDataAccessException e) {
        if (e.getActualSize() == 0) {
            return null;
        }
        // else
        throw e;
    }
}

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

/**
 * Queries the database to get the role of a user in a class. If a user 
 * doesn't have a role in a class, null is returned.
 * //  w ww  .j a va2  s.com
 * @param classId A class' unique identifier.
 * 
 * @param username A the username of the user whose role is being checked.
 * 
 * @return Returns the user's role in the class unless they have no role in
 *          the class in which case null is returned.
 */
public Clazz.Role getUserClassRole(String classId, String username) throws DataAccessException {
    try {
        return Clazz.Role.getValue(getJdbcTemplate().queryForObject(SQL_GET_USER_ROLE,
                new Object[] { username, classId }, String.class));
    } catch (org.springframework.dao.IncorrectResultSizeDataAccessException e) {
        if (e.getActualSize() > 1) {
            LOGGER.error("A user has more than one role in a class.", e);
            throw new DataAccessException(e);
        }

        return null;
    } catch (org.springframework.dao.DataAccessException e) {
        throw new DataAccessException(
                "Error executing SQL '" + SQL_GET_USER_ROLE + "' with parameters: " + username + ", " + classId,
                e);
    }
}

From source file:iplatform.admin.ui.server.auth.ad.ActiveDirectoryLdapAuthenticationProvider.java

@SuppressWarnings("deprecation")
private DirContextOperations searchForUser(DirContext ctx, String username) throws NamingException {
    SearchControls searchCtls = new SearchControls();
    searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);

    String searchFilter = "(&(objectClass=user)(userPrincipalName={0}))";

    final String bindPrincipal = createBindPrincipal(username);

    String searchRoot = rootDn != null ? rootDn : searchRootFromPrincipal(bindPrincipal);

    try {//from   w  w w .  ja v a  2s .c om
        return SpringSecurityLdapTemplate.searchForSingleEntryInternal(ctx, searchCtls, searchRoot,
                searchFilter, new Object[] { bindPrincipal });
    } catch (IncorrectResultSizeDataAccessException incorrectResults) {
        if (incorrectResults.getActualSize() == 0) {
            UsernameNotFoundException userNameNotFoundException = new UsernameNotFoundException(
                    "User " + username + " not found in directory.", username);
            userNameNotFoundException.initCause(incorrectResults);
            throw badCredentials(userNameNotFoundException);
        }
        // Search should never return multiple results if properly configured, so just rethrow
        throw incorrectResults;
    }
}

From source file:architecture.user.dao.impl.ExternalJdbcUserDao.java

public User getUserById(long userId) {

    if (userId <= 0L) {
        return null;
    }//from   w w  w . j  a  v  a 2s. co  m

    UserTemplate user = null;
    try {
        user = (UserTemplate) getExtendedJdbcTemplate().queryForObject(getSql("SELECT_USER_BY_ID"),
                getExternalUserRowMapper(), new SqlParameterValue(Types.INTEGER, userId));
        user.setProfile(getUserProfile(user.getUserId()));
        user.setProperties(getUserProperties(user.getUserId()));
    } catch (IncorrectResultSizeDataAccessException e) {
        if (e.getActualSize() > 1) {
            log.warn((new StringBuilder()).append("Multiple occurrances of the same user ID found: ")
                    .append(userId).toString());
            throw e;
        }
    } catch (DataAccessException e) {
        String message = (new StringBuilder()).append("Failure attempting to load user by ID : ").append(userId)
                .append(".").toString();
        log.fatal(message, e);
    }
    return user;

}

From source file:architecture.user.dao.impl.ExternalJdbcUserDao.java

public User getUserByEmail(String email) {

    UserTemplate usertemplate = null;// ww w  . j a v  a 2  s .  c  o  m
    if (null != email) {
        String emailMatch = email.replace('*', '%');
        try {
            UserTemplate user = (UserTemplate) getExtendedJdbcTemplate().queryForObject(
                    getSql("SELECT_USER_BY_ENAIL"), getExternalUserRowMapper(), new Object[] { emailMatch },
                    new int[] { Types.VARCHAR });

            user.setProfile(getUserProfile(user.getUserId()));
            user.setProperties(getUserProperties(user.getUserId()));

            usertemplate = user;
        } catch (IncorrectResultSizeDataAccessException e) {
            if (e.getActualSize() > 1) {
                log.warn((new StringBuilder()).append("Multiple occurrances of the same email found: ")
                        .append(email).toString());
                throw e;
            }
        } catch (DataAccessException e) {
            String message = (new StringBuilder()).append("Failure attempting to load user by email '")
                    .append(emailMatch).append("'").toString();
            log.fatal(message, e);
            throw e;
        }
    }
    return usertemplate;
}

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

public String getUserForId(final UUID mobilityId) throws DataAccessException {

    String sql = "SELECT u.username " + "FROM user u, mobility m " + "WHERE u.id = m.user_id "
            + "AND m.uuid = ?";

    try {//from  w w w.ja  va  2 s.  c om
        return getJdbcTemplate().queryForObject(sql, new Object[] { mobilityId.toString() },
                new SingleColumnRowMapper<String>());
    } catch (org.springframework.dao.IncorrectResultSizeDataAccessException e) {
        if (e.getActualSize() == 0) {
            return null;
        } else {
            throw new DataAccessException(
                    "Error executing SQL '" + sql + "' with parameter: " + mobilityId.toString(), e);
        }
    } catch (org.springframework.dao.DataAccessException e) {
        throw new DataAccessException(
                "Error executing SQL '" + sql + "' with parameter: " + mobilityId.toString(), e);
    }
}