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