Search in sources :

Example 56 with PersistenceException

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

the class ContestPersistenceImpl method updateContest.

/**
     * <p>
     * Updates the specified contest in persistence layer.
     * </p>
     * 
     * @param contest
     *            the AbstractContest instance to update
     * @param user
     *            the id of the user who made this modification
     * @throws PersistenceException
     *             wrapping a persistence implementation specific exception
     */
public void updateContest(AbstractContest contest, long user) throws PersistenceException {
    Connection conn = null;
    try {
        conn = Database.createConnection();
        conn.setAutoCommit(false);
        PreparedStatement ps = null;
        long contestLimitId = ContestPersistenceImpl.DEFAULT_LIMIT_ID;
        try {
            ps = conn.prepareStatement(ContestPersistenceImpl.GET_CONTEST_LIMIT_ID);
            ps.setLong(1, contest.getId());
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                contestLimitId = rs.getLong(1);
            }
        } finally {
            Database.dispose(ps);
        }
        // update the limit
        Limit limit = contest.getLimit();
        if (limit.getId() != ContestPersistenceImpl.DEFAULT_LIMIT_ID) {
            try {
                ps = conn.prepareStatement(ContestPersistenceImpl.INSERT_LIMIT);
                ps.setInt(1, limit.getTimeLimit());
                ps.setInt(2, limit.getMemoryLimit());
                ps.setInt(3, limit.getOutputLimit());
                ps.setInt(4, limit.getSubmissionLimit());
                ps.executeUpdate();
            } finally {
                Database.dispose(ps);
            }
            limit.setId(Database.getLastId(conn));
        }
        if (contestLimitId != limit.getId()) {
            // TODO(xuchuan) I don't understand what's that.
            try {
                ps = conn.prepareStatement(ContestPersistenceImpl.UPDATE_PROBLEM_LIMIT);
                ps.setLong(1, limit.getId());
                ps.setLong(2, contest.getId());
                ps.setLong(3, contestLimitId);
                ps.executeUpdate();
            } finally {
                Database.dispose(ps);
            }
        }
        try {
            // update the contest
            ps = conn.prepareStatement(ContestPersistenceImpl.UPDATE_CONTEST);
            ps.setString(1, contest.getTitle());
            ps.setString(2, contest.getDescription());
            if (contest.getStartTime() != null) {
                ps.setTimestamp(3, new Timestamp(contest.getStartTime().getTime()));
            } else {
                ps.setTimestamp(3, null);
            }
            if (contest.getEndTime() != null) {
                ps.setTimestamp(4, new Timestamp(contest.getEndTime().getTime()));
            } else {
                ps.setTimestamp(4, null);
            }
            ps.setLong(5, contest.getForumId());
            if (limit == null || limit.getId() == ContestPersistenceImpl.DEFAULT_LIMIT_ID) {
                ps.setLong(6, ContestPersistenceImpl.DEFAULT_LIMIT_ID);
            } else {
                ps.setLong(6, limit.getId());
            }
            ps.setBoolean(7, contest instanceof Problemset);
            ps.setLong(8, user);
            ps.setTimestamp(9, new Timestamp(new Date().getTime()));
            ps.setBoolean(10, contest.isCheckIp());
            ps.setLong(11, contest.getId());
            ps.executeUpdate();
        } finally {
            Database.dispose(ps);
        }
        try {
            // delete languages
            ps = conn.prepareStatement(ContestPersistenceImpl.DELETE_CONTEST_LANGUAGE);
            ps.setLong(1, contest.getId());
            ps.executeUpdate();
        } finally {
            Database.dispose(ps);
        }
        // insert languages
        if (contest.getLanguages() != null) {
            for (Language language : contest.getLanguages()) {
                try {
                    ps = conn.prepareStatement(ContestPersistenceImpl.INSERT_CONTEST_LANGUAGE);
                    ps.setLong(1, contest.getId());
                    ps.setLong(2, language.getId());
                    ps.executeUpdate();
                } finally {
                    Database.dispose(ps);
                }
            }
        }
        conn.commit();
    } catch (Exception e) {
        Database.rollback(conn);
        throw new PersistenceException("Failed to create contest.", e);
    } finally {
        Database.dispose(conn);
    }
}
Also used : Language(cn.edu.zju.acm.onlinejudge.bean.enumeration.Language) 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) Problemset(cn.edu.zju.acm.onlinejudge.bean.Problemset) Timestamp(java.sql.Timestamp) Date(java.util.Date) PersistenceException(cn.edu.zju.acm.onlinejudge.persistence.PersistenceException) SQLException(java.sql.SQLException)

Example 57 with PersistenceException

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

the class ContestPersistenceImpl method getContest.

/**
     * <p>
     * Gets the contest with given id in persistence layer.
     * </p>
     * 
     * @param id
     *            the id of the contest
     * @return the contest with given id in persistence layer
     * @throws PersistenceException
     *             wrapping a persistence implementation specific exception
     */
public AbstractContest getContest(long id) throws PersistenceException {
    Connection conn = null;
    try {
        conn = Database.createConnection();
        PreparedStatement ps = null;
        AbstractContest contest;
        try {
            ps = conn.prepareStatement(ContestPersistenceImpl.GET_CONTEST + " AND " + DatabaseConstants.CONTEST_CONTEST_ID + "=" + id);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                contest = this.populateContest(rs);
            } else {
                return null;
            }
        } finally {
            Database.dispose(ps);
        }
        List<AbstractContest> contests = new ArrayList<AbstractContest>();
        contests.add(contest);
        this.populatesLanguages(conn, contests);
        return contest;
    } catch (SQLException e) {
        throw new PersistenceException("Failed to get the contest with id " + id, e);
    } finally {
        Database.dispose(conn);
    }
}
Also used : AbstractContest(cn.edu.zju.acm.onlinejudge.bean.AbstractContest) 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)

Example 58 with PersistenceException

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

the class ContestPersistenceImpl method setLastSubmitIP.

public void setLastSubmitIP(long userId, long contestId, String ip) throws PersistenceException {
    Connection conn = null;
    try {
        conn = Database.createConnection();
        PreparedStatement ps = null;
        int ret;
        try {
            ps = conn.prepareStatement("UPDATE user_contest_ip SET ip=? WHERE user_profile_id=? AND contest_id=?");
            ps.setString(1, ip);
            ps.setLong(2, userId);
            ps.setLong(3, contestId);
            ret = ps.executeUpdate();
        } finally {
            Database.dispose(ps);
        }
        if (ret == 0) {
            try {
                ps = conn.prepareStatement("INSERT INTO user_contest_ip(user_profile_id, contest_id, ip) VALUES(?,?,?)");
                ps.setLong(1, userId);
                ps.setLong(2, contestId);
                ps.setString(3, ip);
                ps.executeUpdate();
            } finally {
                Database.dispose(ps);
            }
        }
    } catch (SQLException e) {
        throw new PersistenceException("Failed to set last submit ip", 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 59 with PersistenceException

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

the class ContestPersistenceImpl method loadDefaultLimit.

private static void loadDefaultLimit() throws PersistenceException {
    Connection conn = null;
    try {
        conn = Database.createConnection();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(ContestPersistenceImpl.SELECT_DEFAULT_LIMIT);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                ContestPersistenceImpl.defaultLimit = new Limit();
                ContestPersistenceImpl.defaultLimit.setId(rs.getLong(DatabaseConstants.LIMITS_LIMITS_ID));
                ContestPersistenceImpl.defaultLimit.setMemoryLimit(rs.getInt(DatabaseConstants.LIMITS_MEMORY_LIMIT));
                ContestPersistenceImpl.defaultLimit.setOutputLimit(rs.getInt(DatabaseConstants.LIMITS_OUTPUT_LIMIT));
                ContestPersistenceImpl.defaultLimit.setSubmissionLimit(rs.getInt(DatabaseConstants.LIMITS_SUBMISSION_LIMIT));
                ContestPersistenceImpl.defaultLimit.setTimeLimit(rs.getInt(DatabaseConstants.LIMITS_TIME_LIMIT));
            }
        } finally {
            Database.dispose(ps);
        }
    } catch (SQLException e) {
        throw new PersistenceException("Failed to get the default limit", 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) Limit(cn.edu.zju.acm.onlinejudge.bean.Limit)

Example 60 with PersistenceException

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

the class ContestPersistenceImpl method getContests.

/**
     * <p>
     * Gets a list of contests with given type in persistence layer.
     * </p>
     * 
     * @param isProblemset
     * @return a list of ProblemSet instances containing all problem sets in persistence layer
     * @throws PersistenceException
     *             wrapping a persistence implementation specific exception
     */
private List<AbstractContest> getContests(int contestType) throws PersistenceException {
    Connection conn = null;
    try {
        conn = Database.createConnection();
        PreparedStatement ps = null;
        List<AbstractContest> contests = new ArrayList<AbstractContest>();
        try {
            ps = conn.prepareStatement(ContestPersistenceImpl.GET_CONTEST + " AND " + DatabaseConstants.CONTEST_PROBLEMSET + "=" + contestType + " ORDER BY start_time DESC");
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                AbstractContest contest = this.populateContest(rs);
                contests.add(contest);
            }
        } finally {
            Database.dispose(ps);
        }
        this.populatesLanguages(conn, contests);
        return contests;
    } catch (Exception e) {
        throw new PersistenceException("Failed to get the contests", e);
    } finally {
        Database.dispose(conn);
    }
}
Also used : AbstractContest(cn.edu.zju.acm.onlinejudge.bean.AbstractContest) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) PersistenceException(cn.edu.zju.acm.onlinejudge.persistence.PersistenceException) PreparedStatement(java.sql.PreparedStatement) 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