Search in sources :

Example 46 with PersistenceException

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

the class UserPersistenceImpl method updateUserPreference.

/**
     * <p>
     * Updates the specified user preference in persistence layer.
     * </p>
     * 
     * @param preference
     *            the UserPreference instance to update
     * @param user
     *            the id of the user who made this modification
     * @throws NullPointerException
     *             if argument is null
     * @throws PersistenceException
     *             wrapping a persistence implementation specific exception
     */
public void updateUserPreference(UserPreference preference, long user) throws PersistenceException {
    Connection conn = null;
    try {
        conn = Database.createConnection();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(UserPersistenceImpl.UPDATE_USER_PREFERENCE);
            ps.setString(1, preference.getPlan());
            ps.setInt(2, preference.getProblemPaging());
            ps.setInt(3, preference.getSubmissionPaging());
            ps.setInt(4, preference.getStatusPaging());
            ps.setInt(5, preference.getUserPaging());
            ps.setInt(6, preference.getThreadPaging());
            ps.setInt(7, preference.getPostPaging());
            ps.setLong(8, user);
            ps.setTimestamp(9, new Timestamp(new Date().getTime()));
            ps.setLong(10, preference.getId());
            ps.executeUpdate();
        } finally {
            Database.dispose(ps);
        }
    } catch (SQLException e) {
        throw new PersistenceException("Failed to create preference.", 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) Timestamp(java.sql.Timestamp) Date(java.util.Date)

Example 47 with PersistenceException

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

the class UserPersistenceImpl method getUserProfile.

/**
     * <p>
     * Gets the user profile with given id in persistence layer.
     * </p>
     * 
     * @param id
     *            the id of the user profile
     * @return the user profile with given id in persistence layer
     * @throws PersistenceException
     *             wrapping a persistence implementation specific exception
     */
public UserProfile getUserProfile(long id) throws PersistenceException {
    Connection conn = null;
    try {
        conn = Database.createConnection();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(UserPersistenceImpl.GET_USER_BY_ID);
            ps.setLong(1, id);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                return this.populateUserProfile(rs);
            } else {
                return null;
            }
        } finally {
            Database.dispose(ps);
        }
    } catch (SQLException e) {
        throw new PersistenceException("Failed to get the user profile with id " + id, e);
    } finally {
        Database.dispose(conn);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PersistenceException(cn.edu.zju.acm.onlinejudge.persistence.PersistenceException) PreparedStatement(java.sql.PreparedStatement)

Example 48 with PersistenceException

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

the class AuthorizationPersistenceImplTest method testUpdateForum2.

/**
	 * Tests updateForum method
	 * @throws Exception to JUnit
	 */
public void testUpdateForum2() throws Exception {
    try {
        Forum forum = new Forum();
        forum.setName("foo");
        forum.setDescription("bar");
        persistence.updateForum(forum, 1);
        fail("PersistenceException should be thrown");
    } catch (PersistenceException pe) {
    // ok
    }
}
Also used : PersistenceException(cn.edu.zju.acm.onlinejudge.persistence.PersistenceException) Forum(cn.edu.zju.acm.onlinejudge.bean.Forum)

Example 49 with PersistenceException

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

the class ForumPersistenceImplTest method testUpdateForum2.

/**
	 * Tests updateForum method
	 * @throws Exception to JUnit
	 */
public void testUpdateForum2() throws Exception {
    try {
        Forum forum = new Forum();
        forum.setName("foo");
        forum.setDescription("bar");
        persistence.updateForum(forum, 1);
        fail("PersistenceException should be thrown");
    } catch (PersistenceException pe) {
    // ok
    }
}
Also used : PersistenceException(cn.edu.zju.acm.onlinejudge.persistence.PersistenceException) Forum(cn.edu.zju.acm.onlinejudge.bean.Forum)

Example 50 with PersistenceException

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

the class AuthorizationPersistenceImpl method deleteRole.

/**
     * <p>
     * Deletes the specified role in the persistence layer.
     * </p>
     * 
     * @param id
     *            the id of the role to be deleted
     * @param user
     *            the id of the user who made this modification
     * @throws PersistenceException
     *             wrapping a persistence implementation specific exception
     */
public void deleteRole(long id, long user) throws PersistenceException {
    Connection conn = null;
    try {
        conn = Database.createConnection();
        conn.setAutoCommit(false);
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement("DELETE FROM user_role WHERE role_id=?");
            ps.setLong(1, id);
            ps.executeUpdate();
        } finally {
            Database.dispose(ps);
        }
        try {
            ps = conn.prepareStatement("DELETE FROM contest_permission WHERE role_id=?");
            ps.setLong(1, id);
            ps.executeUpdate();
        } finally {
            Database.dispose(ps);
        }
        try {
            ps = conn.prepareStatement("DELETE FROM forum_permission WHERE role_id=?");
            ps.setLong(1, id);
            ps.executeUpdate();
        } finally {
            Database.dispose(ps);
        }
        try {
            ps = conn.prepareStatement("DELETE FROM role WHERE role_id=?");
            ps.setLong(1, id);
            ps.executeUpdate();
        } finally {
            Database.dispose(ps);
        }
        conn.commit();
    } catch (SQLException e) {
        Database.rollback(conn);
        throw new PersistenceException("Failed to delete 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)

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