001    /**
002     * Copyright (c) 2000-2013 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.exception.SystemException;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
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.UserNotificationEvent;
025    import com.liferay.portal.service.ServiceContext;
026    import com.liferay.portal.service.UserNotificationDeliveryLocalServiceUtil;
027    
028    /**
029     * @author Jonathan Lee
030     */
031    public abstract class BaseUserNotificationHandler
032            implements UserNotificationHandler {
033    
034            @Override
035            public String getPortletId() {
036                    return _portletId;
037            }
038    
039            @Override
040            public String getSelector() {
041                    return _selector;
042            }
043    
044            @Override
045            @SuppressWarnings("unused")
046            public UserNotificationFeedEntry interpret(
047                            UserNotificationEvent userNotificationEvent,
048                            ServiceContext serviceContext)
049                    throws PortalException {
050    
051                    try {
052                            UserNotificationFeedEntry userNotificationFeedEntry = doInterpret(
053                                    userNotificationEvent, serviceContext);
054    
055                            if (userNotificationFeedEntry != null) {
056                                    userNotificationFeedEntry.setPortletId(getPortletId());
057                            }
058    
059                            return userNotificationFeedEntry;
060                    }
061                    catch (Exception e) {
062                            _log.error("Unable to interpret notification", e);
063                    }
064    
065                    return null;
066            }
067    
068            @Override
069            public boolean isDeliver(
070                            long userId, long classNameId, int notificationType,
071                            int deliveryType, ServiceContext serviceContext)
072                    throws PortalException, SystemException {
073    
074                    UserNotificationDefinition userNotificationDefinition =
075                            UserNotificationManagerUtil.fetchUserNotificationDefinition(
076                                    _portletId, classNameId, notificationType);
077    
078                    UserNotificationDeliveryType userNotificationDeliveryType =
079                            userNotificationDefinition.getUserNotificationDeliveryType(
080                                    deliveryType);
081    
082                    UserNotificationDelivery userNotificationDelivery =
083                            UserNotificationDeliveryLocalServiceUtil.
084                                    getUserNotificationDelivery(
085                                            userId, _portletId, classNameId, notificationType,
086                                            deliveryType, userNotificationDeliveryType.isDefault());
087    
088                    return userNotificationDelivery.isDeliver();
089            }
090    
091            protected UserNotificationFeedEntry doInterpret(
092                            UserNotificationEvent userNotificationEvent,
093                            ServiceContext serviceContext)
094                    throws Exception {
095    
096                    String body = getBody(userNotificationEvent, serviceContext);
097    
098                    if (Validator.isNull(body)) {
099                            return null;
100                    }
101    
102                    String link = getLink(userNotificationEvent, serviceContext);
103    
104                    return new UserNotificationFeedEntry(body, link);
105            }
106    
107            protected String getBody(
108                            UserNotificationEvent userNotificationEvent,
109                            ServiceContext serviceContext)
110                    throws Exception {
111    
112                    return StringPool.BLANK;
113            }
114    
115            protected String getLink(
116                            UserNotificationEvent userNotificationEvent,
117                            ServiceContext serviceContext)
118                    throws Exception {
119    
120                    return StringPool.BLANK;
121            }
122    
123            protected void setPortletId(String portletId) {
124                    _portletId = portletId;
125            }
126    
127            protected void setSelector(String selector) {
128                    _selector = selector;
129            }
130    
131            private static Log _log = LogFactoryUtil.getLog(
132                    BaseUserNotificationHandler.class);
133    
134            private String _portletId;
135            private String _selector = StringPool.BLANK;
136    
137    }