/**
  * This recovers a set of html-sanitized string to original encoding for appropriate display in
  * files such as csv file <br>
  * It restores encoding for < > \ / ' & <br>
  *
  * @param sanitized string set
  * @return recovered string set
  */
 public static Set<String> recoverFromSanitizedText(Set<String> textSet) {
   Set<String> textSetTemp = new HashSet<String>();
   for (String text : textSet) {
     textSetTemp.add(StringHelper.recoverFromSanitizedText(text));
   }
   return textSetTemp;
 }
  @Override
  protected ActionResult execute() throws EntityDoesNotExistException {
    courseId = getRequestParamValue(Const.ParamsNames.COURSE_ID);
    feedbackSessionName = getRequestParamValue(Const.ParamsNames.FEEDBACK_SESSION_NAME);
    Assumption.assertPostParamNotNull(Const.ParamsNames.COURSE_ID, courseId);
    Assumption.assertPostParamNotNull(Const.ParamsNames.FEEDBACK_SESSION_NAME, feedbackSessionName);

    setAdditionalParameters();
    verifyAccesibleForSpecificUser();

    String userEmailForCourse = getUserEmailForCourse();
    String userTeamForCourse = getUserTeamForCourse();
    String userSectionForCourse = getUserSectionForCourse();
    data = new FeedbackSubmissionEditPageData(account, student);
    data.bundle = getDataBundle(userEmailForCourse);
    Assumption.assertNotNull(
        "Feedback session " + feedbackSessionName + " does not exist in " + courseId + ".",
        data.bundle);

    checkAdditionalConstraints();

    setStatusToAdmin();

    if (!isSessionOpenForSpecificUser(data.bundle.feedbackSession)) {
      isError = true;
      statusToUser.add(
          new StatusMessage(
              Const.StatusMessages.FEEDBACK_SUBMISSIONS_NOT_OPEN, StatusMessageColor.WARNING));
      return createSpecificRedirectResult();
    }

    int numOfQuestionsToGet = data.bundle.questionResponseBundle.size();
    for (int questionIndx = 1; questionIndx <= numOfQuestionsToGet; questionIndx++) {
      String totalResponsesForQuestion =
          getRequestParamValue(
              Const.ParamsNames.FEEDBACK_QUESTION_RESPONSETOTAL + "-" + questionIndx);

      if (totalResponsesForQuestion == null) {
        continue; // question has been skipped (not displayed).
      }

      List<FeedbackResponseAttributes> responsesForQuestion =
          new ArrayList<FeedbackResponseAttributes>();
      String questionId =
          HttpRequestHelper.getValueFromParamMap(
              requestParameters, Const.ParamsNames.FEEDBACK_QUESTION_ID + "-" + questionIndx);
      FeedbackQuestionAttributes questionAttributes = data.bundle.getQuestionAttributes(questionId);
      if (questionAttributes == null) {
        statusToUser.add(
            new StatusMessage(
                "The feedback session or questions may have changed while you were submitting. "
                    + "Please check your responses to make sure they are saved correctly.",
                StatusMessageColor.WARNING));
        isError = true;
        log.warning(
            "Question not found. (deleted or invalid id passed?) id: "
                + questionId
                + " index: "
                + questionIndx);
        continue;
      }

      FeedbackQuestionDetails questionDetails = questionAttributes.getQuestionDetails();

      int numOfResponsesToGet = Integer.parseInt(totalResponsesForQuestion);
      String qnId = "";

      Set<String> emailSet = data.bundle.getRecipientEmails(questionAttributes.getId());
      emailSet.add("");
      emailSet = StringHelper.recoverFromSanitizedText(emailSet);

      ArrayList<String> responsesRecipients = new ArrayList<String>();
      List<String> errors = new ArrayList<String>();

      for (int responseIndx = 0; responseIndx < numOfResponsesToGet; responseIndx++) {
        FeedbackResponseAttributes response =
            extractFeedbackResponseData(
                requestParameters, questionIndx, responseIndx, questionAttributes);

        if (response.feedbackQuestionType != questionAttributes.questionType) {
          errors.add(
              String.format(
                  Const.StatusMessages.FEEDBACK_RESPONSES_WRONG_QUESTION_TYPE, questionIndx));
        }

        qnId = response.feedbackQuestionId;

        boolean isExistingResponse = response.getId() != null;
        // test that if editing an existing response, that the edited response's id
        // came from the original set of existing responses loaded on the submission page
        if (isExistingResponse && !isExistingResponseValid(response)) {
          errors.add(
              String.format(Const.StatusMessages.FEEDBACK_RESPONSES_INVALID_ID, questionIndx));
          continue;
        }

        responsesRecipients.add(response.recipientEmail);
        // if the answer is not empty but the recipient is empty
        if (response.recipientEmail.isEmpty() && !response.responseMetaData.getValue().isEmpty()) {
          errors.add(
              String.format(
                  Const.StatusMessages.FEEDBACK_RESPONSES_MISSING_RECIPIENT, questionIndx));
        }

        if (response.responseMetaData.getValue().isEmpty()) {
          // deletes the response since answer is empty
          saveResponse(response);
        } else {
          response.giverEmail =
              questionAttributes.giverType.isTeam() ? userTeamForCourse : userEmailForCourse;
          response.giverSection = userSectionForCourse;
          responsesForQuestion.add(response);
        }
      }

      List<String> questionSpecificErrors =
          questionDetails.validateResponseAttributes(
              responsesForQuestion, data.bundle.recipientList.get(qnId).size());
      errors.addAll(questionSpecificErrors);

      if (!emailSet.containsAll(responsesRecipients)) {
        errors.add(
            String.format(Const.StatusMessages.FEEDBACK_RESPONSE_INVALID_RECIPIENT, questionIndx));
      }

      if (errors.isEmpty()) {
        for (FeedbackResponseAttributes response : responsesForQuestion) {
          saveResponse(response);
        }
      } else {
        List<StatusMessage> errorMessages = new ArrayList<StatusMessage>();

        for (String error : errors) {
          errorMessages.add(new StatusMessage(error, StatusMessageColor.DANGER));
        }

        statusToUser.addAll(errorMessages);
        isError = true;
      }
    }

    if (!isError) {
      statusToUser.add(
          new StatusMessage(
              Const.StatusMessages.FEEDBACK_RESPONSES_SAVED, StatusMessageColor.SUCCESS));
    }

    if (isUserRespondentOfSession()) {
      appendRespondant();
    } else {
      removeRespondant();
    }

    return createSpecificRedirectResult();
  }