/**
   * Persist notifications
   *
   * @param pl Susbcriptors for each event
   * @param audits The events
   */
  private void createNotifications(
      List<Profile> pl, List<UGCAudit> audits, Map<String, Profile> actionOwnersCache) {
    if (pl == null || pl.size() == 0) {
      return;
    }

    for (UGCAudit currentAudit : audits) {
      UGC ugc = ugcRepository.findOne(currentAudit.getUgcId());
      if (ugc != null) {
        UGC.ModerationStatus modStatus = ugc.getModerationStatus();
        if (modStatus != UGC.ModerationStatus.SPAM && modStatus != UGC.ModerationStatus.TRASH) {
          for (Profile profile : pl) {
            if (log.isDebugEnabled()) {
              log.debug(
                  "Audit harvester creating notification event ROW "
                      + currentAudit.getRow()
                      + " for the subscriber: "
                      + profile.getUserName());
            }

            createNotification(profile, currentAudit, actionOwnersCache);
          }
        }
      }
    }
  }
  private void createNotification(
      Profile profile, UGCAudit currentAudit, Map<String, Profile> actionOwnersCache) {
    if (currentAudit.getProfileId() != null
        && !currentAudit.getProfileId().equalsIgnoreCase("anonymous")) {
      Notification notification = new Notification();
      // ACTION of the current JOB
      notification.setAction(profile.getSubscriptions().getAction());

      notification.setCreatedDate(new Date());
      notification.setRow(currentAudit.getRow());

      // FORMAT of the current JOB
      notification.setFormat(profile.getSubscriptions().getFormat());

      // FORMAT of the current JOB
      notification.setFrequency(profile.getSubscriptions().getFrequency());
      notification.setSubscriberUsername(profile.getUserName());

      notification.setSubscriberEmail(profile.getEmail());
      notification.setSubscriberId(profile.getId());
      notification.setTransmitedStatus(TransmittedStatus.PENDING);

      notification.setEvent(
          createEvent(currentAudit, getActionAuditOwner(actionOwnersCache, currentAudit)));

      this.notificationRepository.save(notification);
    }
  }
 private Event createEvent(UGCAudit currentAudit, Profile actionOwner) {
   Event event = new Event();
   event.setAction(currentAudit.getAction());
   event.setProfile(actionOwner);
   event.setTarget(currentAudit.getTarget());
   event.setUgcId(currentAudit.getUgcId());
   event.setTenantName(currentAudit.getTenant());
   event.setAuditDate(currentAudit.getCreatedDate());
   return event;
 }
 private Map<String, List<UGCAudit>> flatAuditsByTarget(List<UGCAudit> listUGCAudit) {
   List<UGCAudit> lua;
   Map<String, List<UGCAudit>> targetIdUGCAuditMap = new HashMap<String, List<UGCAudit>>();
   // flatten audits by target
   for (UGCAudit current : listUGCAudit) {
     lua = targetIdUGCAuditMap.get(current.getTarget().getTargetId());
     if (lua == null) {
       lua = new ArrayList<UGCAudit>();
       targetIdUGCAuditMap.put(current.getTarget().getTargetId(), lua);
     }
     lua.add(current);
   }
   return targetIdUGCAuditMap;
 }
  private Profile getActionAuditOwner(
      Map<String, Profile> actionOwnersCache, UGCAudit currentAudit) {
    Profile p = actionOwnersCache.get(currentAudit.getProfileId());
    if (p == null) {
      Profile currentProfile =
          this.profileRepository.findOne(new ObjectId(currentAudit.getProfileId()));
      p = new Profile();
      if (currentProfile != null) {
        p.setUserName(currentProfile.getUserName());
        p.setTenantName(currentProfile.getTenantName());
        p.setEmail(currentProfile.getEmail());
        p.setId(currentProfile.getId());
      }
      actionOwnersCache.put(currentAudit.getProfileId(), p);
    }

    return p;
  }