Example usage for com.liferay.portal.util PropsValues POP_SERVER_NOTIFICATIONS_ENABLED

List of usage examples for com.liferay.portal.util PropsValues POP_SERVER_NOTIFICATIONS_ENABLED

Introduction

In this page you can find the example usage for com.liferay.portal.util PropsValues POP_SERVER_NOTIFICATIONS_ENABLED.

Prototype

boolean POP_SERVER_NOTIFICATIONS_ENABLED

To view the source code for com.liferay.portal.util PropsValues POP_SERVER_NOTIFICATIONS_ENABLED.

Click Source Link

Usage

From source file:com.liferay.message.boards.internal.service.SubscriptionMBMessageLocalServiceWrapper.java

License:Open Source License

protected void notifySubscribers(long userId, MBMessage message, String messageURL,
        ServiceContext serviceContext) throws PortalException {

    if (!message.isApproved() || Validator.isNull(messageURL)) {
        return;// w w  w . j a v  a  2  s.c o m
    }

    if (message.isDiscussion()) {
        try {
            notifyDiscussionSubscribers(userId, message, serviceContext);
        } catch (Exception e) {
            _log.error(e, e);
        }

        return;
    }

    MBGroupServiceSettings mbGroupServiceSettings = MBGroupServiceSettings.getInstance(message.getGroupId());

    if (serviceContext.isCommandAdd() && mbGroupServiceSettings.isEmailMessageAddedEnabled()) {
    } else if (serviceContext.isCommandUpdate() && mbGroupServiceSettings.isEmailMessageUpdatedEnabled()) {
    } else {
        return;
    }

    Company company = _companyLocalService.getCompany(message.getCompanyId());

    User user = _userLocalService.getUser(userId);

    String emailAddress = user.getEmailAddress();
    String fullName = user.getFullName();

    if (message.isAnonymous()) {
        emailAddress = StringPool.BLANK;
        fullName = serviceContext.translate("anonymous");
    }

    MBCategory category = message.getCategory();

    List<Long> categoryIds = new ArrayList<>();

    categoryIds.add(message.getCategoryId());

    if (message.getCategoryId() != MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {

        categoryIds.addAll(category.getAncestorCategoryIds());
    }

    String entryTitle = message.getSubject();

    String fromName = mbGroupServiceSettings.getEmailFromName();
    String fromAddress = mbGroupServiceSettings.getEmailFromAddress();

    String replyToAddress = StringPool.BLANK;

    if (PropsValues.POP_SERVER_NOTIFICATIONS_ENABLED) {
        replyToAddress = MBUtil.getReplyToAddress(message.getCategoryId(), message.getMessageId(),
                company.getMx(), fromAddress);
    }

    LocalizedValuesMap subjectLocalizedValuesMap = null;
    LocalizedValuesMap bodyLocalizedValuesMap = null;

    if (serviceContext.isCommandUpdate()) {
        subjectLocalizedValuesMap = mbGroupServiceSettings.getEmailMessageUpdatedSubject();
        bodyLocalizedValuesMap = mbGroupServiceSettings.getEmailMessageUpdatedBody();
    } else {
        subjectLocalizedValuesMap = mbGroupServiceSettings.getEmailMessageAddedSubject();
        bodyLocalizedValuesMap = mbGroupServiceSettings.getEmailMessageAddedBody();
    }

    boolean htmlFormat = mbGroupServiceSettings.isEmailHtmlFormat();

    String messageBody = message.getBody();

    if (htmlFormat && message.isFormatBBCode()) {
        try {
            messageBody = BBCodeTranslatorUtil.getHTML(messageBody);

            HttpServletRequest request = serviceContext.getRequest();

            if (request != null) {
                ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);

                messageBody = MBUtil.replaceMessageBodyPaths(themeDisplay, messageBody);
            }
        } catch (Exception e) {
            _log.error(StringBundler.concat("Unable to parse message ", String.valueOf(message.getMessageId()),
                    ": ", e.getMessage()));
        }
    }

    String inReplyTo = null;
    String messageSubject = message.getSubject();
    String messageSubjectPrefix = StringPool.BLANK;

    if (message.getParentMessageId() != MBMessageConstants.DEFAULT_PARENT_MESSAGE_ID) {

        MBMessage parentMessage = _mbMessageLocalService.getMessage(message.getParentMessageId());

        Date modifiedDate = parentMessage.getModifiedDate();

        inReplyTo = _portal.getMailId(company.getMx(), MBUtil.MESSAGE_POP_PORTLET_PREFIX,
                message.getCategoryId(), parentMessage.getMessageId(), modifiedDate.getTime());

        if (messageSubject.startsWith(MBMessageConstants.MESSAGE_SUBJECT_PREFIX_RE)) {

            messageSubjectPrefix = MBMessageConstants.MESSAGE_SUBJECT_PREFIX_RE;

            messageSubject = messageSubject.substring(messageSubjectPrefix.length());
        }
    }

    SubscriptionSender subscriptionSender = getSubscriptionSender(userId, category, message, messageURL,
            entryTitle, htmlFormat, messageBody, messageSubject, messageSubjectPrefix, inReplyTo, fromName,
            fromAddress, replyToAddress, emailAddress, fullName, subjectLocalizedValuesMap,
            bodyLocalizedValuesMap, serviceContext);

    subscriptionSender.addPersistedSubscribers(MBCategory.class.getName(), message.getGroupId());

    for (long categoryId : categoryIds) {
        if (categoryId != MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {
            subscriptionSender.addPersistedSubscribers(MBCategory.class.getName(), categoryId);
        }
    }

    subscriptionSender.addPersistedSubscribers(MBThread.class.getName(), message.getThreadId());

    subscriptionSender.flushNotificationsAsync();

    if (!MailingListThreadLocal.isSourceMailingList()) {
        for (long categoryId : categoryIds) {
            MBSubscriptionSender sourceMailingListSubscriptionSender = getSubscriptionSender(userId, category,
                    message, messageURL, entryTitle, htmlFormat, messageBody, messageSubject,
                    messageSubjectPrefix, inReplyTo, fromName, fromAddress, replyToAddress, emailAddress,
                    fullName, subjectLocalizedValuesMap, bodyLocalizedValuesMap, serviceContext);

            sourceMailingListSubscriptionSender.setBulk(false);

            sourceMailingListSubscriptionSender.addMailingListSubscriber(message.getGroupId(), categoryId);

            sourceMailingListSubscriptionSender.flushNotificationsAsync();
        }
    }
}

From source file:com.liferay.portlet.messageboards.service.impl.MBMessageLocalServiceImpl.java

License:Open Source License

protected void notifySubscribers(MBMessage message, ServiceContext serviceContext)
        throws PortalException, SystemException {

    String layoutFullURL = serviceContext.getLayoutFullURL();

    if (!message.isApproved() || Validator.isNull(layoutFullURL)) {
        return;//from  w  w w. j  a  va 2 s .com
    }

    if (message.isDiscussion()) {
        try {
            notifyDiscussionSubscribers(message, serviceContext);
        } catch (Exception e) {
            _log.error(e, e);
        }

        return;
    }

    PortletPreferences preferences = ServiceContextUtil.getPortletPreferences(serviceContext);

    if (preferences == null) {
        long ownerId = message.getGroupId();
        int ownerType = PortletKeys.PREFS_OWNER_TYPE_GROUP;
        long plid = PortletKeys.PREFS_PLID_SHARED;
        String portletId = PortletKeys.MESSAGE_BOARDS;
        String defaultPreferences = null;

        preferences = portletPreferencesLocalService.getPreferences(message.getCompanyId(), ownerId, ownerType,
                plid, portletId, defaultPreferences);
    }

    boolean update = GetterUtil.getBoolean((String) serviceContext.getAttribute("update"));

    if (!update && MBUtil.getEmailMessageAddedEnabled(preferences)) {
    } else if (update && MBUtil.getEmailMessageUpdatedEnabled(preferences)) {
    } else {
        return;
    }

    Company company = companyPersistence.findByPrimaryKey(message.getCompanyId());

    Group group = groupPersistence.findByPrimaryKey(message.getGroupId());

    String emailAddress = PortalUtil.getUserEmailAddress(message.getUserId());
    String fullName = PortalUtil.getUserName(message.getUserId(), message.getUserName());

    if (message.isAnonymous()) {
        emailAddress = StringPool.BLANK;
        fullName = serviceContext.translate("anonymous");
    }

    MBCategory category = message.getCategory();

    String categoryName = category.getName();

    if (category.getCategoryId() == MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {

        categoryName = serviceContext.translate("message-boards-home");

        categoryName += " - " + group.getDescriptiveName();
    }

    List<Long> categoryIds = new ArrayList<Long>();

    categoryIds.add(message.getCategoryId());

    if ((message.getCategoryId() != MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID)
            && (message.getCategoryId() != MBCategoryConstants.DISCUSSION_CATEGORY_ID)) {

        categoryIds.addAll(category.getAncestorCategoryIds());
    }

    String messageURL = layoutFullURL + Portal.FRIENDLY_URL_SEPARATOR + "message_boards/view_message/"
            + message.getMessageId();

    String fromName = MBUtil.getEmailFromName(preferences, message.getCompanyId());
    String fromAddress = MBUtil.getEmailFromAddress(preferences, message.getCompanyId());

    String mailingListAddress = StringPool.BLANK;

    if (PropsValues.POP_SERVER_NOTIFICATIONS_ENABLED) {
        mailingListAddress = MBUtil.getMailingListAddress(message.getGroupId(), message.getCategoryId(),
                message.getMessageId(), company.getMx(), fromAddress);
    }

    String subjectPrefix = null;
    String body = null;
    String signature = null;

    if (update) {
        subjectPrefix = MBUtil.getEmailMessageUpdatedSubjectPrefix(preferences);
        body = MBUtil.getEmailMessageUpdatedBody(preferences);
        signature = MBUtil.getEmailMessageUpdatedSignature(preferences);
    } else {
        subjectPrefix = MBUtil.getEmailMessageAddedSubjectPrefix(preferences);
        body = MBUtil.getEmailMessageAddedBody(preferences);
        signature = MBUtil.getEmailMessageAddedSignature(preferences);
    }

    String subject = message.getSubject();

    if (!subjectPrefix.contains("[$MESSAGE_SUBJECT$]")) {
        subject = subjectPrefix.trim() + " " + subject.trim();
    }

    if (Validator.isNotNull(signature)) {
        body += "\n--\n" + signature;
    }

    String messageBody = message.getBody();

    boolean htmlFormat = MBUtil.getEmailHtmlFormat(preferences);

    if (htmlFormat) {
        try {
            messageBody = BBCodeTranslatorUtil.getHTML(messageBody);
        } catch (Exception e) {
            _log.error("Could not parse message " + message.getMessageId() + " " + e.getMessage());
        }
    }

    String inReplyTo = null;

    if (message.getParentMessageId() != MBMessageConstants.DEFAULT_PARENT_MESSAGE_ID) {

        inReplyTo = PortalUtil.getMailId(company.getMx(), MBUtil.MESSAGE_POP_PORTLET_PREFIX,
                message.getCategoryId(), message.getParentMessageId());
    }

    SubscriptionSender subscriptionSenderPrototype = new MBSubscriptionSender();

    subscriptionSenderPrototype.setBody(body);
    subscriptionSenderPrototype.setBulk(true);
    subscriptionSenderPrototype.setCompanyId(message.getCompanyId());
    subscriptionSenderPrototype.setContextAttribute("[$MESSAGE_BODY$]", messageBody, false);
    subscriptionSenderPrototype.setContextAttributes("[$CATEGORY_NAME$]", categoryName,
            "[$MAILING_LIST_ADDRESS$]", mailingListAddress, "[$MESSAGE_ID$]", message.getMessageId(),
            "[$MESSAGE_SUBJECT$]", message.getSubject(), "[$MESSAGE_URL$]", messageURL,
            "[$MESSAGE_USER_ADDRESS$]", emailAddress, "[$MESSAGE_USER_NAME$]", fullName);
    subscriptionSenderPrototype.setFrom(fromAddress, fromName);
    subscriptionSenderPrototype.setHtmlFormat(htmlFormat);
    subscriptionSenderPrototype.setInReplyTo(inReplyTo);
    subscriptionSenderPrototype.setMailId(MBUtil.MESSAGE_POP_PORTLET_PREFIX, message.getCategoryId(),
            message.getMessageId());
    subscriptionSenderPrototype.setPortletId(PortletKeys.MESSAGE_BOARDS);
    subscriptionSenderPrototype.setReplyToAddress(mailingListAddress);
    subscriptionSenderPrototype.setScopeGroupId(message.getGroupId());
    subscriptionSenderPrototype.setServiceContext(serviceContext);
    subscriptionSenderPrototype.setSubject(message.getSubject());
    subscriptionSenderPrototype.setUserId(message.getUserId());

    SubscriptionSender subscriptionSender = (SubscriptionSender) SerializableUtil
            .clone(subscriptionSenderPrototype);

    for (long categoryId : categoryIds) {
        if (categoryId == MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {
            categoryId = message.getGroupId();
        }

        subscriptionSender.addPersistedSubscribers(MBCategory.class.getName(), categoryId);
    }

    subscriptionSender.addPersistedSubscribers(MBThread.class.getName(), message.getThreadId());

    subscriptionSender.flushNotificationsAsync();

    if (!MailingListThreadLocal.isSourceMailingList()) {
        for (long categoryId : categoryIds) {
            MBSubscriptionSender sourceMailingListSubscriptionSender = (MBSubscriptionSender) SerializableUtil
                    .clone(subscriptionSenderPrototype);

            sourceMailingListSubscriptionSender.setBulk(false);

            sourceMailingListSubscriptionSender.addMailingListSubscriber(message.getGroupId(), categoryId);

            sourceMailingListSubscriptionSender.flushNotificationsAsync();
        }
    }
}