Search in sources :

Example 21 with AbstractContest

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

the class ContextAdapter method getCheckedContests.

protected List<AbstractContest> getCheckedContests(List<AbstractContest> contests) throws PersistenceException {
    UserSecurity userSecurity = this.getUserSecurity();
    List<AbstractContest> ret = new ArrayList<AbstractContest>();
    for (AbstractContest contest : contests) {
        if (userSecurity.canViewContest(contest.getId())) {
            ret.add(contest);
        }
    }
    return ret;
}
Also used : AbstractContest(cn.edu.zju.acm.onlinejudge.bean.AbstractContest) UserSecurity(cn.edu.zju.acm.onlinejudge.security.UserSecurity) ArrayList(java.util.ArrayList)

Example 22 with AbstractContest

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

the class ExportProblemsAction method execute.

/**
     * ExportProblemsAction.
     * 
     * @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 {
    // check contest
    boolean isProblemset = context.getRequest().getRequestURI().endsWith("exportProblems.do");
    ActionForward forward = this.checkContestAdminPermission(mapping, context, isProblemset, false);
    if (forward != null) {
        return forward;
    }
    AbstractContest contest = context.getContest();
    List<Problem> problems = ContestManager.getInstance().getContestProblems(contest.getId());
    HttpServletResponse response = context.getResponse();
    response.setContentType("application/zip");
    response.setHeader("Content-disposition", "attachment; filename=" + this.convertFilename(contest.getTitle()) + ".zip");
    ZipOutputStream out = null;
    StringBuilder sb = new StringBuilder();
    try {
        out = new ZipOutputStream(response.getOutputStream());
        for (Object obj : problems) {
            Problem p = (Problem) obj;
            this.zipReference(p, "text", ReferenceType.DESCRIPTION, out);
            this.zipReference(p, "input", ReferenceType.INPUT, out);
            this.zipReference(p, "output", ReferenceType.OUTPUT, out);
            this.zipReference(p, "solution", ReferenceType.JUDGE_SOLUTION, out);
            this.zipReference(p, "checker", ReferenceType.HEADER, out);
            this.zipReference(p, "checker", ReferenceType.CHECKER_SOURCE, out);
            sb.append(toCsvString(p.getCode())).append(",");
            sb.append(toCsvString(p.getTitle())).append(",");
            sb.append(p.isChecker()).append(",");
            sb.append(p.getLimit().getTimeLimit()).append(",");
            sb.append(p.getLimit().getMemoryLimit()).append(",");
            sb.append(p.getLimit().getOutputLimit()).append(",");
            sb.append(p.getLimit().getSubmissionLimit()).append(",");
            sb.append(toCsvString(p.getAuthor())).append(",");
            sb.append(toCsvString(p.getSource())).append(",");
            sb.append(toCsvString(p.getContest())).append("\n");
        }
        out.putNextEntry(new ZipEntry("problems.csv"));
        out.write(sb.toString().getBytes());
    } catch (IOException e) {
        throw e;
    } finally {
        if (out != null) {
            out.close();
        }
    }
    return null;
}
Also used : AbstractContest(cn.edu.zju.acm.onlinejudge.bean.AbstractContest) ZipOutputStream(java.util.zip.ZipOutputStream) ZipEntry(java.util.zip.ZipEntry) HttpServletResponse(javax.servlet.http.HttpServletResponse) Problem(cn.edu.zju.acm.onlinejudge.bean.Problem) IOException(java.io.IOException) ActionForward(org.apache.struts.action.ActionForward)

Example 23 with AbstractContest

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

the class AddProblemAction method execute.

/**
     * AddProblemAction.
     * 
     * @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 {
    // check contest
    boolean isProblemset = context.getRequest().getRequestURI().endsWith("addProblem.do");
    ActionForward forward = this.checkContestAdminPermission(mapping, context, isProblemset, false);
    if (forward != null) {
        return forward;
    }
    ProblemForm problemForm = (ProblemForm) form;
    if (problemForm == null || problemForm.getProblemId() == null) {
        return this.handleSuccess(mapping, context);
    }
    // check title and code
    ActionMessages errors = this.validate(problemForm, context);
    if (errors.size() > 0) {
        return this.handleFailure(mapping, context, errors);
    }
    ProblemPersistence problemPersistence = PersistenceManager.getInstance().getProblemPersistence();
    AbstractContest contest = context.getContest();
    Problem problem = problemForm.toProblem();
    if (problemForm.isUseContestDefault()) {
        problem.getLimit().setId(contest.getLimit().getId());
    }
    long userId = context.getUserSecurity().getId();
    // create problem
    problemPersistence.createProblem(problem, userId);
    // cprete problem reference, i.e. text, input, output, checker, judge solution, checker source
    this.createReference(ReferenceType.DESCRIPTION, problemForm.getDescription(), problem.getId(), userId);
    this.createReference(ReferenceType.INPUT, problemForm.getInputData(), problem.getId(), userId);
    this.createReference(ReferenceType.OUTPUT, problemForm.getOutputData(), problem.getId(), userId);
    this.createReference(ReferenceType.HEADER, problemForm.getChecker(), problem.getId(), userId);
    this.createReference(ReferenceType.CHECKER_SOURCE, problemForm.getCheckerSource(), problem.getId(), userId);
    this.createReference(ReferenceType.JUDGE_SOLUTION, problemForm.getJudgeSolution(), problem.getId(), userId);
    ContestManager.getInstance().refreshContest(problem.getContestId());
    return this.handleSuccess(mapping, context, "success", "?contestId=" + contest.getId());
}
Also used : AbstractContest(cn.edu.zju.acm.onlinejudge.bean.AbstractContest) ProblemForm(cn.edu.zju.acm.onlinejudge.form.ProblemForm) ActionMessages(org.apache.struts.action.ActionMessages) Problem(cn.edu.zju.acm.onlinejudge.bean.Problem) ActionForward(org.apache.struts.action.ActionForward) ProblemPersistence(cn.edu.zju.acm.onlinejudge.persistence.ProblemPersistence)

Example 24 with AbstractContest

use of cn.edu.zju.acm.onlinejudge.bean.AbstractContest 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");
}
Also used : AbstractContest(cn.edu.zju.acm.onlinejudge.bean.AbstractContest) UserProfile(cn.edu.zju.acm.onlinejudge.bean.UserProfile) Submission(cn.edu.zju.acm.onlinejudge.bean.Submission) Reference(cn.edu.zju.acm.onlinejudge.bean.Reference) SubmissionPersistence(cn.edu.zju.acm.onlinejudge.persistence.SubmissionPersistence) ActionForward(org.apache.struts.action.ActionForward) Date(java.util.Date) Language(cn.edu.zju.acm.onlinejudge.bean.enumeration.Language) ActionMessages(org.apache.struts.action.ActionMessages) ActionMessage(org.apache.struts.action.ActionMessage) Problem(cn.edu.zju.acm.onlinejudge.bean.Problem) List(java.util.List)

Example 25 with AbstractContest

use of cn.edu.zju.acm.onlinejudge.bean.AbstractContest 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");
}
Also used : AbstractContest(cn.edu.zju.acm.onlinejudge.bean.AbstractContest) UserProfile(cn.edu.zju.acm.onlinejudge.bean.UserProfile) Submission(cn.edu.zju.acm.onlinejudge.bean.Submission) Reference(cn.edu.zju.acm.onlinejudge.bean.Reference) SubmissionPersistence(cn.edu.zju.acm.onlinejudge.persistence.SubmissionPersistence) ActionForward(org.apache.struts.action.ActionForward) Date(java.util.Date) Language(cn.edu.zju.acm.onlinejudge.bean.enumeration.Language) Random(java.util.Random) ActionMessages(org.apache.struts.action.ActionMessages) ActionMessage(org.apache.struts.action.ActionMessage) Problem(cn.edu.zju.acm.onlinejudge.bean.Problem) List(java.util.List)

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