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