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