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