Search in sources :

Example 81 with PersistenceException

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

the class ReferencePersistenceImpl method updateReference.

/**
     * <p>
     * Updates the specified reference in persistence layer.
     * </p>
     * 
     * @param reference
     *            the Reference instance to update
     * @param user
     *            the id of the user who made this modification
     * @throws PersistenceException
     *             wrapping a persistence implementation specific exception
     */
public void updateReference(Reference reference, long user) throws PersistenceException {
    Connection conn = null;
    try {
        conn = Database.createConnection();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement("UPDATE reference SET reference_type_id=?, name=?, content_type=?, " + "content=?, size=?, compressed=?, last_update_user=?, last_update_date=NOW() WHERE reference_id=?");
            ps.setLong(1, reference.getReferenceType().getId());
            ps.setString(2, reference.getName());
            ps.setString(3, reference.getContentType());
            ps.setBytes(4, reference.getContent());
            ps.setLong(5, reference.getSize());
            ps.setBoolean(6, reference.isCompressed());
            ps.setLong(7, user);
            ps.setLong(8, reference.getId());
            ps.executeUpdate();
        } finally {
            Database.dispose(ps);
        }
    } catch (SQLException e) {
        throw new PersistenceException("Failed to create problem.", 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 82 with PersistenceException

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

the class ReferencePersistenceImpl method deleteReference.

/**
     * <p>
     * Deletes the specified reference in persistence layer.
     * </p>
     * 
     * @param id
     *            the id of the reference to delete
     * @param user
     *            the id of the user who made this modification
     * @throws PersistenceException
     *             wrapping a persistence implementation specific exception
     */
public void deleteReference(long id, long user) throws PersistenceException {
    Connection conn = null;
    try {
        conn = Database.createConnection();
        conn.setAutoCommit(false);
        PreparedStatement ps = null;
        try {
            String query = "DELETE FROM problem_reference WHERE reference_id = ?";
            ps = conn.prepareStatement(query);
            ps.setLong(1, id);
            ps.executeUpdate();
        } finally {
            Database.dispose(ps);
        }
        try {
            ps = conn.prepareStatement("DELETE FROM reference WHERE reference_id = ?");
            ps.setLong(1, id);
            ps.executeUpdate();
        } 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) 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 83 with PersistenceException

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

the class SubmissionPersistenceImpl method getQueueingSubmissions.

public List<Submission> getQueueingSubmissions(long maxSubmissionId, int count) throws PersistenceException {
    Connection conn = null;
    try {
        conn = Database.createConnection();
        PreparedStatement ps = null;
        if (maxSubmissionId < 0) {
            ps = conn.prepareStatement("SELECT MAX(submission_id) FROM submission;");
            try {
                ResultSet rs = ps.executeQuery();
                rs.next();
                maxSubmissionId = rs.getLong(1);
            } finally {
                Database.dispose(ps);
            }
        }
        StringBuilder query = new StringBuilder(SubmissionPersistenceImpl.GET_SUBMISSIONS_WITH_CONTENT.replace("FORCE_INDEX", ""));
        query.append(" AND s.judge_reply_id=");
        query.append(JudgeReply.QUEUING.getId());
        query.append(" AND s.submission_id<=");
        query.append(maxSubmissionId);
        query.append(" ORDER BY s.submission_id DESC LIMIT ");
        query.append(count);
        System.out.println(query.toString());
        ps = conn.prepareStatement(query.toString());
        try {
            ResultSet rs = ps.executeQuery();
            List<Submission> submissions = new ArrayList<Submission>();
            Map<Long, Language> languageMap = PersistenceManager.getInstance().getLanguagePersistence().getLanguageMap();
            while (rs.next()) {
                Submission submission = this.populateSubmission(rs, true, languageMap);
                submissions.add(submission);
            }
            return submissions;
        } finally {
            Database.dispose(ps);
        }
    } catch (SQLException e) {
        throw new PersistenceException("Failed to get queueing submissions", e);
    } finally {
        Database.dispose(conn);
    }
}
Also used : Submission(cn.edu.zju.acm.onlinejudge.bean.Submission) Language(cn.edu.zju.acm.onlinejudge.bean.enumeration.Language) 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 84 with PersistenceException

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

the class SubmissionPersistenceImpl method getProblemStatistics.

public ProblemStatistics getProblemStatistics(long problemId, String orderBy, int count) throws PersistenceException {
    Connection conn = null;
    String ob = null;
    ProblemStatistics ret = null;
    if ("time".equals(orderBy)) {
        ob = "s.time_consumption ASC,memory_consumption ASC,s.submission_date ASC";
        ret = new ProblemStatistics(problemId, "time");
    } else if ("memory".equals(orderBy)) {
        ob = "s.memory_consumption ASC,s.time_consumption ASC,submission_date ASC";
        ret = new ProblemStatistics(problemId, "memory");
    } else {
        ob = "s.submission_date ASC,s.time_consumption ASC,memory_consumption ASC";
        ret = new ProblemStatistics(problemId, "date");
    }
    Map<Long, Language> languageMap = PersistenceManager.getInstance().getLanguagePersistence().getLanguageMap();
    try {
        conn = Database.createConnection();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement("SELECT judge_reply_id, count(*) FROM submission WHERE problem_id=? GROUP BY judge_reply_id");
            ps.setLong(1, problemId);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                long jid = rs.getLong(1);
                int c = rs.getInt(2);
                ret.setCount(jid, c);
            }
        } finally {
            Database.dispose(ps);
        }
        String sql = SubmissionPersistenceImpl.GET_SUBMISSIONS + " AND s.problem_id=? AND s.judge_reply_id=? ORDER BY " + ob + " LIMIT " + count;
        sql = sql.replace("FORCE_INDEX", "USE INDEX (index_submission_problem_reply)");
        try {
            ps = conn.prepareStatement(sql);
            ps.setLong(1, problemId);
            ps.setLong(2, JudgeReply.ACCEPTED.getId());
            ResultSet rs = ps.executeQuery();
            List<Submission> submissions = new ArrayList<Submission>();
            while (rs.next()) {
                Submission submission = this.populateSubmission(rs, false, languageMap);
                submissions.add(submission);
            }
            ret.setBestRuns(submissions);
            return ret;
        } finally {
            Database.dispose(ps);
        }
    } catch (SQLException e) {
        throw new PersistenceException("Failed to get the QQs", e);
    } finally {
        Database.dispose(conn);
    }
}
Also used : ProblemStatistics(cn.edu.zju.acm.onlinejudge.util.ProblemStatistics) Submission(cn.edu.zju.acm.onlinejudge.bean.Submission) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) Language(cn.edu.zju.acm.onlinejudge.bean.enumeration.Language) ResultSet(java.sql.ResultSet) PersistenceException(cn.edu.zju.acm.onlinejudge.persistence.PersistenceException)

Example 85 with PersistenceException

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

the class PerformanceManager method searchLogs.

public synchronized List<AccessLog> searchLogs(LogCriteria criteria, int offset, int count, String orderBy) throws PersistenceException {
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    try {
        conn = Database.createConnection();
        ps = buildQuery(criteria, offset, count, orderBy, conn);
        rs = ps.executeQuery();
        List<AccessLog> logs = new ArrayList<AccessLog>();
        while (rs.next()) {
            AccessLog log = new AccessLog();
            log.setId(rs.getLong("access_log_id"));
            if (rs.getObject("user_profile_id") != null) {
                log.setUserId(rs.getLong("user_profile_id"));
            }
            if (rs.getObject("handle") != null) {
                log.setHandle(rs.getString("handle"));
            }
            log.setUrl(rs.getString("url"));
            log.setAction(rs.getString("action"));
            log.setIp(rs.getString("ip"));
            log.setTimestamp(new Date(rs.getTimestamp("timestamp").getTime()));
            log.setAccessTime(rs.getLong("access_time"));
            logs.add(log);
        }
        return logs;
    } catch (SQLException e) {
        throw new PersistenceException("Failed to search logs.", e);
    } finally {
        Database.dispose(ps);
        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) 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