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");
}
Aggregations