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.portlet.announcements.service.impl;
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.model.User;
022    import com.liferay.portlet.announcements.model.AnnouncementsDelivery;
023    import com.liferay.portlet.announcements.model.AnnouncementsEntryConstants;
024    import com.liferay.portlet.announcements.service.base.AnnouncementsDeliveryLocalServiceBaseImpl;
025    
026    import java.util.ArrayList;
027    import java.util.List;
028    
029    /**
030     * @author Brian Wing Shun Chan
031     */
032    public class AnnouncementsDeliveryLocalServiceImpl
033            extends AnnouncementsDeliveryLocalServiceBaseImpl {
034    
035            @Override
036            public AnnouncementsDelivery addUserDelivery(long userId, String type)
037                    throws PortalException {
038    
039                    User user = userPersistence.findByPrimaryKey(userId);
040    
041                    long deliveryId = counterLocalService.increment();
042    
043                    AnnouncementsDelivery delivery =
044                            announcementsDeliveryPersistence.create(deliveryId);
045    
046                    delivery.setCompanyId(user.getCompanyId());
047                    delivery.setUserId(user.getUserId());
048                    delivery.setType(type);
049                    delivery.setEmail(false);
050                    delivery.setSms(false);
051                    delivery.setWebsite(true);
052    
053                    try {
054                            announcementsDeliveryPersistence.update(delivery);
055                    }
056                    catch (SystemException se) {
057                            if (_log.isWarnEnabled()) {
058                                    _log.warn(
059                                            "Add failed, fetch {userId=" + userId + ", type=" +
060                                                    type + "}");
061                            }
062    
063                            delivery = announcementsDeliveryPersistence.fetchByU_T(
064                                    userId, type, false);
065    
066                            if (delivery == null) {
067                                    throw se;
068                            }
069                    }
070    
071                    return delivery;
072            }
073    
074            @Override
075            public void deleteDeliveries(long userId) {
076                    List<AnnouncementsDelivery> deliveries =
077                            announcementsDeliveryPersistence.findByUserId(userId);
078    
079                    for (AnnouncementsDelivery delivery : deliveries) {
080                            deleteDelivery(delivery);
081                    }
082            }
083    
084            @Override
085            public void deleteDelivery(AnnouncementsDelivery delivery) {
086                    announcementsDeliveryPersistence.remove(delivery);
087            }
088    
089            @Override
090            public void deleteDelivery(long deliveryId) throws PortalException {
091                    AnnouncementsDelivery delivery =
092                            announcementsDeliveryPersistence.findByPrimaryKey(deliveryId);
093    
094                    deleteDelivery(delivery);
095            }
096    
097            @Override
098            public void deleteDelivery(long userId, String type) {
099                    AnnouncementsDelivery delivery =
100                            announcementsDeliveryPersistence.fetchByU_T(userId, type);
101    
102                    if (delivery != null) {
103                            deleteDelivery(delivery);
104                    }
105            }
106    
107            @Override
108            public AnnouncementsDelivery getDelivery(long deliveryId)
109                    throws PortalException {
110    
111                    return announcementsDeliveryPersistence.findByPrimaryKey(deliveryId);
112            }
113    
114            @Override
115            public List<AnnouncementsDelivery> getUserDeliveries(long userId)
116                    throws PortalException {
117    
118                    List<AnnouncementsDelivery> deliveries = new ArrayList<>(
119                            AnnouncementsEntryConstants.TYPES.length);
120    
121                    for (String type : AnnouncementsEntryConstants.TYPES) {
122                            deliveries.add(getUserDelivery(userId, type));
123                    }
124    
125                    return deliveries;
126            }
127    
128            @Override
129            public AnnouncementsDelivery getUserDelivery(long userId, String type)
130                    throws PortalException {
131    
132                    AnnouncementsDelivery delivery =
133                            announcementsDeliveryPersistence.fetchByU_T(userId, type);
134    
135                    if (delivery == null) {
136                            delivery = announcementsDeliveryLocalService.addUserDelivery(
137                                    userId, type);
138                    }
139    
140                    return delivery;
141            }
142    
143            @Override
144            public AnnouncementsDelivery updateDelivery(
145                            long userId, String type, boolean email, boolean sms,
146                            boolean website)
147                    throws PortalException {
148    
149                    AnnouncementsDelivery delivery = getUserDelivery(userId, type);
150    
151                    delivery.setEmail(email);
152                    delivery.setSms(sms);
153                    delivery.setWebsite(website);
154    
155                    announcementsDeliveryPersistence.update(delivery);
156    
157                    return delivery;
158            }
159    
160            private static final Log _log = LogFactoryUtil.getLog(
161                    AnnouncementsDeliveryLocalServiceImpl.class);
162    
163    }