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.portal.kernel.notifications;
016    
017    import aQute.bnd.annotation.ProviderType;
018    
019    import com.liferay.portal.kernel.json.JSONFactoryUtil;
020    import com.liferay.portal.kernel.json.JSONObject;
021    import com.liferay.portal.kernel.language.LanguageUtil;
022    import com.liferay.portal.kernel.util.HtmlUtil;
023    import com.liferay.portal.kernel.util.StringPool;
024    import com.liferay.portal.kernel.util.StringUtil;
025    import com.liferay.portal.model.UserNotificationEvent;
026    import com.liferay.portal.service.ServiceContext;
027    import com.liferay.portal.service.UserNotificationEventLocalServiceUtil;
028    import com.liferay.portlet.asset.AssetRendererFactoryRegistryUtil;
029    import com.liferay.portlet.asset.model.AssetRenderer;
030    import com.liferay.portlet.asset.model.AssetRendererFactory;
031    
032    /**
033     * @author Brian Wing Shun Chan
034     * @author Sergio Gonz??lez
035     */
036    @ProviderType
037    public abstract class BaseModelUserNotificationHandler
038            extends BaseUserNotificationHandler {
039    
040            protected AssetRenderer getAssetRenderer(JSONObject jsonObject) {
041                    String className = jsonObject.getString("className");
042                    long classPK = jsonObject.getLong("classPK");
043    
044                    return getAssetRenderer(className, classPK);
045            }
046    
047            protected AssetRenderer getAssetRenderer(String className, long classPK) {
048                    AssetRendererFactory assetRendererFactory =
049                            AssetRendererFactoryRegistryUtil.getAssetRendererFactoryByClassName(
050                                    className);
051    
052                    if (assetRendererFactory == null) {
053                            return null;
054                    }
055    
056                    AssetRenderer assetRenderer = null;
057    
058                    try {
059                            assetRenderer = assetRendererFactory.getAssetRenderer(classPK);
060                    }
061                    catch (Exception e) {
062                    }
063    
064                    return assetRenderer;
065            }
066    
067            @Override
068            protected String getBody(
069                            UserNotificationEvent userNotificationEvent,
070                            ServiceContext serviceContext)
071                    throws Exception {
072    
073                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject(
074                            userNotificationEvent.getPayload());
075    
076                    AssetRenderer assetRenderer = getAssetRenderer(jsonObject);
077    
078                    if (assetRenderer == null) {
079                            UserNotificationEventLocalServiceUtil.deleteUserNotificationEvent(
080                                    userNotificationEvent.getUserNotificationEventId());
081    
082                            return null;
083                    }
084    
085                    return StringUtil.replace(
086                            getBodyTemplate(), new String[] {"[$BODY$]", "[$TITLE$]"},
087                            new String[] {
088                                    HtmlUtil.escape(
089                                            StringUtil.shorten(jsonObject.getString("entryTitle"), 70)),
090                                    getTitle(jsonObject, assetRenderer, serviceContext)
091                            });
092            }
093    
094            @Override
095            protected String getLink(
096                            UserNotificationEvent userNotificationEvent,
097                            ServiceContext serviceContext)
098                    throws Exception {
099    
100                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject(
101                            userNotificationEvent.getPayload());
102    
103                    return jsonObject.getString("entryURL");
104            }
105    
106            protected String getTitle(
107                    JSONObject jsonObject, AssetRenderer assetRenderer,
108                    ServiceContext serviceContext) {
109    
110                    String message = StringPool.BLANK;
111    
112                    AssetRendererFactory assetRendererFactory =
113                            AssetRendererFactoryRegistryUtil.getAssetRendererFactoryByClassName(
114                                    assetRenderer.getClassName());
115    
116                    String typeName = assetRendererFactory.getTypeName(
117                            serviceContext.getLocale());
118    
119                    int notificationType = jsonObject.getInt("notificationType");
120    
121                    if (notificationType ==
122                                    UserNotificationDefinition.NOTIFICATION_TYPE_ADD_ENTRY) {
123    
124                            message = "x-added-a-new-x";
125                    }
126                    else if (notificationType ==
127                                            UserNotificationDefinition.
128                                                    NOTIFICATION_TYPE_UPDATE_ENTRY) {
129    
130                            message = "x-updated-a-x";
131                    }
132    
133                    return LanguageUtil.format(
134                            serviceContext.getLocale(), message,
135                            new String[] {
136                                    HtmlUtil.escape(assetRenderer.getUserName()),
137                                    StringUtil.toLowerCase(HtmlUtil.escape(typeName))
138                            },
139                            false);
140            }
141    
142    }