Search in sources :

Example 16 with Problem

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

the class ProblemPersistenceImplTest method testGetProblem3.

/**
	 * Tests getProblem method
	 * @throws Exception to JUnit
	 */
public void testGetProblem3() throws Exception {
    Problem problem = persistence.getProblem(problem3.getId());
    checkProblem(problem3, problem);
}
Also used : Problem(cn.edu.zju.acm.onlinejudge.bean.Problem)

Example 17 with Problem

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

the class SearchProblemAction method execute.

@Override
protected ActionForward execute(ActionMapping mapping, ActionForm form, ContextAdapter context) throws Exception {
    //System.out.println("SearchProblemAction begin");
    List<Problem> TitleQueryResult = new ArrayList<Problem>();
    List<Problem> AuthorQueryResult = new ArrayList<Problem>();
    List<Problem> SourceQueryResult = new ArrayList<Problem>();
    AbstractContest contest = context.getContest();
    String query = context.getRequest().getParameter("query").toLowerCase();
    String temp = context.getRequest().getParameter("titlefrom");
    int titlefrom = Integer.parseInt(temp);
    temp = context.getRequest().getParameter("authorfrom");
    int authorfrom = Integer.parseInt(temp);
    temp = context.getRequest().getParameter("sourcefrom");
    int sourcefrom = Integer.parseInt(temp);
    List<Problem> problems = ContestManager.getInstance().getContestProblems(contest.getId());
    for (int i = 0; i < problems.size(); ++i) {
        Problem p = problems.get(i);
        if (p.getTitle() != null) {
            if (p.getTitle().toLowerCase().indexOf(query) >= 0) {
                TitleQueryResult.add(p);
            }
        }
        if (p.getAuthor() != null) {
            if (p.getAuthor().toLowerCase().indexOf(query) >= 0) {
                AuthorQueryResult.add(p);
            }
        }
        if (p.getSource() != null) {
            if (p.getSource().toLowerCase().indexOf(query) >= 0) {
                SourceQueryResult.add(p);
            }
        }
    }
    context.setAttribute("TitleQueryResultCount", TitleQueryResult.size());
    if (titlefrom * 50 + 49 > TitleQueryResult.size()) {
        context.setAttribute("TitleQueryResult", TitleQueryResult.subList(titlefrom * 50, TitleQueryResult.size()));
    } else {
        context.setAttribute("TitleQueryResult", TitleQueryResult.subList(titlefrom * 50, titlefrom * 50 + 49));
    }
    context.setAttribute("titlefrom", titlefrom);
    context.setAttribute("AuthorQueryResultCount", AuthorQueryResult.size());
    if (authorfrom * 50 + 49 > AuthorQueryResult.size()) {
        context.setAttribute("AuthorQueryResult", AuthorQueryResult.subList(authorfrom * 50, AuthorQueryResult.size()));
    } else {
        context.setAttribute("AuthorQueryResult", AuthorQueryResult.subList(authorfrom * 50, authorfrom * 50 + 49));
    }
    context.setAttribute("authorfrom", authorfrom);
    context.setAttribute("SourceQueryResultCount", SourceQueryResult.size());
    if (sourcefrom * 50 + 49 > SourceQueryResult.size()) {
        context.setAttribute("SourceQueryResult", SourceQueryResult.subList(sourcefrom * 50, SourceQueryResult.size()));
    } else {
        context.setAttribute("SourceQueryResult", SourceQueryResult.subList(sourcefrom * 50, sourcefrom * 50 + 49));
    }
    context.setAttribute("sourcefrom", sourcefrom);
    context.setAttribute("query", query);
    //System.out.println("SearchProblemAction end");
    return this.handleSuccess(mapping, context, "success");
}
Also used : AbstractContest(cn.edu.zju.acm.onlinejudge.bean.AbstractContest) ArrayList(java.util.ArrayList) Problem(cn.edu.zju.acm.onlinejudge.bean.Problem)

Example 18 with Problem

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

the class ShowProblemsAction method execute.

/**
     * ShowProblemsAction.
     * 
     * @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
    //System.out.println("in show problems");
    boolean isProblemset = context.getRequest().getRequestURI().endsWith("showProblems.do");
    //boolean isCourse=context.getRequest().getRequestURI().endsWith("showCourseProblems.do");
    ActionForward forward = this.checkContestViewPermission(mapping, context, isProblemset, true);
    if (forward != null) {
        return forward;
    }
    AbstractContest contest = context.getContest();
    //System.out.println("" + contest.getId());
    long problemsCount = ContestManager.getInstance().getProblemsCount(contest.getId());
    long pageNumber = Utility.parseLong(context.getRequest().getParameter("pageNumber"));
    if (pageNumber < 1) {
        pageNumber = 1;
    }
    long problemsPerPage = 100;
    if (problemsCount <= (pageNumber - 1) * problemsPerPage) {
        pageNumber = 1;
    }
    long totalPages = 1;
    if (problemsCount > 0) {
        totalPages = (problemsCount - 1) / problemsPerPage + 1;
    }
    List<Problem> oproblems = ContestManager.getInstance().getContestProblems(contest.getId(), (int) ((pageNumber - 1) * problemsPerPage), (int) problemsPerPage);
    List<Problem> problems = new ArrayList<Problem>();
    problems.addAll(oproblems);
    ContestStatistics contestStatistics = null;
    contestStatistics = StatisticsManager.getInstance().getContestStatistics(contest.getId(), (int) ((pageNumber - 1) * problemsPerPage), (int) problemsPerPage);
    for (int i = 0; i < problems.size(); ++i) {
        Problem p = (Problem) problems.get(i);
        int ac = contestStatistics.getCount(i, 0);
        int total = contestStatistics.getProblemCount(i);
        p.setTotal(total);
        p.setAC(ac);
        if (total == 0) {
            p.setRatio(0);
        } else {
            p.setRatio((double) ac / (double) total);
        }
    }
    String order = context.getRequest().getParameter("order");
    if (order != null) {
        if (order.equalsIgnoreCase("ratio")) {
            Collections.sort(problems, new Comparator() {

                public int compare(Object o1, Object o2) {
                    return (((Problem) o2).getRatio() - ((Problem) o1).getRatio()) > 0 ? 1 : -1;
                }
            });
        } else if (order.equalsIgnoreCase("ac")) {
            Collections.sort(problems, new Comparator() {

                public int compare(Object o1, Object o2) {
                    return ((Problem) o2).getAC() - ((Problem) o1).getAC();
                }
            });
        } else if (order.equalsIgnoreCase("all")) {
            Collections.sort(problems, new Comparator() {

                public int compare(Object o1, Object o2) {
                    return ((Problem) o2).getTotal() - ((Problem) o1).getTotal();
                }
            });
        }
    }
    UserStatistics userStatistics = null;
    if (context.getUserProfile() != null && problems.size() > 0) {
        userStatistics = StatisticsManager.getInstance().getUserStatistics(contest.getId(), context.getUserProfile().getId());
    }
    context.setAttribute("problems", problems);
    context.setAttribute("pageNumber", (new Long(pageNumber)).toString());
    context.setAttribute("ContestStatistics", contestStatistics);
    context.setAttribute("UserStatistics", userStatistics);
    context.setAttribute("totalPages", new Long(totalPages));
    context.setAttribute("currentPage", new Long(pageNumber));
    if (this.checkContestAdminPermission(mapping, context, isProblemset, true) == null && "true".equalsIgnoreCase(context.getRequest().getParameter("check"))) {
        List<String> checkMessages = new ArrayList<String>();
        for (Object obj : problems) {
            Problem p = (Problem) obj;
            this.checkExists("Text", this.getReferenceLength(p, ReferenceType.DESCRIPTION), "ERROR", checkMessages, p);
            this.checkExists("Input", this.getReferenceLength(p, ReferenceType.INPUT), "ERROR", checkMessages, p);
            if (p.isChecker()) {
                this.checkExists("Output", this.getReferenceLength(p, ReferenceType.OUTPUT), "WARNING", checkMessages, p);
                // checkExists("Checker", getReferenceLength(p, ReferenceType.CHECKER), "WARNING", checkMessages,
                // p);
                this.checkExists("Checker source", this.getReferenceLength(p, ReferenceType.CHECKER_SOURCE), "ERROR", checkMessages, p);
            } else {
                this.checkExists("Output", this.getReferenceLength(p, ReferenceType.OUTPUT), "ERROR", checkMessages, p);
            }
            this.checkExists("Judge solution", this.getReferenceLength(p, ReferenceType.JUDGE_SOLUTION), "WARNING", checkMessages, p);
        }
        context.setAttribute("CheckMessages", checkMessages);
    }
    return this.handleSuccess(mapping, context, "success");
}
Also used : AbstractContest(cn.edu.zju.acm.onlinejudge.bean.AbstractContest) ArrayList(java.util.ArrayList) ActionForward(org.apache.struts.action.ActionForward) UserStatistics(cn.edu.zju.acm.onlinejudge.util.UserStatistics) Problem(cn.edu.zju.acm.onlinejudge.bean.Problem) ContestStatistics(cn.edu.zju.acm.onlinejudge.util.ContestStatistics)

Example 19 with Problem

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

the class ShowRankListAction method exportToText.

private byte[] exportToText(AbstractContest contest, List<Problem> problems, RankList ranklist, boolean windows) throws Exception {
    List<RankListEntry> entries = ranklist.getEntries();
    String lineHolder = windows ? "\r\n" : "\n";
    long time = this.getTimeEscaped(contest);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out));
    writer.write(contest.getTitle());
    writer.write(lineHolder);
    writer.write(ranklist.getRole() == null ? "" : ranklist.getRole().getDescription());
    writer.write(lineHolder);
    writer.write("Length: " + Utility.toTime(contest.getLength() / 1000));
    writer.write(lineHolder);
    writer.write("Time Escaped: " + Utility.toTime(time / 1000));
    writer.write(lineHolder);
    writer.write(lineHolder);
    writer.write("Rank\tHandle\tNickname\tSolved\t");
    for (Problem problem2 : problems) {
        Problem problem = problem2;
        writer.write(problem.getCode());
        writer.write("\t");
    }
    writer.write("Penalty");
    writer.write(lineHolder);
    int index = 0;
    for (RankListEntry rankListEntry : entries) {
        index++;
        RankListEntry entry = rankListEntry;
        writer.write(index + "\t");
        writer.write(entry.getUserProfile().getHandle() + "\t");
        writer.write((entry.getUserProfile().getNickName() == null ? entry.getUserProfile().getHandle() : entry.getUserProfile().getNickName()) + "\t");
        writer.write(entry.getSolved() + "\t");
        for (int i = 0; i < problems.size(); ++i) {
            String score = entry.getAcceptTime(i) > 0 ? entry.getAcceptTime(i) + "(" + entry.getSubmitNumber(i) + ")" : "" + entry.getSubmitNumber(i);
            writer.write(score + "\t");
        }
        writer.write(entry.getPenalty() + lineHolder);
    }
    writer.close();
    return out.toByteArray();
}
Also used : RankListEntry(cn.edu.zju.acm.onlinejudge.util.RankListEntry) OutputStreamWriter(java.io.OutputStreamWriter) Problem(cn.edu.zju.acm.onlinejudge.bean.Problem) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BufferedWriter(java.io.BufferedWriter)

Example 20 with Problem

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

the class ShowSubmissionAction method execute.

/**
     * ShowSourceAction.
     * 
     * @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 {
    HttpServletResponse response = context.getResponse();
    long id = Utility.parseLong(context.getRequest().getParameter("submissionId"));
    Submission submission = ContestManager.getInstance().getSubmission(id);
    if (submission == null) {
        response.sendError(404);
        return null;
    }
    // user can always view their own submission
    UserProfile user = context.getUserProfile();
    if (user == null || user.getId() != submission.getUserProfileId()) {
        Problem problem = ContestManager.getInstance().getProblem(submission.getProblemId());
        context.setAttribute("problem", problem);
        ActionForward forward = this.checkProblemViewSourecPermission(mapping, context, null);
        if (forward != null) {
            response.sendError(404);
            return null;
        }
    }
    response.setContentType("text/plain");
    boolean download = "true".equalsIgnoreCase(context.getRequest().getParameter("download"));
    if (download) {
        response.setHeader("Content-disposition", "attachment; filename=" + id + "." + submission.getLanguage().getOptions());
    }
    response.getOutputStream().write(submission.getContent().getBytes());
    response.getOutputStream().close();
    return null;
}
Also used : Submission(cn.edu.zju.acm.onlinejudge.bean.Submission) UserProfile(cn.edu.zju.acm.onlinejudge.bean.UserProfile) HttpServletResponse(javax.servlet.http.HttpServletResponse) Problem(cn.edu.zju.acm.onlinejudge.bean.Problem) ActionForward(org.apache.struts.action.ActionForward)

Aggregations

Problem (cn.edu.zju.acm.onlinejudge.bean.Problem)44 ActionForward (org.apache.struts.action.ActionForward)11 AbstractContest (cn.edu.zju.acm.onlinejudge.bean.AbstractContest)10 ArrayList (java.util.ArrayList)9 Limit (cn.edu.zju.acm.onlinejudge.bean.Limit)8 ActionMessage (org.apache.struts.action.ActionMessage)7 ProblemPersistence (cn.edu.zju.acm.onlinejudge.persistence.ProblemPersistence)6 ActionMessages (org.apache.struts.action.ActionMessages)6 Reference (cn.edu.zju.acm.onlinejudge.bean.Reference)5 PersistenceException (cn.edu.zju.acm.onlinejudge.persistence.PersistenceException)5 Connection (java.sql.Connection)5 PreparedStatement (java.sql.PreparedStatement)5 ResultSet (java.sql.ResultSet)5 SQLException (java.sql.SQLException)5 UserProfile (cn.edu.zju.acm.onlinejudge.bean.UserProfile)4 Submission (cn.edu.zju.acm.onlinejudge.bean.Submission)3 RankListEntry (cn.edu.zju.acm.onlinejudge.util.RankListEntry)3 List (java.util.List)3 Language (cn.edu.zju.acm.onlinejudge.bean.enumeration.Language)2 ProblemForm (cn.edu.zju.acm.onlinejudge.form.ProblemForm)2