Search in sources :

Example 6 with PersistenceException

use of cn.edu.zju.acm.onlinejudge.persistence.PersistenceException in project zoj by licheng.

the class AuthorizationPersistenceImpl method getUserSecurity.

/**
     * <p>
     * Gets a UserSecurity instance with the given user id from persistence layer.
     * </p>
     * 
     * @param userProfileId
     *            the id of user profile used to get the UserSecurity instance
     * @return the UserSecurity instance with the given user id
     * @throws PersistenceException
     *             wrapping a persistence implementation specific exception
     */
public UserSecurity getUserSecurity(long userProfileId) throws PersistenceException {
    Connection conn = null;
    try {
        conn = Database.createConnection();
        PreparedStatement ps = null;
        ResultSet rs = null;
        boolean superAdmin = false;
        try {
            ps = conn.prepareStatement("SELECT super_admin FROM user_profile where user_profile_id=?");
            ps.setLong(1, userProfileId);
            rs = ps.executeQuery();
            if (rs.next()) {
                superAdmin = rs.getBoolean("super_admin");
            } else {
                return null;
            }
        } finally {
            Database.dispose(ps);
        }
        UserSecurity security = new UserSecurity(userProfileId, superAdmin);
        List<RoleSecurity> roles = new ArrayList<RoleSecurity>();
        Map<Long, RoleSecurity> roleIds = new HashMap<Long, RoleSecurity>();
        try {
            // select the roles;
            ps = conn.prepareStatement("SELECT role_id, name, description FROM role " + "WHERE role_id IN " + "(SELECT role_id from user_role WHERE user_profile_id = ?)");
            ps.setLong(1, userProfileId);
            rs = ps.executeQuery();
            while (rs.next()) {
                RoleSecurity role = new RoleSecurity(rs.getLong(1), rs.getString(2), rs.getString(3));
                roles.add(role);
                roleIds.put(role.getId(), role);
            }
        } finally {
            Database.dispose(ps);
        }
        try {
            // select the contests permissions
            ps = conn.prepareStatement("SELECT role_id, contest_id, permission_level_id FROM contest_permission " + "WHERE role_id IN " + "(SELECT role_id from user_role WHERE user_profile_id = ?)");
            ps.setLong(1, userProfileId);
            rs = ps.executeQuery();
            while (rs.next()) {
                RoleSecurity role = roleIds.get(rs.getLong(1));
                role.getContestPermission().addPermission(rs.getLong(2), PermissionLevel.findById(rs.getLong(3)));
            }
        } finally {
            Database.dispose(ps);
        }
        try {
            // select the forum permissions
            ps = conn.prepareStatement("SELECT role_id, forum_id, permission_level_id FROM forum_permission " + "WHERE role_id IN " + "(SELECT role_id from user_role WHERE user_profile_id = ?)");
            ps.setLong(1, userProfileId);
            rs = ps.executeQuery();
            while (rs.next()) {
                RoleSecurity role = roleIds.get(rs.getLong(1));
                role.getForumPermission().addPermission(rs.getLong(2), PermissionLevel.findById(rs.getLong(3)));
            }
        } finally {
            Database.dispose(ps);
        }
        for (RoleSecurity role : roles) {
            security.importRole(role);
        }
        return security;
    } catch (SQLException e) {
        throw new PersistenceException("Failed to get user security with id " + userProfileId, e);
    } finally {
        Database.dispose(conn);
    }
}
Also used : HashMap(java.util.HashMap) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) RoleSecurity(cn.edu.zju.acm.onlinejudge.security.RoleSecurity) UserSecurity(cn.edu.zju.acm.onlinejudge.security.UserSecurity) ResultSet(java.sql.ResultSet) PersistenceException(cn.edu.zju.acm.onlinejudge.persistence.PersistenceException)

Example 7 with PersistenceException

use of cn.edu.zju.acm.onlinejudge.persistence.PersistenceException in project zoj by licheng.

the class AuthorizationPersistenceImpl method updateRole.

/**
     * <p>
     * Updates the specified role in the persistence layer.
     * </p>
     * 
     * @param role
     *            the RoleSecurity instance to update
     * @param user
     *            the id of the user who made this modification
     * @throws PersistenceException
     *             wrapping a persistence implementation specific exception
     */
public void updateRole(RoleSecurity role, long user) throws PersistenceException {
    Connection conn = null;
    try {
        conn = Database.createConnection();
        conn.setAutoCommit(false);
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement("UPDATE role " + "SET name=?, description=?, last_update_user=?, last_update_date=NOW() " + "WHERE role_id=?");
            ps.setString(1, role.getName());
            ps.setString(2, role.getDescription());
            ps.setLong(3, user);
            ps.setLong(4, role.getId());
            ps.executeUpdate();
        } finally {
            Database.dispose(ps);
        }
        this.updateRolePermission("contest", role.getId(), role.getContestPermission(), conn);
        this.updateRolePermission("forum", role.getId(), role.getForumPermission(), conn);
        conn.commit();
    } catch (SQLException e) {
        Database.rollback(conn);
        throw new PersistenceException("Failed to update role.", e);
    } finally {
        Database.dispose(conn);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) PersistenceException(cn.edu.zju.acm.onlinejudge.persistence.PersistenceException) PreparedStatement(java.sql.PreparedStatement)

Example 8 with PersistenceException

use of cn.edu.zju.acm.onlinejudge.persistence.PersistenceException in project zoj by licheng.

the class AuthorizationPersistenceImpl method createRole.

/**
     * <p>
     * Creates the specified role in the persistence layer.
     * </p>
     * 
     * @param role
     *            the RoleSecurity instance to be created
     * @param user
     *            the id of the user who made this modification
     * @throws PersistenceException
     *             wrapping a persistence implementation specific exception
     */
public void createRole(RoleSecurity role, long user) throws PersistenceException {
    Connection conn = null;
    try {
        conn = Database.createConnection();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement("INSERT INTO role" + "(name, description, create_user, create_date, last_update_user, last_update_date) " + "VALUES(?, ?, ?, NOW(), ?, NOW())");
            ps.setString(1, role.getName());
            ps.setString(2, role.getDescription());
            ps.setLong(3, user);
            ps.setLong(4, user);
            ps.executeUpdate();
        } finally {
            Database.dispose(ps);
        }
    } catch (SQLException e) {
        throw new PersistenceException("Failed to create role.", e);
    } finally {
        Database.dispose(conn);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) PersistenceException(cn.edu.zju.acm.onlinejudge.persistence.PersistenceException) PreparedStatement(java.sql.PreparedStatement)

Example 9 with PersistenceException

use of cn.edu.zju.acm.onlinejudge.persistence.PersistenceException in project zoj by licheng.

the class ReferencePersistenceImpl method createProblemReference.

/**
     * <p>
     * Creates the specified problem reference in persistence layer.
     * </p>
     * 
     * @param problemId
     *            the id of the referred problem
     * @param reference
     *            the reference which the problem refer to
     * @param user
     *            the id of the user who made this modification
     * @throws PersistenceException
     *             wrapping a persistence implementation specific exception
     */
public void createProblemReference(long problemId, Reference reference, long user) throws PersistenceException {
    Connection conn = null;
    try {
        conn = Database.createConnection();
        conn.setAutoCommit(false);
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement("INSERT INTO reference (reference_type_id, name, content_type, " + "content, size, compressed, create_user, create_date, last_update_user, last_update_date) " + "VALUES(?,?,?,?,?,?,?,?,?,?)");
            ps.setLong(1, reference.getReferenceType().getId());
            ps.setString(2, reference.getName());
            ps.setString(3, reference.getContentType());
            ps.setBytes(4, reference.getContent());
            ps.setLong(5, reference.getSize());
            ps.setBoolean(6, reference.isCompressed());
            ps.setLong(7, user);
            ps.setTimestamp(8, new Timestamp(System.currentTimeMillis()));
            ps.setLong(9, user);
            ps.setTimestamp(10, new Timestamp(System.currentTimeMillis()));
            ps.executeUpdate();
        } finally {
            Database.dispose(ps);
        }
        reference.setId(Database.getLastId(conn));
        try {
            ps = conn.prepareStatement("INSERT INTO problem_reference (problem_id, reference_id) VALUES (?,?)");
            ps.setLong(1, problemId);
            ps.setLong(2, reference.getId());
            ps.executeUpdate();
        } finally {
            Database.dispose(ps);
        }
        conn.commit();
    } catch (Exception e) {
        Database.rollback(conn);
        throw new PersistenceException("Failed to create problem reference.", e);
    } finally {
        Database.dispose(conn);
    }
}
Also used : Connection(java.sql.Connection) PersistenceException(cn.edu.zju.acm.onlinejudge.persistence.PersistenceException) PreparedStatement(java.sql.PreparedStatement) Timestamp(java.sql.Timestamp) PersistenceException(cn.edu.zju.acm.onlinejudge.persistence.PersistenceException) SQLException(java.sql.SQLException)

Example 10 with PersistenceException

use of cn.edu.zju.acm.onlinejudge.persistence.PersistenceException in project zoj by licheng.

the class SubmissionPersistenceImpl method updateSubmission.

/**
     * <p>
     * Updates the specified submission in persistence layer.
     * </p>
     * 
     * @param submission
     *            the Submission instance to update
     * @param user
     *            the id of the user who made this modification
     * @throws PersistenceException
     *             wrapping a persistence implementation specific exception
     */
public void updateSubmission(Submission submission, long user) throws PersistenceException {
    Connection conn = null;
    try {
        conn = Database.createConnection();
        conn.setAutoCommit(false);
        // update the submission
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(submission.getContent() == null ? SubmissionPersistenceImpl.UPDATE_SUBMISSION_WITHOUT_CONTENT : SubmissionPersistenceImpl.UPDATE_SUBMISSION);
            ps.setLong(1, submission.getProblemId());
            ps.setLong(2, submission.getLanguage().getId());
            ps.setLong(3, submission.getJudgeReply().getId());
            ps.setLong(4, submission.getUserProfileId());
            ps.setInt(5, submission.getTimeConsumption());
            ps.setInt(6, submission.getMemoryConsumption());
            ps.setTimestamp(7, Database.toTimestamp(submission.getSubmitDate()));
            ps.setTimestamp(8, Database.toTimestamp(submission.getJudgeDate()));
            ps.setString(9, submission.getJudgeComment());
            ps.setLong(10, user);
            ps.setTimestamp(11, new Timestamp(new Date().getTime()));
            if (submission.getContent() == null) {
                ps.setLong(12, submission.getId());
            } else {
                ps.setString(12, submission.getContent());
                ps.setLong(13, submission.getId());
            }
            ps.executeUpdate();
        } finally {
            Database.dispose(ps);
        }
        // TODO(ob): update the user statistics if no tiger?
        conn.commit();
    } catch (Exception e) {
        Database.rollback(conn);
        throw new PersistenceException("Failed to update submission.", e);
    } finally {
        Database.dispose(conn);
    }
}
Also used : Connection(java.sql.Connection) PersistenceException(cn.edu.zju.acm.onlinejudge.persistence.PersistenceException) PreparedStatement(java.sql.PreparedStatement) Timestamp(java.sql.Timestamp) Date(java.util.Date) PersistenceException(cn.edu.zju.acm.onlinejudge.persistence.PersistenceException) SQLException(java.sql.SQLException)

Aggregations

PersistenceException (cn.edu.zju.acm.onlinejudge.persistence.PersistenceException)90 Connection (java.sql.Connection)87 PreparedStatement (java.sql.PreparedStatement)87 SQLException (java.sql.SQLException)87 ResultSet (java.sql.ResultSet)55 Timestamp (java.sql.Timestamp)29 Date (java.util.Date)28 ArrayList (java.util.ArrayList)25 Limit (cn.edu.zju.acm.onlinejudge.bean.Limit)7 Language (cn.edu.zju.acm.onlinejudge.bean.enumeration.Language)6 Problem (cn.edu.zju.acm.onlinejudge.bean.Problem)5 Forum (cn.edu.zju.acm.onlinejudge.bean.Forum)4 Submission (cn.edu.zju.acm.onlinejudge.bean.Submission)4 UserProfile (cn.edu.zju.acm.onlinejudge.bean.UserProfile)4 RoleSecurity (cn.edu.zju.acm.onlinejudge.security.RoleSecurity)4 HashMap (java.util.HashMap)4 PersistenceCreationException (cn.edu.zju.acm.onlinejudge.persistence.PersistenceCreationException)3 AbstractContest (cn.edu.zju.acm.onlinejudge.bean.AbstractContest)2 Configuration (cn.edu.zju.acm.onlinejudge.bean.Configuration)2 Post (cn.edu.zju.acm.onlinejudge.bean.Post)2