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:com.echounion.portal.util.MyQueryDatabaseAuthenticationHandler.java

@Override
protected HandlerResult authenticateUsernamePasswordInternal(final UsernamePasswordCredential credential)
        throws GeneralSecurityException, PreventedException {
    final String username = credential.getUsername();
    final ShiroKit shiroKit = (ShiroKit) this.getPasswordEncoder();
    shiroKit.setSalt(username);/*from   www . j a va 2s .c o  m*/
    final String encryptedPassword = shiroKit.encode(credential.getPassword());

    try {
        final String e = (String) this.getJdbcTemplate().queryForObject(this.sql, String.class,
                new Object[] { username });
        if (!e.equals(encryptedPassword)) {
            throw new FailedLoginException("Password does not match value on record.");
        }
    } catch (final IncorrectResultSizeDataAccessException var5) {
        if (var5.getActualSize() == 0) {
            throw new AccountNotFoundException(username + " not found with SQL query");
        }

        throw new FailedLoginException("Multiple records found for " + username);
    } catch (final DataAccessException var6) {
        throw new PreventedException("SQL exception while executing query for " + username, var6);
    }

    return this.createHandlerResult(credential, this.principalFactory.createPrincipal(username), (List) null);
}

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

public Document.Role getClassDocumentRole(String classId, String documentId) throws DataAccessException {
    try {// www. ja  va 2 s .  c  om
        return Document.Role.getValue(getJdbcTemplate().queryForObject(SQL_GET_CLASS_DOCUMENT_ROLE,
                new Object[] { classId, documentId }, String.class));
    } catch (org.springframework.dao.IncorrectResultSizeDataAccessException e) {
        if (e.getActualSize() > 1) {
            throw new DataAccessException("A ckass has more than one role with a document.", e);
        }

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

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

public String getCampaignIdFromSurveyId(UUID surveyResponseId) throws DataAccessException {
    try {/*from   w ww .ja  va2  s . c om*/
        return getJdbcTemplate().queryForObject(SQL_GET_CAMPAIGN_ID_FROM_SURVEY_RESPONSE_ID,
                new Object[] { surveyResponseId.toString() }, String.class);
    } catch (org.springframework.dao.IncorrectResultSizeDataAccessException e) {
        if (e.getActualSize() > 1) {
            throw new DataAccessException("One survey response belongs to multiple campaigns.", e);
        }

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

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

@Override
public Document.Role getCampaignDocumentRole(String campaignId, String documentId) throws DataAccessException {
    try {/*w ww  . ja v  a 2s.  c  om*/
        return getJdbcTemplate().queryForObject(SQL_GET_CAMPAIGN_DOCUMENT_ROLE,
                new Object[] { campaignId, documentId }, new RowMapper<Document.Role>() {
                    @Override
                    public Document.Role mapRow(ResultSet rs, int rowNum) throws SQLException {
                        return Document.Role.getValue(rs.getString("role"));
                    }
                });
    } catch (org.springframework.dao.IncorrectResultSizeDataAccessException e) {
        if (e.getActualSize() > 1) {
            throw new DataAccessException("A campaign has more than one role with a document.", e);
        }

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

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

@Override
public Integer retrievePromptResponseIdFor(UUID surveyResponseId, String promptId, String repeatableSetId,
        Integer repeatableSetIteration) throws DataAccessException {

    StringBuilder sql = new StringBuilder(SQL_GET_PROMPT_RESPONSE_ID);

    try {//w ww.  j a v  a  2s  .co m

        if (repeatableSetId != null && repeatableSetIteration != null) {

            sql.append(SQL_GET_PROMPT_RESPONSE_ID_WHERE_REPEATABLE_SET);

            return getJdbcTemplate().queryForInt(sql.toString(), new Object[] { surveyResponseId.toString(),
                    promptId, repeatableSetId, repeatableSetIteration });

        } else {

            return getJdbcTemplate().queryForInt(sql.toString(),
                    new Object[] { surveyResponseId.toString(), promptId });
        }
    } catch (IncorrectResultSizeDataAccessException e) {
        int numberReturned = e.getActualSize();

        if (numberReturned == 0) {
            if (repeatableSetId != null && repeatableSetIteration != null) {
                LOGGER.warn("Attempt to annotate a prompt response that doesn't exist with survey_id="
                        + surveyResponseId.toString() + ", prompt_id=" + promptId);
            } else {
                LOGGER.warn("Attempt to annotate a prompt response that doesn't exist with survey_id="
                        + surveyResponseId.toString() + ", prompt_id=" + promptId + ", repeatable_set_id="
                        + repeatableSetId + ", repeatable_set_iteration=" + repeatableSetIteration);
            }

            // Just throw the general exception in case someone attempted
            // to annotate a prompt response that was just deleted or
            // someone is hacking around

            throw new DataAccessException("Error executing SQL '" + sql.toString() + "'.", e);
        } else { // more than one row returned; logical error

            if (repeatableSetId != null && repeatableSetIteration != null) {
                LOGGER.warn("Attempt to annotate a multiple prompt responses with survey_id="
                        + surveyResponseId.toString() + ", prompt_id=" + promptId);
            } else {
                LOGGER.warn("Attempt to annotate a multiple prompt responses with survey_id="
                        + surveyResponseId.toString() + ", prompt_id=" + promptId + ", repeatable_set_id="
                        + repeatableSetId + ", repeatable_set_iteration=" + repeatableSetIteration);
            }

            throw new DataAccessException("Error executing SQL '" + sql.toString() + "'.", e);
        }
    } catch (org.springframework.dao.DataAccessException e) {
        throw new DataAccessException("Error executing SQL '" + sql.toString() + "'.", e);
    }
}

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

/**
 * Retrieves the username of the user that created this image.
 * /*from  w  ww .  j  a v a 2  s .  c  o m*/
 * @param imageId The image's unique identifier.
 * 
 * @return Returns the creator of the image or null if the image doesn't
 *          exist.
 * 
 * @throws DataAccessException Thrown if there is an error.
 */
public String getImageOwner(UUID imageId) throws DataAccessException {
    try {
        return getJdbcTemplate().queryForObject(SQL_GET_IMAGE_OWNER, new Object[] { imageId.toString() },
                String.class);
    } catch (org.springframework.dao.IncorrectResultSizeDataAccessException e) {
        if (e.getActualSize() > 1) {
            throw new DataAccessException("More than one image has the same ID.", e);
        }

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

From source file:com.orangeleap.common.security.OrangeLeapLdapUserSearch.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.
 *//*ww w . jav a2s.co  m*/
public DirContextOperations searchForUser(String username, String site) {
    SpringSecurityLdapTemplate template = new SpringSecurityLdapTemplate(contextSource);

    template.setSearchControls(searchControls);

    try {
        String searchBase = "o=" + site;
        return template.searchForSingleEntry(searchBase, searchFilter, new String[] { username });

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

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

public SurveyResponse.PrivacyState getImagePrivacyStateInCampaign(String campaignId, UUID imageId)
        throws DataAccessException {
    try {//from   ww  w . j a  v  a  2s .com
        return SurveyResponse.PrivacyState
                .getValue(getJdbcTemplate().queryForObject(SQL_GET_IMAGE_PRIVACY_STATE_IN_CAMPAIGN,
                        new Object[] { campaignId, imageId.toString() }, String.class));
    } catch (org.springframework.dao.IncorrectResultSizeDataAccessException e) {
        if (e.getActualSize() > 1) {
            throw new DataAccessException("An image, '" + imageId
                    + "' has multiple privacy states in the same campaign: " + campaignId, e);
        }

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

From source file:com.orangeleap.common.security.OrangeLeapLdapUserSearch.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.
 *//*  w  ww .  j a  va  2s .  com*/
public DirContextOperations searchForUser(String username) {
    SpringSecurityLdapTemplate template = new SpringSecurityLdapTemplate(contextSource);

    template.setSearchControls(searchControls);

    try {

        String[] filterArgs;
        String aSearchBase;

        if (username.indexOf("@") > -1) {
            String[] split = username.split("@");
            filterArgs = new String[] { split[0] };
            aSearchBase = "o=" + split[1];
        } else {
            filterArgs = new String[] { username };
            aSearchBase = searchBase;
        }

        return template.searchForSingleEntry(aSearchBase, searchFilter, filterArgs);

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

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

/**
 * Returns the document role for a document that is directly associated
 * with a user. If the user is not directly associated with the document or
 * it doesn't exist, null is returned./* w w  w  .  j a v  a 2 s .  com*/
 * 
 * @param username The username of the user whose personal documents are
 *                being checked.
 * 
 * @param documentId The unique identifier for the document whose role is
 *                 desired.
 * 
 * @return If the document exist and the user is directly associated with 
 *          it, then their document role with said document is returned.
 *          Otherwise, null is returned.
 */
public Document.Role getDocumentRoleForDocumentSpecificToUser(String username, String documentId)
        throws DataAccessException {
    try {
        return Document.Role.getValue(getJdbcTemplate().queryForObject(
                SQL_GET_DOCUMENT_ROLES_FOR_DOCUMENT_SPECIFIC_TO_REQUESTING_USER,
                new Object[] { username, documentId }, String.class));
    } catch (org.springframework.dao.IncorrectResultSizeDataAccessException e) {
        if (e.getActualSize() > 1) {
            throw new DataAccessException("A user has more than one role with a document.", e);
        }

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