001
014
015 package com.liferay.portal.kernel.notifications;
016
017 import aQute.bnd.annotation.ProviderType;
018
019 import com.liferay.asset.kernel.AssetRendererFactoryRegistryUtil;
020 import com.liferay.asset.kernel.model.AssetRenderer;
021 import com.liferay.asset.kernel.model.AssetRendererFactory;
022 import com.liferay.portal.kernel.json.JSONFactoryUtil;
023 import com.liferay.portal.kernel.json.JSONObject;
024 import com.liferay.portal.kernel.language.LanguageUtil;
025 import com.liferay.portal.kernel.model.UserNotificationEvent;
026 import com.liferay.portal.kernel.service.ServiceContext;
027 import com.liferay.portal.kernel.service.UserNotificationEventLocalServiceUtil;
028 import com.liferay.portal.kernel.util.HtmlUtil;
029 import com.liferay.portal.kernel.util.PortalUtil;
030 import com.liferay.portal.kernel.util.StringPool;
031 import com.liferay.portal.kernel.util.StringUtil;
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 protected String getFormattedMessage(
102 JSONObject jsonObject, ServiceContext serviceContext, String message,
103 String typeName) {
104
105 return LanguageUtil.format(
106 serviceContext.getLocale(), message,
107 new String[] {
108 HtmlUtil.escape(
109 PortalUtil.getUserName(
110 jsonObject.getLong("userId"), StringPool.BLANK)),
111 StringUtil.toLowerCase(HtmlUtil.escape(typeName))
112 },
113 false);
114 }
115
116 @Override
117 protected String getLink(
118 UserNotificationEvent userNotificationEvent,
119 ServiceContext serviceContext)
120 throws Exception {
121
122 JSONObject jsonObject = JSONFactoryUtil.createJSONObject(
123 userNotificationEvent.getPayload());
124
125 return jsonObject.getString("entryURL");
126 }
127
128 protected String getTitle(
129 JSONObject jsonObject, AssetRenderer<?> assetRenderer,
130 ServiceContext serviceContext) {
131
132 String message = StringPool.BLANK;
133
134 AssetRendererFactory<?> assetRendererFactory =
135 AssetRendererFactoryRegistryUtil.getAssetRendererFactoryByClassName(
136 assetRenderer.getClassName());
137
138 String typeName = assetRendererFactory.getTypeName(
139 serviceContext.getLocale());
140
141 int notificationType = jsonObject.getInt("notificationType");
142
143 if (notificationType ==
144 UserNotificationDefinition.NOTIFICATION_TYPE_ADD_ENTRY) {
145
146 message = "x-added-a-new-x";
147 }
148 else if (notificationType ==
149 UserNotificationDefinition.
150 NOTIFICATION_TYPE_UPDATE_ENTRY) {
151
152 message = "x-updated-a-x";
153 }
154
155 return getFormattedMessage(
156 jsonObject, serviceContext, message, typeName);
157 }
158
159 }