Search in sources :

Example 66 with PersistenceException

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

the class ForumPersistenceImpl method createThread.

/**
     * <p>
     * Creates the specified thread in persistence layer.
     * </p>
     * 
     * @param thread
     *            the Thread instance to create
     * @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 createThread(Thread thread, long user) throws PersistenceException {
    Connection conn = null;
    try {
        conn = Database.createConnection();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(ForumPersistenceImpl.INSERT_THREAD);
            ps.setLong(1, thread.getForumId());
            ps.setLong(2, thread.getUserProfileId());
            ps.setString(3, thread.getTitle());
            ps.setLong(4, user);
            ps.setTimestamp(5, new Timestamp(new Date().getTime()));
            ps.setLong(6, user);
            ps.setTimestamp(7, new Timestamp(new Date().getTime()));
            ps.executeUpdate();
        } finally {
            Database.dispose(ps);
        }
        try {
            ps = conn.prepareStatement(ForumPersistenceImpl.GET_LAST_ID);
            ResultSet rs = ps.executeQuery();
            rs.next();
            thread.setId(rs.getLong(1));
        } finally {
            Database.dispose(ps);
        }
    } catch (SQLException e) {
        throw new PersistenceException("Failed to create thread.", 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) Timestamp(java.sql.Timestamp) Date(java.util.Date)

Example 67 with PersistenceException

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

the class ForumPersistenceImpl method createForum.

/**
     * <p>
     * Creates the specified forum in persistence layer.
     * </p>
     * 
     * @param forum
     *            the Forum instance to create
     * @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 createForum(Forum forum, long user) throws PersistenceException {
    Connection conn = null;
    try {
        conn = Database.createConnection();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(ForumPersistenceImpl.INSERT_FORUM);
            ps.setString(1, forum.getName());
            ps.setString(2, forum.getDescription());
            ps.setLong(3, user);
            ps.setTimestamp(4, new Timestamp(new Date().getTime()));
            ps.setLong(5, user);
            ps.setTimestamp(6, new Timestamp(new Date().getTime()));
            ps.executeUpdate();
        } finally {
            Database.dispose(ps);
        }
        try {
            // TODO(xuchuan): check all get last id. They should be put in a transaction. Or think about using a id
            // generator.
            ps = conn.prepareStatement(ForumPersistenceImpl.GET_LAST_ID);
            ResultSet rs = ps.executeQuery();
            rs.next();
            forum.setId(rs.getLong(1));
        } finally {
            Database.dispose(ps);
        }
    } catch (SQLException e) {
        throw new PersistenceException("Failed to create forum.", 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) Timestamp(java.sql.Timestamp) Date(java.util.Date)

Example 68 with PersistenceException

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

the class ForumPersistenceImpl method getPost.

/**
     * <p>
     * Gets the post with given id in persistence layer.
     * </p>
     * 
     * @param id
     *            the id of the post
     * @return the post with given id in persistence layer
     * @throws PersistenceException
     *             wrapping a persistence implementation specific exception
     */
public Post getPost(long id) throws PersistenceException {
    Connection conn = null;
    try {
        conn = Database.createConnection();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(ForumPersistenceImpl.GET_POST);
            ps.setLong(1, id);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                Post post = new Post();
                post.setId(rs.getLong(DatabaseConstants.POST_POST_ID));
                post.setThreadId(rs.getLong(DatabaseConstants.POST_THREAD_ID));
                post.setUserProfileId(rs.getLong(DatabaseConstants.POST_USER_PROFILE_ID));
                post.setContent(rs.getString(DatabaseConstants.POST_CONTENT));
                return post;
            } else {
                return null;
            }
        } finally {
            Database.dispose(ps);
        }
    } catch (SQLException e) {
        throw new PersistenceException("Failed to get the post with id " + id, e);
    } finally {
        Database.dispose(conn);
    }
}
Also used : SQLException(java.sql.SQLException) Post(cn.edu.zju.acm.onlinejudge.bean.Post) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PersistenceException(cn.edu.zju.acm.onlinejudge.persistence.PersistenceException) PreparedStatement(java.sql.PreparedStatement)

Example 69 with PersistenceException

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

the class ProblemPersistenceImpl method getProblemsCount.

public int getProblemsCount(long contestId) throws PersistenceException {
    Connection conn = null;
    try {
        conn = Database.createConnection();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement("select count(*) from problem where contest_id=? and active=1");
            ps.setLong(1, contestId);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                return rs.getInt(1);
            } else {
                return 0;
            }
        } finally {
            Database.dispose(ps);
        }
    } catch (SQLException e) {
        throw new PersistenceException("Failed to search the problems count", 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 70 with PersistenceException

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

the class SubmissionPersistenceImpl method getSubmissionSource.

public String getSubmissionSource(long id) throws PersistenceException {
    Connection conn = null;
    try {
        conn = Database.createConnection();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement("SELECT content FROM submission WHERE submission_id=?");
            ps.setLong(1, id);
            ResultSet rs = ps.executeQuery();
            if (!rs.next()) {
                throw new PersistenceException("Submission id " + id + " not found");
            }
            String content = rs.getString("content");
            if (content == null) {
                return "";
            } else {
                return content;
            }
        } finally {
            Database.dispose(ps);
        }
    } catch (SQLException e) {
        throw new PersistenceException("Failed to get the submission 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)

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