001
014
015 package com.liferay.portal.kernel.notifications;
016
017 import com.liferay.portal.kernel.exception.PortalException;
018 import com.liferay.portal.kernel.log.Log;
019 import com.liferay.portal.kernel.log.LogFactoryUtil;
020 import com.liferay.portal.kernel.util.StringBundler;
021 import com.liferay.portal.kernel.util.StringPool;
022 import com.liferay.portal.kernel.util.Validator;
023 import com.liferay.portal.model.UserNotificationDelivery;
024 import com.liferay.portal.model.UserNotificationDeliveryConstants;
025 import com.liferay.portal.model.UserNotificationEvent;
026 import com.liferay.portal.service.ServiceContext;
027 import com.liferay.portal.service.UserNotificationDeliveryLocalServiceUtil;
028
029
032 public abstract class BaseUserNotificationHandler
033 implements UserNotificationHandler {
034
035 @Override
036 public String getPortletId() {
037 return _portletId;
038 }
039
040 @Override
041 public String getSelector() {
042 return _selector;
043 }
044
045 @Override
046 @SuppressWarnings("unused")
047 public UserNotificationFeedEntry interpret(
048 UserNotificationEvent userNotificationEvent,
049 ServiceContext serviceContext)
050 throws PortalException {
051
052 try {
053 UserNotificationFeedEntry userNotificationFeedEntry = doInterpret(
054 userNotificationEvent, serviceContext);
055
056 if (userNotificationFeedEntry != null) {
057 userNotificationFeedEntry.setOpenDialog(isOpenDialog());
058 userNotificationFeedEntry.setPortletId(getPortletId());
059 }
060
061 return userNotificationFeedEntry;
062 }
063 catch (Exception e) {
064 _log.error("Unable to interpret notification", e);
065 }
066
067 return null;
068 }
069
070 @Override
071 public boolean isDeliver(
072 long userId, long classNameId, int notificationType,
073 int deliveryType, ServiceContext serviceContext)
074 throws PortalException {
075
076 UserNotificationDefinition userNotificationDefinition =
077 UserNotificationManagerUtil.fetchUserNotificationDefinition(
078 _portletId, classNameId, notificationType);
079
080 if (userNotificationDefinition == null) {
081 if (deliveryType == UserNotificationDeliveryConstants.TYPE_EMAIL) {
082 return true;
083 }
084
085 return false;
086 }
087
088 UserNotificationDeliveryType userNotificationDeliveryType =
089 userNotificationDefinition.getUserNotificationDeliveryType(
090 deliveryType);
091
092 if (userNotificationDeliveryType == null) {
093 return false;
094 }
095
096 UserNotificationDelivery userNotificationDelivery =
097 UserNotificationDeliveryLocalServiceUtil.
098 getUserNotificationDelivery(
099 userId, _portletId, classNameId, notificationType,
100 deliveryType, userNotificationDeliveryType.isDefault());
101
102 return userNotificationDelivery.isDeliver();
103 }
104
105 @Override
106 public boolean isOpenDialog() {
107 return _openDialog;
108 }
109
110 protected UserNotificationFeedEntry doInterpret(
111 UserNotificationEvent userNotificationEvent,
112 ServiceContext serviceContext)
113 throws Exception {
114
115 String body = getBody(userNotificationEvent, serviceContext);
116
117 if (Validator.isNull(body)) {
118 return null;
119 }
120
121 String link = getLink(userNotificationEvent, serviceContext);
122
123 return new UserNotificationFeedEntry(isActionable(), body, link);
124 }
125
126 protected String getBody(
127 UserNotificationEvent userNotificationEvent,
128 ServiceContext serviceContext)
129 throws Exception {
130
131 return StringPool.BLANK;
132 }
133
134 protected String getBodyTemplate() throws Exception {
135 if (isActionable()) {
136 StringBundler sb = new StringBundler(5);
137
138 sb.append("<div class=\"title\">[$TITLE$]</div><div ");
139 sb.append("class=\"body\"><a class=\"btn btn-action ");
140 sb.append("btn-success\" href=\"[$CONFIRM_URL$]\">[$CONFIRM$]</a>");
141 sb.append("<a class=\"btn btn-action btn-warning\" href=\"");
142 sb.append("[$IGNORE_URL$]\">[$IGNORE$]</a></div>");
143
144 return sb.toString();
145 }
146 else {
147 return "<div class=\"title\">[$TITLE$]</div><div class=\"body\">" +
148 "[$BODY$]</div>";
149 }
150 }
151
152 protected String getLink(
153 UserNotificationEvent userNotificationEvent,
154 ServiceContext serviceContext)
155 throws Exception {
156
157 return StringPool.BLANK;
158 }
159
160 protected boolean isActionable() {
161 return _actionable;
162 }
163
164 protected void setActionable(boolean actionable) {
165 _actionable = actionable;
166 }
167
168 protected void setOpenDialog(boolean openDialog) {
169 _openDialog = openDialog;
170 }
171
172 protected void setPortletId(String portletId) {
173 _portletId = portletId;
174 }
175
176 protected void setSelector(String selector) {
177 _selector = selector;
178 }
179
180 private static final Log _log = LogFactoryUtil.getLog(
181 BaseUserNotificationHandler.class);
182
183 private boolean _actionable;
184 private boolean _openDialog;
185 private String _portletId;
186 private String _selector = StringPool.BLANK;
187
188 }