001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.comments.notifications;
016    
017    import com.liferay.portal.kernel.exception.SystemException;
018    import com.liferay.portal.kernel.json.JSONObject;
019    import com.liferay.portal.kernel.language.LanguageUtil;
020    import com.liferay.portal.kernel.notifications.BaseModelUserNotificationHandler;
021    import com.liferay.portal.kernel.notifications.UserNotificationDefinition;
022    import com.liferay.portal.kernel.util.HtmlUtil;
023    import com.liferay.portal.kernel.util.StringPool;
024    import com.liferay.portal.service.ServiceContext;
025    import com.liferay.portal.util.PortalUtil;
026    import com.liferay.portal.util.PortletKeys;
027    import com.liferay.portlet.asset.model.AssetRenderer;
028    import com.liferay.portlet.messageboards.model.MBDiscussion;
029    import com.liferay.portlet.messageboards.service.MBDiscussionLocalServiceUtil;
030    
031    /**
032     * @author Roberto D??az
033     * @author Sergio Gonz??lez
034     */
035    public class CommentsUserNotificationHandler
036            extends BaseModelUserNotificationHandler {
037    
038            public CommentsUserNotificationHandler() {
039                    setPortletId(PortletKeys.COMMENTS);
040            }
041    
042            protected MBDiscussion fetchDiscussion(JSONObject jsonObject) {
043                    long classPK = jsonObject.getLong("classPK");
044    
045                    try {
046                            return MBDiscussionLocalServiceUtil.fetchDiscussion(classPK);
047                    }
048                    catch (SystemException se) {
049                            return null;
050                    }
051            }
052    
053            @Override
054            protected AssetRenderer getAssetRenderer(JSONObject jsonObject) {
055                    MBDiscussion mbDiscussion = fetchDiscussion(jsonObject);
056    
057                    if (mbDiscussion == null) {
058                            return null;
059                    }
060    
061                    return getAssetRenderer(
062                            mbDiscussion.getClassName(), mbDiscussion.getClassPK());
063            }
064    
065            @Override
066            protected String getTitle(
067                    JSONObject jsonObject, AssetRenderer assetRenderer,
068                    ServiceContext serviceContext) {
069    
070                    MBDiscussion mbDiscussion = fetchDiscussion(jsonObject);
071    
072                    if (mbDiscussion == null) {
073                            return null;
074                    }
075    
076                    String message = StringPool.BLANK;
077    
078                    int notificationType = jsonObject.getInt("notificationType");
079    
080                    if (notificationType ==
081                                    UserNotificationDefinition.NOTIFICATION_TYPE_ADD_ENTRY) {
082    
083                            if (assetRenderer != null) {
084                                    message = "x-added-a-new-comment-to-x";
085                            }
086                            else {
087                                    message = "x-added-a-new-comment";
088                            }
089                    }
090                    else if (notificationType ==
091                                            UserNotificationDefinition.NOTIFICATION_TYPE_UPDATE_ENTRY) {
092    
093                            if (assetRenderer != null) {
094                                    message = "x-updated-a-comment-to-x";
095                            }
096                            else {
097                                    message = "x-updated-a-comment";
098                            }
099                    }
100    
101                    if (assetRenderer != null) {
102                            message = LanguageUtil.format(
103                                    serviceContext.getLocale(), message,
104                                    new String[] {
105                                            HtmlUtil.escape(
106                                                    PortalUtil.getUserName(
107                                                            jsonObject.getLong("userId"), StringPool.BLANK)),
108                                            HtmlUtil.escape(
109                                                    assetRenderer.getTitle(serviceContext.getLocale()))
110                                    },
111                                    false);
112                    }
113                    else {
114                            message = LanguageUtil.format(
115                                    serviceContext.getLocale(), message,
116                                    new String[] {
117                                            HtmlUtil.escape(
118                                                    PortalUtil.getUserName(
119                                                            jsonObject.getLong("userId"), StringPool.BLANK))
120                                    },
121                                    false);
122                    }
123    
124                    return message;
125            }
126    
127    }