Search in sources :

Example 16 with Language

use of cn.edu.zju.acm.onlinejudge.bean.enumeration.Language in project zoj by licheng.

the class ContestPersistenceImpl method populatesLanguages.

/**
     * Populates a Limit with given ResultSet.
     * 
     * @param rs
     * @return a Limit instance
     * @throws SQLException
     * @throws PersistenceException
     */
private void populatesLanguages(Connection conn, List<AbstractContest> contests) throws SQLException, PersistenceException {
    if (contests.size() == 0) {
        return;
    }
    Map<Long, Language> languageMap = PersistenceManager.getInstance().getLanguagePersistence().getLanguageMap();
    PreparedStatement ps = null;
    try {
        Map<Long, AbstractContest> contestMap = new HashMap<Long, AbstractContest>();
        List<Long> contestIds = new ArrayList<Long>();
        for (AbstractContest contest : contests) {
            contestIds.add(contest.getId());
            contestMap.put(contest.getId(), contest);
            contest.setLanguages(new ArrayList<Language>());
        }
        ps = conn.prepareStatement(MessageFormat.format(ContestPersistenceImpl.GET_CONTEST_LANGUAGE, new Object[] { Database.createNumberValues(contestIds) }));
        ResultSet rs = ps.executeQuery();
        while (rs.next()) {
            Long contestId = new Long(rs.getLong(DatabaseConstants.CONTEST_LANGUAGE_CONTEST_ID));
            Long languageId = new Long(rs.getLong(DatabaseConstants.CONTEST_LANGUAGE_LANGUAGE_ID));
            contestMap.get(contestId).getLanguages().add(languageMap.get(languageId));
        }
    } finally {
        Database.dispose(ps);
    }
}
Also used : AbstractContest(cn.edu.zju.acm.onlinejudge.bean.AbstractContest) Language(cn.edu.zju.acm.onlinejudge.bean.enumeration.Language) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 17 with Language

use of cn.edu.zju.acm.onlinejudge.bean.enumeration.Language in project zoj by licheng.

the class JudgeClient method run.

@Override
public void run() {
    try {
        this.logger.info("Start to get information");
        try {
            this.sendInfoCommand();
            this.port = this.in.readInt();
            this.logger.info("port=" + this.port);
            this.supportedLanguages.clear();
            int n = this.in.readInt();
            if (n < 0 || n > LanguageManager.getNumberOfLanguages()) {
                this.logger.error("Invalid number of supported languages:" + n);
                return;
            }
            for (int i = 0; i < n; ++i) {
                int id = this.in.readInt();
                Language language = LanguageManager.getLanguage(id);
                if (language == null) {
                    this.logger.error("Invalid language id:" + id);
                    return;
                }
                this.supportedLanguages.add(language);
                this.logger.info("Supported language:" + language.getName());
            }
            this.initialized = true;
            for (int i = 0; i < this.defaultNumberOfJudgeThreads; ++i) {
                this.addJudgeThread();
            }
        } catch (IOException e) {
            this.logger.error("Fail to get information", e);
            return;
        }
        for (; ; ) {
            synchronized (this.pingCounter) {
                if (this.pingCounter[0] == 0) {
                    this.pingCounter.wait(JudgeClient.HEART_BEAT_INTERVAL);
                }
            }
            try {
                if (this.sendPingCommand() != JudgeReply.READY.getId()) {
                    break;
                }
                synchronized (this.pingBarrier) {
                    this.pingCounter[0] = 0;
                    this.pingBarrier.notifyAll();
                }
            } catch (IOException e) {
                this.logger.error("Send ping command failure", e);
                break;
            }
        }
    } catch (InterruptedException e) {
        this.logger.info("Interrupted");
    } finally {
        synchronized (this.pingBarrier) {
            this.pingCounter[0] = 0;
            Utility.closeSocket(this.socket);
            this.pingBarrier.notifyAll();
        }
        for (JudgeClientJudgeThread judgeThread : this.judgeThreads) {
            judgeThread.interrupt();
        }
    }
}
Also used : Language(cn.edu.zju.acm.onlinejudge.bean.enumeration.Language) IOException(java.io.IOException)

Example 18 with Language

use of cn.edu.zju.acm.onlinejudge.bean.enumeration.Language in project zoj by licheng.

the class SubmissionPersistenceImpl method populateSubmission.

/**
     * Populates an ExtendedSubmission with given ResultSet.
     * 
     * @param rs
     * @return an ExtendedSubmission instance
     * @throws SQLException
     */
private Submission populateSubmission(ResultSet rs, boolean withContent, Map<Long, Language> languageMap) throws SQLException {
    Submission submission = new Submission();
    submission.setId(rs.getLong(DatabaseConstants.SUBMISSION_SUBMISSION_ID));
    submission.setProblemId(rs.getLong(DatabaseConstants.SUBMISSION_PROBLEM_ID));
    submission.setUserProfileId(rs.getLong(DatabaseConstants.SUBMISSION_USER_PROFILE_ID));
    submission.setJudgeComment(rs.getString(DatabaseConstants.SUBMISSION_JUDGE_COMMENT));
    submission.setJudgeDate(Database.getDate(rs, DatabaseConstants.SUBMISSION_JUDGE_DATE));
    submission.setSubmitDate(Database.getDate(rs, DatabaseConstants.SUBMISSION_SUBMISSION_DATE));
    submission.setMemoryConsumption(rs.getInt(DatabaseConstants.SUBMISSION_MEMORY_CONSUMPTION));
    submission.setTimeConsumption(rs.getInt(DatabaseConstants.SUBMISSION_TIME_CONSUMPTION));
    submission.setUserName(rs.getString(DatabaseConstants.USER_PROFILE_NICKNAME));
    if (submission.getUserName().equals("")) {
        submission.setUserName(rs.getString(DatabaseConstants.USER_PROFILE_HANDLE));
    }
    submission.setProblemCode(rs.getString(DatabaseConstants.PROBLEM_CODE));
    submission.setContestId(rs.getLong("contest_id"));
    submission.setContestOrder(rs.getLong("contest_order"));
    if (withContent) {
        submission.setContent(rs.getString("content"));
    }
    // set language
    long languageId = rs.getLong(DatabaseConstants.SUBMISSION_LANGUAGE_ID);
    Language language = languageMap.get(languageId);
    submission.setLanguage(language);
    // set judge reply
    long judgeReplyId = rs.getLong(DatabaseConstants.SUBMISSION_JUDGE_REPLY_ID);
    JudgeReply judgeReply = JudgeReply.findById(judgeReplyId);
    submission.setJudgeReply(judgeReply);
    return submission;
}
Also used : Submission(cn.edu.zju.acm.onlinejudge.bean.Submission) Language(cn.edu.zju.acm.onlinejudge.bean.enumeration.Language) JudgeReply(cn.edu.zju.acm.onlinejudge.bean.enumeration.JudgeReply)

Example 19 with Language

use of cn.edu.zju.acm.onlinejudge.bean.enumeration.Language in project zoj by licheng.

the class SubmissionPersistenceImpl method getSubmission.

/**
     * <p>
     * Gets the submission with given id in persistence layer.
     * </p>
     * 
     * @param id
     *            the id of the submission
     * @return the submission with given id in persistence layer
     * @throws PersistenceException
     *             wrapping a persistence implementation specific exception
     */
public Submission getSubmission(long id) throws PersistenceException {
    Connection conn = null;
    try {
        conn = Database.createConnection();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(SubmissionPersistenceImpl.GET_SUBMISSION.replace("FORCE_INDEX", ""));
            ps.setLong(1, id);
            ResultSet rs = ps.executeQuery();
            if (!rs.next()) {
                return null;
            }
            Map<Long, Language> languageMap = PersistenceManager.getInstance().getLanguagePersistence().getLanguageMap();
            Submission submission = this.populateSubmission(rs, true, languageMap);
            return submission;
        } 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 : Language(cn.edu.zju.acm.onlinejudge.bean.enumeration.Language) Submission(cn.edu.zju.acm.onlinejudge.bean.Submission) 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 20 with Language

use of cn.edu.zju.acm.onlinejudge.bean.enumeration.Language 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)

Aggregations

Language (cn.edu.zju.acm.onlinejudge.bean.enumeration.Language)21 ArrayList (java.util.ArrayList)12 PreparedStatement (java.sql.PreparedStatement)8 Submission (cn.edu.zju.acm.onlinejudge.bean.Submission)7 ResultSet (java.sql.ResultSet)7 List (java.util.List)7 PersistenceException (cn.edu.zju.acm.onlinejudge.persistence.PersistenceException)6 Connection (java.sql.Connection)6 SQLException (java.sql.SQLException)6 Date (java.util.Date)5 AbstractContest (cn.edu.zju.acm.onlinejudge.bean.AbstractContest)4 Limit (cn.edu.zju.acm.onlinejudge.bean.Limit)3 Problemset (cn.edu.zju.acm.onlinejudge.bean.Problemset)3 JudgeReply (cn.edu.zju.acm.onlinejudge.bean.enumeration.JudgeReply)3 ActionForward (org.apache.struts.action.ActionForward)3 Course (cn.edu.zju.acm.onlinejudge.bean.Course)2 Problem (cn.edu.zju.acm.onlinejudge.bean.Problem)2 Reference (cn.edu.zju.acm.onlinejudge.bean.Reference)2 UserProfile (cn.edu.zju.acm.onlinejudge.bean.UserProfile)2 SubmissionCriteria (cn.edu.zju.acm.onlinejudge.bean.request.SubmissionCriteria)2