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.notifications.BaseModelUserNotificationHandler;
020    import com.liferay.portal.kernel.notifications.UserNotificationDefinition;
021    import com.liferay.portal.kernel.util.HtmlUtil;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.service.ServiceContext;
024    import com.liferay.portal.util.PortalUtil;
025    import com.liferay.portal.util.PortletKeys;
026    import com.liferay.portlet.asset.model.AssetRenderer;
027    import com.liferay.portlet.messageboards.model.MBDiscussion;
028    import com.liferay.portlet.messageboards.service.MBDiscussionLocalServiceUtil;
029    
030    /**
031     * @author Roberto D??az
032     * @author Sergio Gonz??lez
033     */
034    public class CommentsUserNotificationHandler
035            extends BaseModelUserNotificationHandler {
036    
037            public CommentsUserNotificationHandler() {
038                    setPortletId(PortletKeys.COMMENTS);
039            }
040    
041            protected MBDiscussion fetchDiscussion(JSONObject jsonObject) {
042                    long classPK = jsonObject.getLong("classPK");
043    
044                    try {
045                            return MBDiscussionLocalServiceUtil.fetchDiscussion(classPK);
046                    }
047                    catch (SystemException se) {
048                            return null;
049                    }
050            }
051    
052            @Override
053            protected AssetRenderer getAssetRenderer(JSONObject jsonObject) {
054                    MBDiscussion mbDiscussion = fetchDiscussion(jsonObject);
055    
056                    if (mbDiscussion == null) {
057                            return null;
058                    }
059    
060                    return getAssetRenderer(
061                            mbDiscussion.getClassName(), mbDiscussion.getClassPK());
062            }
063    
064            @Override
065            protected String getTitle(
066                    JSONObject jsonObject, AssetRenderer assetRenderer,
067                    ServiceContext serviceContext) {
068    
069                    MBDiscussion mbDiscussion = fetchDiscussion(jsonObject);
070    
071                    if (mbDiscussion == null) {
072                            return null;
073                    }
074    
075                    String title = StringPool.BLANK;
076    
077                    int notificationType = jsonObject.getInt("notificationType");
078    
079                    if (notificationType ==
080                                    UserNotificationDefinition.NOTIFICATION_TYPE_ADD_ENTRY) {
081    
082                            if (assetRenderer != null) {
083                                    title = "x-added-a-new-comment-to-x";
084                            }
085                            else {
086                                    title = "x-added-a-new-comment";
087                            }
088                    }
089                    else if (notificationType ==
090                                            UserNotificationDefinition.NOTIFICATION_TYPE_UPDATE_ENTRY) {
091    
092                            if (assetRenderer != null) {
093                                    title = "x-updated-a-comment-to-x";
094                            }
095                            else {
096                                    title = "x-updated-a-comment";
097                            }
098                    }
099    
100                    if (assetRenderer != null) {
101                            title = serviceContext.translate(
102                                    title,
103                                    HtmlUtil.escape(
104                                            PortalUtil.getUserName(
105                                                    mbDiscussion.getUserId(), StringPool.BLANK)),
106                                    HtmlUtil.escape(
107                                            assetRenderer.getTitle(serviceContext.getLocale())));
108                    }
109                    else {
110                            title = serviceContext.translate(
111                                    title,
112                                    HtmlUtil.escape(
113                                            PortalUtil.getUserName(
114                                                    mbDiscussion.getUserId(), StringPool.BLANK)));
115                    }
116    
117                    return title;
118            }
119    
120    }