Search in sources :

Example 1 with Language

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

the class LanguageManager method reload.

public static synchronized void reload() throws PersistenceException {
    LanguageManager.languageMap.clear();
    LanguageManager.languageExtensionMap.clear();
    for (Language language : LanguageManager.languageDAO.getAllLanguages()) {
        LanguageManager.languageMap.put(language.getId(), language);
        LanguageManager.languageExtensionMap.put(language.getOptions(), language);
    }
}
Also used : Language(cn.edu.zju.acm.onlinejudge.bean.enumeration.Language)

Example 2 with Language

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

the class SubmissionSearchForm method toSubmissionCriteria.

public SubmissionCriteria toSubmissionCriteria() throws ParseException, NumberFormatException, PersistenceException {
    SubmissionCriteria criteria = new SubmissionCriteria();
    if (this.contestId != null && this.contestId.trim().length() > 0) {
        criteria.setContestId(Long.valueOf(this.contestId.trim()));
    }
    if (this.problemCode != null && this.problemCode.trim().length() > 0) {
        criteria.setProblemCode(this.problemCode);
    }
    if (this.handle != null && this.handle.trim().length() > 0) {
        criteria.setHandle(this.handle);
    }
    if (this.idStart != null && this.idStart.trim().length() > 0) {
        criteria.setIdStart(Long.valueOf(this.idStart.trim()));
    }
    if (this.idEnd != null && this.idEnd.trim().length() > 0) {
        criteria.setIdEnd(Long.valueOf(this.idEnd.trim()));
    }
    if (this.timeStart != null && this.timeStart.trim().length() > 0) {
        criteria.setTimeStart(Utility.parseTimestamp(this.timeStart));
    }
    if (this.timeEnd != null && this.timeEnd.trim().length() > 0) {
        criteria.setTimeEnd(Utility.parseTimestamp(this.timeEnd));
    }
    if (this.languageIds != null && this.languageIds.length > 0) {
        LanguagePersistence persistence = PersistenceManager.getInstance().getLanguagePersistence();
        List<Language> languages = new ArrayList<Language>();
        for (int i = 0; i < this.languageIds.length; ++i) {
            languages.add(persistence.getLanguage(Long.parseLong(this.languageIds[i])));
        }
        criteria.setLanguages(languages);
    }
    if (this.judgeReplyIds != null && this.judgeReplyIds.length > 0) {
        List<JudgeReply> judgeReplies = new ArrayList<JudgeReply>();
        for (int i = 0; i < this.judgeReplyIds.length; ++i) {
            judgeReplies.add(JudgeReply.findById(Long.parseLong(this.judgeReplyIds[i])));
        }
        criteria.setJudgeReplies(judgeReplies);
    }
    return criteria;
}
Also used : Language(cn.edu.zju.acm.onlinejudge.bean.enumeration.Language) JudgeReply(cn.edu.zju.acm.onlinejudge.bean.enumeration.JudgeReply) ArrayList(java.util.ArrayList) SubmissionCriteria(cn.edu.zju.acm.onlinejudge.bean.request.SubmissionCriteria) LanguagePersistence(cn.edu.zju.acm.onlinejudge.persistence.LanguagePersistence)

Example 3 with Language

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

the class SubmissionPersistenceImpl method buildQuery.

/**
     * Build search query.
     * 
     * @param criteria
     * @param lastId
     * @param count
     * @param conn
     * @param ps
     * @param rs
     * @return search query.
     * @throws SQLException
     */
private PreparedStatement buildQuery(String perfix, SubmissionCriteria criteria, long firstId, long lastId, int count, Connection conn) throws SQLException {
    // String userIndex = "index_submission_user";
    // String problemIndex = "index_submission_problem";
    String userIndex = "index_submission_user_reply_contest";
    String problemIndex = "index_submission_problem_reply";
    String judgeReplyIndex = "fk_submission_reply";
    String languageIndex = "index_submission_contest_language_order";
    String defaultIndex = "index_submission_contest_order";
    Set<String> easyProblems = new HashSet<String>(Arrays.asList(new String[] { "2060", "1180", "1067", "1292", "1295", "1951", "1025", "2095", "2105", "1008", "1005", "1152", "1240", "2107", "1037", "1205", "1113", "1045", "1489", "1241", "1101", "1049", "1057", "1003", "1151", "1048", "1002", "1115", "1001" }));
    Set<JudgeReply> easyJudgeReply = new HashSet<JudgeReply>(Arrays.asList(new JudgeReply[] { JudgeReply.ACCEPTED, JudgeReply.WRONG_ANSWER, JudgeReply.TIME_LIMIT_EXCEEDED, JudgeReply.MEMORY_LIMIT_EXCEEDED, JudgeReply.SEGMENTATION_FAULT, JudgeReply.COMPILATION_ERROR, JudgeReply.PRESENTATION_ERROR }));
    /*
         * INDEX optimization If user id presents, use fk_submission_user If problem id presents and submission number <
         * 5000, use fk_submission_problem; If judge_reply_id presents and none of id is 4,5,6,7,12,13 or 16, use
         * fk_submission_reply when otherwise use index_submission_contest_order;
         */
    String order = firstId == -1 ? "DESC" : "ASC";
    if (criteria.getIdStart() != null && firstId < criteria.getIdStart() - 1) {
        firstId = criteria.getIdStart() - 1;
    }
    if (criteria.getIdEnd() != null && lastId > criteria.getIdEnd() + 1) {
        lastId = criteria.getIdEnd() + 1;
    }
    StringBuilder query = new StringBuilder();
    query.append(perfix);
    query.append(" AND s.contest_id=" + criteria.getContestId());
    query.append(" AND contest_order BETWEEN " + (firstId + 1) + " and " + (lastId - 1));
    String index = null;
    if (criteria.getUserId() != null) {
        query.append(" AND s.user_profile_id=" + criteria.getUserId());
        index = userIndex;
    }
    if (criteria.getProblemId() != null) {
        query.append(" AND s.problem_id=" + criteria.getProblemId());
        if (index == null && !easyProblems.contains(criteria.getProblemCode())) {
            index = problemIndex;
        }
    }
    String inCondition = null;
    if (criteria.getJudgeReplies() != null) {
        if (criteria.getJudgeReplies().size() == 0) {
            return null;
        }
        List<Long> judgeRepliesIds = new ArrayList<Long>();
        boolean easy = false;
        for (JudgeReply judgeReply : criteria.getJudgeReplies()) {
            judgeRepliesIds.add(judgeReply.getId());
            if (easyJudgeReply.contains(judgeReply)) {
                easy = true;
            }
        }
        inCondition = " AND s.judge_reply_id IN " + Database.createNumberValues(judgeRepliesIds);
        query.append(inCondition);
        if (index == null && !easy) {
            if (criteria.getProblemId() != null) {
                index = problemIndex;
            } else {
                index = judgeReplyIndex;
            }
        }
    }
    PreparedStatement ps = null;
    if (index == null && criteria.getJudgeReplies() != null && criteria.getProblemId() != null) {
        try {
            ps = conn.prepareStatement("SELECT count(*) from submission s where problem_id=" + criteria.getProblemId() + inCondition);
            ResultSet rs = ps.executeQuery();
            rs.next();
            long cnt = rs.getLong(1);
            if (cnt < 10000) {
                index = problemIndex;
            }
        } finally {
            Database.dispose(ps);
        }
    }
    if (criteria.getLanguages() != null) {
        if (criteria.getLanguages().size() == 0) {
            return null;
        }
        List<Long> languageIds = new ArrayList<Long>();
        for (Language language : criteria.getLanguages()) {
            languageIds.add(language.getId());
        }
        query.append(" AND s.language_id IN " + Database.createNumberValues(languageIds));
        if (index == null)
            index = languageIndex;
    }
    query.append(" ORDER BY contest_order " + order);
    query.append(" LIMIT " + count);
    if (index == null) {
        index = defaultIndex;
    }
    String queryString = query.toString().replace("FORCE_INDEX", "USE INDEX (" + index + ")");
    return conn.prepareStatement(queryString);
}
Also used : ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) Language(cn.edu.zju.acm.onlinejudge.bean.enumeration.Language) JudgeReply(cn.edu.zju.acm.onlinejudge.bean.enumeration.JudgeReply) ResultSet(java.sql.ResultSet) HashSet(java.util.HashSet)

Example 4 with Language

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

the class SubmissionPersistenceImpl method searchSubmissions.

/**
     * <p>
     * Searches all submissions according with the given criteria in persistence layer.
     * </p>
     * 
     * @return a list of submissions according with the given criteria
     * @param criteria
     *            the submission search criteria
     * @param lastId
     *            the last id
     * @param count
     *            the maximum number of submissions in returned list
     * @throws PersistenceException
     *             wrapping a persistence implementation specific exception
     */
public List<Submission> searchSubmissions(SubmissionCriteria criteria, long firstId, long lastId, int count, boolean withContent) throws PersistenceException {
    if (lastId < 0) {
        throw new IllegalArgumentException("offset is negative");
    }
    if (count < 0) {
        throw new IllegalArgumentException("count is negative");
    }
    Connection conn = null;
    Map<Long, Language> languageMap = PersistenceManager.getInstance().getLanguagePersistence().getLanguageMap();
    try {
        conn = Database.createConnection();
        PreparedStatement ps = null;
        if (criteria.getUserId() == null && criteria.getHandle() != null) {
            try {
                ps = conn.prepareStatement("select user_profile_id from user_profile where handle=? AND active=1");
                ps.setString(1, criteria.getHandle());
                ResultSet rs = ps.executeQuery();
                if (!rs.next()) {
                    return new ArrayList<Submission>();
                }
                long userId = rs.getLong(1);
                criteria.setUserId(userId);
            } finally {
                Database.dispose(ps);
            }
        }
        if (criteria.getProblemId() == null && criteria.getProblemCode() != null) {
            try {
                ps = conn.prepareStatement("select problem_id from problem where code=? AND contest_id=? AND active=1");
                ps.setString(1, criteria.getProblemCode());
                ps.setLong(2, criteria.getContestId());
                ResultSet rs = ps.executeQuery();
                if (!rs.next()) {
                    return new ArrayList<Submission>();
                }
                long problemId = rs.getLong(1);
                criteria.setProblemId(problemId);
            } finally {
                Database.dispose(ps);
            }
        }
        try {
            ps = this.buildQuery(withContent ? SubmissionPersistenceImpl.GET_SUBMISSIONS_WITH_CONTENT : SubmissionPersistenceImpl.GET_SUBMISSIONS, criteria, firstId, lastId, count, conn);
            if (ps == null) {
                return new ArrayList<Submission>();
            }
            ResultSet rs = ps.executeQuery();
            List<Submission> submissions = new ArrayList<Submission>();
            while (rs.next()) {
                Submission submission = this.populateSubmission(rs, withContent, languageMap);
                submissions.add(submission);
            }
            return submissions;
        } finally {
            Database.dispose(ps);
        }
    } catch (SQLException e) {
        throw new PersistenceException("Failed to get the submissions", e);
    } finally {
        Database.dispose(conn);
    }
}
Also used : 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 5 with Language

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

the class ContestPersistenceImplTest method testUpdateLanguage.

/**
	 * Tests updateLanguage method
	 * @throws Exception to JUnit
	 */
public void testUpdateLanguage() throws Exception {
    List languages = persistence.getAllLanguages();
    for (int i = 0; i < 3; ++i) {
        Language language = (Language) languages.get(i);
        long id = language.getId();
        persistence.updateLanguage(new Language(id, "new language" + id, "new Language " + id, "new compiler" + id, "new options" + id), 10);
    }
    languages = persistence.getAllLanguages();
    for (int i = 0; i < 3; ++i) {
        Language language = (Language) languages.get(i);
        long id = language.getId();
        assertEquals("wrong name", "new language" + id, language.getName());
        assertEquals("wrong desc", "new Language " + id, language.getDescription());
        assertEquals("wrong options", "new options" + id, language.getOptions());
        assertEquals("wrong compiler", "new compiler" + id, language.getCompiler());
    }
}
Also used : Language(cn.edu.zju.acm.onlinejudge.bean.enumeration.Language) List(java.util.List) ArrayList(java.util.ArrayList)

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