@Transactional
  public Result removeProblem(long contestId, long contestProblemId)
      throws ContestNotFoundException, ContestProblemNotFoundException {
    Contest contest = contestService.findContestById(contestId);
    ContestProblem contestProblem = contestProblemService.findContestProblemById(contestProblemId);
    if (contest.isLocked()
        || !ContestControllerUtils.getInstance()
            .isManagerOrAbove(contest, IdentityUtils.getUserJid())
        || !contestProblem.getContestJid().equals(contest.getJid())) {
      return ContestControllerUtils.getInstance()
          .tryEnteringContest(contest, IdentityUtils.getUserJid());
    }

    contestProblemService.deleteContestProblem(contestProblem.getId());

    UrielControllerUtils.getInstance()
        .addActivityLog(
            BasicActivityKeys.REMOVE_FROM.construct(
                CONTEST,
                contest.getJid(),
                contest.getName(),
                PROBLEM,
                contestProblem.getProblemJid(),
                SandalphonResourceDisplayNameUtils.parseSlugByLanguage(
                    JidCacheServiceImpl.getInstance()
                        .getDisplayName(contestProblem.getProblemJid()))));

    return redirect(routes.ContestProblemController.viewProblems(contest.getId()));
  }
  @Transactional(readOnly = true)
  public Result listUsedProblems(long contestId, long pageIndex) throws ContestNotFoundException {
    Contest contest = contestService.findContestById(contestId);
    if (!ContestControllerUtils.getInstance()
        .isAllowedToEnterContest(contest, IdentityUtils.getUserJid())) {
      return ContestControllerUtils.getInstance()
          .tryEnteringContest(contest, IdentityUtils.getUserJid());
    }

    Page<ContestProblem> pageOfContestProblems =
        contestProblemService.getPageOfUsedProblemsInContest(
            contest.getJid(), pageIndex, PAGE_SIZE);
    ImmutableList.Builder<ContestProblem> replacementBuilder = ImmutableList.builder();
    for (ContestProblem contestProblem : pageOfContestProblems.getData()) {
      contestProblem.setTotalSubmissions(
          programmingSubmissionService.countProgrammingSubmissionsByUserJid(
              contest.getJid(), contestProblem.getProblemJid(), IdentityUtils.getUserJid()));
      replacementBuilder.add(contestProblem);
    }
    pageOfContestProblems =
        new Page<>(
            replacementBuilder.build(),
            pageOfContestProblems.getTotalRowsCount(),
            pageOfContestProblems.getPageIndex(),
            pageOfContestProblems.getPageSize());
    List<String> problemJids =
        pageOfContestProblems
            .getData()
            .stream()
            .map(cp -> cp.getProblemJid())
            .collect(Collectors.toList());
    Map<String, String> problemTitlesMap =
        SandalphonResourceDisplayNameUtils.buildTitlesMap(
            JidCacheServiceImpl.getInstance().getDisplayNames(problemJids),
            ContestControllerUtils.getInstance().getCurrentStatementLanguage());

    LazyHtml content =
        new LazyHtml(
            listUsedProblemsView.render(
                contest.getId(), pageOfContestProblems, pageIndex, problemTitlesMap));
    content.appendLayout(c -> heading3Layout.render(Messages.get("problem.problems"), c));
    if (isAllowedToSuperviseProblems(contest)) {
      appendSubtabsLayout(content, contest);
    }
    ContestControllerUtils.getInstance()
        .appendTabsLayout(content, contest, IdentityUtils.getUserJid());
    UrielControllerUtils.getInstance().appendSidebarLayout(content);
    appendBreadcrumbsLayout(
        content,
        contest,
        new InternalLink(
            Messages.get("problem.list"),
            routes.ContestProblemController.viewUsedProblems(contest.getId())));

    UrielControllerUtils.getInstance().appendTemplateLayout(content, "Contest - Problems");

    return UrielControllerUtils.getInstance().lazyOk(content);
  }
  @Transactional
  @RequireCSRFCheck
  public Result postEditProblem(long contestId, long contestProblemId)
      throws ContestNotFoundException, ContestProblemNotFoundException {
    Contest contest = contestService.findContestById(contestId);
    ContestProblem contestProblem = contestProblemService.findContestProblemById(contestProblemId);
    if (contest.isLocked()
        || !isAllowedToSuperviseProblems(contest)
        || !contestProblem.getContestJid().equals(contest.getJid())) {
      return ContestControllerUtils.getInstance()
          .tryEnteringContest(contest, IdentityUtils.getUserJid());
    }

    Form<ContestProblemEditForm> contestProblemEditForm =
        Form.form(ContestProblemEditForm.class).bindFromRequest();

    if (formHasErrors(contestProblemEditForm)) {
      return showEditProblem(contestProblemEditForm, contest, contestProblem);
    }

    ContestProblemEditForm contestProblemEditData = contestProblemEditForm.get();
    contestProblemService.updateContestProblem(
        contestProblem.getId(),
        contestProblemEditData.alias,
        contestProblemEditData.submissionsLimit,
        ContestProblemStatus.valueOf(contestProblemEditData.status),
        IdentityUtils.getUserJid(),
        IdentityUtils.getIpAddress());

    UrielControllerUtils.getInstance()
        .addActivityLog(
            BasicActivityKeys.EDIT_IN.construct(
                CONTEST,
                contest.getJid(),
                contest.getName(),
                PROBLEM,
                contestProblem.getProblemJid(),
                SandalphonResourceDisplayNameUtils.parseSlugByLanguage(
                    JidCacheServiceImpl.getInstance()
                        .getDisplayName(contestProblem.getProblemJid()))));

    return redirect(routes.ContestProblemController.viewProblems(contest.getId()));
  }
  @Transactional(readOnly = true)
  public Result listProblems(
      long contestId, long page, String sortBy, String orderBy, String filterString)
      throws ContestNotFoundException {
    Contest contest = contestService.findContestById(contestId);
    if (!isAllowedToSuperviseProblems(contest)) {
      return ContestControllerUtils.getInstance()
          .tryEnteringContest(contest, IdentityUtils.getUserJid());
    }

    boolean canDelete =
        ContestControllerUtils.getInstance().isManagerOrAbove(contest, IdentityUtils.getUserJid());

    Page<ContestProblem> pageOfContestProblems =
        contestProblemService.getPageOfProblemsInContest(
            contest.getJid(), page, PAGE_SIZE, sortBy, orderBy, filterString, null);
    List<String> problemJids =
        pageOfContestProblems
            .getData()
            .stream()
            .map(cp -> cp.getProblemJid())
            .collect(Collectors.toList());
    Map<String, String> problemSlugsMap =
        SandalphonResourceDisplayNameUtils.buildSlugsMap(
            JidCacheServiceImpl.getInstance().getDisplayNames(problemJids));

    LazyHtml content =
        new LazyHtml(
            listProblemsView.render(
                contest.getId(),
                pageOfContestProblems,
                page,
                sortBy,
                orderBy,
                filterString,
                canDelete,
                problemSlugsMap));
    content.appendLayout(
        c ->
            heading3WithActionsLayout.render(
                Messages.get("problem.list"),
                new InternalLink[] {
                  new InternalLink(
                      Messages.get("commons.create"),
                      routes.ContestProblemController.addProblem(contestId)),
                  new InternalLink(
                      Messages.get("problem.refresh"),
                      routes.ContestProblemController.refreshProblems(contestId))
                },
                c));
    appendSubtabsLayout(content, contest);
    ContestControllerUtils.getInstance()
        .appendTabsLayout(content, contest, IdentityUtils.getUserJid());
    UrielControllerUtils.getInstance().appendSidebarLayout(content);
    appendBreadcrumbsLayout(
        content,
        contest,
        new InternalLink(
            Messages.get("problem.list"),
            routes.ContestProblemController.viewProblems(contest.getId())));
    UrielControllerUtils.getInstance().appendTemplateLayout(content, "Contest - Problems");

    return UrielControllerUtils.getInstance().lazyOk(content);
  }