/** @inheritDoc */
  public void sendSavedSearch(SavedSearchJob searchJob) {

    log.debug("Received thread Name: {}", Thread.currentThread().getName());
    log.debug(
        "Send emails for search ID: {}. {}",
        searchJob.getSavedSearchQueryID(),
        searchJob.getFrequency());

    final Map<String, Object> context = new HashMap<String, Object>();

    context.put("searchParameters", searchJob.getSearchParams());
    context.put("searchHitList", searchJob.getSearchHitList());
    context.put("startTime", searchJob.getStartDate());
    context.put("endTime", searchJob.getEndDate());
    context.put("imagePath", this.imagePath);
    context.put("resultLimit", this.resultLimit);

    // Create message
    Multipart content = createContent(context, searchJob.getType());

    List<Object[]> searchDetails =
        getSavedSearchDetails(searchJob.getSavedSearchQueryID(), searchJob.getFrequency());

    String fromAddress = this.mailFromAddress;

    for (int a = 0; a < searchDetails.size(); a++) {
      String toAddress = (String) searchDetails.get(a)[1];
      String subject;

      if (searchJob.getType().equals(SavedSearchType.USER_DEFINED)) {
        subject = "Search Alert - " + searchDetails.get(a)[2];

        log.debug("Job result count: {}", searchJob.getSearchHitList().size());

        // We might filter the search hitlist based on publish and the last time the search was run
        // for each user
        // here.  We track the last time a search was run in the user's savedSearch table, seemed
        // like overkill to
        // to me though.

        // We might group email addresses and send batches of emails to java mail to send.  It's
        // possible we'll
        // get a performance gain there if needed.  Would take a bit of refactoring to do though...
        // TBD

        if (searchJob.getSearchHitList().size() > 0) {

          log.debug("Sending mail: {}", toAddress);

          mail(toAddress, fromAddress, subject, context, content);
        } else {
          log.debug("Not sending mail: {}", toAddress);
        }
      } else {
        String[] journals = searchJob.getSearchParams().getFilterJournals();

        // Each alert can only be for one journal
        if (journals.length != 1) {
          throw new RuntimeException(
              "Journal alert defined for multiple journals or journal filter not defined");
        }

        Journal j = journalService.getJournal(journals[0]);
        subject = j.getTitle() + " Journal Alert";

        log.debug("Job Result count: {}", searchJob.getSearchHitList().size());
        log.debug("Sending mail: {}", toAddress);

        mail(toAddress, fromAddress, subject, context, content);
      }

      // When results are sent update the records to indicate
      markSearchRun(
          (Long) searchDetails.get(a)[0], searchJob.getFrequency(), searchJob.getEndDate());
    }

    log.debug("Completed thread Name: {}", Thread.currentThread().getName());
    log.debug(
        "Completed send request for search ID: {}. {}",
        searchJob.getSavedSearchQueryID(),
        searchJob.getFrequency());
  }