use of cn.edu.zju.acm.onlinejudge.bean.enumeration.Language in project zoj by licheng.
the class SubmitAction method execute.
/**
* SubmitAction.
*
* @param mapping
* action mapping
* @param form
* action form
* @param request
* http servlet request
* @param response
* http servlet response
*
* @return action forward instance
*
* @throws Exception
* any errors happened
*/
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form, ContextAdapter context) throws Exception {
if (!this.isLogin(context, true)) {
return this.handleSuccess(mapping, context, "login");
}
boolean isProblemset = context.getRequest().getRequestURI().endsWith("submit.do");
ActionForward forward = this.checkProblemParticipatePermission(mapping, context, isProblemset);
if (forward != null) {
return forward;
}
AbstractContest contest = context.getContest();
Problem problem = context.getProblem();
long languageId = Utility.parseLong(context.getRequest().getParameter("languageId"));
Language language = PersistenceManager.getInstance().getLanguagePersistence().getLanguage(languageId);
if (language == null) {
return this.handleSuccess(mapping, context, "submit");
}
String source = context.getRequest().getParameter("source");
if (source == null || source.length() == 0) {
return this.handleSuccess(mapping, context, "submit");
}
List refrance = PersistenceManager.getInstance().getReferencePersistence().getProblemReferences(problem.getId(), ReferenceType.HEADER);
if (refrance.size() != 0) {
Reference r = (Reference) refrance.get(0);
String percode = new String(r.getContent());
source = percode + "\n" + source;
}
UserProfile user = context.getUserProfile();
if (submitCache != null && submitCache.contains(user.getId())) {
ActionMessages messages = new ActionMessages();
messages.add("message", new ActionMessage("onlinejudge.submit.interval"));
this.saveErrors(context.getRequest(), messages);
context.setAttribute("source", source);
return handleSuccess(mapping, context, "submit");
}
if (contest.isCheckIp()) {
forward = this.checkLastLoginIP(mapping, context, isProblemset);
if (forward != null) {
return forward;
}
}
Submission submission = new Submission();
submission.setContestId(contest.getId());
submission.setLanguage(language);
submission.setProblemId(problem.getId());
submission.setUserProfileId(user.getId());
submission.setContent(source);
submission.setMemoryConsumption(0);
submission.setTimeConsumption(0);
submission.setSubmitDate(new Date());
SubmissionPersistence submissionPersistence = PersistenceManager.getInstance().getSubmissionPersistence();
if (contest.getEndTime() != null && new Date().after(contest.getEndTime())) {
submission.setJudgeReply(JudgeReply.OUT_OF_CONTEST_TIME);
submissionPersistence.createSubmission(submission, user.getId());
} else if (source.getBytes().length > problem.getLimit().getSubmissionLimit() * 1024) {
submission.setContent(source.substring(0, problem.getLimit().getSubmissionLimit() * 1024));
submission.setJudgeReply(JudgeReply.SUBMISSION_LIMIT_EXCEEDED);
submissionPersistence.createSubmission(submission, user.getId());
} else {
submission.setJudgeReply(JudgeReply.QUEUING);
submissionPersistence.createSubmission(submission, user.getId());
JudgeService.getInstance().judge(submission, Priority.NORMAL);
}
context.setAttribute("contestOrder", submission.getContestOrder());
if (submitCache != null) {
submitCache.put(user.getId(), user.getId());
}
return this.handleSuccess(mapping, context, "success");
}
use of cn.edu.zju.acm.onlinejudge.bean.enumeration.Language in project zoj by licheng.
the class TestSubmitAction method execute.
/**
* SubmitAction.
*
* @param mapping
* action mapping
* @param form
* action form
* @param request
* http servlet request
* @param response
* http servlet response
*
* @return action forward instance
*
* @throws Exception
* any errors happened
*/
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form, ContextAdapter context) throws Exception {
if (!this.isLogin(context, true)) {
return this.handleSuccess(mapping, context, "login");
}
boolean isProblemset = context.getRequest().getRequestURI().endsWith("submit.do");
ActionForward forward = this.checkProblemParticipatePermission(mapping, context, isProblemset);
if (forward != null) {
return forward;
}
AbstractContest contest = context.getContest();
Problem problem = context.getProblem();
long languageId = Utility.parseLong(context.getRequest().getParameter("languageId"));
Language language = PersistenceManager.getInstance().getLanguagePersistence().getLanguage(languageId);
if (language == null) {
return this.handleSuccess(mapping, context, "submit");
}
String source = context.getRequest().getParameter("source");
if (source == null || source.length() == 0) {
return this.handleSuccess(mapping, context, "submit");
}
List refrance = PersistenceManager.getInstance().getReferencePersistence().getProblemReferences(problem.getId(), ReferenceType.HEADER);
if (refrance.size() != 0) {
Reference r = (Reference) refrance.get(0);
String percode = new String(r.getContent());
source = percode + "\n" + source;
}
UserProfile user = context.getUserProfile();
if (submitCache != null && submitCache.contains(user.getId())) {
ActionMessages messages = new ActionMessages();
messages.add("message", new ActionMessage("onlinejudge.submit.interval"));
this.saveErrors(context.getRequest(), messages);
context.setAttribute("source", source);
return handleSuccess(mapping, context, "submit");
}
if (contest.isCheckIp()) {
forward = this.checkLastLoginIP(mapping, context, isProblemset);
if (forward != null) {
return forward;
}
}
Submission submission = new Submission();
submission.setContestId(contest.getId());
submission.setLanguage(language);
submission.setProblemId(problem.getId());
submission.setUserProfileId(user.getId());
submission.setContent(source);
submission.setMemoryConsumption(0);
submission.setTimeConsumption(0);
submission.setSubmitDate(new Date());
SubmissionPersistence submissionPersistence = PersistenceManager.getInstance().getSubmissionPersistence();
if (contest.getEndTime() != null && new Date().after(contest.getEndTime())) {
submission.setJudgeReply(JudgeReply.OUT_OF_CONTEST_TIME);
submissionPersistence.createSubmission(submission, user.getId());
} else if (source.getBytes().length > problem.getLimit().getSubmissionLimit() * 1024) {
submission.setContent(source.substring(0, problem.getLimit().getSubmissionLimit() * 1024));
submission.setJudgeReply(JudgeReply.SUBMISSION_LIMIT_EXCEEDED);
submissionPersistence.createSubmission(submission, user.getId());
} else {
Random ran = new Random();
submission.setJudgeReply(ran.nextInt() % 2 == 0 ? JudgeReply.WRONG_ANSWER : JudgeReply.ACCEPTED);
submissionPersistence.createSubmission(submission, user.getId());
JudgeService.getInstance().judge(submission, Priority.NORMAL);
}
context.setAttribute("contestOrder", submission.getContestOrder());
if (submitCache != null) {
submitCache.put(user.getId(), user.getId());
}
return this.handleSuccess(mapping, context, "success");
}
use of cn.edu.zju.acm.onlinejudge.bean.enumeration.Language 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.enumeration.Language in project zoj by licheng.
the class ContestPersistenceImpl method createContest.
/**
* <p>
* Creates the specified contest in persistence layer.
* </p>
*
* @param contest
* the AbstractContest instance to create
* @param user
* the id of the user who made this modification
* @throws PersistenceException
* wrapping a persistence implementation specific exception
*/
public void createContest(AbstractContest contest, long user) throws PersistenceException {
Connection conn = null;
try {
conn = Database.createConnection();
conn.setAutoCommit(false);
PreparedStatement ps = null;
Limit limit = contest.getLimit();
try {
// create a new limit
if (limit != null && limit.getId() != ContestPersistenceImpl.DEFAULT_LIMIT_ID) {
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();
limit.setId(Database.getLastId(conn));
}
} finally {
Database.dispose(ps);
}
try {
// create the contest
ps = conn.prepareStatement(ContestPersistenceImpl.INSERT_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());
}
int contesttype = 0;
if (contest instanceof Problemset) {
contesttype = 1;
}
if (contest instanceof Course) {
contesttype = 2;
}
ps.setInt(7, contesttype);
ps.setLong(8, user);
ps.setTimestamp(9, new Timestamp(new Date().getTime()));
ps.setLong(10, user);
ps.setTimestamp(11, new Timestamp(new Date().getTime()));
ps.setBoolean(12, contest.isCheckIp());
ps.executeUpdate();
} finally {
Database.dispose(ps);
}
contest.setId(Database.getLastId(conn));
// create 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.enumeration.Language 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);
}
}
Aggregations