Search in sources :

Example 21 with Problem

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

the class EditProblemAction method validate.

/**
     * Validates the form.
     * 
     * @param mapping
     *            the action mapping.
     * @param request
     *            the user request.
     * 
     * @return collection of validation errors.
     */
public ActionErrors validate(ProblemForm form, ContextAdapter context) throws Exception {
    ActionErrors errors = new ActionErrors();
    this.checkInteger(form.getProblemId(), 0, Integer.MAX_VALUE, "id", errors);
    String name = form.getName();
    String code = form.getCode();
    if (name == null || name.trim().length() == 0) {
        errors.add("name", new ActionMessage("ProblemForm.name.required"));
    }
    if (code == null || code.trim().length() == 0) {
        errors.add("code", new ActionMessage("ProblemForm.code.required"));
    }
    if (!form.isUseContestDefault()) {
        this.checkInteger(form.getTimeLimit(), 0, 3600, "timeLimit", errors);
        this.checkInteger(form.getMemoryLimit(), 0, 1024 * 1024, "memoryLimit", errors);
        this.checkInteger(form.getOutputLimit(), 0, 100 * 1024, "outputLimit", errors);
        this.checkInteger(form.getSubmissionLimit(), 0, 10 * 1024, "submissionLimit", errors);
    }
    List<Problem> problems = ContestManager.getInstance().getContestProblems(context.getContest().getId());
    /*for (Object obj : problems) {
            Problem p = (Problem) obj;
            if (!form.getProblemId().equals("" + p.getId()) && p.getTitle().equals(name)) {
                errors.add("name", new ActionMessage("ProblemForm.name.used"));
                break;
            }
        }*/
    for (Object obj : problems) {
        Problem p = (Problem) obj;
        if (!form.getProblemId().equals("" + p.getId()) && p.getCode().equals(code)) {
            errors.add("code", new ActionMessage("ProblemForm.code.used"));
            break;
        }
    }
    return errors;
}
Also used : ActionMessage(org.apache.struts.action.ActionMessage) Problem(cn.edu.zju.acm.onlinejudge.bean.Problem) ActionErrors(org.apache.struts.action.ActionErrors)

Example 22 with Problem

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

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

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

the class AddProblemAction method validate.

/**
     * Validates the form.
     * 
     * @param mapping
     *            the action mapping.
     * @param request
     *            the user request.
     * 
     * @return collection of validation errors.
     */
public ActionErrors validate(ProblemForm form, ContextAdapter context) throws Exception {
    ActionErrors errors = new ActionErrors();
    this.checkInteger(form.getProblemId(), 0, Integer.MAX_VALUE, "id", errors);
    String name = form.getName();
    String code = form.getCode();
    if (name == null || name.trim().length() == 0) {
        errors.add("name", new ActionMessage("ProblemForm.name.required"));
    }
    if (code == null || code.trim().length() == 0) {
        errors.add("code", new ActionMessage("ProblemForm.code.required"));
    }
    if (!form.isUseContestDefault()) {
        this.checkInteger(form.getTimeLimit(), 0, 3600, "timeLimit", errors);
        this.checkInteger(form.getMemoryLimit(), 0, 1024 * 1024, "memoryLimit", errors);
        this.checkInteger(form.getOutputLimit(), 0, 100 * 1024, "outputLimit", errors);
        this.checkInteger(form.getSubmissionLimit(), 0, 10 * 1024, "submissionLimit", errors);
    }
    List<Problem> problems = ContestManager.getInstance().getContestProblems(context.getContest().getId());
    /*for (Object problem : problems) {
            if (((Problem) problem).getTitle().equals(name)) {
                errors.add("name", new ActionMessage("ProblemForm.name.used"));
                break;
            }
        }*/
    for (Object problem : problems) {
        if (((Problem) problem).getCode().equals(code)) {
            errors.add("code", new ActionMessage("ProblemForm.code.used"));
            break;
        }
    }
    return errors;
}
Also used : ActionMessage(org.apache.struts.action.ActionMessage) Problem(cn.edu.zju.acm.onlinejudge.bean.Problem) ActionErrors(org.apache.struts.action.ActionErrors)

Example 25 with Problem

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

the class ContextAdapter method getProblem.

public Problem getProblem() throws PersistenceException {
    if (this.getAttribute("problem") instanceof Problem) {
        return (Problem) this.getAttribute("problem");
    }
    long problemId = -1;
    String stringCode = this.request.getParameter("problemCode");
    String stringId = this.request.getParameter("problemId");
    if (stringCode != null) {
        ProblemCriteria pc = new ProblemCriteria();
        pc.setContestId(new Long(ConfigManager.getDefaultProblemSetId()));
        pc.setCode(stringCode);
        ProblemPersistence problemPersistence = PersistenceManager.getInstance().getProblemPersistence();
        List problems = problemPersistence.searchProblems(pc, 0, 1);
        if (problems.size() != 0) {
            problemId = ((Problem) (problems.get(0))).getId();
            stringId = "" + problemId;
        }
    }
    this.setAttribute("problemId", stringId);
    if (stringId != null) {
        try {
            problemId = Long.parseLong(stringId);
        } catch (NumberFormatException e) {
        }
    }
    if (problemId < 0) {
        return null;
    }
    Problem problem = ContestManager.getInstance().getProblem(problemId);
    this.setAttribute("problem", problem);
    return problem;
}
Also used : Problem(cn.edu.zju.acm.onlinejudge.bean.Problem) ArrayList(java.util.ArrayList) List(java.util.List) ProblemCriteria(cn.edu.zju.acm.onlinejudge.bean.request.ProblemCriteria) ProblemPersistence(cn.edu.zju.acm.onlinejudge.persistence.ProblemPersistence)

Aggregations

Problem (cn.edu.zju.acm.onlinejudge.bean.Problem)44 ActionForward (org.apache.struts.action.ActionForward)11 AbstractContest (cn.edu.zju.acm.onlinejudge.bean.AbstractContest)10 ArrayList (java.util.ArrayList)9 Limit (cn.edu.zju.acm.onlinejudge.bean.Limit)8 ActionMessage (org.apache.struts.action.ActionMessage)7 ProblemPersistence (cn.edu.zju.acm.onlinejudge.persistence.ProblemPersistence)6 ActionMessages (org.apache.struts.action.ActionMessages)6 Reference (cn.edu.zju.acm.onlinejudge.bean.Reference)5 PersistenceException (cn.edu.zju.acm.onlinejudge.persistence.PersistenceException)5 Connection (java.sql.Connection)5 PreparedStatement (java.sql.PreparedStatement)5 ResultSet (java.sql.ResultSet)5 SQLException (java.sql.SQLException)5 UserProfile (cn.edu.zju.acm.onlinejudge.bean.UserProfile)4 Submission (cn.edu.zju.acm.onlinejudge.bean.Submission)3 RankListEntry (cn.edu.zju.acm.onlinejudge.util.RankListEntry)3 List (java.util.List)3 Language (cn.edu.zju.acm.onlinejudge.bean.enumeration.Language)2 ProblemForm (cn.edu.zju.acm.onlinejudge.form.ProblemForm)2