Search in sources :

Example 26 with PersistenceException

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

the class ContestPersistenceImpl method getLastSubmitIP.

public String getLastSubmitIP(long userId, long contestId) throws PersistenceException {
    Connection conn = null;
    try {
        conn = Database.createConnection();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement("SELECT ip FROM user_contest_ip WHERE user_profile_id=? AND contest_id=?");
            ps.setLong(1, userId);
            ps.setLong(2, contestId);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                return rs.getString(1);
            } else {
                return null;
            }
        } finally {
            Database.dispose(ps);
        }
    } catch (SQLException e) {
        throw new PersistenceException("Failed to get last submit ip", 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 27 with PersistenceException

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

the class ContestPersistenceImpl method deleteContest.

/**
     * <p>
     * Deletes the specified contest in persistence layer.
     * </p>
     * 
     * @param id
     *            the id of the contest to delete
     * @param user
     *            the id of the user who made this modification
     * @throws PersistenceException
     *             wrapping a persistence implementation specific exception
     */
public void deleteContest(long id, long user) throws PersistenceException {
    Connection conn = null;
    try {
        conn = Database.createConnection();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(ContestPersistenceImpl.DELETE_CONTEST);
            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 contest");
            }
        } finally {
            Database.dispose(ps);
        }
    } catch (PersistenceException e) {
        throw e;
    } catch (SQLException e) {
        throw new PersistenceException("Failed to delete contest.", 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 28 with PersistenceException

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

the class ContestPersistenceImpl method updateDefaultLimit.

/**
     * Update the default limit.
     * 
     * @param limit
     *            the default limit.
     * @throws PersistenceException
     *             if failed to update the default limit
     */
public void updateDefaultLimit(Limit limit) throws PersistenceException {
    Connection conn = null;
    try {
        conn = Database.createConnection();
        PreparedStatement ps = null;
        synchronized (ContestPersistenceImpl.class) {
            try {
                ps = conn.prepareStatement(ContestPersistenceImpl.UPDATE_LIMIT);
                ps.setInt(1, limit.getTimeLimit());
                ps.setInt(2, limit.getMemoryLimit());
                ps.setInt(3, limit.getOutputLimit());
                ps.setInt(4, limit.getSubmissionLimit());
                ps.setLong(5, ContestPersistenceImpl.DEFAULT_LIMIT_ID);
                ps.executeUpdate();
            } finally {
                Database.dispose(ps);
            }
            ContestPersistenceImpl.defaultLimit = limit;
        }
    } catch (Exception e) {
        throw new PersistenceException("Failed to update the default limit", e);
    } finally {
        Database.dispose(conn);
    }
}
Also used : Connection(java.sql.Connection) PersistenceException(cn.edu.zju.acm.onlinejudge.persistence.PersistenceException) PreparedStatement(java.sql.PreparedStatement) PersistenceException(cn.edu.zju.acm.onlinejudge.persistence.PersistenceException) SQLException(java.sql.SQLException)

Example 29 with PersistenceException

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

the class ProblemPersistenceImpl method getProblem.

/**
     * <p>
     * Gets the problem with given id in persistence layer.
     * </p>
     * 
     * @param id
     *            the id of the problem
     * @return the problem with given id in persistence layer
     * @throws PersistenceException
     *             wrapping a persistence implementation specific exception
     */
public Problem getProblem(long id) throws PersistenceException {
    Connection conn = null;
    try {
        conn = Database.createConnection();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(ProblemPersistenceImpl.GET_PROBLEM);
            ps.setLong(1, id);
            ResultSet rs = ps.executeQuery();
            if (!rs.next()) {
                return null;
            }
            Problem problem = this.populateProblem(rs);
            long limitId = rs.getLong(DatabaseConstants.PROBLEM_LIMITS_ID);
            ps = conn.prepareStatement(ProblemPersistenceImpl.GET_LIMIT);
            ps.setLong(1, limitId);
            rs = ps.executeQuery();
            if (rs.next()) {
                Limit limit = this.populateLimit(rs);
                problem.setLimit(limit);
            }
            return problem;
        } finally {
            Database.dispose(ps);
        }
    } catch (SQLException e) {
        throw new PersistenceException("Failed to get the contest 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) Problem(cn.edu.zju.acm.onlinejudge.bean.Problem) Limit(cn.edu.zju.acm.onlinejudge.bean.Limit)

Example 30 with PersistenceException

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

the class ProblemPersistenceImpl method searchProblems.

/**
     * <p>
     * Searches all problems according with the given criteria in persistence layer.
     * </p>
     * 
     * @return a list of problems according with the given criteria
     * @param criteria
     *            the problem search criteria
     * @param offset
     *            the offset of the start position to search
     * @param count
     *            the maximum number of problems in returned list
     * @throws PersistenceException
     *             wrapping a persistence implementation specific exception
     */
public List<Problem> searchProblems(ProblemCriteria criteria, int offset, int count) throws PersistenceException {
    Connection conn = null;
    try {
        conn = Database.createConnection();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(this.buildSearchQuery(criteria, offset, count));
            ResultSet rs = ps.executeQuery();
            List<Problem> problems = new ArrayList<Problem>();
            while (rs.next()) {
                Problem problem = this.populateProblem(rs);
                Limit limit = this.populateLimit(rs);
                problem.setLimit(limit);
                problems.add(problem);
            }
            return problems;
        } finally {
            Database.dispose(ps);
        }
    } catch (SQLException e) {
        throw new PersistenceException("Failed to search the problems", e);
    } finally {
        Database.dispose(conn);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) PersistenceException(cn.edu.zju.acm.onlinejudge.persistence.PersistenceException) PreparedStatement(java.sql.PreparedStatement) Problem(cn.edu.zju.acm.onlinejudge.bean.Problem) Limit(cn.edu.zju.acm.onlinejudge.bean.Limit)

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