Search in sources :

Example 11 with Language

use of cn.edu.zju.acm.onlinejudge.bean.enumeration.Language in project zoj by licheng.

the class SubmitAction method execute.

/**
     * SubmitAction.
     * 
     * @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 (!this.isLogin(context, true)) {
        return this.handleSuccess(mapping, context, "login");
    }
    boolean isProblemset = context.getRequest().getRequestURI().endsWith("submit.do");
    ActionForward forward = this.checkProblemParticipatePermission(mapping, context, isProblemset);
    if (forward != null) {
        return forward;
    }
    AbstractContest contest = context.getContest();
    Problem problem = context.getProblem();
    long languageId = Utility.parseLong(context.getRequest().getParameter("languageId"));
    Language language = PersistenceManager.getInstance().getLanguagePersistence().getLanguage(languageId);
    if (language == null) {
        return this.handleSuccess(mapping, context, "submit");
    }
    String source = context.getRequest().getParameter("source");
    if (source == null || source.length() == 0) {
        return this.handleSuccess(mapping, context, "submit");
    }
    List refrance = PersistenceManager.getInstance().getReferencePersistence().getProblemReferences(problem.getId(), ReferenceType.HEADER);
    if (refrance.size() != 0) {
        Reference r = (Reference) refrance.get(0);
        String percode = new String(r.getContent());
        source = percode + "\n" + source;
    }
    UserProfile user = context.getUserProfile();
    if (submitCache != null && submitCache.contains(user.getId())) {
        ActionMessages messages = new ActionMessages();
        messages.add("message", new ActionMessage("onlinejudge.submit.interval"));
        this.saveErrors(context.getRequest(), messages);
        context.setAttribute("source", source);
        return handleSuccess(mapping, context, "submit");
    }
    if (contest.isCheckIp()) {
        forward = this.checkLastLoginIP(mapping, context, isProblemset);
        if (forward != null) {
            return forward;
        }
    }
    Submission submission = new Submission();
    submission.setContestId(contest.getId());
    submission.setLanguage(language);
    submission.setProblemId(problem.getId());
    submission.setUserProfileId(user.getId());
    submission.setContent(source);
    submission.setMemoryConsumption(0);
    submission.setTimeConsumption(0);
    submission.setSubmitDate(new Date());
    SubmissionPersistence submissionPersistence = PersistenceManager.getInstance().getSubmissionPersistence();
    if (contest.getEndTime() != null && new Date().after(contest.getEndTime())) {
        submission.setJudgeReply(JudgeReply.OUT_OF_CONTEST_TIME);
        submissionPersistence.createSubmission(submission, user.getId());
    } else if (source.getBytes().length > problem.getLimit().getSubmissionLimit() * 1024) {
        submission.setContent(source.substring(0, problem.getLimit().getSubmissionLimit() * 1024));
        submission.setJudgeReply(JudgeReply.SUBMISSION_LIMIT_EXCEEDED);
        submissionPersistence.createSubmission(submission, user.getId());
    } else {
        submission.setJudgeReply(JudgeReply.QUEUING);
        submissionPersistence.createSubmission(submission, user.getId());
        JudgeService.getInstance().judge(submission, Priority.NORMAL);
    }
    context.setAttribute("contestOrder", submission.getContestOrder());
    if (submitCache != null) {
        submitCache.put(user.getId(), user.getId());
    }
    return this.handleSuccess(mapping, context, "success");
}
Also used : AbstractContest(cn.edu.zju.acm.onlinejudge.bean.AbstractContest) UserProfile(cn.edu.zju.acm.onlinejudge.bean.UserProfile) Submission(cn.edu.zju.acm.onlinejudge.bean.Submission) Reference(cn.edu.zju.acm.onlinejudge.bean.Reference) SubmissionPersistence(cn.edu.zju.acm.onlinejudge.persistence.SubmissionPersistence) ActionForward(org.apache.struts.action.ActionForward) Date(java.util.Date) Language(cn.edu.zju.acm.onlinejudge.bean.enumeration.Language) ActionMessages(org.apache.struts.action.ActionMessages) ActionMessage(org.apache.struts.action.ActionMessage) Problem(cn.edu.zju.acm.onlinejudge.bean.Problem) List(java.util.List)

Example 12 with Language

use of cn.edu.zju.acm.onlinejudge.bean.enumeration.Language in project zoj by licheng.

the class TestSubmitAction method execute.

/**
     * SubmitAction.
     * 
     * @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 (!this.isLogin(context, true)) {
        return this.handleSuccess(mapping, context, "login");
    }
    boolean isProblemset = context.getRequest().getRequestURI().endsWith("submit.do");
    ActionForward forward = this.checkProblemParticipatePermission(mapping, context, isProblemset);
    if (forward != null) {
        return forward;
    }
    AbstractContest contest = context.getContest();
    Problem problem = context.getProblem();
    long languageId = Utility.parseLong(context.getRequest().getParameter("languageId"));
    Language language = PersistenceManager.getInstance().getLanguagePersistence().getLanguage(languageId);
    if (language == null) {
        return this.handleSuccess(mapping, context, "submit");
    }
    String source = context.getRequest().getParameter("source");
    if (source == null || source.length() == 0) {
        return this.handleSuccess(mapping, context, "submit");
    }
    List refrance = PersistenceManager.getInstance().getReferencePersistence().getProblemReferences(problem.getId(), ReferenceType.HEADER);
    if (refrance.size() != 0) {
        Reference r = (Reference) refrance.get(0);
        String percode = new String(r.getContent());
        source = percode + "\n" + source;
    }
    UserProfile user = context.getUserProfile();
    if (submitCache != null && submitCache.contains(user.getId())) {
        ActionMessages messages = new ActionMessages();
        messages.add("message", new ActionMessage("onlinejudge.submit.interval"));
        this.saveErrors(context.getRequest(), messages);
        context.setAttribute("source", source);
        return handleSuccess(mapping, context, "submit");
    }
    if (contest.isCheckIp()) {
        forward = this.checkLastLoginIP(mapping, context, isProblemset);
        if (forward != null) {
            return forward;
        }
    }
    Submission submission = new Submission();
    submission.setContestId(contest.getId());
    submission.setLanguage(language);
    submission.setProblemId(problem.getId());
    submission.setUserProfileId(user.getId());
    submission.setContent(source);
    submission.setMemoryConsumption(0);
    submission.setTimeConsumption(0);
    submission.setSubmitDate(new Date());
    SubmissionPersistence submissionPersistence = PersistenceManager.getInstance().getSubmissionPersistence();
    if (contest.getEndTime() != null && new Date().after(contest.getEndTime())) {
        submission.setJudgeReply(JudgeReply.OUT_OF_CONTEST_TIME);
        submissionPersistence.createSubmission(submission, user.getId());
    } else if (source.getBytes().length > problem.getLimit().getSubmissionLimit() * 1024) {
        submission.setContent(source.substring(0, problem.getLimit().getSubmissionLimit() * 1024));
        submission.setJudgeReply(JudgeReply.SUBMISSION_LIMIT_EXCEEDED);
        submissionPersistence.createSubmission(submission, user.getId());
    } else {
        Random ran = new Random();
        submission.setJudgeReply(ran.nextInt() % 2 == 0 ? JudgeReply.WRONG_ANSWER : JudgeReply.ACCEPTED);
        submissionPersistence.createSubmission(submission, user.getId());
        JudgeService.getInstance().judge(submission, Priority.NORMAL);
    }
    context.setAttribute("contestOrder", submission.getContestOrder());
    if (submitCache != null) {
        submitCache.put(user.getId(), user.getId());
    }
    return this.handleSuccess(mapping, context, "success");
}
Also used : AbstractContest(cn.edu.zju.acm.onlinejudge.bean.AbstractContest) UserProfile(cn.edu.zju.acm.onlinejudge.bean.UserProfile) Submission(cn.edu.zju.acm.onlinejudge.bean.Submission) Reference(cn.edu.zju.acm.onlinejudge.bean.Reference) SubmissionPersistence(cn.edu.zju.acm.onlinejudge.persistence.SubmissionPersistence) ActionForward(org.apache.struts.action.ActionForward) Date(java.util.Date) Language(cn.edu.zju.acm.onlinejudge.bean.enumeration.Language) Random(java.util.Random) ActionMessages(org.apache.struts.action.ActionMessages) ActionMessage(org.apache.struts.action.ActionMessage) Problem(cn.edu.zju.acm.onlinejudge.bean.Problem) List(java.util.List)

Example 13 with Language

use of cn.edu.zju.acm.onlinejudge.bean.enumeration.Language in project zoj by licheng.

the class ContestForm method toContest.

public AbstractContest toContest() throws ParseException, NumberFormatException, PersistenceException {
    AbstractContest contest = null;
    if (this.contestType == 1) {
        contest = new Problemset();
    } else if (this.contestType == 0) {
        contest = new Contest();
    } else {
        contest = new Course();
    }
    if (this.startTime != null && this.startTime.trim().length() > 0) {
        contest.setStartTime(Utility.parseTimestamp(this.startTime));
        contest.setEndTime(new Date(Utility.parseTimestamp(this.startTime).getTime() + Utility.parseTime(this.contestLength) * 1000));
    }
    try {
        if (this.id != null) {
            contest.setId(Long.parseLong(this.id));
        }
    } catch (NumberFormatException e) {
    }
    contest.setTitle(this.name);
    contest.setDescription(this.description);
    contest.setCheckIp(this.checkIp);
    Limit limit = new Limit();
    if (this.useGlobalDefault) {
        limit.setId(1);
    } else {
        limit.setTimeLimit(Integer.parseInt(this.timeLimit));
        limit.setMemoryLimit(Integer.parseInt(this.memoryLimit));
        limit.setSubmissionLimit(Integer.parseInt(this.submissionLimit));
        limit.setOutputLimit(Integer.parseInt(this.outputLimit));
    }
    contest.setLimit(limit);
    contest.setForumId(Long.parseLong(this.forumId));
    List<Language> languages = new ArrayList<Language>();
    LanguagePersistence languagePersistence = PersistenceManager.getInstance().getLanguagePersistence();
    if (this.languageIds != null) {
        for (int i = 0; i < this.languageIds.length; ++i) {
            Language language = languagePersistence.getLanguage(Long.parseLong(this.languageIds[i]));
            if (language != null) {
                languages.add(language);
            }
        }
    }
    contest.setLanguages(languages);
    return contest;
}
Also used : AbstractContest(cn.edu.zju.acm.onlinejudge.bean.AbstractContest) Language(cn.edu.zju.acm.onlinejudge.bean.enumeration.Language) ArrayList(java.util.ArrayList) AbstractContest(cn.edu.zju.acm.onlinejudge.bean.AbstractContest) Contest(cn.edu.zju.acm.onlinejudge.bean.Contest) Limit(cn.edu.zju.acm.onlinejudge.bean.Limit) Problemset(cn.edu.zju.acm.onlinejudge.bean.Problemset) Course(cn.edu.zju.acm.onlinejudge.bean.Course) Date(java.util.Date) LanguagePersistence(cn.edu.zju.acm.onlinejudge.persistence.LanguagePersistence)

Example 14 with Language

use of cn.edu.zju.acm.onlinejudge.bean.enumeration.Language in project zoj by licheng.

the class ContestPersistenceImpl method createContest.

/**
     * <p>
     * Creates the specified contest in persistence layer.
     * </p>
     * 
     * @param contest
     *            the AbstractContest instance to create
     * @param user
     *            the id of the user who made this modification
     * @throws PersistenceException
     *             wrapping a persistence implementation specific exception
     */
public void createContest(AbstractContest contest, long user) throws PersistenceException {
    Connection conn = null;
    try {
        conn = Database.createConnection();
        conn.setAutoCommit(false);
        PreparedStatement ps = null;
        Limit limit = contest.getLimit();
        try {
            // create a new limit
            if (limit != null && limit.getId() != ContestPersistenceImpl.DEFAULT_LIMIT_ID) {
                ps = conn.prepareStatement(ContestPersistenceImpl.INSERT_LIMIT);
                ps.setInt(1, limit.getTimeLimit());
                ps.setInt(2, limit.getMemoryLimit());
                ps.setInt(3, limit.getOutputLimit());
                ps.setInt(4, limit.getSubmissionLimit());
                ps.executeUpdate();
                limit.setId(Database.getLastId(conn));
            }
        } finally {
            Database.dispose(ps);
        }
        try {
            // create the contest
            ps = conn.prepareStatement(ContestPersistenceImpl.INSERT_CONTEST);
            ps.setString(1, contest.getTitle());
            ps.setString(2, contest.getDescription());
            if (contest.getStartTime() != null) {
                ps.setTimestamp(3, new Timestamp(contest.getStartTime().getTime()));
            } else {
                ps.setTimestamp(3, null);
            }
            if (contest.getEndTime() != null) {
                ps.setTimestamp(4, new Timestamp(contest.getEndTime().getTime()));
            } else {
                ps.setTimestamp(4, null);
            }
            ps.setLong(5, contest.getForumId());
            if (limit == null || limit.getId() == ContestPersistenceImpl.DEFAULT_LIMIT_ID) {
                ps.setLong(6, ContestPersistenceImpl.DEFAULT_LIMIT_ID);
            } else {
                ps.setLong(6, limit.getId());
            }
            int contesttype = 0;
            if (contest instanceof Problemset) {
                contesttype = 1;
            }
            if (contest instanceof Course) {
                contesttype = 2;
            }
            ps.setInt(7, contesttype);
            ps.setLong(8, user);
            ps.setTimestamp(9, new Timestamp(new Date().getTime()));
            ps.setLong(10, user);
            ps.setTimestamp(11, new Timestamp(new Date().getTime()));
            ps.setBoolean(12, contest.isCheckIp());
            ps.executeUpdate();
        } finally {
            Database.dispose(ps);
        }
        contest.setId(Database.getLastId(conn));
        // create languages
        if (contest.getLanguages() != null) {
            for (Language language : contest.getLanguages()) {
                try {
                    ps = conn.prepareStatement(ContestPersistenceImpl.INSERT_CONTEST_LANGUAGE);
                    ps.setLong(1, contest.getId());
                    ps.setLong(2, language.getId());
                    ps.executeUpdate();
                } finally {
                    Database.dispose(ps);
                }
            }
        }
        conn.commit();
    } catch (Exception e) {
        Database.rollback(conn);
        throw new PersistenceException("Failed to create contest.", e);
    } finally {
        Database.dispose(conn);
    }
}
Also used : Language(cn.edu.zju.acm.onlinejudge.bean.enumeration.Language) Connection(java.sql.Connection) PersistenceException(cn.edu.zju.acm.onlinejudge.persistence.PersistenceException) PreparedStatement(java.sql.PreparedStatement) Limit(cn.edu.zju.acm.onlinejudge.bean.Limit) Problemset(cn.edu.zju.acm.onlinejudge.bean.Problemset) Course(cn.edu.zju.acm.onlinejudge.bean.Course) Timestamp(java.sql.Timestamp) Date(java.util.Date) PersistenceException(cn.edu.zju.acm.onlinejudge.persistence.PersistenceException) SQLException(java.sql.SQLException)

Example 15 with Language

use of cn.edu.zju.acm.onlinejudge.bean.enumeration.Language in project zoj by licheng.

the class ContestPersistenceImpl method updateContest.

/**
     * <p>
     * Updates the specified contest in persistence layer.
     * </p>
     * 
     * @param contest
     *            the AbstractContest instance to update
     * @param user
     *            the id of the user who made this modification
     * @throws PersistenceException
     *             wrapping a persistence implementation specific exception
     */
public void updateContest(AbstractContest contest, long user) throws PersistenceException {
    Connection conn = null;
    try {
        conn = Database.createConnection();
        conn.setAutoCommit(false);
        PreparedStatement ps = null;
        long contestLimitId = ContestPersistenceImpl.DEFAULT_LIMIT_ID;
        try {
            ps = conn.prepareStatement(ContestPersistenceImpl.GET_CONTEST_LIMIT_ID);
            ps.setLong(1, contest.getId());
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                contestLimitId = rs.getLong(1);
            }
        } finally {
            Database.dispose(ps);
        }
        // update the limit
        Limit limit = contest.getLimit();
        if (limit.getId() != ContestPersistenceImpl.DEFAULT_LIMIT_ID) {
            try {
                ps = conn.prepareStatement(ContestPersistenceImpl.INSERT_LIMIT);
                ps.setInt(1, limit.getTimeLimit());
                ps.setInt(2, limit.getMemoryLimit());
                ps.setInt(3, limit.getOutputLimit());
                ps.setInt(4, limit.getSubmissionLimit());
                ps.executeUpdate();
            } finally {
                Database.dispose(ps);
            }
            limit.setId(Database.getLastId(conn));
        }
        if (contestLimitId != limit.getId()) {
            // TODO(xuchuan) I don't understand what's that.
            try {
                ps = conn.prepareStatement(ContestPersistenceImpl.UPDATE_PROBLEM_LIMIT);
                ps.setLong(1, limit.getId());
                ps.setLong(2, contest.getId());
                ps.setLong(3, contestLimitId);
                ps.executeUpdate();
            } finally {
                Database.dispose(ps);
            }
        }
        try {
            // update the contest
            ps = conn.prepareStatement(ContestPersistenceImpl.UPDATE_CONTEST);
            ps.setString(1, contest.getTitle());
            ps.setString(2, contest.getDescription());
            if (contest.getStartTime() != null) {
                ps.setTimestamp(3, new Timestamp(contest.getStartTime().getTime()));
            } else {
                ps.setTimestamp(3, null);
            }
            if (contest.getEndTime() != null) {
                ps.setTimestamp(4, new Timestamp(contest.getEndTime().getTime()));
            } else {
                ps.setTimestamp(4, null);
            }
            ps.setLong(5, contest.getForumId());
            if (limit == null || limit.getId() == ContestPersistenceImpl.DEFAULT_LIMIT_ID) {
                ps.setLong(6, ContestPersistenceImpl.DEFAULT_LIMIT_ID);
            } else {
                ps.setLong(6, limit.getId());
            }
            ps.setBoolean(7, contest instanceof Problemset);
            ps.setLong(8, user);
            ps.setTimestamp(9, new Timestamp(new Date().getTime()));
            ps.setBoolean(10, contest.isCheckIp());
            ps.setLong(11, contest.getId());
            ps.executeUpdate();
        } finally {
            Database.dispose(ps);
        }
        try {
            // delete languages
            ps = conn.prepareStatement(ContestPersistenceImpl.DELETE_CONTEST_LANGUAGE);
            ps.setLong(1, contest.getId());
            ps.executeUpdate();
        } finally {
            Database.dispose(ps);
        }
        // insert languages
        if (contest.getLanguages() != null) {
            for (Language language : contest.getLanguages()) {
                try {
                    ps = conn.prepareStatement(ContestPersistenceImpl.INSERT_CONTEST_LANGUAGE);
                    ps.setLong(1, contest.getId());
                    ps.setLong(2, language.getId());
                    ps.executeUpdate();
                } finally {
                    Database.dispose(ps);
                }
            }
        }
        conn.commit();
    } catch (Exception e) {
        Database.rollback(conn);
        throw new PersistenceException("Failed to create contest.", e);
    } finally {
        Database.dispose(conn);
    }
}
Also used : Language(cn.edu.zju.acm.onlinejudge.bean.enumeration.Language) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PersistenceException(cn.edu.zju.acm.onlinejudge.persistence.PersistenceException) PreparedStatement(java.sql.PreparedStatement) Limit(cn.edu.zju.acm.onlinejudge.bean.Limit) Problemset(cn.edu.zju.acm.onlinejudge.bean.Problemset) Timestamp(java.sql.Timestamp) Date(java.util.Date) PersistenceException(cn.edu.zju.acm.onlinejudge.persistence.PersistenceException) SQLException(java.sql.SQLException)

Aggregations

Language (cn.edu.zju.acm.onlinejudge.bean.enumeration.Language)21 ArrayList (java.util.ArrayList)12 PreparedStatement (java.sql.PreparedStatement)8 Submission (cn.edu.zju.acm.onlinejudge.bean.Submission)7 ResultSet (java.sql.ResultSet)7 List (java.util.List)7 PersistenceException (cn.edu.zju.acm.onlinejudge.persistence.PersistenceException)6 Connection (java.sql.Connection)6 SQLException (java.sql.SQLException)6 Date (java.util.Date)5 AbstractContest (cn.edu.zju.acm.onlinejudge.bean.AbstractContest)4 Limit (cn.edu.zju.acm.onlinejudge.bean.Limit)3 Problemset (cn.edu.zju.acm.onlinejudge.bean.Problemset)3 JudgeReply (cn.edu.zju.acm.onlinejudge.bean.enumeration.JudgeReply)3 ActionForward (org.apache.struts.action.ActionForward)3 Course (cn.edu.zju.acm.onlinejudge.bean.Course)2 Problem (cn.edu.zju.acm.onlinejudge.bean.Problem)2 Reference (cn.edu.zju.acm.onlinejudge.bean.Reference)2 UserProfile (cn.edu.zju.acm.onlinejudge.bean.UserProfile)2 SubmissionCriteria (cn.edu.zju.acm.onlinejudge.bean.request.SubmissionCriteria)2