/**
   * 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;
  }
  @Override
  public MBThread moveThread(long groupId, long categoryId, long threadId)
      throws PortalException, SystemException {

    MBThread thread = mbThreadPersistence.findByPrimaryKey(threadId);

    long oldCategoryId = thread.getCategoryId();

    MBCategory oldCategory = null;

    if (oldCategoryId != MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {
      oldCategory = mbCategoryPersistence.fetchByPrimaryKey(oldCategoryId);
    }

    MBCategory category = null;

    if (categoryId != MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {
      category = mbCategoryPersistence.fetchByPrimaryKey(categoryId);
    }

    // Thread

    thread.setModifiedDate(new Date());
    thread.setCategoryId(categoryId);

    mbThreadPersistence.update(thread);

    // Messages

    List<MBMessage> messages =
        mbMessagePersistence.findByG_C_T(groupId, oldCategoryId, thread.getThreadId());

    for (MBMessage message : messages) {
      message.setCategoryId(categoryId);

      mbMessagePersistence.update(message);

      // Indexer

      if (!message.isDiscussion()) {
        Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(MBMessage.class);

        indexer.reindex(message);
      }
    }

    // Category

    if ((oldCategory != null) && (categoryId != oldCategoryId)) {
      MBUtil.updateCategoryStatistics(oldCategory.getCompanyId(), oldCategory.getCategoryId());
    }

    if ((category != null) && (categoryId != oldCategoryId)) {
      MBUtil.updateCategoryStatistics(category.getCompanyId(), category.getCategoryId());
    }

    return thread;
  }
  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 mergeCategories(MBCategory fromCategory, long toCategoryId)
      throws PortalException, SystemException {

    if ((toCategoryId == MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID)
        || (toCategoryId == MBCategoryConstants.DISCUSSION_CATEGORY_ID)) {

      return;
    }

    List<MBCategory> categories =
        mbCategoryPersistence.findByG_P(fromCategory.getGroupId(), fromCategory.getCategoryId());

    for (MBCategory category : categories) {
      mergeCategories(category, toCategoryId);
    }

    List<MBThread> threads =
        mbThreadPersistence.findByG_C(fromCategory.getGroupId(), fromCategory.getCategoryId());

    for (MBThread thread : threads) {

      // Thread

      thread.setCategoryId(toCategoryId);

      mbThreadPersistence.update(thread);

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

      for (MBMessage message : messages) {

        // Message

        message.setCategoryId(toCategoryId);

        mbMessagePersistence.update(message);

        // Indexer

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

        indexer.reindex(message);
      }
    }

    MBCategory toCategory = mbCategoryPersistence.findByPrimaryKey(toCategoryId);

    toCategory.setThreadCount(fromCategory.getThreadCount() + toCategory.getThreadCount());
    toCategory.setMessageCount(fromCategory.getMessageCount() + toCategory.getMessageCount());

    mbCategoryPersistence.update(toCategory);

    deleteCategory(fromCategory);
  }
  protected void moveChildrenMessages(
      MBMessage parentMessage, MBCategory category, long oldThreadId) throws PortalException {

    List<MBMessage> messages =
        mbMessagePersistence.findByT_P(oldThreadId, parentMessage.getMessageId());

    for (MBMessage message : messages) {
      message.setCategoryId(parentMessage.getCategoryId());
      message.setThreadId(parentMessage.getThreadId());
      message.setRootMessageId(parentMessage.getRootMessageId());

      mbMessagePersistence.update(message);

      if (!message.isDiscussion()) {
        Indexer<MBMessage> indexer = IndexerRegistryUtil.nullSafeGetIndexer(MBMessage.class);

        indexer.reindex(message);
      }

      moveChildrenMessages(message, category, oldThreadId);
    }
  }
  protected void mergeCategories(MBCategory fromCategory, long toCategoryId)
      throws PortalException, SystemException {

    Iterator itr =
        mbCategoryPersistence
            .findByG_P(fromCategory.getGroupId(), fromCategory.getCategoryId())
            .iterator();

    while (itr.hasNext()) {
      MBCategory category = (MBCategory) itr.next();

      mergeCategories(category, toCategoryId);
    }

    Iterator itr1 = mbThreadPersistence.findByCategoryId(fromCategory.getCategoryId()).iterator();

    while (itr1.hasNext()) {

      // Thread

      MBThread thread = (MBThread) itr1.next();

      thread.setCategoryId(toCategoryId);

      mbThreadPersistence.update(thread);

      Iterator itr2 = mbMessagePersistence.findByThreadId(thread.getThreadId()).iterator();

      while (itr2.hasNext()) {

        // Message

        MBMessage message = (MBMessage) itr2.next();

        message.setCategoryId(toCategoryId);

        mbMessagePersistence.update(message);

        // Lucene

        try {
          if (!fromCategory.isDiscussion()) {
            String[] tagsEntries =
                tagsEntryLocalService.getEntryNames(
                    MBMessage.class.getName(), message.getMessageId());

            Indexer.updateMessage(
                message.getCompanyId(),
                fromCategory.getGroupId(),
                message.getUserName(),
                toCategoryId,
                message.getThreadId(),
                message.getMessageId(),
                message.getSubject(),
                message.getBody(),
                tagsEntries);
          }
        } catch (IOException ioe) {
          _log.error("Indexing " + message.getMessageId(), ioe);
        }
      }
    }

    mbCategoryPersistence.remove(fromCategory.getCategoryId());
  }
  @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()));
  }
  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);
  }