001    /**
002     * Copyright (c) 2000-2012 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            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, false);
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            public void deleteDeliveries(long userId) throws SystemException {
075                    List<AnnouncementsDelivery> deliveries =
076                            announcementsDeliveryPersistence.findByUserId(userId);
077    
078                    for (AnnouncementsDelivery delivery : deliveries) {
079                            deleteDelivery(delivery);
080                    }
081            }
082    
083            public void deleteDelivery(AnnouncementsDelivery delivery)
084                    throws SystemException {
085    
086                    announcementsDeliveryPersistence.remove(delivery);
087            }
088    
089            public void deleteDelivery(long deliveryId)
090                    throws PortalException, SystemException {
091    
092                    AnnouncementsDelivery delivery =
093                            announcementsDeliveryPersistence.findByPrimaryKey(deliveryId);
094    
095                    deleteDelivery(delivery);
096            }
097    
098            public void deleteDelivery(long userId, String type)
099                    throws SystemException {
100    
101                    try {
102                            AnnouncementsDelivery delivery =
103                                    announcementsDeliveryPersistence.findByU_T(userId, type);
104    
105                            deleteDelivery(delivery);
106                    }
107                    catch (NoSuchDeliveryException nsde) {
108                    }
109            }
110    
111            public AnnouncementsDelivery getDelivery(long deliveryId)
112                    throws PortalException, SystemException {
113    
114                    return announcementsDeliveryPersistence.findByPrimaryKey(deliveryId);
115            }
116    
117            public List<AnnouncementsDelivery> getUserDeliveries(long userId)
118                    throws PortalException, SystemException {
119    
120                    List<AnnouncementsDelivery> deliveries =
121                            new ArrayList<AnnouncementsDelivery>(
122                                    AnnouncementsEntryConstants.TYPES.length);
123    
124                    for (String type : AnnouncementsEntryConstants.TYPES) {
125                            deliveries.add(getUserDelivery(userId, type));
126                    }
127    
128                    return deliveries;
129            }
130    
131            public AnnouncementsDelivery getUserDelivery(long userId, String type)
132                    throws PortalException, SystemException {
133    
134                    AnnouncementsDelivery delivery =
135                            announcementsDeliveryPersistence.fetchByU_T(userId, type);
136    
137                    if (delivery == null) {
138                            delivery = announcementsDeliveryLocalService.addUserDelivery(
139                                    userId, type);
140                    }
141    
142                    return delivery;
143            }
144    
145            public AnnouncementsDelivery updateDelivery(
146                            long userId, String type, boolean email, boolean sms,
147                            boolean website)
148                    throws PortalException, SystemException {
149    
150                    AnnouncementsDelivery delivery = getUserDelivery(userId, type);
151    
152                    delivery.setEmail(email);
153                    delivery.setSms(sms);
154                    delivery.setWebsite(website);
155    
156                    announcementsDeliveryPersistence.update(delivery, false);
157    
158                    return delivery;
159            }
160    
161            private static Log _log = LogFactoryUtil.getLog(
162                    AnnouncementsDeliveryLocalServiceImpl.class);
163    
164    }