Search in sources :

Example 1 with ProblemImportForm

use of cn.edu.zju.acm.onlinejudge.form.ProblemImportForm in project zoj by licheng.

the class ProblemImportAction method execute.

/**
     * ProblemImportAction.
     * 
     * @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("importProblems.do");
    ActionForward forward = this.checkContestAdminPermission(mapping, context, isProblemset, false);
    if (forward != null) {
        return forward;
    }
    ProblemPackage pack = (ProblemPackage) context.getSessionAttribute("ProblemPackage");
    context.setSessionAttribute("ProblemPackage", null);
    AbstractContest contest = context.getContest();
    InputStream in = null;
    String filePath = context.getRequest().getParameter("problemFilePath");
    FormFile file = ((ProblemImportForm) form).getProblemFile();
    if (filePath != null && filePath.trim().length() > 0) {
        in = new FileInputStream(filePath);
    } else if (file != null) {
        in = file.getInputStream();
    }
    if (in != null) {
        ActionMessages messages = new ActionMessages();
        pack = ProblemManager.importProblem(in, messages);
        if (messages.size() > 0) {
            return this.handleFailure(mapping, context, messages);
        }
        context.setSessionAttribute("ProblemPackage", pack);
        return this.handleSuccess(mapping, context, "preview");
    }
    if (pack == null) {
        return this.handleSuccess(mapping, context, "selectcontest");
    }
    try {
        ProblemImportAction.createProblems(pack, contest.getId());
        /*
             * ProblemCriteria criteria = new ProblemCriteria(); criteria.setContestId(new Long(contest.getId()));
             * 
             * // remove List oldProblems = problemPersistence.searchProblems(criteria); for (Iterator it =
             * oldProblems.iterator(); it.hasNext();) { problemPersistence.deleteProblem(((Problem) it.next()).getId(),
             * 0); }
             */
        ContestManager.getInstance().refreshContest(contest.getId());
    } catch (Exception pe) {
        this.error(pe);
        ActionMessages messages = new ActionMessages();
        messages.add("message", new ActionMessage("onlinejudge.importProblems.failure"));
        this.saveErrors(context.getRequest(), messages);
        context.setAttribute("back", "editContest.do?contestId=" + contest.getId());
        return this.handleSuccess(mapping, context, "success");
    }
    ActionMessages messages = new ActionMessages();
    messages.add("message", new ActionMessage("onlinejudge.importProblems.success"));
    this.saveErrors(context.getRequest(), messages);
    context.setAttribute("back", "showContestProblems.do?contestId=" + contest.getId());
    return this.handleSuccess(mapping, context, "success");
}
Also used : AbstractContest(cn.edu.zju.acm.onlinejudge.bean.AbstractContest) ProblemImportForm(cn.edu.zju.acm.onlinejudge.form.ProblemImportForm) ProblemPackage(cn.edu.zju.acm.onlinejudge.util.ProblemPackage) ActionMessages(org.apache.struts.action.ActionMessages) FileInputStream(java.io.FileInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ActionMessage(org.apache.struts.action.ActionMessage) ActionForward(org.apache.struts.action.ActionForward) FileInputStream(java.io.FileInputStream) FormFile(org.apache.struts.upload.FormFile)

Aggregations

AbstractContest (cn.edu.zju.acm.onlinejudge.bean.AbstractContest)1 ProblemImportForm (cn.edu.zju.acm.onlinejudge.form.ProblemImportForm)1 ProblemPackage (cn.edu.zju.acm.onlinejudge.util.ProblemPackage)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 ActionForward (org.apache.struts.action.ActionForward)1 ActionMessage (org.apache.struts.action.ActionMessage)1 ActionMessages (org.apache.struts.action.ActionMessages)1 FormFile (org.apache.struts.upload.FormFile)1