use of org.apache.struts.action.ActionMessages in project zoj by licheng.
the class EditProblemAction method execute.
/**
* EditProblemAction.
*
* @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("editProblem.do");
ActionForward forward = this.checkProblemAdminPermission(mapping, context, isProblemset);
if (forward != null) {
return forward;
}
ProblemForm problemForm = (ProblemForm) form;
if (problemForm == null || problemForm.getName() == null) {
Problem problem = context.getProblem();
problemForm.populate(problem, context.getContest());
this.setReference("DescriptionRef", ReferenceType.DESCRIPTION, problem.getId(), context);
this.setReference("InputRef", ReferenceType.INPUT, problem.getId(), context);
this.setReference("OutputRef", ReferenceType.OUTPUT, problem.getId(), context);
this.setReference("JudgeSolutionRef", ReferenceType.JUDGE_SOLUTION, problem.getId(), context);
this.setReference("HeaderRef", ReferenceType.HEADER, problem.getId(), context);
this.setReference("CheckerSourceRef", ReferenceType.CHECKER_SOURCE, problem.getId(), context);
return this.handleSuccess(mapping, context, "failure");
}
// 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.updateProblem(problem, userId);
// cprete problem reference, i.e. text, input, output, checker, judge solution, checker source
this.updateReference(ReferenceType.DESCRIPTION, problemForm.getDescription(), problem.getId(), userId);
this.updateReference(ReferenceType.INPUT, problemForm.getInputData(), problem.getId(), userId);
this.updateReference(ReferenceType.OUTPUT, problemForm.getOutputData(), problem.getId(), userId);
this.updateReference(ReferenceType.HEADER, problemForm.getChecker(), problem.getId(), userId);
this.updateReference(ReferenceType.CHECKER_SOURCE, problemForm.getCheckerSource(), problem.getId(), userId);
this.updateReference(ReferenceType.JUDGE_SOLUTION, problemForm.getJudgeSolution(), problem.getId(), userId);
ContestManager.getInstance().refreshProblem(problem);
return this.handleSuccess(mapping, context, "success", "?contestId=" + contest.getId());
}
use of org.apache.struts.action.ActionMessages in project zoj by licheng.
the class EditProfileAction method execute.
/**
* Edit Profile.
*
* @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 (!Features.editProfile()) {
context.getResponse().sendError(404);
return null;
}
if (!this.isLogin(context)) {
return this.handleSuccess(mapping, context, "login");
}
UserPersistence userPersistence = PersistenceManager.getInstance().getUserPersistence();
ProfileForm profileForm = (ProfileForm) form;
UserProfile profile = context.getUserProfile();
UserPreference perference = userPersistence.getUserPreference(profile.getId());
if (profileForm.getHandle() == null) {
profileForm.populate(profile, perference);
context.setAttribute("ProfileForm", profileForm);
return this.handleSuccess(mapping, context, "failure");
}
if (userPersistence.login(profileForm.getHandle(), profileForm.getPassword()) == null) {
return this.handleFailure(mapping, context, "password", "ProfileForm.password.invalid");
}
UserProfile newProfile = profileForm.toUserProfile();
newProfile.setId(profile.getId());
newProfile.setRegDate(profile.getRegDate());
if (!profile.getHandle().equals(newProfile.getHandle())) {
return this.handleFailure(mapping, context, "handle", "ProfileForm.handle.changed");
}
if (!profile.getEmail().equals(newProfile.getEmail())) {
UserProfile temp = userPersistence.getUserProfileByEmail(newProfile.getEmail());
if (temp != null && temp.getId() != profile.getId()) {
return this.handleFailure(mapping, context, "email", "ProfileForm.email.used");
}
}
userPersistence.updateUserProfile(newProfile, profile.getId());
UserPreference newPerference = profileForm.toUserPreference();
newPerference.setId(profile.getId());
userPersistence.updateUserPreference(newPerference, profile.getId());
context.setUserProfile(newProfile);
context.getRequest().setAttribute("Countries", PersistenceManager.getInstance().getUserPersistence().getAllCountries());
ActionMessages messages = new ActionMessages();
messages.add("message", new ActionMessage("onlinejudge.editProfile.success"));
this.saveErrors(context.getRequest(), messages);
context.setAttribute("back", "");
return this.handleSuccess(mapping, context, "success");
}
use of org.apache.struts.action.ActionMessages in project zoj by licheng.
the class LoginAction method authenticate.
/**
* Authenticate.
*
* @param form
* @return
* @throws Exception
*/
private ActionMessages authenticate(LoginForm form, ContextAdapter context) throws PersistenceException {
context.getRequest().getSession().invalidate();
ActionMessages errors = new ActionMessages();
UserPersistence userPersistence = PersistenceManager.getInstance().getUserPersistence();
UserProfile profile = userPersistence.login(form.getHandle(), form.getPassword());
// no such user
if (profile == null) {
errors.add("password", new ActionMessage("LoginForm.password.invalid"));
return errors;
}
// deactivated
if (!profile.isActive()) {
errors.add("password", new ActionMessage("LoginForm.password.deactivated"));
return errors;
}
AuthorizationPersistence authorizationPersistence = PersistenceManager.getInstance().getAuthorizationPersistence();
// get UserSecurity
UserSecurity security = authorizationPersistence.getUserSecurity(profile.getId());
// get UserPreference
UserPreference perference = userPersistence.getUserPreference(profile.getId());
context.setUserProfile(profile);
context.setUserSecurity(security);
if (context.getAllCourses().size() != 0) {
security.setHasCourses(true);
} else {
security.setHasCourses(false);
}
context.setUserPreference(perference);
return errors;
}
use of org.apache.struts.action.ActionMessages in project zoj by licheng.
the class ShowRunsAction method execute.
/**
* ShowRunsAction.
*
* @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("showRuns.do");
ActionForward forward = this.checkContestViewPermission(mapping, context, isProblemset, true);
if (forward != null) {
return forward;
}
context.setAttribute("judgeReplies", context.isAdmin() ? this.adminJudgeReplies : this.judgeReplies);
// check contest
boolean isRejudge = "true".equalsIgnoreCase(context.getRequest().getParameter("rejudge"));
if (isRejudge) {
this.checkContestAdminPermission(mapping, context, isProblemset, true);
} else {
this.checkContestViewPermission(mapping, context, isProblemset, true);
}
if (forward != null) {
return forward;
}
SubmissionSearchForm serachForm = (SubmissionSearchForm) form;
ActionMessages errors = serachForm.check();
if (errors.size() > 0) {
// TODO
context.setAttribute("runs", new ArrayList<Submission>());
return this.handleFailure(mapping, context, errors);
}
long lastId = Utility.parseLong(serachForm.getLastId());
long firstId = -1;
if (lastId < 0) {
lastId = Long.MAX_VALUE;
firstId = Utility.parseLong(serachForm.getFirstId());
}
int RUNS_PER_PAGE = 15;
SubmissionCriteria criteria = serachForm.toSubmissionCriteria();
if (isRejudge) {
int maxN = 100;
List<Submission> allRuns = PersistenceManager.getInstance().getSubmissionPersistence().searchSubmissions(criteria, -1, Long.MAX_VALUE, maxN + 1, true);
if (allRuns.size() > maxN) {
// TODO
}
this.rejudge(allRuns);
// TODO
}
List<Submission> runs = StatisticsManager.getInstance().getSubmissions(criteria, firstId, lastId, RUNS_PER_PAGE + 1);
long newLastId = -1;
long newFirstId = -1;
long nextId = -1;
long startId = -1;
if (runs.size() > 0) {
startId = runs.get(0).getContestOrder();
}
if (runs.size() > RUNS_PER_PAGE) {
nextId = runs.get(runs.size() - 2).getContestOrder();
runs = runs.subList(0, runs.size() - 1);
}
if (firstId > -1) {
runs = new ArrayList<Submission>(runs);
Collections.reverse(runs);
}
if (runs.size() > 0) {
if (lastId == Long.MAX_VALUE && firstId == -1) {
newLastId = nextId;
} else if (firstId == -1) {
newLastId = nextId;
newFirstId = startId;
} else {
newFirstId = nextId;
newLastId = startId;
}
}
context.setAttribute("runs", runs);
if (newFirstId > -1) {
context.setAttribute("firstId", newFirstId);
}
if (newLastId > -1) {
context.setAttribute("lastId", newLastId);
}
return this.handleSuccess(mapping, context, "success");
}
use of org.apache.struts.action.ActionMessages in project zoj by licheng.
the class LoginAction method execute.
/**
* Login.
*
* <pre>
* </pre>
*
* @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 {
LoginForm loginForm = (LoginForm) form;
String forwardPath = context.getRequest().getParameter("forward");
if (loginForm.getHandle() == null) {
context.setAttribute("forward", forwardPath);
return this.handleSuccess(mapping, context, "failure");
}
ActionMessages errors = this.authenticate(loginForm, context);
if (errors.size() > 0) {
context.setAttribute("forward", forwardPath);
return this.handleFailure(mapping, context, errors);
}
if (loginForm.isRememberMe()) {
Cookie ch = new Cookie("oj_handle", loginForm.getHandle());
ch.setMaxAge(3600 * 24 * 30);
ch.setPath("/");
context.getResponse().addCookie(ch);
Cookie cp = new Cookie("oj_password", loginForm.getPassword());
cp.setMaxAge(3600 * 24 * 30);
cp.setPath("/");
context.getResponse().addCookie(cp);
}
if (forwardPath != null) {
return this.handleSuccess(new ActionForward(forwardPath, true), context, forwardPath);
}
return this.handleSuccess(mapping, context, "success");
}
Aggregations