Search in sources :

Example 11 with Reference

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

the class ShowProblemsAction method getReferenceLength.

private long getReferenceLength(Problem p, ReferenceType type) throws Exception {
    ReferencePersistence referencePersistence = PersistenceManager.getInstance().getReferencePersistence();
    List<Reference> refs = referencePersistence.getProblemReferenceInfo(p.getId(), type);
    if (refs.size() == 0) {
        return -1;
    }
    Reference ref = refs.get(0);
    return ref.getSize();
}
Also used : ReferencePersistence(cn.edu.zju.acm.onlinejudge.persistence.ReferencePersistence) Reference(cn.edu.zju.acm.onlinejudge.bean.Reference)

Example 12 with Reference

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

the class ShowReferenceAction method execute.

/**
     * ShowRankListAction.
     * 
     * @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 {
    HttpServletResponse response = context.getResponse();
    if (context.getUserSecurity() == context.getDefaultUserSecurity()) {
        response.sendError(404);
        return null;
    }
    try {
        if (!context.getUserSecurity().canAdminContest(context.getContest().getId())) {
            response.sendError(404);
            return null;
        }
    } catch (Exception e) {
    }
    long id = Utility.parseLong(context.getRequest().getParameter("referenceId"));
    String problemCode = context.getRequest().getParameter("code");
    boolean download = "true".equalsIgnoreCase(context.getRequest().getParameter("download"));
    ReferencePersistence referencePersistence = PersistenceManager.getInstance().getReferencePersistence();
    Reference ref = referencePersistence.getReference(id);
    if (ref == null) {
        response.sendError(404);
        return null;
    }
    response.setContentType("text/plain");
    if (download) {
        response.setHeader("Content-disposition", "attachment; filename=" + problemCode + "_" + ref.getReferenceType().getDescription() + ".txt");
        response.getOutputStream().write(ref.getContent());
    } else {
        int length = ref.getContent().length;
        if (length > 100 * 1024) {
            response.getOutputStream().write(ref.getContent(), 0, 100 * 1024);
            response.getOutputStream().write("\n\n...\n".getBytes());
        } else {
            response.getOutputStream().write(ref.getContent());
        }
    }
    response.getOutputStream().close();
    return null;
}
Also used : ReferencePersistence(cn.edu.zju.acm.onlinejudge.persistence.ReferencePersistence) Reference(cn.edu.zju.acm.onlinejudge.bean.Reference) HttpServletResponse(javax.servlet.http.HttpServletResponse)

Example 13 with Reference

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

the class ExportProblemsAction method zipReference.

private void zipReference(Problem p, String fileName, ReferenceType type, ZipOutputStream out) throws Exception {
    ReferencePersistence referencePersistence = PersistenceManager.getInstance().getReferencePersistence();
    List<Reference> refs = referencePersistence.getProblemReferences(p.getId(), type);
    if (refs.size() == 0) {
        return;
    }
    Reference ref = refs.get(0);
    if (type == ReferenceType.CHECKER_SOURCE || type == ReferenceType.JUDGE_SOLUTION) {
        String contentType = ref.getContentType();
        if (contentType == null) {
            contentType = "cc";
        }
        fileName += "." + contentType;
    }
    out.putNextEntry(new ZipEntry(p.getCode() + "/" + fileName));
    byte[] data = ref.getContent();
    out.write(data);
    out.closeEntry();
}
Also used : ReferencePersistence(cn.edu.zju.acm.onlinejudge.persistence.ReferencePersistence) Reference(cn.edu.zju.acm.onlinejudge.bean.Reference) ZipEntry(java.util.zip.ZipEntry)

Example 14 with Reference

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

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

Reference (cn.edu.zju.acm.onlinejudge.bean.Reference)20 ReferencePersistence (cn.edu.zju.acm.onlinejudge.persistence.ReferencePersistence)8 Problem (cn.edu.zju.acm.onlinejudge.bean.Problem)5 Limit (cn.edu.zju.acm.onlinejudge.bean.Limit)3 ArrayList (java.util.ArrayList)3 BeforeClass (org.junit.BeforeClass)3 AbstractContest (cn.edu.zju.acm.onlinejudge.bean.AbstractContest)2 Submission (cn.edu.zju.acm.onlinejudge.bean.Submission)2 UserProfile (cn.edu.zju.acm.onlinejudge.bean.UserProfile)2 Language (cn.edu.zju.acm.onlinejudge.bean.enumeration.Language)2 PersistenceException (cn.edu.zju.acm.onlinejudge.persistence.PersistenceException)2 SubmissionPersistence (cn.edu.zju.acm.onlinejudge.persistence.SubmissionPersistence)2 Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 ResultSet (java.sql.ResultSet)2 SQLException (java.sql.SQLException)2 Date (java.util.Date)2 List (java.util.List)2 ZipEntry (java.util.zip.ZipEntry)2 ActionForward (org.apache.struts.action.ActionForward)2