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.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, SystemException {
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) throws SystemException {
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                    throws SystemException {
087    
088                    announcementsDeliveryPersistence.remove(delivery);
089            }
090    
091            @Override
092            public void deleteDelivery(long deliveryId)
093                    throws PortalException, SystemException {
094    
095                    AnnouncementsDelivery delivery =
096                            announcementsDeliveryPersistence.findByPrimaryKey(deliveryId);
097    
098                    deleteDelivery(delivery);
099            }
100    
101            @Override
102            public void deleteDelivery(long userId, String type)
103                    throws SystemException {
104    
105                    AnnouncementsDelivery delivery =
106                            announcementsDeliveryPersistence.fetchByU_T(userId, type);
107    
108                    if (delivery != null) {
109                            deleteDelivery(delivery);
110                    }
111            }
112    
113            @Override
114            public AnnouncementsDelivery getDelivery(long deliveryId)
115                    throws PortalException, SystemException {
116    
117                    return announcementsDeliveryPersistence.findByPrimaryKey(deliveryId);
118            }
119    
120            @Override
121            public List<AnnouncementsDelivery> getUserDeliveries(long userId)
122                    throws PortalException, SystemException {
123    
124                    List<AnnouncementsDelivery> deliveries =
125                            new ArrayList<AnnouncementsDelivery>(
126                                    AnnouncementsEntryConstants.TYPES.length);
127    
128                    for (String type : AnnouncementsEntryConstants.TYPES) {
129                            deliveries.add(getUserDelivery(userId, type));
130                    }
131    
132                    return deliveries;
133            }
134    
135            @Override
136            public AnnouncementsDelivery getUserDelivery(long userId, String type)
137                    throws PortalException, SystemException {
138    
139                    AnnouncementsDelivery delivery =
140                            announcementsDeliveryPersistence.fetchByU_T(userId, type);
141    
142                    if (delivery == null) {
143                            delivery = announcementsDeliveryLocalService.addUserDelivery(
144                                    userId, type);
145                    }
146    
147                    return delivery;
148            }
149    
150            @Override
151            public AnnouncementsDelivery updateDelivery(
152                            long userId, String type, boolean email, boolean sms,
153                            boolean website)
154                    throws PortalException, SystemException {
155    
156                    AnnouncementsDelivery delivery = getUserDelivery(userId, type);
157    
158                    delivery.setEmail(email);
159                    delivery.setSms(sms);
160                    delivery.setWebsite(website);
161    
162                    announcementsDeliveryPersistence.update(delivery);
163    
164                    return delivery;
165            }
166    
167            private static Log _log = LogFactoryUtil.getLog(
168                    AnnouncementsDeliveryLocalServiceImpl.class);
169    
170    }