Search in sources :

Example 61 with DatabaseException

use of net.jforum.exceptions.DatabaseException in project jforum2 by rafaelsteil.

the class GenericGroupSecurityDAO method loadRoles.

protected RoleCollection loadRoles(int[] groupIds) {
    String sql = SystemGlobals.getSql("PermissionControl.loadGroupRoles");
    String groupIdAsString = SecurityCommon.groupIdAsString(groupIds);
    if ("".equals(groupIdAsString)) {
        // We suppose there is no "negative" group ids
        sql = sql.replaceAll("#IN#", "-1");
    } else {
        sql = sql.replaceAll("#IN#", groupIdAsString);
    }
    RoleCollection roles = new RoleCollection();
    PreparedStatement p = null;
    ResultSet rs = null;
    try {
        p = JForumExecutionContext.getConnection().prepareStatement(sql);
        rs = p.executeQuery();
        roles = SecurityCommon.loadRoles(rs);
    } catch (Exception e) {
        throw new DatabaseException(e);
    } finally {
        DbUtils.close(rs, p);
    }
    return roles;
}
Also used : ResultSet(java.sql.ResultSet) RoleCollection(net.jforum.security.RoleCollection) PreparedStatement(java.sql.PreparedStatement) DatabaseException(net.jforum.exceptions.DatabaseException) DatabaseException(net.jforum.exceptions.DatabaseException) SQLException(java.sql.SQLException)

Example 62 with DatabaseException

use of net.jforum.exceptions.DatabaseException in project jforum2 by rafaelsteil.

the class SecurityCommon method executeAddRole.

/**
	 * Execute the <i>add role</i> thing. As the SQL statement to insert user and group roles are
	 * diferent, they cannot be manipuled with a 'generic' statement, and is for this reason that
	 * <code>addRole</code> method is marked abstract. <br>
	 * The only job the <code>addRole</code> method should do is to get the correct SQL statement
	 * for each case - user or group - and the repass it to this method, who then do the job for us.
	 * 
	 * @param sql The SQL statement to be executed.
	 * @param id The ID do insert. May be user's or group's id, depending of the situation ( the caller )
	 * @param role The role name to insert
	 * @param roleValues A <code>RoleValueCollection</code> collection containing the role values to
	 * insert. If none is wanted, just pass null as argument.
	 * @param supportAutoGeneratedKeys Set to <code>true</code> if <i>Statement.RETURN_GENERATED_KEYS</i> is supported
	 * by the Driver, or <code>false</code> if not.
	 * @param autoKeysQuery String
	 */
public static void executeAddRole(String sql, int id, Role role, RoleValueCollection roleValues, boolean supportAutoGeneratedKeys, String autoKeysQuery) {
    PreparedStatement p = null;
    ResultSet rs = null;
    try {
        if (supportAutoGeneratedKeys) {
            p = JForumExecutionContext.getConnection().prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
        } else {
            p = JForumExecutionContext.getConnection().prepareStatement(sql);
        }
        p.setInt(1, id);
        p.setString(2, role.getName());
        p.executeUpdate();
        if (roleValues != null) {
            int roleId = -1;
            if (supportAutoGeneratedKeys) {
                rs = p.getGeneratedKeys();
                rs.next();
                roleId = rs.getInt(1);
            } else {
                p = JForumExecutionContext.getConnection().prepareStatement(autoKeysQuery);
                rs = p.executeQuery();
                if (rs.next()) {
                    roleId = rs.getInt(1);
                }
            }
            rs.close();
            rs = null;
            p.close();
            p = null;
            if (roleId == -1) {
                throw new SQLException("Could not obtain the latest role id");
            }
            p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("PermissionControl.addRoleValues"));
            for (Iterator iter = roleValues.iterator(); iter.hasNext(); ) {
                RoleValue rv = (RoleValue) iter.next();
                p.setInt(1, roleId);
                p.setString(2, rv.getValue());
                p.executeUpdate();
            }
        }
    } catch (SQLException e) {
        throw new DatabaseException(e);
    } finally {
        DbUtils.close(rs, p);
    }
}
Also used : SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) Iterator(java.util.Iterator) RoleValue(net.jforum.security.RoleValue) PreparedStatement(java.sql.PreparedStatement) DatabaseException(net.jforum.exceptions.DatabaseException)

Example 63 with DatabaseException

use of net.jforum.exceptions.DatabaseException in project jforum2 by rafaelsteil.

the class GenericSmilieDAO method selectById.

/**
	 * @see net.jforum.dao.SmilieDAO#selectById(int)
	 */
public Smilie selectById(int id) {
    PreparedStatement p = null;
    ResultSet rs = null;
    try {
        p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("SmiliesModel.selectById"));
        p.setInt(1, id);
        Smilie s = new Smilie();
        rs = p.executeQuery();
        if (rs.next()) {
            s = this.getSmilie(rs);
        }
        return s;
    } catch (SQLException e) {
        throw new DatabaseException(e);
    } finally {
        DbUtils.close(rs, p);
    }
}
Also used : SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Smilie(net.jforum.entities.Smilie) DatabaseException(net.jforum.exceptions.DatabaseException)

Example 64 with DatabaseException

use of net.jforum.exceptions.DatabaseException in project jforum2 by rafaelsteil.

the class GenericSummaryDAO method selectLastPosts.

/**
	 * @see net.jforum.dao.SummaryDAO#selectById(Date, Date)
	 */
public List selectLastPosts(Date firstDate, Date lastDate) {
    String query = SystemGlobals.getSql("SummaryDAO.selectPosts");
    PreparedStatement p = null;
    ResultSet rs = null;
    try {
        p = JForumExecutionContext.getConnection().prepareStatement(query);
        p.setTimestamp(1, new Timestamp(firstDate.getTime()));
        p.setTimestamp(2, new Timestamp(lastDate.getTime()));
        List posts = new ArrayList();
        rs = p.executeQuery();
        while (rs.next()) {
            posts.add(this.fillPost(rs));
        }
        return posts;
    } catch (SQLException e) {
        throw new DatabaseException(e);
    } finally {
        DbUtils.close(rs, p);
    }
}
Also used : SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) ArrayList(java.util.ArrayList) List(java.util.List) Timestamp(java.sql.Timestamp) DatabaseException(net.jforum.exceptions.DatabaseException)

Example 65 with DatabaseException

use of net.jforum.exceptions.DatabaseException in project jforum2 by rafaelsteil.

the class GenericSummaryDAO method listRecipients.

public List listRecipients() {
    String query = SystemGlobals.getSql("SummaryDAO.selectAllRecipients");
    PreparedStatement p = null;
    ResultSet rs = null;
    try {
        p = JForumExecutionContext.getConnection().prepareStatement(query);
        List recipients = new ArrayList();
        rs = p.executeQuery();
        String mail = null;
        while (rs.next()) {
            mail = rs.getString("user_email");
            if (mail != null && !mail.trim().equals("")) {
                recipients.add(mail);
            }
        }
        return recipients;
    } catch (SQLException e) {
        throw new DatabaseException(e);
    } finally {
        DbUtils.close(rs, p);
    }
}
Also used : SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) ArrayList(java.util.ArrayList) List(java.util.List) DatabaseException(net.jforum.exceptions.DatabaseException)

Aggregations

DatabaseException (net.jforum.exceptions.DatabaseException)264 PreparedStatement (java.sql.PreparedStatement)255 SQLException (java.sql.SQLException)254 ResultSet (java.sql.ResultSet)138 List (java.util.List)64 ArrayList (java.util.ArrayList)63 Timestamp (java.sql.Timestamp)17 User (net.jforum.entities.User)15 Iterator (java.util.Iterator)12 Post (net.jforum.entities.Post)9 Date (java.util.Date)8 HashMap (java.util.HashMap)8 Map (java.util.Map)8 Topic (net.jforum.entities.Topic)8 Connection (java.sql.Connection)5 Bookmark (net.jforum.entities.Bookmark)5 KarmaStatus (net.jforum.entities.KarmaStatus)4 SimpleDateFormat (java.text.SimpleDateFormat)3 PrivateMessage (net.jforum.entities.PrivateMessage)3 Ranking (net.jforum.entities.Ranking)3