use of cn.edu.zju.acm.onlinejudge.bean.Limit in project zoj by licheng.
the class ContestPersistenceImpl method updateContest.
/**
* <p>
* Updates the specified contest in persistence layer.
* </p>
*
* @param contest
* the AbstractContest instance to update
* @param user
* the id of the user who made this modification
* @throws PersistenceException
* wrapping a persistence implementation specific exception
*/
public void updateContest(AbstractContest contest, long user) throws PersistenceException {
Connection conn = null;
try {
conn = Database.createConnection();
conn.setAutoCommit(false);
PreparedStatement ps = null;
long contestLimitId = ContestPersistenceImpl.DEFAULT_LIMIT_ID;
try {
ps = conn.prepareStatement(ContestPersistenceImpl.GET_CONTEST_LIMIT_ID);
ps.setLong(1, contest.getId());
ResultSet rs = ps.executeQuery();
if (rs.next()) {
contestLimitId = rs.getLong(1);
}
} finally {
Database.dispose(ps);
}
// update the limit
Limit limit = contest.getLimit();
if (limit.getId() != ContestPersistenceImpl.DEFAULT_LIMIT_ID) {
try {
ps = conn.prepareStatement(ContestPersistenceImpl.INSERT_LIMIT);
ps.setInt(1, limit.getTimeLimit());
ps.setInt(2, limit.getMemoryLimit());
ps.setInt(3, limit.getOutputLimit());
ps.setInt(4, limit.getSubmissionLimit());
ps.executeUpdate();
} finally {
Database.dispose(ps);
}
limit.setId(Database.getLastId(conn));
}
if (contestLimitId != limit.getId()) {
// TODO(xuchuan) I don't understand what's that.
try {
ps = conn.prepareStatement(ContestPersistenceImpl.UPDATE_PROBLEM_LIMIT);
ps.setLong(1, limit.getId());
ps.setLong(2, contest.getId());
ps.setLong(3, contestLimitId);
ps.executeUpdate();
} finally {
Database.dispose(ps);
}
}
try {
// update the contest
ps = conn.prepareStatement(ContestPersistenceImpl.UPDATE_CONTEST);
ps.setString(1, contest.getTitle());
ps.setString(2, contest.getDescription());
if (contest.getStartTime() != null) {
ps.setTimestamp(3, new Timestamp(contest.getStartTime().getTime()));
} else {
ps.setTimestamp(3, null);
}
if (contest.getEndTime() != null) {
ps.setTimestamp(4, new Timestamp(contest.getEndTime().getTime()));
} else {
ps.setTimestamp(4, null);
}
ps.setLong(5, contest.getForumId());
if (limit == null || limit.getId() == ContestPersistenceImpl.DEFAULT_LIMIT_ID) {
ps.setLong(6, ContestPersistenceImpl.DEFAULT_LIMIT_ID);
} else {
ps.setLong(6, limit.getId());
}
ps.setBoolean(7, contest instanceof Problemset);
ps.setLong(8, user);
ps.setTimestamp(9, new Timestamp(new Date().getTime()));
ps.setBoolean(10, contest.isCheckIp());
ps.setLong(11, contest.getId());
ps.executeUpdate();
} finally {
Database.dispose(ps);
}
try {
// delete languages
ps = conn.prepareStatement(ContestPersistenceImpl.DELETE_CONTEST_LANGUAGE);
ps.setLong(1, contest.getId());
ps.executeUpdate();
} finally {
Database.dispose(ps);
}
// insert languages
if (contest.getLanguages() != null) {
for (Language language : contest.getLanguages()) {
try {
ps = conn.prepareStatement(ContestPersistenceImpl.INSERT_CONTEST_LANGUAGE);
ps.setLong(1, contest.getId());
ps.setLong(2, language.getId());
ps.executeUpdate();
} finally {
Database.dispose(ps);
}
}
}
conn.commit();
} catch (Exception e) {
Database.rollback(conn);
throw new PersistenceException("Failed to create contest.", e);
} finally {
Database.dispose(conn);
}
}
use of cn.edu.zju.acm.onlinejudge.bean.Limit in project zoj by licheng.
the class ContestPersistenceImpl method loadDefaultLimit.
private static void loadDefaultLimit() throws PersistenceException {
Connection conn = null;
try {
conn = Database.createConnection();
PreparedStatement ps = null;
try {
ps = conn.prepareStatement(ContestPersistenceImpl.SELECT_DEFAULT_LIMIT);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
ContestPersistenceImpl.defaultLimit = new Limit();
ContestPersistenceImpl.defaultLimit.setId(rs.getLong(DatabaseConstants.LIMITS_LIMITS_ID));
ContestPersistenceImpl.defaultLimit.setMemoryLimit(rs.getInt(DatabaseConstants.LIMITS_MEMORY_LIMIT));
ContestPersistenceImpl.defaultLimit.setOutputLimit(rs.getInt(DatabaseConstants.LIMITS_OUTPUT_LIMIT));
ContestPersistenceImpl.defaultLimit.setSubmissionLimit(rs.getInt(DatabaseConstants.LIMITS_SUBMISSION_LIMIT));
ContestPersistenceImpl.defaultLimit.setTimeLimit(rs.getInt(DatabaseConstants.LIMITS_TIME_LIMIT));
}
} finally {
Database.dispose(ps);
}
} catch (SQLException e) {
throw new PersistenceException("Failed to get the default limit", e);
} finally {
Database.dispose(conn);
}
}
use of cn.edu.zju.acm.onlinejudge.bean.Limit in project zoj by licheng.
the class ProblemPersistenceImpl method createProblem.
/**
* <p>
* Creates the specified problem in persistence layer.
* </p>
*
* @param problem
* the Problem instance to create
* @param user
* the id of the user who made this modification
* @throws PersistenceException
* wrapping a persistence implementation specific exception
*/
public void createProblem(Problem problem, long user) throws PersistenceException {
Connection conn = null;
try {
conn = Database.createConnection();
conn.setAutoCommit(false);
PreparedStatement ps = null;
Limit limit;
try {
long contestLimitId = ProblemPersistenceImpl.DEFAULT_LIMIT_ID;
ps = conn.prepareStatement(ProblemPersistenceImpl.GET_CONTEST_LIMIT_ID);
ps.setLong(1, problem.getContestId());
ResultSet rs = ps.executeQuery();
if (rs.next()) {
contestLimitId = rs.getLong(1);
}
limit = problem.getLimit();
if (limit == null) {
limit = new Limit();
limit.setId(contestLimitId);
problem.setLimit(limit);
}
if (limit.getId() != contestLimitId) {
ps = conn.prepareStatement(ProblemPersistenceImpl.INSERT_LIMIT);
ps.setInt(1, limit.getTimeLimit());
ps.setInt(2, limit.getMemoryLimit());
ps.setInt(3, limit.getOutputLimit());
ps.setInt(4, limit.getSubmissionLimit());
ps.executeUpdate();
limit.setId(Database.getLastId(conn));
}
} finally {
Database.dispose(ps);
}
try {
// create the problem
ps = conn.prepareStatement(ProblemPersistenceImpl.INSERT_PROBLEM);
ps.setLong(1, problem.getContestId());
ps.setString(2, problem.getTitle());
ps.setString(3, problem.getCode());
ps.setLong(4, limit.getId());
ps.setString(5, problem.getAuthor());
ps.setString(6, problem.getSource());
ps.setString(7, problem.getContest());
ps.setBoolean(8, problem.isChecker());
ps.setInt(9, 0);
ps.setLong(10, user);
ps.setTimestamp(11, new Timestamp(new Date().getTime()));
ps.setLong(12, user);
ps.setTimestamp(13, new Timestamp(new Date().getTime()));
ps.setString(14, problem.getColor());
ps.setInt(15, problem.getScore());
ps.executeUpdate();
problem.setId(Database.getLastId(conn));
} finally {
Database.dispose(ps);
}
conn.commit();
} catch (Exception e) {
Database.rollback(conn);
throw new PersistenceException("Failed to create problem.", e);
} finally {
Database.dispose(conn);
}
}
use of cn.edu.zju.acm.onlinejudge.bean.Limit in project zoj by licheng.
the class JudgeClientJudgeThread method judge.
private void judge(Submission submission) throws JudgeServerErrorException, IOException, PersistenceException, JudgeClientErrorException, ProblemDataErrorException {
Problem problem = this.problemDAO.getProblem(submission.getProblemId());
int reply = this.sendJudgeCommand(problem.getId(), problem.getRevision(), submission.getId());
if (reply == JudgeReply.NO_SUCH_PROBLEM.getId()) {
reply = this.sendDataCommand(problem);
}
if (reply == JudgeReply.COMPILATION_ERROR.getId()) {
int length = this.in.readInt();
byte[] bytes = new byte[length];
this.in.read(bytes);
throw new ProblemDataErrorException("Special judge compilation failure for problem " + problem.getId() + ": " + new String(bytes));
}
if (reply != JudgeReply.READY.getId()) {
throw new JudgeClientErrorException();
}
String content = submission.getContent();
if (content == null) {
content = this.submissionDAO.getSubmissionSource(submission.getId());
}
reply = this.sendCompileCommand(submission.getId(), submission.getLanguage(), content);
if (reply != JudgeReply.COMPILING.getId()) {
throw new JudgeClientErrorException();
}
submission.setJudgeReply(JudgeReply.COMPILING);
reply = this.readJudgeReply();
if (reply == JudgeReply.COMPILATION_ERROR.getId()) {
submission.setJudgeReply(JudgeReply.COMPILATION_ERROR);
int length = this.in.readInt();
byte[] bytes = new byte[length];
this.in.read(bytes);
submission.setJudgeComment(new String(bytes));
return;
} else if (reply != JudgeReply.READY.getId()) {
throw new JudgeClientErrorException();
}
Limit limit = problem.getLimit();
reply = this.sendTestcaseCommand(1, limit.getTimeLimit(), limit.getMemoryLimit(), limit.getOutputLimit());
submission.setJudgeReply(JudgeReply.RUNNING);
submission.setTimeConsumption(0);
submission.setMemoryConsumption(0);
while (reply == JudgeReply.RUNNING.getId()) {
int timeConsumption = this.in.readInt();
int memoryConsumption = this.in.readInt();
submission.setTimeConsumption(timeConsumption);
submission.setMemoryConsumption(memoryConsumption);
this.logger.info("Running " + timeConsumption + " " + memoryConsumption);
reply = this.readJudgeReply();
}
while (reply == JudgeReply.JUDGING.getId()) {
submission.setJudgeReply(JudgeReply.JUDGING);
reply = this.readJudgeReply();
}
if (reply == JudgeReply.JUDGE_INTERNAL_ERROR.getId()) {
throw new JudgeClientErrorException();
}
submission.setJudgeReply(JudgeReply.findById(reply));
if (submission.getJudgeReply() == null || submission.getJudgeReply() != JudgeReply.TIME_LIMIT_EXCEEDED && submission.getJudgeReply() != JudgeReply.MEMORY_LIMIT_EXCEEDED && submission.getJudgeReply() != JudgeReply.OUTPUT_LIMIT_EXCEEDED && submission.getJudgeReply() != JudgeReply.FLOATING_POINT_ERROR && submission.getJudgeReply() != JudgeReply.SEGMENTATION_FAULT && submission.getJudgeReply() != JudgeReply.RUNTIME_ERROR && submission.getJudgeReply() != JudgeReply.NON_ZERO_EXIT_CODE && submission.getJudgeReply() != JudgeReply.ACCEPTED && submission.getJudgeReply() != JudgeReply.WRONG_ANSWER && submission.getJudgeReply() != JudgeReply.PRESENTATION_ERROR) {
throw new JudgeClientErrorException();
}
}
use of cn.edu.zju.acm.onlinejudge.bean.Limit in project zoj by licheng.
the class ProblemForm method toProblem.
public Problem toProblem() throws ParseException, NumberFormatException, PersistenceException {
Problem problem = new Problem();
if (this.problemId != null) {
problem.setId(Long.parseLong(this.problemId));
}
if (this.contestId != null) {
problem.setContestId(Long.parseLong(this.contestId));
}
problem.setTitle(this.name);
problem.setCode(this.code);
problem.setAuthor(this.author);
problem.setSource(this.source);
problem.setContest(this.contest);
Limit limit = new Limit();
if (!this.useContestDefault) {
limit.setTimeLimit(Integer.parseInt(this.timeLimit));
limit.setMemoryLimit(Integer.parseInt(this.memoryLimit));
limit.setSubmissionLimit(Integer.parseInt(this.submissionLimit));
limit.setOutputLimit(Integer.parseInt(this.outputLimit));
}
problem.setLimit(limit);
problem.setChecker(this.specialJudge);
problem.setColor(this.color);
problem.setScore(this.score);
return problem;
}
Aggregations