Search in sources :

Example 61 with PersistenceException

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

the class ForumPersistenceImpl method getForum.

/**
     * <p>
     * Get the forum with given id in persistence layer.
     * </p>
     * 
     * @param id
     *            the id of the forum
     * @return the forum with given id in persistence layer
     * @throws PersistenceException
     *             wrapping a persistence implementation specific exception
     */
public Forum getForum(long id) throws PersistenceException {
    Connection conn = null;
    try {
        conn = Database.createConnection();
        PreparedStatement ps = null;
        try {
            // TODO(xuchuan): move all prepareStatement out of try-catch
            ps = conn.prepareStatement(ForumPersistenceImpl.GET_FORUM);
            ps.setLong(1, id);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                Forum forum = new Forum();
                forum.setId(rs.getLong(DatabaseConstants.FORUM_FORUM_ID));
                forum.setName(rs.getString(DatabaseConstants.FORUM_NAME));
                forum.setDescription(rs.getString(DatabaseConstants.FORUM_DESCRIPTION));
                return forum;
            } else {
                return null;
            }
        } finally {
            Database.dispose(ps);
        }
    } catch (SQLException e) {
        throw new PersistenceException("Failed to get the forum 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) Forum(cn.edu.zju.acm.onlinejudge.bean.Forum)

Example 62 with PersistenceException

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

the class LanguagePersistenceImpl method updateLanguage.

/*
     * (non-Javadoc)
     * 
     * @see
     * cn.edu.zju.acm.onlinejudge.persistence.sql.LanguagePersistence#updateLanguage(cn.edu.zju.acm.onlinejudge.bean
     * .enumeration.Language, long)
     */
public void updateLanguage(Language language, long user) throws PersistenceException {
    Connection conn = null;
    try {
        conn = Database.createConnection();
        PreparedStatement ps = null;
        synchronized (this.allLanguages) {
            try {
                ps = conn.prepareStatement(LanguagePersistenceImpl.UPDATE_LANGUAGE);
                ps.setString(1, language.getName());
                ps.setString(2, language.getDescription());
                ps.setString(3, language.getOptions());
                ps.setString(4, language.getCompiler());
                ps.setLong(5, user);
                ps.setTimestamp(6, new Timestamp(new Date().getTime()));
                ps.setLong(7, language.getId());
                if (ps.executeUpdate() == 0) {
                    throw new PersistenceException("no such language");
                }
            } finally {
                Database.dispose(ps);
            }
            this.allLanguages.put(language.getId(), language);
        }
    } catch (PersistenceException pe) {
        throw pe;
    } catch (Exception e) {
        throw new PersistenceException("Failed to update language.", 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) PersistenceCreationException(cn.edu.zju.acm.onlinejudge.persistence.PersistenceCreationException)

Example 63 with PersistenceException

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

the class ProblemPersistenceImpl method deleteProblem.

/**
     * <p>
     * Deletes the specified problem in persistence layer.
     * </p>
     * 
     * @param id
     *            the id of the problem to delete
     * @param user
     *            the id of the user who made this modification
     * @throws PersistenceException
     *             wrapping a persistence implementation specific exception
     */
public void deleteProblem(long id, long user) throws PersistenceException {
    Connection conn = null;
    try {
        conn = Database.createConnection();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(ProblemPersistenceImpl.DELETE_PROBLEM);
            ps.setLong(1, user);
            ps.setTimestamp(2, new Timestamp(new Date().getTime()));
            ps.setLong(3, id);
            if (ps.executeUpdate() == 0) {
                throw new PersistenceException("no such problem");
            }
        } finally {
            Database.dispose(ps);
        }
    } catch (PersistenceException pe) {
        throw pe;
    } catch (Exception e) {
        throw new PersistenceException("Failed to delete contest.", 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)

Example 64 with PersistenceException

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

the class ProblemPersistenceImpl method createProblem.

/**
     * <p>
     * Creates the specified problem in persistence layer.
     * </p>
     * 
     * @param problem
     *            the Problem instance to create
     * @param user
     *            the id of the user who made this modification
     * @throws PersistenceException
     *             wrapping a persistence implementation specific exception
     */
public void createProblem(Problem problem, long user) throws PersistenceException {
    Connection conn = null;
    try {
        conn = Database.createConnection();
        conn.setAutoCommit(false);
        PreparedStatement ps = null;
        Limit limit;
        try {
            long contestLimitId = ProblemPersistenceImpl.DEFAULT_LIMIT_ID;
            ps = conn.prepareStatement(ProblemPersistenceImpl.GET_CONTEST_LIMIT_ID);
            ps.setLong(1, problem.getContestId());
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                contestLimitId = rs.getLong(1);
            }
            limit = problem.getLimit();
            if (limit == null) {
                limit = new Limit();
                limit.setId(contestLimitId);
                problem.setLimit(limit);
            }
            if (limit.getId() != contestLimitId) {
                ps = conn.prepareStatement(ProblemPersistenceImpl.INSERT_LIMIT);
                ps.setInt(1, limit.getTimeLimit());
                ps.setInt(2, limit.getMemoryLimit());
                ps.setInt(3, limit.getOutputLimit());
                ps.setInt(4, limit.getSubmissionLimit());
                ps.executeUpdate();
                limit.setId(Database.getLastId(conn));
            }
        } finally {
            Database.dispose(ps);
        }
        try {
            // create the problem
            ps = conn.prepareStatement(ProblemPersistenceImpl.INSERT_PROBLEM);
            ps.setLong(1, problem.getContestId());
            ps.setString(2, problem.getTitle());
            ps.setString(3, problem.getCode());
            ps.setLong(4, limit.getId());
            ps.setString(5, problem.getAuthor());
            ps.setString(6, problem.getSource());
            ps.setString(7, problem.getContest());
            ps.setBoolean(8, problem.isChecker());
            ps.setInt(9, 0);
            ps.setLong(10, user);
            ps.setTimestamp(11, new Timestamp(new Date().getTime()));
            ps.setLong(12, user);
            ps.setTimestamp(13, new Timestamp(new Date().getTime()));
            ps.setString(14, problem.getColor());
            ps.setInt(15, problem.getScore());
            ps.executeUpdate();
            problem.setId(Database.getLastId(conn));
        } finally {
            Database.dispose(ps);
        }
        conn.commit();
    } catch (Exception e) {
        Database.rollback(conn);
        throw new PersistenceException("Failed to create problem.", e);
    } finally {
        Database.dispose(conn);
    }
}
Also used : Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PersistenceException(cn.edu.zju.acm.onlinejudge.persistence.PersistenceException) PreparedStatement(java.sql.PreparedStatement) Limit(cn.edu.zju.acm.onlinejudge.bean.Limit) Timestamp(java.sql.Timestamp) Date(java.util.Date) PersistenceException(cn.edu.zju.acm.onlinejudge.persistence.PersistenceException) SQLException(java.sql.SQLException)

Example 65 with PersistenceException

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

the class ForumPersistenceImpl method updateThread.

/**
     * <p>
     * Updates the specified thread in persistence layer.
     * </p>
     * 
     * @param thread
     *            the Thread 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 updateThread(Thread thread, long user) throws PersistenceException {
    Connection conn = null;
    try {
        conn = Database.createConnection();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(ForumPersistenceImpl.UPDATE_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, thread.getId());
            if (ps.executeUpdate() == 0) {
                throw new PersistenceException("no such thread");
            }
        } finally {
            Database.dispose(ps);
        }
    } catch (PersistenceException pe) {
        throw pe;
    } catch (SQLException e) {
        throw new PersistenceException("Failed to update thread.", 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)

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