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");
}
use of org.apache.struts.action.ActionMessages 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