protected void sendNotificationEvent(MBMessage mbMessage) throws PortalException {

    JSONObject notificationEventJSONObject = JSONFactoryUtil.createJSONObject();

    notificationEventJSONObject.put("classPK", mbMessage.getMessageId());
    notificationEventJSONObject.put("userId", mbMessage.getUserId());

    List<UserThread> userThreads =
        UserThreadLocalServiceUtil.getMBThreadUserThreads(mbMessage.getThreadId());

    for (UserThread userThread : userThreads) {
      if ((userThread.getUserId() == mbMessage.getUserId())
          || ((userThread.getUserId() != mbMessage.getUserId())
              && !UserNotificationManagerUtil.isDeliver(
                  userThread.getUserId(),
                  PortletKeys.PRIVATE_MESSAGING,
                  0,
                  PrivateMessagingConstants.NEW_MESSAGE,
                  UserNotificationDeliveryConstants.TYPE_WEBSITE))) {

        continue;
      }

      UserNotificationEventLocalServiceUtil.sendUserNotificationEvents(
          userThread.getUserId(),
          PortletKeys.PRIVATE_MESSAGING,
          UserNotificationDeliveryConstants.TYPE_WEBSITE,
          notificationEventJSONObject);
    }
  }
  public static List<User> getThreadUsers(long userId, long mbThreadId)
      throws PortalException, SystemException {

    List<User> users = new ArrayList<User>();

    // Users who have contributed to the thread

    List<MBMessage> mbMessages =
        UserThreadServiceUtil.getThreadMessages(
            mbThreadId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, false);

    for (MBMessage mbMessage : mbMessages) {
      if (userId == mbMessage.getUserId()) {
        continue;
      }

      User user = UserLocalServiceUtil.fetchUser(mbMessage.getUserId());

      if (user == null) {
        user = UserLocalServiceUtil.createUser(mbMessage.getUserId());

        user.setFirstName(mbMessage.getUserName());
        user.setStatus(WorkflowConstants.STATUS_INACTIVE);
      }

      if (!users.contains(user)) {
        users.add(user);
      }
    }

    // Users who can view the thread

    List<UserThread> userThreads = UserThreadLocalServiceUtil.getMBThreadUserThreads(mbThreadId);

    for (UserThread userThread : userThreads) {
      if (userId == userThread.getUserId()) {
        continue;
      }

      User user = UserLocalServiceUtil.fetchUser(userThread.getUserId());

      if (user == null) {
        user = UserLocalServiceUtil.createUser(userThread.getUserId());

        user.setFirstName(userThread.getUserName());
        user.setStatus(WorkflowConstants.STATUS_INACTIVE);
      }

      if (!users.contains(user)) {
        users.add(user);
      }
    }

    return users;
  }
  public static boolean isViewableMessage(
      ThemeDisplay themeDisplay, MBMessage message, MBMessage parentMessage) throws Exception {

    PermissionChecker permissionChecker = themeDisplay.getPermissionChecker();

    if (!MBMessagePermission.contains(permissionChecker, parentMessage, ActionKeys.VIEW)) {

      return false;
    }

    if ((message.getMessageId() != parentMessage.getMessageId())
        && !MBMessagePermission.contains(permissionChecker, message, ActionKeys.VIEW)) {

      return false;
    }

    if (!message.isApproved()
        && !Validator.equals(message.getUserId(), themeDisplay.getUserId())
        && !permissionChecker.isGroupAdmin(themeDisplay.getScopeGroupId())) {

      return false;
    }

    return true;
  }
  protected void moveAttachmentsFolders(
      MBMessage message,
      long oldAttachmentsFolderId,
      MBThread oldThread,
      MBThread newThread,
      ServiceContext serviceContext)
      throws PortalException {

    if (oldAttachmentsFolderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {

      Folder newThreadFolder = newThread.addAttachmentsFolder();

      PortletFileRepositoryUtil.movePortletFolder(
          message.getGroupId(),
          message.getUserId(),
          oldAttachmentsFolderId,
          newThreadFolder.getFolderId(),
          serviceContext);
    }

    List<MBMessage> childMessages =
        mbMessagePersistence.findByT_P(oldThread.getThreadId(), message.getMessageId());

    for (MBMessage childMessage : childMessages) {
      moveAttachmentsFolders(
          childMessage,
          childMessage.getAttachmentsFolderId(),
          oldThread,
          newThread,
          serviceContext);
    }
  }
  public MBMessage addPrivateMessageBranch(
      long userId,
      long parentMBMessageId,
      String body,
      List<ObjectValuePair<String, InputStream>> inputStreamOVPs,
      ThemeDisplay themeDisplay)
      throws PortalException {

    long mbThreadId = 0;

    MBMessage parentMessage = MBMessageLocalServiceUtil.getMBMessage(parentMBMessageId);

    List<User> recipients = new ArrayList<>();

    recipients.add(UserLocalServiceUtil.getUser(parentMessage.getUserId()));

    return addPrivateMessage(
        userId,
        mbThreadId,
        parentMBMessageId,
        recipients,
        parentMessage.getSubject(),
        body,
        inputStreamOVPs,
        themeDisplay);
  }
  public void addAnswerFlag(long messageId) throws PortalException, SystemException {

    MBMessage message = mbMessagePersistence.findByPrimaryKey(messageId);

    if (message.isRoot()) {
      return;
    }

    MBThread thread = mbThreadPersistence.findByPrimaryKey(message.getThreadId());

    MBMessage rootMessage = mbMessagePersistence.findByPrimaryKey(thread.getRootMessageId());

    MBMessagePermission.check(
        getPermissionChecker(), rootMessage.getMessageId(), ActionKeys.UPDATE);

    MBMessageFlag questionMessageFlag =
        mbMessageFlagPersistence.fetchByU_M_F(
            rootMessage.getUserId(), rootMessage.getMessageId(), MBMessageFlagImpl.QUESTION_FLAG);

    MBMessageFlag answerMessageFlag =
        mbMessageFlagPersistence.fetchByU_M_F(
            rootMessage.getUserId(), rootMessage.getMessageId(), MBMessageFlagImpl.ANSWER_FLAG);

    if ((questionMessageFlag != null) && (answerMessageFlag == null)) {
      questionMessageFlag.setFlag(MBMessageFlagImpl.ANSWER_FLAG);

      mbMessageFlagPersistence.update(questionMessageFlag, false);
    }

    MBMessageFlag messageFlag =
        mbMessageFlagPersistence.fetchByU_M_F(
            message.getUserId(), message.getMessageId(), MBMessageFlagImpl.ANSWER_FLAG);

    if (messageFlag == null) {
      long messageFlagId = counterLocalService.increment();

      messageFlag = mbMessageFlagPersistence.create(messageFlagId);

      messageFlag.setUserId(message.getUserId());
      messageFlag.setMessageId(message.getMessageId());
      messageFlag.setFlag(MBMessageFlagImpl.ANSWER_FLAG);

      mbMessageFlagPersistence.update(messageFlag, false);
    }
  }
  /**
   * Each thread has a user that represents that thread. This person is either the last user to post
   * on that thread (exluding himself), or if he is the only person to have posted on the thread,
   * then he will the represenative.
   */
  public static long getThreadRepresentativeUserId(long userId, long mbThreadId) {

    List<MBMessage> mbMessages =
        MBMessageLocalServiceUtil.getThreadMessages(
            mbThreadId, WorkflowConstants.STATUS_ANY, new MessageCreateDateComparator(false));

    for (MBMessage mbMessage : mbMessages) {
      if (userId != mbMessage.getUserId()) {
        return mbMessage.getUserId();
      }
    }

    List<UserThread> userThreads = UserThreadLocalServiceUtil.getMBThreadUserThreads(mbThreadId);

    for (UserThread userThread : userThreads) {
      if (userId != userThread.getUserId()) {
        return userThread.getUserId();
      }
    }

    return userId;
  }
  public void deleteAnswerFlag(long messageId) throws PortalException, SystemException {

    MBMessage message = mbMessagePersistence.findByPrimaryKey(messageId);

    if (message.isRoot()) {
      return;
    }

    MBThread thread = mbThreadPersistence.findByPrimaryKey(message.getThreadId());

    MBMessage rootMessage = mbMessagePersistence.findByPrimaryKey(thread.getRootMessageId());

    MBMessagePermission.check(
        getPermissionChecker(), rootMessage.getMessageId(), ActionKeys.UPDATE);

    try {
      mbMessageFlagPersistence.removeByU_M_F(
          message.getUserId(), message.getMessageId(), MBMessageFlagImpl.ANSWER_FLAG);
    } catch (NoSuchMessageFlagException nsmfe) {
    }

    MBMessageFlag answerMessageFlag =
        mbMessageFlagPersistence.fetchByU_M_F(
            rootMessage.getUserId(), rootMessage.getMessageId(), MBMessageFlagImpl.ANSWER_FLAG);

    if (answerMessageFlag == null) {
      return;
    }

    int answerFlagsCount =
        mbMessageFlagPersistence.countByT_F(message.getThreadId(), MBMessageFlagImpl.ANSWER_FLAG);

    if (answerFlagsCount == 0) {
      answerMessageFlag.setFlag(MBMessageFlagImpl.QUESTION_FLAG);

      mbMessageFlagPersistence.update(answerMessageFlag, false);
    }
  }
  protected long importMBMessage(
      MBMessage mbMessage, long threadId, long calendarBookingId, Map<Long, Long> mbMessageIds)
      throws PortalException, SystemException {

    Long messageId = mbMessageIds.get(mbMessage.getMessageId());

    if (messageId != null) {
      return messageId;
    }

    messageId = counterLocalService.increment();

    addMBMessage(
        PortalUUIDUtil.generate(),
        messageId,
        mbMessage.getGroupId(),
        mbMessage.getCompanyId(),
        mbMessage.getUserId(),
        mbMessage.getUserName(),
        mbMessage.getCreateDate(),
        mbMessage.getModifiedDate(),
        classNameLocalService.getClassNameId(CalendarBooking.class.getName()),
        calendarBookingId,
        mbMessage.getCategoryId(),
        threadId,
        mbMessage.getRootMessageId(),
        mbMessage.getParentMessageId(),
        mbMessage.getSubject(),
        mbMessage.getBody(),
        mbMessage.getFormat(),
        mbMessage.isAnonymous(),
        mbMessage.getPriority(),
        mbMessage.getAllowPingbacks(),
        mbMessage.isAnswer(),
        mbMessage.getStatus(),
        mbMessage.getStatusByUserId(),
        mbMessage.getStatusByUserName(),
        mbMessage.getStatusDate(),
        mbMessageIds);

    long mbDiscussionClassNameId =
        classNameLocalService.getClassNameId(MBDiscussion.class.getName());

    importRatings(
        mbDiscussionClassNameId, mbMessage.getMessageId(), mbDiscussionClassNameId, messageId);

    mbMessageIds.put(mbMessage.getMessageId(), messageId);

    return messageId;
  }
  public static JSONObject toJSONObject(MBMessage model) {
    JSONObject jsonObj = new JSONObject();

    JSONUtil.put(jsonObj, "uuid", model.getUuid());
    JSONUtil.put(jsonObj, "messageId", model.getMessageId());
    JSONUtil.put(jsonObj, "companyId", model.getCompanyId());
    JSONUtil.put(jsonObj, "userId", model.getUserId());
    JSONUtil.put(jsonObj, "userName", model.getUserName());
    JSONUtil.put(jsonObj, "createDate", model.getCreateDate());
    JSONUtil.put(jsonObj, "modifiedDate", model.getModifiedDate());
    JSONUtil.put(jsonObj, "categoryId", model.getCategoryId());
    JSONUtil.put(jsonObj, "threadId", model.getThreadId());
    JSONUtil.put(jsonObj, "parentMessageId", model.getParentMessageId());
    JSONUtil.put(jsonObj, "subject", model.getSubject());
    JSONUtil.put(jsonObj, "body", model.getBody());
    JSONUtil.put(jsonObj, "attachments", model.getAttachments());
    JSONUtil.put(jsonObj, "anonymous", model.getAnonymous());

    return jsonObj;
  }
  protected void updateDependentStatus(long groupId, long threadId, int status)
      throws PortalException, SystemException {

    Set<Long> userIds = new HashSet<Long>();

    List<MBMessage> messages =
        mbMessageLocalService.getThreadMessages(threadId, WorkflowConstants.STATUS_ANY);

    for (MBMessage message : messages) {
      if (message.isDiscussion()) {
        continue;
      }

      userIds.add(message.getUserId());

      if (status == WorkflowConstants.STATUS_IN_TRASH) {

        // Asset

        if (message.getStatus() == WorkflowConstants.STATUS_APPROVED) {
          assetEntryLocalService.updateVisible(
              MBMessage.class.getName(), message.getMessageId(), false);
        }

        // Social

        socialActivityCounterLocalService.disableActivityCounters(
            MBMessage.class.getName(), message.getMessageId());

        // Index

        Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(MBMessage.class);

        indexer.reindex(message);

        // Workflow

        if (message.getStatus() == WorkflowConstants.STATUS_PENDING) {
          message.setStatus(WorkflowConstants.STATUS_DRAFT);

          mbMessagePersistence.update(message);

          workflowInstanceLinkLocalService.deleteWorkflowInstanceLink(
              message.getCompanyId(), message.getGroupId(),
              MBMessage.class.getName(), message.getMessageId());
        }
      } else {

        // Asset

        if (message.getStatus() == WorkflowConstants.STATUS_APPROVED) {
          assetEntryLocalService.updateVisible(
              MBMessage.class.getName(), message.getMessageId(), true);
        }

        // Social

        socialActivityCounterLocalService.enableActivityCounters(
            MBMessage.class.getName(), message.getMessageId());

        // Index

        Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(MBMessage.class);

        indexer.reindex(message);
      }
    }

    // Statistics

    for (long userId : userIds) {
      mbStatsUserLocalService.updateStatsUser(groupId, userId);
    }
  }
  @Override
  public MBThread addThread(long categoryId, MBMessage message, ServiceContext serviceContext)
      throws PortalException, SystemException {

    // Thread

    Date now = new Date();

    long threadId = message.getThreadId();

    if (!message.isRoot() || (threadId <= 0)) {
      threadId = counterLocalService.increment();
    }

    MBThread thread = mbThreadPersistence.create(threadId);

    thread.setUuid(serviceContext.getUuid());
    thread.setGroupId(message.getGroupId());
    thread.setCompanyId(message.getCompanyId());
    thread.setUserId(message.getUserId());
    thread.setUserName(message.getUserName());
    thread.setCreateDate(serviceContext.getCreateDate(now));
    thread.setModifiedDate(serviceContext.getModifiedDate(now));
    thread.setCategoryId(categoryId);
    thread.setRootMessageId(message.getMessageId());
    thread.setRootMessageUserId(message.getUserId());

    if (message.isAnonymous()) {
      thread.setLastPostByUserId(0);
    } else {
      thread.setLastPostByUserId(message.getUserId());
    }

    thread.setLastPostDate(message.getCreateDate());

    if (message.getPriority() != MBThreadConstants.PRIORITY_NOT_GIVEN) {
      thread.setPriority(message.getPriority());
    }

    thread.setStatus(message.getStatus());
    thread.setStatusByUserId(message.getStatusByUserId());
    thread.setStatusByUserName(message.getStatusByUserName());
    thread.setStatusDate(message.getStatusDate());

    mbThreadPersistence.update(thread);

    // Asset

    if (categoryId >= 0) {
      assetEntryLocalService.updateEntry(
          message.getUserId(),
          message.getGroupId(),
          thread.getStatusDate(),
          thread.getLastPostDate(),
          MBThread.class.getName(),
          thread.getThreadId(),
          thread.getUuid(),
          0,
          new long[0],
          new String[0],
          false,
          null,
          null,
          null,
          null,
          String.valueOf(thread.getRootMessageId()),
          null,
          null,
          null,
          null,
          0,
          0,
          null,
          false);
    }

    return thread;
  }
  @Override
  @SystemEvent(
      action = SystemEventConstants.ACTION_SKIP,
      send = false,
      type = SystemEventConstants.TYPE_DELETE)
  public void deleteThread(MBThread thread) throws PortalException, SystemException {

    MBMessage rootMessage = mbMessagePersistence.findByPrimaryKey(thread.getRootMessageId());

    // Indexer

    Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(MBMessage.class);

    indexer.delete(thread);

    // Attachments

    long folderId = thread.getAttachmentsFolderId();

    if (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
      PortletFileRepositoryUtil.deleteFolder(folderId);
    }

    // Subscriptions

    subscriptionLocalService.deleteSubscriptions(
        thread.getCompanyId(), MBThread.class.getName(), thread.getThreadId());

    // Thread flags

    mbThreadFlagPersistence.removeByThreadId(thread.getThreadId());

    // Messages

    List<MBMessage> messages = mbMessagePersistence.findByThreadId(thread.getThreadId());

    for (MBMessage message : messages) {

      // Ratings

      ratingsStatsLocalService.deleteStats(message.getWorkflowClassName(), message.getMessageId());

      // Asset

      assetEntryLocalService.deleteEntry(message.getWorkflowClassName(), message.getMessageId());

      // Resources

      if (!message.isDiscussion()) {
        resourceLocalService.deleteResource(
            message.getCompanyId(),
            message.getWorkflowClassName(),
            ResourceConstants.SCOPE_INDIVIDUAL,
            message.getMessageId());
      }

      // Message

      mbMessagePersistence.remove(message);

      // Statistics

      if (!message.isDiscussion()) {
        mbStatsUserLocalService.updateStatsUser(message.getGroupId(), message.getUserId());
      }

      // Workflow

      workflowInstanceLinkLocalService.deleteWorkflowInstanceLink(
          message.getCompanyId(), message.getGroupId(),
          message.getWorkflowClassName(), message.getMessageId());
    }

    // Category

    if ((rootMessage.getCategoryId() != MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID)
        && (rootMessage.getCategoryId() != MBCategoryConstants.DISCUSSION_CATEGORY_ID)) {

      try {
        MBCategory category = mbCategoryPersistence.findByPrimaryKey(thread.getCategoryId());

        MBUtil.updateCategoryStatistics(category.getCompanyId(), category.getCategoryId());
      } catch (NoSuchCategoryException nsce) {
        if (!thread.isInTrash()) {
          throw nsce;
        }
      }
    }

    // Thread Asset

    AssetEntry assetEntry =
        assetEntryLocalService.fetchEntry(MBThread.class.getName(), thread.getThreadId());

    if (assetEntry != null) {
      assetEntry.setTitle(rootMessage.getSubject());

      assetEntryLocalService.updateAssetEntry(assetEntry);
    }

    assetEntryLocalService.deleteEntry(MBThread.class.getName(), thread.getThreadId());

    // Trash

    trashEntryLocalService.deleteEntry(MBThread.class.getName(), thread.getThreadId());

    // Thread

    mbThreadPersistence.remove(thread);
  }
  @Override
  public void moveDependentsToTrash(long groupId, long threadId, long trashEntryId)
      throws PortalException {

    Set<Long> userIds = new HashSet<>();

    MBThread thread = mbThreadLocalService.getThread(threadId);

    List<MBMessage> messages =
        mbMessageLocalService.getThreadMessages(threadId, WorkflowConstants.STATUS_ANY);

    for (MBMessage message : messages) {

      // Message

      if (message.isDiscussion()) {
        continue;
      }

      int oldStatus = message.getStatus();

      message.setStatus(WorkflowConstants.STATUS_IN_TRASH);

      mbMessagePersistence.update(message);

      userIds.add(message.getUserId());

      // Trash

      int status = oldStatus;

      if (oldStatus == WorkflowConstants.STATUS_PENDING) {
        status = WorkflowConstants.STATUS_DRAFT;
      }

      if (oldStatus != WorkflowConstants.STATUS_APPROVED) {
        trashVersionLocalService.addTrashVersion(
            trashEntryId, MBMessage.class.getName(), message.getMessageId(), status, null);
      }

      // Asset

      if (oldStatus == WorkflowConstants.STATUS_APPROVED) {
        assetEntryLocalService.updateVisible(
            MBMessage.class.getName(), message.getMessageId(), false);
      }

      // Attachments

      for (FileEntry fileEntry : message.getAttachmentsFileEntries()) {
        PortletFileRepositoryUtil.movePortletFileEntryToTrash(
            thread.getStatusByUserId(), fileEntry.getFileEntryId());
      }

      // Indexer

      Indexer<MBMessage> indexer = IndexerRegistryUtil.nullSafeGetIndexer(MBMessage.class);

      indexer.reindex(message);

      // Workflow

      if (oldStatus == WorkflowConstants.STATUS_PENDING) {
        workflowInstanceLinkLocalService.deleteWorkflowInstanceLink(
            message.getCompanyId(), message.getGroupId(),
            MBMessage.class.getName(), message.getMessageId());
      }
    }

    // Statistics

    for (long userId : userIds) {
      mbStatsUserLocalService.updateStatsUser(groupId, userId);
    }
  }
  @Override
  public void restoreDependentsFromTrash(long groupId, long threadId) throws PortalException {

    Set<Long> userIds = new HashSet<>();

    MBThread thread = mbThreadLocalService.getThread(threadId);

    List<MBMessage> messages =
        mbMessageLocalService.getThreadMessages(threadId, WorkflowConstants.STATUS_ANY);

    for (MBMessage message : messages) {

      // Message

      if (message.isDiscussion()) {
        continue;
      }

      TrashVersion trashVersion =
          trashVersionLocalService.fetchVersion(MBMessage.class.getName(), message.getMessageId());

      int oldStatus = WorkflowConstants.STATUS_APPROVED;

      if (trashVersion != null) {
        oldStatus = trashVersion.getStatus();
      }

      message.setStatus(oldStatus);

      mbMessagePersistence.update(message);

      userIds.add(message.getUserId());

      // Trash

      if (trashVersion != null) {
        trashVersionLocalService.deleteTrashVersion(trashVersion);
      }

      // Asset

      if (oldStatus == WorkflowConstants.STATUS_APPROVED) {
        assetEntryLocalService.updateVisible(
            MBMessage.class.getName(), message.getMessageId(), true);
      }

      // Attachments

      for (FileEntry fileEntry : message.getAttachmentsFileEntries()) {
        PortletFileRepositoryUtil.restorePortletFileEntryFromTrash(
            thread.getStatusByUserId(), fileEntry.getFileEntryId());
      }

      // Indexer

      Indexer<MBMessage> indexer = IndexerRegistryUtil.nullSafeGetIndexer(MBMessage.class);

      indexer.reindex(message);
    }

    // Statistics

    for (long userId : userIds) {
      mbStatsUserLocalService.updateStatsUser(groupId, userId);
    }
  }
  protected void sendEmail(long mbMessageId, ThemeDisplay themeDisplay) throws Exception {

    MBMessage mbMessage = MBMessageLocalServiceUtil.getMBMessage(mbMessageId);

    User sender = UserLocalServiceUtil.getUser(mbMessage.getUserId());

    Company company = CompanyLocalServiceUtil.getCompany(sender.getCompanyId());

    InternetAddress from = new InternetAddress(company.getEmailAddress());

    String subject =
        StringUtil.read(
            PrivateMessagingPortlet.class.getResourceAsStream(
                "dependencies/notification_message_subject.tmpl"));

    subject =
        StringUtil.replace(
            subject,
            new String[] {"[$COMPANY_NAME$]", "[$FROM_NAME$]"},
            new String[] {company.getName(), sender.getFullName()});

    String body =
        StringUtil.read(
            PrivateMessagingPortlet.class.getResourceAsStream(
                "dependencies/notification_message_body.tmpl"));

    long portraitId = sender.getPortraitId();
    String tokenId = WebServerServletTokenUtil.getToken(sender.getPortraitId());
    String portraitURL =
        themeDisplay.getPortalURL()
            + themeDisplay.getPathImage()
            + "/user_"
            + (sender.isFemale() ? "female" : "male")
            + "_portrait?img_id="
            + portraitId
            + "&t="
            + tokenId;

    body =
        StringUtil.replace(
            body,
            new String[] {
              "[$BODY$]", "[$COMPANY_NAME$]", "[$FROM_AVATAR$]",
              "[$FROM_NAME$]", "[$FROM_PROFILE_URL$]", "[$SUBJECT$]"
            },
            new String[] {
              mbMessage.getBody(),
              company.getName(),
              portraitURL,
              sender.getFullName(),
              sender.getDisplayURL(themeDisplay),
              mbMessage.getSubject()
            });

    List<UserThread> userThreads =
        UserThreadLocalServiceUtil.getMBThreadUserThreads(mbMessage.getThreadId());

    for (UserThread userThread : userThreads) {
      if ((userThread.getUserId() == mbMessage.getUserId())
          && UserNotificationManagerUtil.isDeliver(
              userThread.getUserId(),
              PortletKeys.PRIVATE_MESSAGING,
              PrivateMessagingConstants.NEW_MESSAGE,
              0,
              UserNotificationDeliveryConstants.TYPE_EMAIL)) {

        continue;
      }

      User recipient = UserLocalServiceUtil.getUser(userThread.getUserId());

      String threadURL = getThreadURL(recipient, mbMessage.getThreadId(), themeDisplay);

      if (Validator.isNull(threadURL)) {
        continue;
      }

      InternetAddress to = new InternetAddress(recipient.getEmailAddress());

      Format dateFormatDateTime =
          FastDateFormatFactoryUtil.getDateTime(
              FastDateFormatConstants.LONG,
              FastDateFormatConstants.SHORT,
              recipient.getLocale(),
              recipient.getTimeZone());

      String userThreadBody =
          StringUtil.replace(
              body,
              new String[] {"[$SENT_DATE$]", "[$THREAD_URL$]"},
              new String[] {dateFormatDateTime.format(mbMessage.getCreateDate()), threadURL});

      MailMessage mailMessage = new MailMessage(from, to, subject, userThreadBody, true);

      MailServiceUtil.sendEmail(mailMessage);
    }
  }
  @Test
  public void testUpdateExisting() throws Exception {
    long pk = RandomTestUtil.nextLong();

    MBMessage newMBMessage = _persistence.create(pk);

    newMBMessage.setUuid(RandomTestUtil.randomString());

    newMBMessage.setGroupId(RandomTestUtil.nextLong());

    newMBMessage.setCompanyId(RandomTestUtil.nextLong());

    newMBMessage.setUserId(RandomTestUtil.nextLong());

    newMBMessage.setUserName(RandomTestUtil.randomString());

    newMBMessage.setCreateDate(RandomTestUtil.nextDate());

    newMBMessage.setModifiedDate(RandomTestUtil.nextDate());

    newMBMessage.setClassNameId(RandomTestUtil.nextLong());

    newMBMessage.setClassPK(RandomTestUtil.nextLong());

    newMBMessage.setCategoryId(RandomTestUtil.nextLong());

    newMBMessage.setThreadId(RandomTestUtil.nextLong());

    newMBMessage.setRootMessageId(RandomTestUtil.nextLong());

    newMBMessage.setParentMessageId(RandomTestUtil.nextLong());

    newMBMessage.setSubject(RandomTestUtil.randomString());

    newMBMessage.setBody(RandomTestUtil.randomString());

    newMBMessage.setFormat(RandomTestUtil.randomString());

    newMBMessage.setAnonymous(RandomTestUtil.randomBoolean());

    newMBMessage.setPriority(RandomTestUtil.nextDouble());

    newMBMessage.setAllowPingbacks(RandomTestUtil.randomBoolean());

    newMBMessage.setAnswer(RandomTestUtil.randomBoolean());

    newMBMessage.setStatus(RandomTestUtil.nextInt());

    newMBMessage.setStatusByUserId(RandomTestUtil.nextLong());

    newMBMessage.setStatusByUserName(RandomTestUtil.randomString());

    newMBMessage.setStatusDate(RandomTestUtil.nextDate());

    _mbMessages.add(_persistence.update(newMBMessage));

    MBMessage existingMBMessage = _persistence.findByPrimaryKey(newMBMessage.getPrimaryKey());

    Assert.assertEquals(existingMBMessage.getUuid(), newMBMessage.getUuid());
    Assert.assertEquals(existingMBMessage.getMessageId(), newMBMessage.getMessageId());
    Assert.assertEquals(existingMBMessage.getGroupId(), newMBMessage.getGroupId());
    Assert.assertEquals(existingMBMessage.getCompanyId(), newMBMessage.getCompanyId());
    Assert.assertEquals(existingMBMessage.getUserId(), newMBMessage.getUserId());
    Assert.assertEquals(existingMBMessage.getUserName(), newMBMessage.getUserName());
    Assert.assertEquals(
        Time.getShortTimestamp(existingMBMessage.getCreateDate()),
        Time.getShortTimestamp(newMBMessage.getCreateDate()));
    Assert.assertEquals(
        Time.getShortTimestamp(existingMBMessage.getModifiedDate()),
        Time.getShortTimestamp(newMBMessage.getModifiedDate()));
    Assert.assertEquals(existingMBMessage.getClassNameId(), newMBMessage.getClassNameId());
    Assert.assertEquals(existingMBMessage.getClassPK(), newMBMessage.getClassPK());
    Assert.assertEquals(existingMBMessage.getCategoryId(), newMBMessage.getCategoryId());
    Assert.assertEquals(existingMBMessage.getThreadId(), newMBMessage.getThreadId());
    Assert.assertEquals(existingMBMessage.getRootMessageId(), newMBMessage.getRootMessageId());
    Assert.assertEquals(existingMBMessage.getParentMessageId(), newMBMessage.getParentMessageId());
    Assert.assertEquals(existingMBMessage.getSubject(), newMBMessage.getSubject());
    Assert.assertEquals(existingMBMessage.getBody(), newMBMessage.getBody());
    Assert.assertEquals(existingMBMessage.getFormat(), newMBMessage.getFormat());
    Assert.assertEquals(existingMBMessage.getAnonymous(), newMBMessage.getAnonymous());
    AssertUtils.assertEquals(existingMBMessage.getPriority(), newMBMessage.getPriority());
    Assert.assertEquals(existingMBMessage.getAllowPingbacks(), newMBMessage.getAllowPingbacks());
    Assert.assertEquals(existingMBMessage.getAnswer(), newMBMessage.getAnswer());
    Assert.assertEquals(existingMBMessage.getStatus(), newMBMessage.getStatus());
    Assert.assertEquals(existingMBMessage.getStatusByUserId(), newMBMessage.getStatusByUserId());
    Assert.assertEquals(
        existingMBMessage.getStatusByUserName(), newMBMessage.getStatusByUserName());
    Assert.assertEquals(
        Time.getShortTimestamp(existingMBMessage.getStatusDate()),
        Time.getShortTimestamp(newMBMessage.getStatusDate()));
  }