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;
}
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);
}
}
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);
}
}
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);
}
}
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;
}
}
Aggregations