Search in sources :

Example 6 with ConnectionCallback

use of org.springframework.jdbc.core.ConnectionCallback in project uPortal by Jasig.

the class RDBMUserIdentityStore method updateUser.

protected void updateUser(final int userId, final IPerson person, final TemplateUser templateUser) throws Exception {
    // Remove my existing group memberships
    IGroupMember me = GroupService.getGroupMember(person.getEntityIdentifier());
    for (IEntityGroup eg : me.getParentGroups()) {
        ILockableEntityGroup leg = getSafeLockableGroup(eg, me);
        if (leg != null) {
            removePersonFromGroup(person, me, leg);
        }
    }
    // Copy template user's groups memberships
    IGroupMember template = GroupService.getEntity(templateUser.getUserName(), IPerson.class);
    for (IEntityGroup eg : template.getParentGroups()) {
        ILockableEntityGroup leg = getSafeLockableGroup(eg, me);
        if (leg != null) {
            addPersonToGroup(person, me, leg);
        }
    }
    this.transactionOperations.execute(new TransactionCallback<Object>() {

        @Override
        public Object doInTransaction(TransactionStatus status) {
            return jdbcOperations.execute(new ConnectionCallback<Object>() {

                @Override
                public Object doInConnection(Connection con) throws SQLException, DataAccessException {
                    PreparedStatement deleteStmt = null;
                    PreparedStatement queryStmt = null;
                    PreparedStatement insertStmt = null;
                    try {
                        // Update UP_USER
                        String update = "UPDATE UP_USER " + "SET USER_DFLT_USR_ID=?, " + "USER_DFLT_LAY_ID=?, " + "NEXT_STRUCT_ID=null " + "WHERE USER_ID=?";
                        insertStmt = con.prepareStatement(update);
                        insertStmt.setInt(1, templateUser.getUserId());
                        insertStmt.setInt(2, templateUser.getDefaultLayoutId());
                        insertStmt.setInt(3, userId);
                        if (log.isDebugEnabled())
                            log.debug("RDBMUserIdentityStore::addNewUser(): " + update);
                        insertStmt.executeUpdate();
                        insertStmt.close();
                        // Start copying...
                        ResultSet rs = null;
                        String delete = null;
                        String query = null;
                        String insert = null;
                        try {
                            // Update UP_USER_PROFILE
                            delete = "DELETE FROM UP_USER_PROFILE " + "WHERE USER_ID=?";
                            deleteStmt = con.prepareStatement(delete);
                            deleteStmt.setInt(1, userId);
                            if (log.isDebugEnabled())
                                log.debug("RDBMUserIdentityStore::updateUser(USER_ID=" + userId + "): " + delete);
                            deleteStmt.executeUpdate();
                            deleteStmt.close();
                            query = "SELECT USER_ID, PROFILE_FNAME, PROFILE_NAME, DESCRIPTION, " + "STRUCTURE_SS_ID, THEME_SS_ID " + "FROM UP_USER_PROFILE " + "WHERE USER_ID=?";
                            queryStmt = con.prepareStatement(query);
                            queryStmt.setInt(1, templateUser.getUserId());
                            if (log.isDebugEnabled())
                                log.debug("RDBMUserIdentityStore::updateUser(USER_ID=" + templateUser.getUserId() + "): " + query);
                            rs = queryStmt.executeQuery();
                            insert = "INSERT INTO UP_USER_PROFILE (USER_ID, PROFILE_ID, PROFILE_FNAME, PROFILE_NAME, DESCRIPTION, LAYOUT_ID, STRUCTURE_SS_ID, THEME_SS_ID) " + "VALUES(?, ?, ?, ?, ?, NULL, ?, ?)";
                            insertStmt = con.prepareStatement(insert);
                            while (rs.next()) {
                                int id = getNextKey();
                                String profileFname = rs.getString("PROFILE_FNAME");
                                String profileName = rs.getString("PROFILE_NAME");
                                String description = rs.getString("DESCRIPTION");
                                int structure = rs.getInt("STRUCTURE_SS_ID");
                                int theme = rs.getInt("THEME_SS_ID");
                                insertStmt.setInt(1, userId);
                                insertStmt.setInt(2, id);
                                insertStmt.setString(3, profileFname);
                                insertStmt.setString(4, profileName);
                                insertStmt.setString(5, description);
                                insertStmt.setInt(6, structure);
                                insertStmt.setInt(7, theme);
                                if (log.isDebugEnabled())
                                    log.debug("RDBMUserIdentityStore::updateUser(USER_ID=" + userId + ", PROFILE_FNAME=" + profileFname + ", PROFILE_NAME=" + profileName + ", DESCRIPTION=" + description + "): " + insert);
                                insertStmt.executeUpdate();
                            }
                            rs.close();
                            queryStmt.close();
                            insertStmt.close();
                            // If we made it all the way though, commit the transaction
                            if (RDBMServices.getDbMetaData().supportsTransactions())
                                con.commit();
                        } finally {
                            try {
                                rs.close();
                            } catch (Exception e) {
                            }
                        }
                    } finally {
                        try {
                            deleteStmt.close();
                        } catch (Exception e) {
                        }
                        try {
                            queryStmt.close();
                        } catch (Exception e) {
                        }
                        try {
                            insertStmt.close();
                        } catch (Exception e) {
                        }
                    }
                    return null;
                }
            });
        }
    });
}
Also used : Connection(java.sql.Connection) ILockableEntityGroup(org.apereo.portal.groups.ILockableEntityGroup) TransactionStatus(org.springframework.transaction.TransactionStatus) PreparedStatement(java.sql.PreparedStatement) DataAccessException(org.springframework.dao.DataAccessException) SQLException(java.sql.SQLException) IEntityGroup(org.apereo.portal.groups.IEntityGroup) IGroupMember(org.apereo.portal.groups.IGroupMember) ResultSet(java.sql.ResultSet) SerializableObject(org.apereo.portal.utils.SerializableObject) ConnectionCallback(org.springframework.jdbc.core.ConnectionCallback)

Example 7 with ConnectionCallback

use of org.springframework.jdbc.core.ConnectionCallback in project uPortal by Jasig.

the class RDBMUserIdentityStore method addNewUser.

protected int addNewUser(final int newUID, final IPerson person, final TemplateUser templateUser) throws Exception {
    // Copy template user's groups memberships
    IGroupMember me = GroupService.getGroupMember(person.getEntityIdentifier());
    IGroupMember template = GroupService.getEntity(templateUser.getUserName(), Class.forName("org.apereo.portal.security.IPerson"));
    for (IEntityGroup eg : template.getParentGroups()) {
        ILockableEntityGroup leg = getSafeLockableGroup(eg, me);
        if (leg != null) {
            addPersonToGroup(person, me, leg);
        }
    }
    return this.transactionOperations.execute(new TransactionCallback<Integer>() {

        @Override
        public Integer doInTransaction(TransactionStatus status) {
            return jdbcOperations.execute(new ConnectionCallback<Integer>() {

                @Override
                public Integer doInConnection(Connection con) throws SQLException, DataAccessException {
                    int uPortalUID = -1;
                    PreparedStatement queryStmt = null;
                    PreparedStatement insertStmt = null;
                    try {
                        // Add to UP_USER
                        String insert = "INSERT INTO UP_USER (USER_ID, USER_NAME, USER_DFLT_USR_ID, USER_DFLT_LAY_ID, NEXT_STRUCT_ID, LST_CHAN_UPDT_DT)" + "VALUES (?, ?, ?, ?, null, null)";
                        String userName = person.getUserName();
                        insertStmt = con.prepareStatement(insert);
                        insertStmt.setInt(1, newUID);
                        insertStmt.setString(2, userName);
                        insertStmt.setInt(3, templateUser.getUserId());
                        insertStmt.setInt(4, templateUser.getDefaultLayoutId());
                        if (log.isDebugEnabled())
                            log.debug("RDBMUserIdentityStore::addNewUser(USER_ID=" + newUID + ", USER_NAME=" + userName + ", USER_DFLT_USR_ID=" + templateUser.getUserId() + ", USER_DFLT_LAY_ID=" + templateUser.getDefaultLayoutId() + "): " + insert);
                        insertStmt.executeUpdate();
                        insertStmt.close();
                        insertStmt = null;
                        // Start copying...
                        ResultSet rs = null;
                        String query = null;
                        try {
                            // Add to UP_USER_PROFILE
                            query = "SELECT USER_ID, PROFILE_FNAME, PROFILE_NAME, DESCRIPTION, " + "STRUCTURE_SS_ID, THEME_SS_ID " + "FROM UP_USER_PROFILE " + "WHERE USER_ID=?";
                            queryStmt = con.prepareStatement(query);
                            queryStmt.setInt(1, templateUser.getUserId());
                            if (log.isDebugEnabled())
                                log.debug("RDBMUserIdentityStore::addNewUser(USER_ID=" + templateUser.getUserId() + "): " + query);
                            rs = queryStmt.executeQuery();
                            insert = "INSERT INTO UP_USER_PROFILE (USER_ID, PROFILE_ID, PROFILE_FNAME, PROFILE_NAME, DESCRIPTION, LAYOUT_ID, STRUCTURE_SS_ID, THEME_SS_ID) " + "VALUES(?, ?, ?, ?, ?, NULL, ?, ?)";
                            insertStmt = con.prepareStatement(insert);
                            while (rs.next()) {
                                int id = getNextKey();
                                String profileFname = rs.getString("PROFILE_FNAME");
                                String profileName = rs.getString("PROFILE_NAME");
                                String description = rs.getString("DESCRIPTION");
                                int structure = rs.getInt("STRUCTURE_SS_ID");
                                int theme = rs.getInt("THEME_SS_ID");
                                insertStmt.setInt(1, newUID);
                                insertStmt.setInt(2, id);
                                insertStmt.setString(3, profileFname);
                                insertStmt.setString(4, profileName);
                                insertStmt.setString(5, description);
                                insertStmt.setInt(6, structure);
                                insertStmt.setInt(7, theme);
                                if (log.isDebugEnabled())
                                    log.debug("RDBMUserIdentityStore::addNewUser(USER_ID=" + newUID + ", PROFILE_FNAME=" + profileFname + ", PROFILE_NAME=" + profileName + ", DESCRIPTION=" + description + "): " + insert);
                                insertStmt.executeUpdate();
                            }
                            rs.close();
                            queryStmt.close();
                            if (insertStmt != null) {
                                insertStmt.close();
                                insertStmt = null;
                            }
                            // If we made it all the way though, commit the transaction
                            if (RDBMServices.getDbMetaData().supportsTransactions())
                                con.commit();
                            uPortalUID = newUID;
                        } finally {
                            try {
                                if (rs != null)
                                    rs.close();
                            } catch (Exception e) {
                            }
                        }
                    } finally {
                        try {
                            if (queryStmt != null)
                                queryStmt.close();
                        } catch (Exception e) {
                        }
                        try {
                            if (insertStmt != null)
                                insertStmt.close();
                        } catch (Exception e) {
                        }
                    }
                    return uPortalUID;
                }
            });
        }
    });
}
Also used : Connection(java.sql.Connection) ILockableEntityGroup(org.apereo.portal.groups.ILockableEntityGroup) TransactionStatus(org.springframework.transaction.TransactionStatus) PreparedStatement(java.sql.PreparedStatement) DataAccessException(org.springframework.dao.DataAccessException) SQLException(java.sql.SQLException) IEntityGroup(org.apereo.portal.groups.IEntityGroup) IGroupMember(org.apereo.portal.groups.IGroupMember) ResultSet(java.sql.ResultSet) ConnectionCallback(org.springframework.jdbc.core.ConnectionCallback)

Example 8 with ConnectionCallback

use of org.springframework.jdbc.core.ConnectionCallback in project spring-boot by spring-projects.

the class AbstractDataSourcePoolMetadataTests method getPoolSizeTwoConnections.

@Test
public void getPoolSizeTwoConnections() {
    final JdbcTemplate jdbcTemplate = new JdbcTemplate(getDataSourceMetadata().getDataSource());
    jdbcTemplate.execute(new ConnectionCallback<Void>() {

        @Override
        public Void doInConnection(Connection connection) throws SQLException, DataAccessException {
            jdbcTemplate.execute(new ConnectionCallback<Void>() {

                @Override
                public Void doInConnection(Connection connection) throws SQLException, DataAccessException {
                    assertThat(getDataSourceMetadata().getActive()).isEqualTo(2);
                    assertThat(getDataSourceMetadata().getUsage()).isEqualTo(1.0f);
                    return null;
                }
            });
            return null;
        }
    });
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) ConnectionCallback(org.springframework.jdbc.core.ConnectionCallback) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) DataAccessException(org.springframework.dao.DataAccessException) Test(org.junit.Test)

Example 9 with ConnectionCallback

use of org.springframework.jdbc.core.ConnectionCallback in project uPortal by Jasig.

the class RDBMLocaleStore method updateUserLocales.

@Override
public void updateUserLocales(final IPerson person, final Locale[] locales) {
    this.transactionOperations.execute(new TransactionCallback<Object>() {

        @Override
        public Object doInTransaction(TransactionStatus status) {
            return jdbcOperations.execute(new ConnectionCallback<Object>() {

                @Override
                public Object doInConnection(Connection con) throws SQLException, DataAccessException {
                    // Delete the existing list of locales
                    final String delete = "DELETE FROM UP_USER_LOCALE WHERE USER_ID=?";
                    PreparedStatement pstmt = con.prepareStatement(delete);
                    try {
                        pstmt.clearParameters();
                        pstmt.setInt(1, person.getID());
                        logger.debug(delete);
                        pstmt.executeUpdate();
                    } finally {
                        pstmt.close();
                    }
                    // Insert the new list of locales
                    final String insert = "INSERT INTO UP_USER_LOCALE VALUES (?, ?, ?)";
                    pstmt = con.prepareStatement(insert);
                    try {
                        for (int i = 0; i < locales.length; i++) {
                            pstmt.clearParameters();
                            pstmt.setInt(1, person.getID());
                            pstmt.setString(2, locales[i].toString());
                            pstmt.setInt(3, i);
                            logger.debug(insert);
                            pstmt.executeUpdate();
                        }
                    } finally {
                        pstmt.close();
                    }
                    return null;
                }
            });
        }
    });
}
Also used : Connection(java.sql.Connection) TransactionStatus(org.springframework.transaction.TransactionStatus) ConnectionCallback(org.springframework.jdbc.core.ConnectionCallback) PreparedStatement(java.sql.PreparedStatement)

Aggregations

Connection (java.sql.Connection)9 ConnectionCallback (org.springframework.jdbc.core.ConnectionCallback)9 PreparedStatement (java.sql.PreparedStatement)6 ResultSet (java.sql.ResultSet)6 SQLException (java.sql.SQLException)6 TransactionStatus (org.springframework.transaction.TransactionStatus)6 DataAccessException (org.springframework.dao.DataAccessException)5 IJoinQueryString (org.apereo.portal.jdbc.IJoinQueryString)3 DatabaseMetaData (java.sql.DatabaseMetaData)2 Table (org.apache.ddlutils.model.Table)2 DatabaseMetaDataWrapper (org.apache.ddlutils.platform.DatabaseMetaDataWrapper)2 IEntityGroup (org.apereo.portal.groups.IEntityGroup)2 IGroupMember (org.apereo.portal.groups.IGroupMember)2 ILockableEntityGroup (org.apereo.portal.groups.ILockableEntityGroup)2 Statement (java.sql.Statement)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 SerializableObject (org.apereo.portal.utils.SerializableObject)1 Test (org.junit.Test)1 JdbcTemplate (org.springframework.jdbc.core.JdbcTemplate)1