/*
  * NOTE FOR DEVELOPERS:
  *
  * Never modify or reference this class directly. All methods that expect a message boards discussion model instance should use the {@link MBDiscussion} interface instead.
  */
 @Override
 public void persist() {
   if (this.isNew()) {
     MBDiscussionLocalServiceUtil.addMBDiscussion(this);
   } else {
     MBDiscussionLocalServiceUtil.updateMBDiscussion(this);
   }
 }
  public void importComments(Class<?> clazz, long classPK, long newClassPK, long groupId)
      throws PortalException, SystemException {

    Map<Long, Long> messagePKs = new HashMap<Long, Long>();
    Map<Long, Long> threadPKs = new HashMap<Long, Long>();

    List<MBMessage> messages = _commentsMap.get(getPrimaryKeyString(clazz, classPK));

    if (messages == null) {
      return;
    }

    MBDiscussion discussion = null;

    try {
      discussion = MBDiscussionLocalServiceUtil.getDiscussion(clazz.getName(), newClassPK);
    } catch (NoSuchDiscussionException nsde) {
    }

    for (MBMessage message : messages) {
      long userId = getUserId(message.getUserUuid());
      long parentMessageId =
          MapUtil.getLong(messagePKs, message.getParentMessageId(), message.getParentMessageId());
      long threadId = MapUtil.getLong(threadPKs, message.getThreadId(), message.getThreadId());

      if ((message.getParentMessageId() == MBMessageConstants.DEFAULT_PARENT_MESSAGE_ID)
          && (discussion != null)) {

        MBThread thread = MBThreadLocalServiceUtil.getThread(discussion.getThreadId());

        long rootMessageId = thread.getRootMessageId();

        messagePKs.put(message.getMessageId(), rootMessageId);
        threadPKs.put(message.getThreadId(), thread.getThreadId());
      } else {
        ServiceContext serviceContext = new ServiceContext();

        serviceContext.setCreateDate(message.getCreateDate());
        serviceContext.setModifiedDate(message.getModifiedDate());
        serviceContext.setScopeGroupId(groupId);

        MBMessage importedMessage = null;

        if (_dataStrategy.equals(PortletDataHandlerKeys.DATA_STRATEGY_MIRROR)
            || _dataStrategy.equals(PortletDataHandlerKeys.DATA_STRATEGY_MIRROR_OVERWRITE)) {

          MBMessage existingMessage = MBMessageUtil.fetchByUUID_G(message.getUuid(), groupId);

          if (existingMessage == null) {
            serviceContext.setUuid(message.getUuid());

            importedMessage =
                MBMessageLocalServiceUtil.addDiscussionMessage(
                    userId,
                    message.getUserName(),
                    groupId,
                    clazz.getName(),
                    newClassPK,
                    threadId,
                    parentMessageId,
                    message.getSubject(),
                    message.getBody(),
                    serviceContext);
          } else {
            serviceContext.setWorkflowAction(WorkflowConstants.ACTION_PUBLISH);

            importedMessage =
                MBMessageLocalServiceUtil.updateDiscussionMessage(
                    userId,
                    existingMessage.getMessageId(),
                    clazz.getName(),
                    newClassPK,
                    message.getSubject(),
                    message.getBody(),
                    serviceContext);
          }
        } else {
          importedMessage =
              MBMessageLocalServiceUtil.addDiscussionMessage(
                  userId,
                  message.getUserName(),
                  groupId,
                  clazz.getName(),
                  newClassPK,
                  threadId,
                  parentMessageId,
                  message.getSubject(),
                  message.getBody(),
                  serviceContext);
        }

        messagePKs.put(message.getMessageId(), importedMessage.getMessageId());
        threadPKs.put(message.getThreadId(), importedMessage.getThreadId());
      }

      importRatingsEntries(
          MBDiscussion.class, message.getPrimaryKey(), messagePKs.get(message.getPrimaryKey()));
    }
  }