/**
   * 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 request The Request that is performing this service.
   * @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 static void userCanDeleteCampaign(Request request, String username, String campaignId)
      throws ServiceException {
    try {
      List<CampaignRoleCache.Role> roles =
          UserCampaignDaos.getUserCampaignRoles(username, campaignId);

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

      if (roles.contains(CampaignRoleCache.Role.AUTHOR)) {
        long numberOfResponses =
            CampaignSurveyResponseDaos.getNumberOfSurveyResponsesForCampaign(campaignId);

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

      request.setFailed(
          ErrorCodes.CAMPAIGN_INSUFFICIENT_PERMISSIONS,
          "You do not have sufficient permissoins to delete this campaign.");
      throw new ServiceException("You do not have sufficient permissoins to delete this campaign.");
    } catch (DataAccessException e) {
      request.setFailed();
      throw new ServiceException(e);
    }
  }
  /**
   * Verifies that a user is allowed to update a campaign's XML.
   *
   * @param request The Request that is performing this service.
   * @param username The username of the user.
   * @param campaignId The campaign's unique identifier.
   * @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 static void verifyUserCanUpdateCampaignXml(
      Request request, String username, String campaignId) throws ServiceException {
    try {
      List<CampaignRoleCache.Role> roles =
          UserCampaignDaos.getUserCampaignRoles(username, campaignId);

      if (roles.contains(CampaignRoleCache.Role.SUPERVISOR)
          || roles.contains(CampaignRoleCache.Role.AUTHOR)) {
        if (CampaignSurveyResponseDaos.getNumberOfSurveyResponsesForCampaign(campaignId) == 0) {
          return;
        }

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

      request.setFailed(
          ErrorCodes.CAMPAIGN_INSUFFICIENT_PERMISSIONS,
          "The user is not allowed to modify the campaign's XML.");
      throw new ServiceException("The user is not allowed to modify the campaign's XML.");
    } catch (DataAccessException e) {
      request.setFailed();
      throw new ServiceException(e);
    }
  }