Search in sources :

Example 66 with ActionMessage

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");
}
Also used : ActionMessages(org.apache.struts.action.ActionMessages) ActionMessage(org.apache.struts.action.ActionMessage)

Example 67 with ActionMessage

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;
}
Also used : ContestPersistence(cn.edu.zju.acm.onlinejudge.persistence.ContestPersistence) ActionMessages(org.apache.struts.action.ActionMessages) ActionMessage(org.apache.struts.action.ActionMessage)

Example 68 with ActionMessage

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;
}
Also used : AbstractContest(cn.edu.zju.acm.onlinejudge.bean.AbstractContest) UserSecurity(cn.edu.zju.acm.onlinejudge.security.UserSecurity) ActionMessages(org.apache.struts.action.ActionMessages) ActionMessage(org.apache.struts.action.ActionMessage) AbstractContest(cn.edu.zju.acm.onlinejudge.bean.AbstractContest) Contest(cn.edu.zju.acm.onlinejudge.bean.Contest) Course(cn.edu.zju.acm.onlinejudge.bean.Course)

Example 69 with ActionMessage

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;
}
Also used : ActionMessage(org.apache.struts.action.ActionMessage) Problem(cn.edu.zju.acm.onlinejudge.bean.Problem) ActionErrors(org.apache.struts.action.ActionErrors)

Example 70 with ActionMessage

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;
}
Also used : ActionMessage(org.apache.struts.action.ActionMessage) Problem(cn.edu.zju.acm.onlinejudge.bean.Problem) ActionErrors(org.apache.struts.action.ActionErrors)

Aggregations

ActionMessage (org.apache.struts.action.ActionMessage)146 ActionErrors (org.apache.struts.action.ActionErrors)68 ActionMessages (org.apache.struts.action.ActionMessages)31 ResourceBundle (java.util.ResourceBundle)29 InvalidDateException (org.mifos.application.admin.servicefacade.InvalidDateException)22 Locale (java.util.Locale)18 JspException (javax.servlet.jsp.JspException)11 Iterator (java.util.Iterator)10 ActionForward (org.apache.struts.action.ActionForward)9 AbstractContest (cn.edu.zju.acm.onlinejudge.bean.AbstractContest)8 List (java.util.List)8 Problem (cn.edu.zju.acm.onlinejudge.bean.Problem)7 ArrayList (java.util.ArrayList)7 DoubleConversionResult (org.mifos.framework.util.helpers.DoubleConversionResult)7 UserProfile (cn.edu.zju.acm.onlinejudge.bean.UserProfile)6 Date (java.sql.Date)6 PageExpiredException (org.mifos.framework.exceptions.PageExpiredException)6 ApplicationException (org.mifos.framework.exceptions.ApplicationException)5 UserPersistence (cn.edu.zju.acm.onlinejudge.persistence.UserPersistence)4 UserSecurity (cn.edu.zju.acm.onlinejudge.security.UserSecurity)4