1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.announcements.service.impl;
24  
25  import com.liferay.portal.PortalException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.log.Log;
28  import com.liferay.portal.kernel.log.LogFactoryUtil;
29  import com.liferay.portal.model.User;
30  import com.liferay.portlet.announcements.NoSuchDeliveryException;
31  import com.liferay.portlet.announcements.model.AnnouncementsDelivery;
32  import com.liferay.portlet.announcements.model.impl.AnnouncementsEntryImpl;
33  import com.liferay.portlet.announcements.service.base.AnnouncementsDeliveryLocalServiceBaseImpl;
34  
35  import java.util.ArrayList;
36  import java.util.List;
37  
38  /**
39   * <a href="AnnouncementsDeliveryLocalServiceImpl.java.html"><b><i>View Source
40   * </i></b></a>
41   *
42   * @author Brian Wing Shun Chan
43   *
44   */
45  public class AnnouncementsDeliveryLocalServiceImpl
46      extends AnnouncementsDeliveryLocalServiceBaseImpl {
47  
48      public AnnouncementsDelivery addUserDelivery(long userId, String type)
49          throws PortalException, SystemException {
50  
51          User user = userPersistence.findByPrimaryKey(userId);
52  
53          long deliveryId = counterLocalService.increment();
54  
55          AnnouncementsDelivery delivery =
56              announcementsDeliveryPersistence.create(deliveryId);
57  
58          delivery.setCompanyId(user.getCompanyId());
59          delivery.setUserId(user.getUserId());
60          delivery.setType(type);
61          delivery.setEmail(false);
62          delivery.setSms(false);
63          delivery.setWebsite(true);
64  
65          try {
66              announcementsDeliveryPersistence.update(delivery, false);
67          }
68          catch (SystemException se) {
69              if (_log.isWarnEnabled()) {
70                  _log.warn(
71                      "Add failed, fetch {userId=" + userId + ", type=" +
72                          type + "}");
73              }
74  
75              delivery = announcementsDeliveryPersistence.fetchByU_T(
76                  userId, type, false);
77  
78              if (delivery == null) {
79                  throw se;
80              }
81          }
82  
83          return delivery;
84      }
85  
86      public void deleteDeliveries(long userId) throws SystemException {
87          announcementsDeliveryPersistence.removeByUserId(userId);
88      }
89  
90      public void deleteDelivery(long deliveryId)
91          throws PortalException, SystemException {
92  
93          announcementsDeliveryPersistence.remove(deliveryId);
94      }
95  
96      public void deleteDelivery(long userId, String type)
97          throws SystemException {
98  
99          try {
100             announcementsDeliveryPersistence.removeByU_T(userId, type);
101         }
102         catch (NoSuchDeliveryException nsde) {
103         }
104     }
105 
106     public AnnouncementsDelivery getDelivery(long deliveryId)
107         throws PortalException, SystemException {
108 
109         return announcementsDeliveryPersistence.findByPrimaryKey(deliveryId);
110     }
111 
112     public List<AnnouncementsDelivery> getUserDeliveries(long userId)
113         throws PortalException, SystemException {
114 
115         List<AnnouncementsDelivery> deliveries =
116             new ArrayList<AnnouncementsDelivery>(
117                 AnnouncementsEntryImpl.TYPES.length);
118 
119         for (String type : AnnouncementsEntryImpl.TYPES) {
120             deliveries.add(getUserDelivery(userId, type));
121         }
122 
123         return deliveries;
124     }
125 
126     public AnnouncementsDelivery getUserDelivery(long userId, String type)
127         throws PortalException, SystemException {
128 
129         AnnouncementsDelivery delivery =
130             announcementsDeliveryPersistence.fetchByU_T(userId, type);
131 
132         if (delivery == null) {
133             delivery = announcementsDeliveryLocalService.addUserDelivery(
134                 userId, type);
135         }
136 
137         return delivery;
138     }
139 
140     public AnnouncementsDelivery updateDelivery(
141             long userId, String type, boolean email, boolean sms,
142             boolean website)
143         throws PortalException, SystemException {
144 
145         AnnouncementsDelivery delivery = getUserDelivery(userId, type);
146 
147         delivery.setEmail(email);
148         delivery.setSms(sms);
149         delivery.setWebsite(website);
150 
151         announcementsDeliveryPersistence.update(delivery, false);
152 
153         return delivery;
154     }
155 
156     private static Log _log =
157         LogFactoryUtil.getLog(AnnouncementsDeliveryLocalServiceImpl.class);
158 
159 }