/**
   * Retrieves the information about a campaign.
   *
   * @param campaignId The campaign's unqiue identifier.
   * @param withClasses Whether or not to populate the campaign with its list of classes.
   * @param withUsers Whether or not to populate the campaign with its list of users and their
   *     respective roles.
   * @return The campaign object with the specified information.
   * @throws ServiceException Thrown if there is an error.
   */
  public Campaign getCampaignInformation(
      final String campaignId, final boolean withClasses, final boolean withUsers)
      throws ServiceException {

    try {
      Campaign result = campaignQueries.findCampaignConfiguration(campaignId);

      if (withClasses) {
        try {
          result.addClasses(campaignClassQueries.getClassesAssociatedWithCampaign(campaignId));
        } catch (DomainException e) {
          throw new ServiceException("There was a problem adding a class.", e);
        }
      }

      if (withUsers) {
        List<String> campaignUsernames = userCampaignQueries.getUsersInCampaign(campaignId);

        for (String campaignUsername : campaignUsernames) {
          List<Campaign.Role> userRoles =
              userCampaignQueries.getUserCampaignRoles(campaignUsername, campaignId);

          for (Campaign.Role userRole : userRoles) {
            try {
              result.addUser(campaignUsername, userRole);
            } catch (DomainException e) {
              throw new ServiceException("There was a problem adding a user.", e);
            }
          }
        }
      }

      return result;
    } catch (DataAccessException e) {
      throw new ServiceException(e);
    }
  }
  /**
   * Gathers the requested information about a campaign. This will be at least its name, description
   * (possibly null), running state, privacy state, creation timestamp, and all of the requesting
   * user's roles in the campaign.<br>
   * <br>
   * The extras include the campaign's XML, all of the users associated with the campaign and their
   * roles, and all of the classes associated with the campaign.
   *
   * @param username The username of the user whose roles in the campaign are desired.
   * @param campaignIds The IDs for the campaigns whose information is desired.
   * @param withExtras A flag to indicate if the extra information should be included in each
   *     Campaign object.
   * @return A map of campaigns and their information to the list of roles for this user in the
   *     campaign.
   * @throws ServiceException Thrown if there is an error.
   */
  public Map<Campaign, List<Campaign.Role>> getCampaignAndUserRolesForCampaigns(
      final String username, final Collection<String> campaignIds, final boolean withExtras)
      throws ServiceException {

    try {
      Map<Campaign, List<Campaign.Role>> result = new HashMap<Campaign, List<Campaign.Role>>();

      for (String campaignId : campaignIds) {
        // Create the Campaign object with the campaign's ID.
        Campaign campaign = campaignQueries.getCampaignInformation(campaignId);

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

        // If we are supposed to get the extra information as well.
        if (withExtras) {

          // Add the classes that are associated with the campaign.
          try {
            campaign.addClasses(campaignClassQueries.getClassesAssociatedWithCampaign(campaignId));
          } catch (DomainException e) {
            throw new ServiceException("There was a problem adding a class.", e);
          }

          // Add the users and their roles to the campaign.
          campaign.addUsers(userCampaignQueries.getUsersAndRolesForCampaign(campaignId));
        }

        // Add the user's roles.
        result.put(campaign, roles);
      }

      return result;
    } catch (DataAccessException e) {
      throw new ServiceException(e);
    }
  }
  /**
   * Gathers the information about the classes that match the criteria based on the user's
   * permissions. If the requesting user is an admin, they will see all campaigns; otherwise, they
   * will only see the campaigns to which they belong.
   *
   * @param username The requesting user's username. This parameter is required.
   * @param campaignIds A list of campaign unique identifiers. This is optional and may be null. It
   *     limits the results to only those campaigns to which the user belongs.
   * @param classIds A list of class unique identifiers. This is optional and may be null. It limits
   *     the results to only those campaigns that are associated with any class in this list.
   * @param nameTokens A collection of token strings which limit the results to only those campaigns
   *     whose name contains at least one of the tokens.
   * @param descriptionTokens A collection of token strings which limit the results to only those
   *     campaigns that have a description and whose description contains at least one of the
   *     tokens.
   * @param startDate A date that limits the results to only those campaigns that were created on or
   *     after this date.
   * @param endDate A date that limits the results to only those campaigns that were created on or
   *     before this date.
   * @param privacyState A campaign privacy state the limits the results to only those campaigns
   *     that have this privacy state.
   * @param runningState A campaign running state that limits the results to only those campaigns
   *     that have this running state.
   * @param role A campaign role which limits the results to only those campaigns where the
   *     requesting user has this role in the campaign.
   * @param withClasses Whether or not to aggregate all of the classes associated with this
   *     campaign.
   * @param withUsers Whether or not to aggregate all of the users and their respective roles for
   *     this campaign.
   * @return A map of Campaign objects to the requesting user's respective roles.
   * @throws ServiceException There was an error.
   */
  public Map<Campaign, Collection<Campaign.Role>> getCampaignInformation(
      final String username,
      final Collection<String> campaignIds,
      final Collection<String> classIds,
      final Collection<String> nameTokens,
      final Collection<String> descriptionTokens,
      final DateTime startDate,
      final DateTime endDate,
      final Campaign.PrivacyState privacyState,
      final Campaign.RunningState runningState,
      final Campaign.Role role,
      final boolean withClasses,
      final boolean withUsers)
      throws ServiceException {

    try {
      QueryResultsList<Campaign> queryResult =
          campaignQueries.getCampaignInformation(
              username,
              campaignIds,
              classIds,
              nameTokens,
              descriptionTokens,
              startDate,
              endDate,
              privacyState,
              runningState,
              role);
      List<Campaign> campaignResults = queryResult.getResults();

      Map<Campaign, Collection<Campaign.Role>> result =
          new HashMap<Campaign, Collection<Campaign.Role>>(campaignResults.size());

      for (Campaign campaign : campaignResults) {
        result.put(campaign, userCampaignQueries.getUserCampaignRoles(username, campaign.getId()));

        if (withClasses) {
          try {
            campaign.addClasses(
                campaignClassQueries.getClassesAssociatedWithCampaign(campaign.getId()));
          } catch (DomainException e) {
            throw new ServiceException("There was a problem adding the classes.", e);
          }
        }

        if (withUsers) {
          // Add the users and their roles to the campaign.
          campaign.addUsers(userCampaignQueries.getUsersAndRolesForCampaign(campaign.getId()));
        }

        // Get and apply the masks for this campaign.
        campaign.addMasks(
            userCampaignQueries.getCampaignMasks(
                null, null, null, null, username, campaign.getId()));
      }

      return result;
    } catch (DataAccessException e) {
      throw new ServiceException(e);
    }
  }