Example usage for org.springframework.jdbc.core PreparedStatementSetter PreparedStatementSetter

List of usage examples for org.springframework.jdbc.core PreparedStatementSetter PreparedStatementSetter

Introduction

In this page you can find the example usage for org.springframework.jdbc.core PreparedStatementSetter PreparedStatementSetter.

Prototype

PreparedStatementSetter

Source Link

Usage

From source file:org.cloudfoundry.identity.uaa.scim.dao.standard.JdbcScimGroupMembershipManager.java

@Override
public Set<ScimGroupInterface> removeMembersByMemberId(final String memberId)
        throws ScimResourceNotFoundException {
    Set<ScimGroupInterface> groups = getGroupsWithMember(memberId, false);
    logger.debug("removing " + memberId + " from groups: " + groups);

    int deleted = jdbcTemplate.update(DELETE_MEMBER_IN_GROUPS_SQL, new PreparedStatementSetter() {
        @Override/*ww w . j  a  v a  2  s .co  m*/
        public void setValues(PreparedStatement ps) throws SQLException {
            ps.setString(1, memberId);
        }
    });
    int expectedDelete = isUser(memberId) ? groups.size() - defaultUserGroups.size() : groups.size();
    if (deleted != expectedDelete) {
        throw new IncorrectResultSizeDataAccessException("unexpected number of members removed", expectedDelete,
                deleted);
    }

    return groups;
}

From source file:org.cloudfoundry.identity.uaa.scim.dao.standard.JdbcScimGroupProvisioning.java

@Override
public ScimGroupInterface create(final ScimGroupInterface group) throws InvalidScimResourceException {
    final String id = UUID.randomUUID().toString();
    logger.debug("creating new group with id: " + id);
    try {//from  w w  w  .ja  v  a  2 s .  c o  m
        jdbcTemplate.update(ADD_GROUP_SQL, new PreparedStatementSetter() {
            @Override
            public void setValues(PreparedStatement ps) throws SQLException {
                ps.setString(1, id);
                ps.setString(2, group.getDisplayName());
                ps.setTimestamp(3, new Timestamp(new Date().getTime()));
                ps.setTimestamp(4, new Timestamp(new Date().getTime()));
                ps.setInt(5, group.getVersion());
            }
        });
    } catch (DuplicateKeyException ex) {
        throw new ScimResourceAlreadyExistsException(
                "A group with displayName: " + group.getDisplayName() + " already exists.");
    }
    return retrieve(id);
}

From source file:org.cloudfoundry.identity.uaa.scim.dao.standard.JdbcScimGroupProvisioning.java

@Override
public ScimGroupInterface update(final String id, final ScimGroupInterface group)
        throws InvalidScimResourceException, ScimResourceNotFoundException {
    try {/*  w  w w  .  jav  a2 s . c o m*/
        int updated = jdbcTemplate.update(UPDATE_GROUP_SQL, new PreparedStatementSetter() {
            @Override
            public void setValues(PreparedStatement ps) throws SQLException {
                ps.setInt(1, group.getVersion() + 1);
                ps.setString(2, group.getDisplayName());
                ps.setTimestamp(3, new Timestamp(new Date().getTime()));
                ps.setString(4, id);
                ps.setInt(5, group.getVersion());
            }
        });
        if (updated != 1) {
            throw new IncorrectResultSizeDataAccessException(1, updated);
        }
        return retrieve(id);
    } catch (DuplicateKeyException ex) {
        throw new InvalidScimResourceException(
                "A group with displayName: " + group.getDisplayName() + " already exists");
    }
}

From source file:org.cloudfoundry.identity.uaa.scim.dao.standard.JdbcScimUserProvisioning.java

@Override
public ScimUserInterface create(final ScimUserInterface user) {

    validate(user);/*from   w w  w  .  ja v a 2 s. com*/
    logger.debug("Creating new user: " + user.getUserName());

    final String id = UUID.randomUUID().toString();
    try {
        jdbcTemplate.update(CREATE_USER_SQL, new PreparedStatementSetter() {
            @Override
            public void setValues(PreparedStatement ps) throws SQLException {

                ps.setString(1, id);
                ps.setInt(2, user.getVersion());
                ps.setTimestamp(3, new Timestamp(new Date().getTime()));
                ps.setTimestamp(4, new Timestamp(new Date().getTime()));
                ps.setString(5, user.getUserName());
                ps.setString(6, user.getPrimaryEmail());
                ps.setString(7, user.getGivenName());
                ps.setString(8, user.getFamilyName());
                ps.setBoolean(9, user.isActive());
                String phoneNumber = extractPhoneNumber(user);
                ps.setString(10, phoneNumber);
                ps.setBoolean(11, user.isVerified());
                ps.setString(12, user.getPassword());
            }
        });
    } catch (DuplicateKeyException e) {
        throw new ScimResourceAlreadyExistsException(
                "Username already in use (could be inactive account): " + user.getUserName());
    }
    return retrieve(id);
}

From source file:org.cloudfoundry.identity.uaa.scim.dao.standard.JdbcScimUserProvisioning.java

@Override
public ScimUserInterface update(final String id, final ScimUserInterface user)
        throws InvalidScimResourceException {

    validate(user);/* w w w. j a  v  a2 s. c  o  m*/
    logger.debug("Updating user " + user.getUserName());

    int updated = jdbcTemplate.update(UPDATE_USER_SQL, new PreparedStatementSetter() {
        @Override
        public void setValues(PreparedStatement ps) throws SQLException {

            ps.setInt(1, user.getVersion() + 1);
            ps.setTimestamp(2, new Timestamp(new Date().getTime()));
            ps.setString(3, user.getUserName());
            ps.setString(4, user.getPrimaryEmail());
            ps.setString(5, user.getGivenName());
            ps.setString(6, user.getFamilyName());
            ps.setBoolean(7, user.isActive());
            ps.setString(8, extractPhoneNumber(user));
            ps.setBoolean(9, user.isVerified());
            ps.setString(10, id);
            ps.setInt(11, user.getVersion());
        }
    });
    ScimUserInterface result = retrieve(id);
    if (updated == 0) {
        throw new OptimisticLockingFailureException(
                String.format("Attempt to update a user (%s) with wrong version: expected=%d but found=%d", id,
                        result.getVersion(), user.getVersion()));
    }
    if (updated > 1) {
        throw new IncorrectResultSizeDataAccessException(1);
    }
    return result;
}

From source file:org.cloudfoundry.identity.uaa.scim.dao.standard.JdbcScimUserProvisioning.java

@Override
public boolean changePassword(final String id, String oldPassword, final String newPassword)
        throws ScimResourceNotFoundException {

    if (oldPassword != null) {
        checkPasswordMatches(id, oldPassword);
    }/*from   w  ww  .  j av a2  s  .  c o  m*/
    passwordValidator.validate(newPassword, retrieve(id));
    final String encNewPassword = passwordEncoder.encode(newPassword);
    int updated = jdbcTemplate.update(CHANGE_PASSWORD_SQL, new PreparedStatementSetter() {
        @Override
        public void setValues(PreparedStatement ps) throws SQLException {

            ps.setTimestamp(1, new Timestamp(new Date().getTime()));
            ps.setString(2, encNewPassword);
            ps.setString(3, id);
        }
    });
    if (updated == 0) {
        throw new ScimResourceNotFoundException("User " + id + " does not exist");
    }
    if (updated != 1) {
        throw new ScimResourceConstraintFailedException("User " + id + " duplicated");
    }
    return true;
}

From source file:org.cloudfoundry.identity.uaa.scim.jdbc.JdbcScimGroupExternalMembershipManager.java

@Override
public ScimGroupExternalMember mapExternalGroup(final String groupId, final String externalGroup,
        final String origin) throws ScimResourceNotFoundException, MemberAlreadyExistsException {

    ScimGroup group = scimGroupProvisioning.retrieve(groupId);
    if (!StringUtils.hasText(externalGroup)) {
        throw new ScimResourceConstraintFailedException(
                "external group must not be null when mapping an external group");
    }/*from  w ww . j  a  v  a 2  s .  com*/
    if (!StringUtils.hasText(origin)) {
        throw new ScimResourceConstraintFailedException(
                "origin must not be null when mapping an external group");
    }
    if (null != group) {
        try {
            int result = jdbcTemplate.update(ADD_EXTERNAL_GROUP_MAPPING_SQL, new PreparedStatementSetter() {
                @Override
                public void setValues(PreparedStatement ps) throws SQLException {
                    ps.setString(1, groupId);
                    ps.setString(2, externalGroup);
                    ps.setTimestamp(3, new Timestamp(System.currentTimeMillis()));
                    ps.setString(4, origin);

                }
            });
            System.out.println("update count = " + result);
        } catch (DuplicateKeyException e) {
            // we should not throw, if the mapping exist, we should leave it
            // there.
            logger.info("The mapping between group " + group.getDisplayName() + " and external group "
                    + externalGroup + " already exists");
            // throw new
            // MemberAlreadyExistsException("The mapping between group " +
            // group.getDisplayName() + " and external group " +
            // externalGroup + " already exists");
        }
        return getExternalGroupMap(groupId, externalGroup, origin);
    } else {
        throw new ScimResourceNotFoundException("Group does not exist");
    }
}

From source file:org.cloudfoundry.identity.uaa.scim.jdbc.JdbcScimGroupExternalMembershipManager.java

@Override
public ScimGroupExternalMember unmapExternalGroup(final String groupId, final String externalGroup,
        final String origin) throws ScimResourceNotFoundException {

    ScimGroup group = scimGroupProvisioning.retrieve(groupId);
    ScimGroupExternalMember result = getExternalGroupMap(groupId, externalGroup, origin);
    if (null != group && null != result) {
        int count = jdbcTemplate.update(DELETE_EXTERNAL_GROUP_MAPPING_SQL, new PreparedStatementSetter() {
            @Override/*w w w .j  av a 2  s .c o m*/
            public void setValues(PreparedStatement ps) throws SQLException {
                ps.setString(1, groupId);
                ps.setString(2, externalGroup);
                ps.setString(3, origin);
            }
        });
        if (count == 1) {
            return result;
        } else if (count == 0) {
            throw new ScimResourceNotFoundException("No group mappings deleted.");
        } else {
            throw new InvalidResultSetAccessException("More than one mapping deleted count=" + count,
                    new SQLException());
        }
    } else {
        return null;
    }
}

From source file:org.cloudfoundry.identity.uaa.scim.jdbc.JdbcScimGroupExternalMembershipManager.java

@Override
public List<ScimGroupExternalMember> getExternalGroupMapsByGroupId(final String groupId, final String origin)
        throws ScimResourceNotFoundException {
    scimGroupProvisioning.retrieve(groupId);
    return jdbcTemplate.query(GET_EXTERNAL_GROUP_MAPPINGS_SQL, new PreparedStatementSetter() {
        @Override/*from ww  w  .j a  va2s  .  c o  m*/
        public void setValues(PreparedStatement ps) throws SQLException {
            ps.setString(1, groupId);
            ps.setString(2, origin);
        }
    }, rowMapper);
}

From source file:org.cloudfoundry.identity.uaa.scim.jdbc.JdbcScimGroupExternalMembershipManager.java

@Override
public List<ScimGroupExternalMember> getExternalGroupMapsByGroupName(final String groupName,
        final String origin) throws ScimResourceNotFoundException {

    final List<ScimGroup> groups = scimGroupProvisioning
            .query(String.format("displayName eq \"%s\"", groupName));

    if (null != groups && groups.size() > 0) {
        return jdbcTemplate.query(GET_EXTERNAL_GROUP_MAPPINGS_SQL, new PreparedStatementSetter() {
            @Override//from  ww w  .  j  a  v  a2  s. c o  m
            public void setValues(PreparedStatement ps) throws SQLException {
                ps.setString(1, groups.get(0).getId());
                ps.setString(2, origin);
            }
        }, rowMapper);
    } else {
        return null;
    }
}