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); }
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); } }
protected void addComment(ClassedModel classedModel, String body, ServiceContext serviceContext) throws Exception { User user = TestPropsValues.getUser(); List<MBMessage> messages = MBMessageLocalServiceUtil.getMessages( getBaseModelClassName(), getBaseModelClassPK(classedModel), WorkflowConstants.STATUS_ANY); MBMessage message = messages.get(0); MBMessageLocalServiceUtil.addDiscussionMessage( user.getUserId(), user.getFullName(), serviceContext.getScopeGroupId(), getBaseModelClassName(), getBaseModelClassPK(classedModel), message.getThreadId(), message.getMessageId(), message.getSubject(), body, serviceContext); }
@Override protected String getTitle( JSONObject jsonObject, AssetRenderer<?> assetRenderer, ServiceContext serviceContext) { MBMessage mbMessage = MBMessageLocalServiceUtil.fetchMBMessage(jsonObject.getLong("classPK")); AssetRendererFactory<?> assetRendererFactory = AssetRendererFactoryRegistryUtil.getAssetRendererFactoryByClassName( assetRenderer.getClassName()); String typeName = assetRendererFactory.getTypeName(serviceContext.getLocale()); ResourceBundle resourceBundle = ResourceBundle.getBundle("content.Language", serviceContext.getLocale()); if ((mbMessage != null) && mbMessage.isDiscussion()) { return LanguageUtil.format( resourceBundle, "x-mentioned-you-in-a-comment-in-a-x", new String[] { HtmlUtil.escape(assetRenderer.getUserName()), StringUtil.toLowerCase(HtmlUtil.escape(typeName)) }, false); } else { return LanguageUtil.format( resourceBundle, "x-mentioned-you-in-a-x", new String[] { HtmlUtil.escape(assetRenderer.getUserName()), StringUtil.toLowerCase(HtmlUtil.escape(typeName)) }, false); } }
@Override protected void addAttachment(ClassedModel classedModel) throws Exception { MBMessage message = (MBMessage) classedModel; List<FileEntry> fileEntries = message.getAttachmentsFileEntries(); List<String> existingFiles = new ArrayList<>(); for (FileEntry fileEntry : fileEntries) { existingFiles.add(fileEntry.getTitle()); } ServiceContext serviceContext = ServiceContextTestUtil.getServiceContext(message.getGroupId()); List<ObjectValuePair<String, InputStream>> inputStreamOVPs = MBTestUtil.getInputStreamOVPs("OSX_Test.docx", getClass(), getSearchKeywords()); MBMessageLocalServiceUtil.updateMessage( TestPropsValues.getUserId(), message.getMessageId(), getSearchKeywords(), getSearchKeywords(), inputStreamOVPs, existingFiles, 0, false, serviceContext); }
@Override public long addComment( long userId, long groupId, String className, long classPK, String userName, String subject, String body, Function<String, ServiceContext> serviceContextFunction) throws PortalException { MBMessageDisplay mbMessageDisplay = _mbMessageLocalService.getDiscussionMessageDisplay( userId, groupId, className, classPK, WorkflowConstants.STATUS_APPROVED); MBThread mbThread = mbMessageDisplay.getThread(); ServiceContext serviceContext = serviceContextFunction.apply(MBMessage.class.getName()); MBMessage mbMessage = _mbMessageLocalService.addDiscussionMessage( userId, userName, groupId, className, classPK, mbThread.getThreadId(), mbThread.getRootMessageId(), subject, body, serviceContext); return mbMessage.getMessageId(); }
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); } }
@Override protected void doExportStagedModel(PortletDataContext portletDataContext, MBThreadFlag threadFlag) throws Exception { MBThread thread = MBThreadLocalServiceUtil.getThread(threadFlag.getThreadId()); MBMessage rootMessage = MBMessageLocalServiceUtil.getMessage(thread.getRootMessageId()); if ((rootMessage.getStatus() != WorkflowConstants.STATUS_APPROVED) || (rootMessage.getCategoryId() == MBCategoryConstants.DISCUSSION_CATEGORY_ID)) { return; } StagedModelDataHandlerUtil.exportStagedModel(portletDataContext, rootMessage); Element threadFlagElement = portletDataContext.getExportDataElement(threadFlag); threadFlagElement.addAttribute("root-message-id", String.valueOf(rootMessage.getMessageId())); portletDataContext.addClassedModel( threadFlagElement, ExportImportPathUtil.getModelPath(threadFlag), threadFlag, MBPortletDataHandler.NAMESPACE); }
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; }
@Override protected String getBody(SocialActivity activity, ServiceContext serviceContext) throws Exception { MBMessage message = getMessage(activity); if (message.getCategoryId() <= 0) { return StringPool.BLANK; } StringBundler sb = new StringBundler(4); sb.append(serviceContext.getPortalURL()); sb.append(serviceContext.getPathMain()); sb.append("/message_boards/find_category?mbCategoryId="); sb.append(message.getCategoryId()); String categoryLink = sb.toString(); categoryLink = addNoSuchEntryRedirect( categoryLink, MBCategory.class.getName(), message.getCategoryId(), serviceContext); return wrapLink(categoryLink, "go-to-category", serviceContext); }
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); }
@Test public void testFetchByPrimaryKeyExisting() throws Exception { MBMessage newMBMessage = addMBMessage(); MBMessage existingMBMessage = _persistence.fetchByPrimaryKey(newMBMessage.getPrimaryKey()); Assert.assertEquals(existingMBMessage, newMBMessage); }
@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; }
@Override public MBThread moveThreadToTrash(long userId, MBThread thread) throws PortalException { // Thread if (thread.getCategoryId() == MBCategoryConstants.DISCUSSION_CATEGORY_ID) { return thread; } int oldStatus = thread.getStatus(); if (oldStatus == WorkflowConstants.STATUS_PENDING) { thread.setStatus(WorkflowConstants.STATUS_DRAFT); mbThreadPersistence.update(thread); } thread = updateStatus(userId, thread.getThreadId(), WorkflowConstants.STATUS_IN_TRASH); // Trash TrashEntry trashEntry = trashEntryLocalService.addTrashEntry( userId, thread.getGroupId(), MBThread.class.getName(), thread.getThreadId(), thread.getUuid(), null, oldStatus, null, null); // Messages moveDependentsToTrash(thread.getGroupId(), thread.getThreadId(), trashEntry.getEntryId()); // Social MBMessage message = mbMessageLocalService.getMBMessage(thread.getRootMessageId()); JSONObject extraDataJSONObject = JSONFactoryUtil.createJSONObject(); extraDataJSONObject.put("rootMessageId", thread.getRootMessageId()); extraDataJSONObject.put("title", message.getSubject()); SocialActivityManagerUtil.addActivity( userId, thread, SocialActivityConstants.TYPE_MOVE_TO_TRASH, extraDataJSONObject.toString(), 0); return thread; }
@Test public void testCreate() throws Exception { long pk = RandomTestUtil.nextLong(); MBMessage mbMessage = _persistence.create(pk); Assert.assertNotNull(mbMessage); Assert.assertEquals(mbMessage.getPrimaryKey(), pk); }
@Override protected AssetRenderer<?> getAssetRenderer(JSONObject jsonObject) { MBMessage mbMessage = MBMessageLocalServiceUtil.fetchMBMessage(jsonObject.getLong("classPK")); if ((mbMessage != null) && mbMessage.isDiscussion()) { return getAssetRenderer(mbMessage.getClassName(), mbMessage.getClassPK()); } else { return getAssetRenderer(jsonObject.getString("className"), jsonObject.getLong("classPK")); } }
@Test public void testRemove() throws Exception { MBMessage newMBMessage = addMBMessage(); _persistence.remove(newMBMessage); MBMessage existingMBMessage = _persistence.fetchByPrimaryKey(newMBMessage.getPrimaryKey()); Assert.assertNull(existingMBMessage); }
@Override protected void deleteBaseModel(long primaryKey) throws Exception { MBMessage message = MBMessageLocalServiceUtil.getMessage(primaryKey); if (!message.isRoot()) { MBMessageLocalServiceUtil.deleteMessage(primaryKey); } else { MBThreadLocalServiceUtil.deleteThread(message.getThreadId()); } }
public static String getSubjectForEmail(MBMessage message) throws Exception { String subject = message.getSubject(); if (subject.startsWith("RE:")) { return subject; } else { return "RE: " + message.getSubject(); } }
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; }
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); }
@Override protected BaseModel<?> updateBaseModel( BaseModel<?> baseModel, String keywords, ServiceContext serviceContext) throws Exception { MBMessage message = (MBMessage) baseModel; message.setSubject(keywords); message.setBody(keywords); return MBTestUtil.updateMessage(message, keywords, keywords, true); }
@Override public void addRelatedEntryFields(Document document, Object obj) throws Exception { MBMessage message = (MBMessage) obj; DLFileEntry dlFileEntry = DLFileEntryLocalServiceUtil.getDLFileEntry(message.getClassPK()); document.addKeyword(Field.FOLDER_ID, dlFileEntry.getFolderId()); document.addKeyword(Field.HIDDEN, dlFileEntry.isInHiddenFolder()); document.addKeyword(Field.RELATED_ENTRY, true); }
@Override public void restoreMessageAttachmentFromTrash(long messageId, String fileName) throws PortalException, SystemException { MBMessage message = mbMessagePersistence.findByPrimaryKey(messageId); MBCategoryPermission.check( getPermissionChecker(), message.getGroupId(), message.getCategoryId(), ActionKeys.ADD_FILE); mbMessageLocalService.restoreMessageAttachmentFromTrash(getUserId(), messageId, fileName); }
@Override protected boolean hasPermissions( PermissionChecker permissionChecker, SocialActivity activity, String actionId, ServiceContext serviceContext) throws Exception { MBMessage message = getMessage(activity); return MBMessagePermission.contains(permissionChecker, message.getMessageId(), actionId); }
@Override public String getGroupMessagesRSS( long groupId, long userId, int status, int max, String type, double version, String displayStyle, String feedURL, String entryURL, ThemeDisplay themeDisplay) throws PortalException, SystemException { String name = StringPool.BLANK; String description = StringPool.BLANK; List<MBMessage> messages = new ArrayList<MBMessage>(); int lastIntervalStart = 0; boolean listNotExhausted = true; MessageCreateDateComparator comparator = new MessageCreateDateComparator(false); while ((messages.size() < max) && listNotExhausted) { List<MBMessage> messageList = mbMessageLocalService.getGroupMessages( groupId, userId, status, lastIntervalStart, lastIntervalStart + max, comparator); lastIntervalStart += max; listNotExhausted = (messageList.size() == max); for (MBMessage message : messageList) { if (messages.size() >= max) { break; } if (MBMessagePermission.contains(getPermissionChecker(), message, ActionKeys.VIEW)) { messages.add(message); } } } if (!messages.isEmpty()) { MBMessage message = messages.get(messages.size() - 1); name = message.getSubject(); description = message.getSubject(); } return exportToRSS( name, description, type, version, displayStyle, feedURL, entryURL, messages, themeDisplay); }
@Override protected void doExportStagedModel(PortletDataContext portletDataContext, MBMessage message) throws Exception { if ((message.getStatus() != WorkflowConstants.STATUS_APPROVED) || (message.getCategoryId() == MBCategoryConstants.DISCUSSION_CATEGORY_ID)) { return; } StagedModelDataHandlerUtil.exportStagedModel(portletDataContext, message.getCategory()); Element messageElement = portletDataContext.getExportDataStagedModelElement(message); message.setPriority(message.getPriority()); MBThread thread = message.getThread(); messageElement.addAttribute("question", String.valueOf(thread.isQuestion())); boolean hasAttachmentsFileEntries = message.getAttachmentsFileEntriesCount() > 0; messageElement.addAttribute( "hasAttachmentsFileEntries", String.valueOf(hasAttachmentsFileEntries)); if (portletDataContext.getBooleanParameter(MBPortletDataHandler.NAMESPACE, "attachments") && hasAttachmentsFileEntries) { for (FileEntry fileEntry : message.getAttachmentsFileEntries()) { String name = fileEntry.getTitle(); String binPath = ExportImportPathUtil.getModelPath(message, name); Element attachmentElement = messageElement.addElement("attachment"); attachmentElement.addAttribute("name", name); attachmentElement.addAttribute("bin-path", binPath); portletDataContext.addZipEntry(binPath, fileEntry.getContentStream()); } long folderId = message.getAttachmentsFolderId(); if (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) { message.setAttachmentsFolderId(folderId); } } portletDataContext.addClassedModel( messageElement, ExportImportPathUtil.getModelPath(message), message, MBPortletDataHandler.NAMESPACE); }
@Override protected void updateBaseModel(long userId, long baseModelId) throws Exception { MBMessage message = MBMessageLocalServiceUtil.getMessage(baseModelId); ServiceContext serviceContext = ServiceContextTestUtil.getServiceContext(message.getGroupId(), userId); MBTestUtil.populateNotificationsServiceContext(serviceContext, Constants.UPDATE); MBMessageLocalServiceUtil.updateMessage( userId, message.getMessageId(), RandomTestUtil.randomString(), serviceContext); }
@Test public void testFetchByPrimaryKeysWithOnePrimaryKey() throws Exception { MBMessage newMBMessage = addMBMessage(); Set<Serializable> primaryKeys = new HashSet<Serializable>(); primaryKeys.add(newMBMessage.getPrimaryKey()); Map<Serializable, MBMessage> mbMessages = _persistence.fetchByPrimaryKeys(primaryKeys); Assert.assertEquals(1, mbMessages.size()); Assert.assertEquals(newMBMessage, mbMessages.get(newMBMessage.getPrimaryKey())); }
public static String getSubjectForEmail(Message message) throws Exception { long parentMessageId = getParentMessageId(message); MBMessage parentMessage = MBMessageLocalServiceUtil.getMBMessage(parentMessageId); String subject = parentMessage.getSubject(); if (subject.startsWith("RE:")) { return subject; } else { return "RE: " + parentMessage.getSubject(); } }