/**
   * Updates a {@link FeedbackResponse} based on it's {@code id}.<br>
   * If the giver/recipient field is changed, the {@link FeedbackResponse} is updated by recreating
   * the response<br>
   * in order to prevent an id clash if the previous email is reused later on.
   */
  public void updateFeedbackResponse(FeedbackResponseAttributes responseToUpdate)
      throws InvalidParametersException, EntityDoesNotExistException, EntityAlreadyExistsException {

    // Create a copy.
    FeedbackResponseAttributes newResponse = new FeedbackResponseAttributes(responseToUpdate);
    FeedbackResponseAttributes oldResponse = null;
    if (newResponse.getId() == null) {
      oldResponse =
          frDb.getFeedbackResponse(
              newResponse.feedbackQuestionId, newResponse.giverEmail, newResponse.recipientEmail);
    } else {
      oldResponse = frDb.getFeedbackResponse(newResponse.getId());
    }

    if (oldResponse == null) {
      throw new EntityDoesNotExistException(
          "Trying to update a feedback response that does not exist.");
    }

    // Copy values that cannot be changed to defensively avoid invalid
    // parameters.
    newResponse.courseId = oldResponse.courseId;
    newResponse.feedbackSessionName = oldResponse.feedbackSessionName;
    newResponse.feedbackQuestionId = oldResponse.feedbackQuestionId;
    newResponse.feedbackQuestionType = oldResponse.feedbackQuestionType;

    if (newResponse.responseMetaData == null) {
      newResponse.responseMetaData = oldResponse.responseMetaData;
    }
    if (newResponse.giverEmail == null) {
      newResponse.giverEmail = oldResponse.giverEmail;
    }
    if (newResponse.recipientEmail == null) {
      newResponse.recipientEmail = oldResponse.recipientEmail;
    }
    if (newResponse.giverSection == null) {
      newResponse.giverSection = oldResponse.giverSection;
    }
    if (newResponse.recipientSection == null) {
      newResponse.recipientSection = oldResponse.recipientSection;
    }

    if (!newResponse.recipientEmail.equals(oldResponse.recipientEmail)
        || !newResponse.giverEmail.equals(oldResponse.giverEmail)) {
      // Recreate response to prevent possible future id conflict.
      try {
        newResponse.setId(null);
        frDb.createEntity(newResponse);
        frDb.deleteEntity(oldResponse);
      } catch (EntityAlreadyExistsException e) {
        log.warning("Trying to update an existing response to one that already exists.");
        throw new EntityAlreadyExistsException(
            e.getMessage()
                + Const.EOL
                + "Trying to update recipient for response to one that already exists for this giver.");
      }
    } else {
      frDb.updateFeedbackResponse(newResponse);
    }
  }
  public void updateFeedbackResponsesForChangingSection(
      String courseId, String userEmail, String oldSection, String newSection)
      throws EntityDoesNotExistException, InvalidParametersException {

    List<FeedbackResponseAttributes> responsesFromUser =
        getFeedbackResponsesFromGiverForCourse(courseId, userEmail);

    for (FeedbackResponseAttributes response : responsesFromUser) {
      response.giverSection = newSection;
      frDb.updateFeedbackResponse(response);
      frcLogic.updateFeedbackResponseCommentsForResponse(response.getId());
    }

    List<FeedbackResponseAttributes> responsesToUser =
        getFeedbackResponsesForReceiverForCourse(courseId, userEmail);

    for (FeedbackResponseAttributes response : responsesToUser) {
      response.recipientSection = newSection;
      frDb.updateFeedbackResponse(response);
      frcLogic.updateFeedbackResponseCommentsForResponse(response.getId());
    }
  }
  public void createFeedbackResponse(FeedbackResponseAttributes fra)
      throws InvalidParametersException {
    try {
      frDb.createEntity(fra);
    } catch (Exception EntityAlreadyExistsException) {
      try {
        FeedbackResponseAttributes existingFeedback = new FeedbackResponseAttributes();

        existingFeedback =
            frDb.getFeedbackResponse(fra.feedbackQuestionId, fra.giverEmail, fra.recipientEmail);
        fra.setId(existingFeedback.getId());

        frDb.updateFeedbackResponse(fra);
      } catch (Exception EntityDoesNotExistException) {
        Assumption.fail();
      }
    }
  }