use of org.apache.struts.action.ActionMessage in project zoj by licheng.
the class BaseAction method handleFailure.
/**
* Provides convenience method to handle errors.
*
* @param mapping
* the action mapping that holds the forwards.
* @param request
* the http servlet request.
* @param messageKey
* the resource key to retrieve the error message.
* @param errorKey
* the error key.
*
* @return the failure forward.
*/
protected ActionForward handleFailure(ActionMapping mapping, ContextAdapter context, String errorKey, String resourceKey) {
this.info(this.makeInfo(BaseAction.EXIT_OP, context.getOperator(), "failure"));
ActionMessages errors = new ActionMessages();
errors.add(errorKey, new ActionMessage(resourceKey));
this.saveErrors(context.getRequest(), errors);
return mapping.findForward("failure");
}
use of org.apache.struts.action.ActionMessage in project zoj by licheng.
the class BaseAction method checkLastLoginIP.
protected ActionForward checkLastLoginIP(ActionMapping mapping, ContextAdapter context, boolean isProblemset) throws Exception {
String ip = context.getRequest().getRemoteHost();
long contestId = context.getContest().getId();
String ipSessionKey = "last_submit_ip" + contestId;
String lastIp = (String) context.getSessionAttribute(ipSessionKey);
if (lastIp == null) {
ContestPersistence contestPersistence = PersistenceManager.getInstance().getContestPersistence();
long userId = context.getUserProfile().getId();
lastIp = contestPersistence.getLastSubmitIP(userId, contestId);
if (lastIp == null) {
// first submit
contestPersistence.setLastSubmitIP(userId, contestId, ip);
context.setSessionAttribute(ipSessionKey, lastIp);
return null;
}
context.setSessionAttribute(ipSessionKey, lastIp);
}
if (!lastIp.equals(ip)) {
ActionMessages messages = new ActionMessages();
messages.add("message", new ActionMessage("onlinejudge.submit.invalid_ip"));
this.saveErrors(context.getRequest(), messages);
context.setAttribute("back", "contestInfo.do?contestId=" + contestId);
return this.handleFailure(mapping, context, messages, "nopermission");
}
return null;
}
use of org.apache.struts.action.ActionMessage in project zoj by licheng.
the class BaseAction method checkContestPermission.
protected ActionForward checkContestPermission(ActionMapping mapping, ContextAdapter context, Boolean isProblemset, boolean checkStart, PermissionLevel level) throws Exception {
// get the contest
AbstractContest contest = context.getContest();
if (contest == null || isProblemset != null && (contest instanceof Contest || contest instanceof Course) == isProblemset.booleanValue()) {
context.setAttribute("contest", null);
ActionMessages messages = new ActionMessages();
messages.add("message", new ActionMessage("onlinejudge.showcontest.nocontestid"));
this.saveErrors(context.getRequest(), messages);
if (isProblemset != null) {
context.setAttribute("back", isProblemset ? "showProblemsets.do" : "showContests.do");
}
return this.handleFailure(mapping, context, messages, "nopermission");
}
context.setAttribute("contest", contest);
// check contest permission
UserSecurity userSecurity = context.getUserSecurity();
boolean hasPermisstion = false;
if (level == PermissionLevel.ADMIN) {
hasPermisstion = userSecurity.canAdminContest(contest.getId());
} else if (level == PermissionLevel.PARTICIPATE) {
hasPermisstion = userSecurity.canParticipateContest(contest.getId());
} else if (level == PermissionLevel.VIEW) {
hasPermisstion = userSecurity.canViewContest(contest.getId());
} else if (level == PermissionLevel.PARTICIPATECANVIEWSOURCE) {
hasPermisstion = userSecurity.canViewSource(contest.getId());
}
if (!hasPermisstion) {
ActionMessages messages = new ActionMessages();
messages.add("message", new ActionMessage("onlinejudge.showcontest.nopermission"));
this.saveErrors(context.getRequest(), messages);
if (isProblemset != null) {
context.setAttribute("back", isProblemset ? "showProblemsets.do" : "showContests.do");
}
return this.handleFailure(mapping, context, messages, "nopermission");
}
// check start time
if (checkStart && !userSecurity.canAdminContest(contest.getId())) {
return this.checkContestStart(mapping, context, contest);
}
return null;
}
use of org.apache.struts.action.ActionMessage 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;
}
use of org.apache.struts.action.ActionMessage 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;
}
Aggregations