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