1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class AnnouncementsDeliveryLocalServiceImpl
45      extends AnnouncementsDeliveryLocalServiceBaseImpl {
46  
47      public AnnouncementsDelivery addUserDelivery(long userId, String type)
48          throws PortalException, SystemException {
49  
50          User user = userPersistence.findByPrimaryKey(userId);
51  
52          long deliveryId = counterLocalService.increment();
53  
54          AnnouncementsDelivery delivery =
55              announcementsDeliveryPersistence.create(deliveryId);
56  
57          delivery.setCompanyId(user.getCompanyId());
58          delivery.setUserId(user.getUserId());
59          delivery.setType(type);
60          delivery.setEmail(false);
61          delivery.setSms(false);
62          delivery.setWebsite(true);
63  
64          try {
65              announcementsDeliveryPersistence.update(delivery, false);
66          }
67          catch (SystemException se) {
68              if (_log.isWarnEnabled()) {
69                  _log.warn(
70                      "Add failed, fetch {userId=" + userId + ", type=" +
71                          type + "}");
72              }
73  
74              delivery = announcementsDeliveryPersistence.fetchByU_T(
75                  userId, type, false);
76  
77              if (delivery == null) {
78                  throw se;
79              }
80          }
81  
82          return delivery;
83      }
84  
85      public void deleteDeliveries(long userId) throws SystemException {
86          announcementsDeliveryPersistence.removeByUserId(userId);
87      }
88  
89      public void deleteDelivery(long deliveryId)
90          throws PortalException, SystemException {
91  
92          announcementsDeliveryPersistence.remove(deliveryId);
93      }
94  
95      public void deleteDelivery(long userId, String type)
96          throws SystemException {
97  
98          try {
99              announcementsDeliveryPersistence.removeByU_T(userId, type);
100         }
101         catch (NoSuchDeliveryException nsde) {
102         }
103     }
104 
105     public AnnouncementsDelivery getDelivery(long deliveryId)
106         throws PortalException, SystemException {
107 
108         return announcementsDeliveryPersistence.findByPrimaryKey(deliveryId);
109     }
110 
111     public List<AnnouncementsDelivery> getUserDeliveries(long userId)
112         throws PortalException, SystemException {
113 
114         List<AnnouncementsDelivery> deliveries =
115             new ArrayList<AnnouncementsDelivery>(
116                 AnnouncementsEntryImpl.TYPES.length);
117 
118         for (String type : AnnouncementsEntryImpl.TYPES) {
119             deliveries.add(getUserDelivery(userId, type));
120         }
121 
122         return deliveries;
123     }
124 
125     public AnnouncementsDelivery getUserDelivery(long userId, String type)
126         throws PortalException, SystemException {
127 
128         AnnouncementsDelivery delivery =
129             announcementsDeliveryPersistence.fetchByU_T(userId, type);
130 
131         if (delivery == null) {
132             delivery = announcementsDeliveryLocalService.addUserDelivery(
133                 userId, type);
134         }
135 
136         return delivery;
137     }
138 
139     public AnnouncementsDelivery updateDelivery(
140             long userId, String type, boolean email, boolean sms,
141             boolean website)
142         throws PortalException, SystemException {
143 
144         AnnouncementsDelivery delivery = getUserDelivery(userId, type);
145 
146         delivery.setEmail(email);
147         delivery.setSms(sms);
148         delivery.setWebsite(website);
149 
150         announcementsDeliveryPersistence.update(delivery, false);
151 
152         return delivery;
153     }
154 
155     private static Log _log =
156         LogFactoryUtil.getLog(AnnouncementsDeliveryLocalServiceImpl.class);
157 
158 }