@Transactional
  public Result refreshProblems(long contestId)
      throws ContestNotFoundException, ContestProblemNotFoundException {
    Contest contest = contestService.findContestById(contestId);
    if (contest.isLocked()
        || !ContestControllerUtils.getInstance()
            .isManagerOrAbove(contest, IdentityUtils.getUserJid())) {
      return ContestControllerUtils.getInstance()
          .tryEnteringContest(contest, IdentityUtils.getUserJid());
    }

    for (ContestProblem problem :
        contestProblemService.getOpenedProblemsInContest(contest.getJid())) {
      SandalphonProblem sandalphonProblem;
      try {
        sandalphonProblem =
            sandalphonClientAPI.findClientProblem(
                problem.getProblemJid(), problem.getProblemSecret());
      } catch (JudgelsAPIClientException e) {
        continue;
      }

      JidCacheServiceImpl.getInstance()
          .putDisplayName(
              problem.getProblemJid(),
              sandalphonProblem.getDisplayName(),
              IdentityUtils.getUserJid(),
              IdentityUtils.getIpAddress());
    }
    return redirect(routes.ContestProblemController.viewProblems(contest.getId()));
  }
  @Transactional(readOnly = true)
  public Result renderImage(long contestId, long contestProblemId, String imageFilename)
      throws ContestNotFoundException, ContestProblemNotFoundException {
    Contest contest = contestService.findContestById(contestId);
    ContestProblem contestProblem = contestProblemService.findContestProblemById(contestProblemId);
    if (!contest.getJid().equals(contestProblem.getContestJid())) {
      return notFound();
    }

    String imageUrl =
        sandalphonClientAPI.getProblemStatementMediaRenderAPIEndpoint(
            contestProblem.getProblemJid(), imageFilename);

    return redirect(imageUrl);
  }
  @Transactional
  @RequireCSRFCheck
  public Result postAddProblem(long contestId) throws ContestNotFoundException {
    Contest contest = contestService.findContestById(contestId);
    if (contest.isLocked() || !isAllowedToSuperviseProblems(contest)) {
      return ContestControllerUtils.getInstance()
          .tryEnteringContest(contest, IdentityUtils.getUserJid());
    }

    Form<ContestProblemAddForm> contestProblemCreateForm =
        Form.form(ContestProblemAddForm.class).bindFromRequest();

    if (formHasErrors(contestProblemCreateForm)) {
      return showAddProblem(contestProblemCreateForm, contest);
    }

    ContestProblemAddForm contestProblemAddData = contestProblemCreateForm.get();

    if (contestProblemService.isProblemInContestByJidOrAlias(
        contest.getJid(), contestProblemAddData.problemJid, contestProblemAddData.alias)) {
      contestProblemCreateForm.reject("error.problem.create.problemJidOrAliasUsed");
      return showAddProblem(contestProblemCreateForm, contest);
    }

    SandalphonProblem sandalphonProblem;
    try {
      sandalphonProblem =
          sandalphonClientAPI.findClientProblem(
              contestProblemAddData.problemJid, contestProblemAddData.problemSecret);
    } catch (JudgelsAPIClientException e) {
      if (e.getStatusCode() >= Http.Status.INTERNAL_SERVER_ERROR) {
        contestProblemCreateForm.reject("error.system.sandalphon.connection");
      } else {
        contestProblemCreateForm.reject("error.problem.create.problemJidOrSecretInvalid");
      }
      return showAddProblem(contestProblemCreateForm, contest);
    }

    contestProblemService.createContestProblem(
        contest.getJid(),
        contestProblemAddData.problemJid,
        contestProblemAddData.problemSecret,
        contestProblemAddData.alias,
        contestProblemAddData.submissionsLimit,
        ContestProblemStatus.valueOf(contestProblemAddData.status),
        IdentityUtils.getUserJid(),
        IdentityUtils.getIpAddress());
    JidCacheServiceImpl.getInstance()
        .putDisplayName(
            contestProblemAddData.problemJid,
            sandalphonProblem.getDisplayName(),
            IdentityUtils.getUserJid(),
            IdentityUtils.getIpAddress());

    UrielControllerUtils.getInstance()
        .addActivityLog(
            BasicActivityKeys.ADD_IN.construct(
                CONTEST,
                contest.getJid(),
                contest.getName(),
                PROBLEM,
                contestProblemAddData.problemJid,
                sandalphonProblem.getSlug()));

    return redirect(routes.ContestProblemController.viewProblems(contest.getId()));
  }
  @Transactional(readOnly = true)
  public Result viewProblem(long contestId, long contestProblemId)
      throws ContestNotFoundException, ContestProblemNotFoundException {
    Contest contest = contestService.findContestById(contestId);
    ContestProblem contestProblem = contestProblemService.findContestProblemById(contestProblemId);
    if (!ContestControllerUtils.getInstance()
            .isAllowedToEnterContest(contest, IdentityUtils.getUserJid())
        || !isAllowedToViewProblem(contest, contestProblem)) {
      return ContestControllerUtils.getInstance()
          .tryEnteringContest(contest, IdentityUtils.getUserJid());
    }

    long submissionsLeft = -1;
    if (contestProblem.getSubmissionsLimit() != 0) {
      submissionsLeft =
          contestProblem.getSubmissionsLimit()
              - programmingSubmissionService.countProgrammingSubmissionsByUserJid(
                  contest.getJid(), contestProblem.getProblemJid(), IdentityUtils.getUserJid());
    }

    SandalphonProgrammingProblemStatementRenderRequestParam param =
        new SandalphonProgrammingProblemStatementRenderRequestParam();

    param.setProblemSecret(contestProblem.getProblemSecret());
    param.setCurrentMillis(System.currentTimeMillis());
    param.setStatementLanguage(ContestControllerUtils.getInstance().getCurrentStatementLanguage());
    param.setSwitchStatementLanguageUrl(
        routes.ContestProblemController.switchLanguage(contestId, contestProblemId)
            .absoluteURL(request(), request().secure()));
    param.setPostSubmitUrl(
        org.iatoki.judgels.uriel.contest.submission.programming.routes
            .ContestProgrammingSubmissionController.postSubmitProblem(
                contest.getId(), contestProblem.getProblemJid())
            .absoluteURL(request(), request().secure()));
    param.setReasonNotAllowedToSubmit(null);

    Set<String> allowedGradingLanguages;

    if (contest.isICPC()) {
      allowedGradingLanguages =
          ((ICPCContestStyleConfig) contest.getStyleConfig())
              .getLanguageRestriction()
              .getAllowedLanguageNames();
    } else {
      allowedGradingLanguages =
          ((IOIContestStyleConfig) contest.getStyleConfig())
              .getLanguageRestriction()
              .getAllowedLanguageNames();
    }

    param.setAllowedGradingLanguages(StringUtils.join(allowedGradingLanguages, ","));

    String requestUrl =
        sandalphonClientAPI.getProgrammingProblemStatementRenderAPIEndpoint(
            contestProblem.getProblemJid());
    String requestBody =
        sandalphonClientAPI.constructProgrammingProblemStatementRenderAPIRequestBody(
            contestProblem.getProblemJid(), param);

    LazyHtml content;
    if (UrielProperties.getInstance().isContestCritial(contest.getJid())) {
      content =
          new LazyHtml(
              viewProblemCriticalView.render(
                  requestUrl,
                  requestBody,
                  submissionsLeft,
                  contestProblem.getStatus() == ContestProblemStatus.CLOSED,
                  contest,
                  contestProblem));
    } else {
      content =
          new LazyHtml(
              viewProblemView.render(
                  requestUrl,
                  requestBody,
                  submissionsLeft,
                  contestProblem.getStatus() == ContestProblemStatus.CLOSED));
    }
    ContestControllerUtils.getInstance()
        .appendTabsLayout(content, contest, IdentityUtils.getUserJid());
    UrielControllerUtils.getInstance().appendSidebarLayout(content);
    appendBreadcrumbsLayout(
        content,
        contest,
        new InternalLink(
            Messages.get("status.contestant"),
            routes.ContestProblemController.viewUsedProblems(contest.getId())),
        new InternalLink(
            contestProblem.getAlias(),
            routes.ContestProblemController.viewProblem(contest.getId(), contestProblem.getId())));

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

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