/**
   * Converts the soap model instance into a normal model instance.
   *
   * @param soapModel the soap model instance to convert
   * @return the normal model instance
   */
  public static MBMessage toModel(MBMessageSoap soapModel) {
    if (soapModel == null) {
      return null;
    }

    MBMessage model = new MBMessageImpl();

    model.setUuid(soapModel.getUuid());
    model.setMessageId(soapModel.getMessageId());
    model.setGroupId(soapModel.getGroupId());
    model.setCompanyId(soapModel.getCompanyId());
    model.setUserId(soapModel.getUserId());
    model.setUserName(soapModel.getUserName());
    model.setCreateDate(soapModel.getCreateDate());
    model.setModifiedDate(soapModel.getModifiedDate());
    model.setClassNameId(soapModel.getClassNameId());
    model.setClassPK(soapModel.getClassPK());
    model.setCategoryId(soapModel.getCategoryId());
    model.setThreadId(soapModel.getThreadId());
    model.setRootMessageId(soapModel.getRootMessageId());
    model.setParentMessageId(soapModel.getParentMessageId());
    model.setSubject(soapModel.getSubject());
    model.setBody(soapModel.getBody());
    model.setFormat(soapModel.getFormat());
    model.setAnonymous(soapModel.getAnonymous());
    model.setPriority(soapModel.getPriority());
    model.setAllowPingbacks(soapModel.getAllowPingbacks());
    model.setAnswer(soapModel.getAnswer());
    model.setStatus(soapModel.getStatus());
    model.setStatusByUserId(soapModel.getStatusByUserId());
    model.setStatusByUserName(soapModel.getStatusByUserName());
    model.setStatusDate(soapModel.getStatusDate());

    return model;
  }
  protected MBMessage addMBMessage() throws Exception {
    long pk = RandomTestUtil.nextLong();

    MBMessage mbMessage = _persistence.create(pk);

    mbMessage.setUuid(RandomTestUtil.randomString());

    mbMessage.setGroupId(RandomTestUtil.nextLong());

    mbMessage.setCompanyId(RandomTestUtil.nextLong());

    mbMessage.setUserId(RandomTestUtil.nextLong());

    mbMessage.setUserName(RandomTestUtil.randomString());

    mbMessage.setCreateDate(RandomTestUtil.nextDate());

    mbMessage.setModifiedDate(RandomTestUtil.nextDate());

    mbMessage.setClassNameId(RandomTestUtil.nextLong());

    mbMessage.setClassPK(RandomTestUtil.nextLong());

    mbMessage.setCategoryId(RandomTestUtil.nextLong());

    mbMessage.setThreadId(RandomTestUtil.nextLong());

    mbMessage.setRootMessageId(RandomTestUtil.nextLong());

    mbMessage.setParentMessageId(RandomTestUtil.nextLong());

    mbMessage.setSubject(RandomTestUtil.randomString());

    mbMessage.setBody(RandomTestUtil.randomString());

    mbMessage.setFormat(RandomTestUtil.randomString());

    mbMessage.setAnonymous(RandomTestUtil.randomBoolean());

    mbMessage.setPriority(RandomTestUtil.nextDouble());

    mbMessage.setAllowPingbacks(RandomTestUtil.randomBoolean());

    mbMessage.setAnswer(RandomTestUtil.randomBoolean());

    mbMessage.setStatus(RandomTestUtil.nextInt());

    mbMessage.setStatusByUserId(RandomTestUtil.nextLong());

    mbMessage.setStatusByUserName(RandomTestUtil.randomString());

    mbMessage.setStatusDate(RandomTestUtil.nextDate());

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

    return mbMessage;
  }
  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);
    }
  }
  @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()));
  }
  @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);
    }
  }
  @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);
    }
  }
  protected void addMBMessage(
      String uuid,
      long messageId,
      long groupId,
      long companyId,
      long userId,
      String userName,
      Date createDate,
      Date modifiedDate,
      long classNameId,
      long classPK,
      long categoryId,
      long threadId,
      long rootMessageId,
      long parentMessageId,
      String subject,
      String body,
      String format,
      boolean anonymous,
      double priority,
      boolean allowPingbacks,
      boolean answer,
      int status,
      long statusByUserId,
      String statusByUserName,
      Date statusDate,
      Map<Long, Long> mbMessageIds)
      throws PortalException, SystemException {

    if (parentMessageId == MBMessageConstants.DEFAULT_PARENT_MESSAGE_ID) {
      rootMessageId = messageId;
    } else {
      rootMessageId = importMBMessage(rootMessageId, threadId, classPK, mbMessageIds);

      parentMessageId = importMBMessage(parentMessageId, threadId, classPK, mbMessageIds);
    }

    MBMessage mbMessage = mbMessagePersistence.create(messageId);

    mbMessage.setUuid(uuid);
    mbMessage.setGroupId(groupId);
    mbMessage.setCompanyId(companyId);
    mbMessage.setUserId(userId);
    mbMessage.setUserName(userName);
    mbMessage.setCreateDate(createDate);
    mbMessage.setModifiedDate(modifiedDate);
    mbMessage.setClassNameId(classNameId);
    mbMessage.setClassPK(classPK);
    mbMessage.setCategoryId(categoryId);
    mbMessage.setThreadId(threadId);
    mbMessage.setRootMessageId(rootMessageId);
    mbMessage.setParentMessageId(parentMessageId);
    mbMessage.setSubject(subject);
    mbMessage.setBody(body);
    mbMessage.setFormat(format);
    mbMessage.setAnonymous(anonymous);
    mbMessage.setPriority(priority);
    mbMessage.setAllowPingbacks(allowPingbacks);
    mbMessage.setAnswer(answer);
    mbMessage.setStatus(status);
    mbMessage.setStatusByUserId(statusByUserId);
    mbMessage.setStatusByUserName(statusByUserName);
    mbMessage.setStatusDate(statusDate);

    mbMessagePersistence.update(mbMessage);
  }