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.service.impl;
016    
017    import com.liferay.portal.exception.NoSuchUserNotificationDeliveryException;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.model.User;
020    import com.liferay.portal.model.UserNotificationDelivery;
021    import com.liferay.portal.service.base.UserNotificationDeliveryLocalServiceBaseImpl;
022    
023    /**
024     * @author Jonathan Lee
025     */
026    public class UserNotificationDeliveryLocalServiceImpl
027            extends UserNotificationDeliveryLocalServiceBaseImpl {
028    
029            @Override
030            public UserNotificationDelivery addUserNotificationDelivery(
031                            long userId, String portletId, long classNameId,
032                            int notificationType, int deliveryType, boolean deliver)
033                    throws PortalException {
034    
035                    User user = userPersistence.findByPrimaryKey(userId);
036    
037                    long userNotificationDeliveryId = counterLocalService.increment();
038    
039                    UserNotificationDelivery userNotificationDelivery =
040                            userNotificationDeliveryPersistence.create(
041                                    userNotificationDeliveryId);
042    
043                    userNotificationDelivery.setCompanyId(user.getCompanyId());
044                    userNotificationDelivery.setUserId(user.getUserId());
045                    userNotificationDelivery.setPortletId(portletId);
046                    userNotificationDelivery.setClassNameId(classNameId);
047                    userNotificationDelivery.setNotificationType(notificationType);
048                    userNotificationDelivery.setDeliveryType(deliveryType);
049                    userNotificationDelivery.setDeliver(deliver);
050    
051                    return userNotificationDeliveryPersistence.update(
052                            userNotificationDelivery);
053            }
054    
055            @Override
056            public void deleteUserNotificationDeliveries(long userId) {
057                    userNotificationDeliveryPersistence.removeByUserId(userId);
058            }
059    
060            @Override
061            public void deleteUserNotificationDelivery(
062                    long userId, String portletId, long classNameId, int notificationType,
063                    int deliveryType) {
064    
065                    try {
066                            userNotificationDeliveryPersistence.removeByU_P_C_N_D(
067                                    userId, portletId, classNameId, notificationType, deliveryType);
068                    }
069                    catch (NoSuchUserNotificationDeliveryException nsnde) {
070                    }
071            }
072    
073            @Override
074            public UserNotificationDelivery fetchUserNotificationDelivery(
075                    long userId, String portletId, long classNameId, int notificationType,
076                    int deliveryType) {
077    
078                    return userNotificationDeliveryPersistence.fetchByU_P_C_N_D(
079                            userId, portletId, classNameId, notificationType, deliveryType);
080            }
081    
082            @Override
083            public UserNotificationDelivery getUserNotificationDelivery(
084                            long userId, String portletId, long classNameId,
085                            int notificationType, int deliveryType, boolean deliver)
086                    throws PortalException {
087    
088                    UserNotificationDelivery userNotificationDelivery =
089                            userNotificationDeliveryPersistence.fetchByU_P_C_N_D(
090                                    userId, portletId, classNameId, notificationType, deliveryType);
091    
092                    if (userNotificationDelivery != null) {
093                            return userNotificationDelivery;
094                    }
095    
096                    return userNotificationDeliveryLocalService.addUserNotificationDelivery(
097                            userId, portletId, classNameId, notificationType, deliveryType,
098                            deliver);
099            }
100    
101            @Override
102            public UserNotificationDelivery updateUserNotificationDelivery(
103                    long userNotificationDeliveryId, boolean deliver) {
104    
105                    UserNotificationDelivery userNotificationDelivery =
106                            fetchUserNotificationDelivery(userNotificationDeliveryId);
107    
108                    userNotificationDelivery.setDeliver(deliver);
109    
110                    return userNotificationDeliveryPersistence.update(
111                            userNotificationDelivery);
112            }
113    
114    }