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.MBMessage;
028    import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
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 MBMessage fetchMBMessage(JSONObject jsonObject) {
042                    long classPK = jsonObject.getLong("classPK");
043    
044                    try {
045                            return MBMessageLocalServiceUtil.fetchMBMessage(classPK);
046                    }
047                    catch (SystemException se) {
048                            return null;
049                    }
050            }
051    
052            @Override
053            protected AssetRenderer getAssetRenderer(JSONObject jsonObject) {
054                    MBMessage message = fetchMBMessage(jsonObject);
055    
056                    if (message == null) {
057                            return null;
058                    }
059    
060                    return getAssetRenderer(message.getClassName(), message.getClassPK());
061            }
062    
063            @Override
064            protected String getTitle(
065                    JSONObject jsonObject, AssetRenderer assetRenderer,
066                    ServiceContext serviceContext) {
067    
068                    MBMessage message = fetchMBMessage(jsonObject);
069    
070                    if (message == null) {
071                            return null;
072                    }
073    
074                    String title = StringPool.BLANK;
075    
076                    int notificationType = jsonObject.getInt("notificationType");
077    
078                    if (notificationType ==
079                                    UserNotificationDefinition.NOTIFICATION_TYPE_ADD_ENTRY) {
080    
081                            if (assetRenderer != null) {
082                                    title = "x-added-a-new-comment-to-x";
083                            }
084                            else {
085                                    title = "x-added-a-new-comment";
086                            }
087                    }
088                    else if (notificationType ==
089                                            UserNotificationDefinition.NOTIFICATION_TYPE_UPDATE_ENTRY) {
090    
091                            if (assetRenderer != null) {
092                                    title = "x-updated-a-comment-to-x";
093                            }
094                            else {
095                                    title = "x-updated-a-comment";
096                            }
097                    }
098    
099                    if (assetRenderer != null) {
100                            title = serviceContext.translate(
101                                    title,
102                                    HtmlUtil.escape(
103                                            PortalUtil.getUserName(
104                                                    message.getUserId(), StringPool.BLANK)),
105                                    HtmlUtil.escape(
106                                            assetRenderer.getTitle(serviceContext.getLocale())));
107                    }
108                    else {
109                            title = serviceContext.translate(
110                                    title,
111                                    HtmlUtil.escape(
112                                            PortalUtil.getUserName(
113                                                    message.getUserId(), StringPool.BLANK)));
114                    }
115    
116                    return title;
117            }
118    
119    }