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