예제 #1
0
  /**
   * Generates a List of unique identifiers for campaigns based on the parameters. The List is based
   * on the 'campaignIds' parameter unless it is null in which case the List is based on all
   * campaigns visible to the user. All parameters except 'request' and 'username' are optional and
   * each will filter the resulting List of campaign identifiers.<br>
   * <br>
   * <br>
   * For example, if 'campaignIds' was null as were 'endDate' and 'privacyState', then what would be
   * returned would be the intersection of the following lists:<br>
   * - All of the campaigns to which the user was associated (because 'campaignIds' was null).<br>
   * - All of the campaigns that are associated with any of the classes whose unique identifier was
   * in the 'classIds' list.<br>
   * - All of the campaigns whose creation timestamp was equal to or after 'startDate'<br>
   * - All of the campaigns whose running state equaled 'runningState'.<br>
   * - All of the campaigns to which the user had the campaign role 'role'. <br>
   * <br>
   * Therefore, if a campaign was associated with a user only through a single class, but that class
   * wasn't in the 'classIds' list, then that campaign ID would not be returned even if all of the
   * other parameters matched.
   *
   * @param username The username of the user.
   * @param campaignIds An optional Collection of campaign identifiers from which to base the List.
   *     If this is empty, the resulting List will be empty. If this is null, the base List will be
   *     all campaigns to which the user is associated.
   * @param classIds A Collection of unique identifiers for classes where the resulting list will
   *     only contain campaign identifiers for campaigns that are associated with any of these
   *     classes.
   * @param startDate A Calendar where only campaigns whose creation timestamp is equal to or after
   *     this date.
   * @param endDate A Calendar where only campaigns whose creation timestamp is equal to or before
   *     this date.
   * @param privacyState A campaign privacy state that trims the resulting list of campaigns to only
   *     those that have this privacy state.
   * @param runningState A campaign running state that trims the resulting list of campaigns to only
   *     those that have this running state.
   * @param role A campaign role that trims the resulting list of campaigns to only those where the
   *     user has that role in the campaign.
   * @return A Set of campaign unique identifiers based on the 'campaignIds' parameter and trimmed
   *     by the rest of the parameters.
   * @throws ServiceException Thrown if there is an error.
   */
  public Set<String> getCampaignsForUser(
      final String username,
      final Collection<String> campaignIds,
      final Collection<String> classIds,
      final DateTime startDate,
      final DateTime endDate,
      final Campaign.PrivacyState privacyState,
      final Campaign.RunningState runningState,
      final Campaign.Role role)
      throws ServiceException {

    try {
      Set<String> desiredCampaignIds = new HashSet<String>();

      if (campaignIds == null) {
        // Initializes the list with all of the campaign IDs for the
        // requesting user.
        desiredCampaignIds.addAll(
            userCampaignQueries.getCampaignIdsAndNameForUser(username).keySet());
      } else {
        // Initializes the list with the campaign IDs in the query.
        desiredCampaignIds.addAll(campaignIds);
      }

      if (desiredCampaignIds.size() == 0) {
        return Collections.emptySet();
      }

      if (classIds != null) {
        // Get all of the campaigns associated with all of the classes in
        // the list.
        for (String classId : classIds) {
          desiredCampaignIds.retainAll(
              campaignClassQueries.getCampaignsAssociatedWithClass(classId));
        }

        if (desiredCampaignIds.size() == 0) {
          return Collections.emptySet();
        }
      }

      if (startDate != null) {
        // Get all of the campaigns whose creation timestamp is greater
        // than or equal to the start date.
        desiredCampaignIds.retainAll(campaignQueries.getCampaignsOnOrAfterDate(startDate));

        if (desiredCampaignIds.size() == 0) {
          return Collections.emptySet();
        }
      }

      if (endDate != null) {
        // Get all of the campaigns whose creation timestamp is less than
        // or equal to the end date.
        desiredCampaignIds.retainAll(campaignQueries.getCampaignsOnOrBeforeDate(endDate));

        if (desiredCampaignIds.size() == 0) {
          return Collections.emptySet();
        }
      }

      if (privacyState != null) {
        // Get all of the campaigns with a privacy state of 'privacyState'.
        desiredCampaignIds.retainAll(campaignQueries.getCampaignsWithPrivacyState(privacyState));

        if (desiredCampaignIds.size() == 0) {
          return Collections.emptySet();
        }
      }

      if (runningState != null) {
        // Get all of the campaigns with a running state of 'runningState'.
        desiredCampaignIds.retainAll(campaignQueries.getCampaignsWithRunningState(runningState));

        if (desiredCampaignIds.size() == 0) {
          return Collections.emptySet();
        }
      }

      if (role != null) {
        // Get all of the campaigns where the user's role is 'role'.
        desiredCampaignIds.retainAll(
            userCampaignQueries.getCampaignIdsForUserWithRole(username, role));

        if (desiredCampaignIds.size() == 0) {
          return Collections.emptySet();
        }
      }

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