/**
   * Ensures that the user can delete the campaign. At least one of the following must be true to
   * delete a campaign:
   *
   * <ul>
   *   <li>The user is a supervisor.
   *   <li>The user is an author and there are no responses.
   * </ul>
   *
   * <br>
   *
   * @param username The user's username.
   * @param campaignId The campaign's unique identifier.
   * @throws ServiceException Thrown if the user doesn't have sufficient permissions to delete the
   *     campaign or if there is an error.
   */
  public void userCanDeleteCampaign(final String username, final String campaignId)
      throws ServiceException {

    try {
      List<Campaign.Role> roles = userCampaignQueries.getUserCampaignRoles(username, campaignId);

      if (roles.contains(Campaign.Role.SUPERVISOR)) {
        return;
      }

      if (roles.contains(Campaign.Role.AUTHOR)) {
        long numberOfResponses =
            campaignSurveyResponseQueries.getNumberOfSurveyResponsesForCampaign(campaignId);

        if (numberOfResponses == 0) {
          return;
        } else {
          throw new ServiceException(
              ErrorCode.CAMPAIGN_INSUFFICIENT_PERMISSIONS,
              "The campaign has responses; therefore, you can no longer delete it.");
        }
      }

      throw new ServiceException(
          ErrorCode.CAMPAIGN_INSUFFICIENT_PERMISSIONS,
          "You do not have sufficient permissoins to delete this campaign.");
    } catch (DataAccessException e) {
      throw new ServiceException(e);
    }
  }
  /**
   * Verifies that a user is allowed to update a campaign's XML.
   *
   * @param username The username of the user.
   * @param campaignId The campaign's unique identifier.
   * @param id The ID from the new XML.
   * @param name The name from the new XML.
   * @throws ServiceException Thrown if the user isn't allowed to modify the campaign, if the user
   *     is allowed to modify the campaign but responses exist, or if there is an error.
   */
  public void verifyUserCanUpdateCampaignXml(
      final String username, final String campaignId, final String id, final String name)
      throws ServiceException {

    try {
      // Get the user's roles for this campaign.
      List<Campaign.Role> roles = userCampaignQueries.getUserCampaignRoles(username, campaignId);

      // If the user isn't a supervisor or an author, then they aren't
      // allowed to update it.
      if (!(roles.contains(Campaign.Role.SUPERVISOR) || roles.contains(Campaign.Role.AUTHOR))) {

        throw new ServiceException(
            ErrorCode.CAMPAIGN_INSUFFICIENT_PERMISSIONS,
            "The user is not allowed to modify the campaign's XML.");
      }

      // If the campaign already has survey responses, then it cannot be
      // updated by anyone.
      if (campaignSurveyResponseQueries.getNumberOfSurveyResponsesForCampaign(campaignId) != 0) {

        throw new ServiceException(
            ErrorCode.CAMPAIGN_INSUFFICIENT_PERMISSIONS,
            "Survey responses exist; therefore the XML can no longer be modified.");
      }

      // Check to ensure that the ID of the campaign hasn't changed.
      if (!campaignId.equals(id)) {
        throw new ServiceException(
            ErrorCode.CAMPAIGN_XML_HEADER_CHANGED,
            "The campaign's ID in the new XML must be the same as the original XML.");
      }

      // Check to ensure that the name of the campaign hasn't changed.
      if (!campaignQueries.getName(campaignId).equals(name)) {
        throw new ServiceException(
            ErrorCode.CAMPAIGN_XML_HEADER_CHANGED,
            "The campaign's name in the new XML must be the same as the original XML.");
      }
    } catch (DataAccessException e) {
      throw new ServiceException(e);
    }
  }