Ejemplo n.º 1
0
  /**
   * 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);
    }
  }