Search in sources :

Example 26 with AbstractContest

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

the class ContestForm method toContest.

public AbstractContest toContest() throws ParseException, NumberFormatException, PersistenceException {
    AbstractContest contest = null;
    if (this.contestType == 1) {
        contest = new Problemset();
    } else if (this.contestType == 0) {
        contest = new Contest();
    } else {
        contest = new Course();
    }
    if (this.startTime != null && this.startTime.trim().length() > 0) {
        contest.setStartTime(Utility.parseTimestamp(this.startTime));
        contest.setEndTime(new Date(Utility.parseTimestamp(this.startTime).getTime() + Utility.parseTime(this.contestLength) * 1000));
    }
    try {
        if (this.id != null) {
            contest.setId(Long.parseLong(this.id));
        }
    } catch (NumberFormatException e) {
    }
    contest.setTitle(this.name);
    contest.setDescription(this.description);
    contest.setCheckIp(this.checkIp);
    Limit limit = new Limit();
    if (this.useGlobalDefault) {
        limit.setId(1);
    } else {
        limit.setTimeLimit(Integer.parseInt(this.timeLimit));
        limit.setMemoryLimit(Integer.parseInt(this.memoryLimit));
        limit.setSubmissionLimit(Integer.parseInt(this.submissionLimit));
        limit.setOutputLimit(Integer.parseInt(this.outputLimit));
    }
    contest.setLimit(limit);
    contest.setForumId(Long.parseLong(this.forumId));
    List<Language> languages = new ArrayList<Language>();
    LanguagePersistence languagePersistence = PersistenceManager.getInstance().getLanguagePersistence();
    if (this.languageIds != null) {
        for (int i = 0; i < this.languageIds.length; ++i) {
            Language language = languagePersistence.getLanguage(Long.parseLong(this.languageIds[i]));
            if (language != null) {
                languages.add(language);
            }
        }
    }
    contest.setLanguages(languages);
    return contest;
}
Also used : AbstractContest(cn.edu.zju.acm.onlinejudge.bean.AbstractContest) Language(cn.edu.zju.acm.onlinejudge.bean.enumeration.Language) ArrayList(java.util.ArrayList) AbstractContest(cn.edu.zju.acm.onlinejudge.bean.AbstractContest) Contest(cn.edu.zju.acm.onlinejudge.bean.Contest) Limit(cn.edu.zju.acm.onlinejudge.bean.Limit) Problemset(cn.edu.zju.acm.onlinejudge.bean.Problemset) Course(cn.edu.zju.acm.onlinejudge.bean.Course) Date(java.util.Date) LanguagePersistence(cn.edu.zju.acm.onlinejudge.persistence.LanguagePersistence)

Example 27 with AbstractContest

use of cn.edu.zju.acm.onlinejudge.bean.AbstractContest 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 28 with AbstractContest

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

Example 29 with AbstractContest

use of cn.edu.zju.acm.onlinejudge.bean.AbstractContest 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 30 with AbstractContest

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

the class ContestManager method getContest.

public AbstractContest getContest(long contestId) throws PersistenceException {
    Object key = new Long(contestId);
    synchronized (this.contestCache) {
        AbstractContest contest = this.contestCache.get(key);
        if (contest == null) {
            ContestPersistence contestPersistence = PersistenceManager.getInstance().getContestPersistence();
            contest = contestPersistence.getContest(contestId);
            this.contestCache.put(key, contest);
        }
        return contest;
    }
}
Also used : AbstractContest(cn.edu.zju.acm.onlinejudge.bean.AbstractContest) ContestPersistence(cn.edu.zju.acm.onlinejudge.persistence.ContestPersistence)

Aggregations

AbstractContest (cn.edu.zju.acm.onlinejudge.bean.AbstractContest)31 ActionForward (org.apache.struts.action.ActionForward)16 Problem (cn.edu.zju.acm.onlinejudge.bean.Problem)10 ActionMessages (org.apache.struts.action.ActionMessages)10 ArrayList (java.util.ArrayList)8 ActionMessage (org.apache.struts.action.ActionMessage)8 Contest (cn.edu.zju.acm.onlinejudge.bean.Contest)5 ContestPersistence (cn.edu.zju.acm.onlinejudge.persistence.ContestPersistence)5 Course (cn.edu.zju.acm.onlinejudge.bean.Course)4 Language (cn.edu.zju.acm.onlinejudge.bean.enumeration.Language)4 Date (java.util.Date)4 Limit (cn.edu.zju.acm.onlinejudge.bean.Limit)3 Problemset (cn.edu.zju.acm.onlinejudge.bean.Problemset)3 UserProfile (cn.edu.zju.acm.onlinejudge.bean.UserProfile)3 UserSecurity (cn.edu.zju.acm.onlinejudge.security.UserSecurity)3 PreparedStatement (java.sql.PreparedStatement)3 ResultSet (java.sql.ResultSet)3 Reference (cn.edu.zju.acm.onlinejudge.bean.Reference)2 Submission (cn.edu.zju.acm.onlinejudge.bean.Submission)2 ContestForm (cn.edu.zju.acm.onlinejudge.form.ContestForm)2