public void addComments(Class<?> clazz, long classPK) throws SystemException { long classNameId = PortalUtil.getClassNameId(clazz); MBDiscussion discussion = MBDiscussionUtil.fetchByC_C(classNameId, classPK); if (discussion == null) { return; } List<MBMessage> messages = MBMessageLocalServiceUtil.getThreadMessages( discussion.getThreadId(), WorkflowConstants.STATUS_APPROVED); if (messages.size() == 0) { return; } for (MBMessage message : messages) { message.setUserUuid(message.getUserUuid()); addRatingsEntries(MBDiscussion.class, message.getPrimaryKey()); } _commentsMap.put(getPrimaryKeyString(clazz, classPK), messages); }
@Override protected void doImportStagedModel(PortletDataContext portletDataContext, MBMessage message) throws Exception { long userId = portletDataContext.getUserId(message.getUserUuid()); String userName = message.getUserName(); Map<Long, Long> categoryIds = (Map<Long, Long>) portletDataContext.getNewPrimaryKeysMap(MBCategory.class); long parentCategoryId = MapUtil.getLong(categoryIds, message.getCategoryId(), message.getCategoryId()); Map<Long, Long> threadIds = (Map<Long, Long>) portletDataContext.getNewPrimaryKeysMap(MBThread.class); long threadId = MapUtil.getLong(threadIds, message.getThreadId(), 0); Map<Long, Long> messageIds = (Map<Long, Long>) portletDataContext.getNewPrimaryKeysMap(MBMessage.class); long parentMessageId = MapUtil.getLong(messageIds, message.getParentMessageId(), message.getParentMessageId()); Element element = portletDataContext.getImportDataStagedModelElement(message); List<ObjectValuePair<String, InputStream>> inputStreamOVPs = getAttachments(portletDataContext, element, message); try { ServiceContext serviceContext = portletDataContext.createServiceContext(message, MBPortletDataHandler.NAMESPACE); if (message.getStatus() != WorkflowConstants.STATUS_APPROVED) { serviceContext.setWorkflowAction(WorkflowConstants.ACTION_SAVE_DRAFT); } if ((parentCategoryId != MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) && (parentCategoryId != MBCategoryConstants.DISCUSSION_CATEGORY_ID) && (parentCategoryId == message.getCategoryId())) { String categoryPath = ExportImportPathUtil.getModelPath( portletDataContext, MBCategory.class.getName(), parentCategoryId); MBCategory category = (MBCategory) portletDataContext.getZipEntryAsObject(categoryPath); StagedModelDataHandlerUtil.importStagedModel(portletDataContext, category); parentCategoryId = MapUtil.getLong(categoryIds, message.getCategoryId(), message.getCategoryId()); } MBMessage importedMessage = null; if (portletDataContext.isDataStrategyMirror()) { MBMessage existingMessage = MBMessageUtil.fetchByUUID_G(message.getUuid(), portletDataContext.getScopeGroupId()); if (existingMessage == null) { serviceContext.setUuid(message.getUuid()); importedMessage = MBMessageLocalServiceUtil.addMessage( userId, userName, portletDataContext.getScopeGroupId(), parentCategoryId, threadId, parentMessageId, message.getSubject(), message.getBody(), message.getFormat(), inputStreamOVPs, message.getAnonymous(), message.getPriority(), message.getAllowPingbacks(), serviceContext); } else { importedMessage = MBMessageLocalServiceUtil.updateMessage( userId, existingMessage.getMessageId(), message.getSubject(), message.getBody(), inputStreamOVPs, new ArrayList<String>(), message.getPriority(), message.getAllowPingbacks(), serviceContext); } } else { importedMessage = MBMessageLocalServiceUtil.addMessage( userId, userName, portletDataContext.getScopeGroupId(), parentCategoryId, threadId, parentMessageId, message.getSubject(), message.getBody(), message.getFormat(), inputStreamOVPs, message.getAnonymous(), message.getPriority(), message.getAllowPingbacks(), serviceContext); } importedMessage.setAnswer(message.getAnswer()); if (importedMessage.isRoot()) { MBThreadLocalServiceUtil.updateQuestion( importedMessage.getThreadId(), GetterUtil.getBoolean(element.attributeValue("question"))); } threadIds.put(message.getThreadId(), importedMessage.getThreadId()); portletDataContext.importClassedModel( message, importedMessage, MBPortletDataHandler.NAMESPACE); } finally { for (ObjectValuePair<String, InputStream> inputStreamOVP : inputStreamOVPs) { InputStream inputStream = inputStreamOVP.getValue(); StreamUtil.cleanUp(inputStream); } } }
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())); } }