1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portlet.announcements.service.impl;
16  
17  import com.liferay.portal.PortalException;
18  import com.liferay.portal.SystemException;
19  import com.liferay.portal.kernel.log.Log;
20  import com.liferay.portal.kernel.log.LogFactoryUtil;
21  import com.liferay.portal.model.User;
22  import com.liferay.portlet.announcements.NoSuchDeliveryException;
23  import com.liferay.portlet.announcements.model.AnnouncementsDelivery;
24  import com.liferay.portlet.announcements.model.impl.AnnouncementsEntryImpl;
25  import com.liferay.portlet.announcements.service.base.AnnouncementsDeliveryLocalServiceBaseImpl;
26  
27  import java.util.ArrayList;
28  import java.util.List;
29  
30  /**
31   * <a href="AnnouncementsDeliveryLocalServiceImpl.java.html"><b><i>View Source
32   * </i></b></a>
33   *
34   * @author Brian Wing Shun Chan
35   */
36  public class AnnouncementsDeliveryLocalServiceImpl
37      extends AnnouncementsDeliveryLocalServiceBaseImpl {
38  
39      public AnnouncementsDelivery addUserDelivery(long userId, String type)
40          throws PortalException, SystemException {
41  
42          User user = userPersistence.findByPrimaryKey(userId);
43  
44          long deliveryId = counterLocalService.increment();
45  
46          AnnouncementsDelivery delivery =
47              announcementsDeliveryPersistence.create(deliveryId);
48  
49          delivery.setCompanyId(user.getCompanyId());
50          delivery.setUserId(user.getUserId());
51          delivery.setType(type);
52          delivery.setEmail(false);
53          delivery.setSms(false);
54          delivery.setWebsite(true);
55  
56          try {
57              announcementsDeliveryPersistence.update(delivery, false);
58          }
59          catch (SystemException se) {
60              if (_log.isWarnEnabled()) {
61                  _log.warn(
62                      "Add failed, fetch {userId=" + userId + ", type=" +
63                          type + "}");
64              }
65  
66              delivery = announcementsDeliveryPersistence.fetchByU_T(
67                  userId, type, false);
68  
69              if (delivery == null) {
70                  throw se;
71              }
72          }
73  
74          return delivery;
75      }
76  
77      public void deleteDeliveries(long userId) throws SystemException {
78          announcementsDeliveryPersistence.removeByUserId(userId);
79      }
80  
81      public void deleteDelivery(long deliveryId)
82          throws PortalException, SystemException {
83  
84          announcementsDeliveryPersistence.remove(deliveryId);
85      }
86  
87      public void deleteDelivery(long userId, String type)
88          throws SystemException {
89  
90          try {
91              announcementsDeliveryPersistence.removeByU_T(userId, type);
92          }
93          catch (NoSuchDeliveryException nsde) {
94          }
95      }
96  
97      public AnnouncementsDelivery getDelivery(long deliveryId)
98          throws PortalException, SystemException {
99  
100         return announcementsDeliveryPersistence.findByPrimaryKey(deliveryId);
101     }
102 
103     public List<AnnouncementsDelivery> getUserDeliveries(long userId)
104         throws PortalException, SystemException {
105 
106         List<AnnouncementsDelivery> deliveries =
107             new ArrayList<AnnouncementsDelivery>(
108                 AnnouncementsEntryImpl.TYPES.length);
109 
110         for (String type : AnnouncementsEntryImpl.TYPES) {
111             deliveries.add(getUserDelivery(userId, type));
112         }
113 
114         return deliveries;
115     }
116 
117     public AnnouncementsDelivery getUserDelivery(long userId, String type)
118         throws PortalException, SystemException {
119 
120         AnnouncementsDelivery delivery =
121             announcementsDeliveryPersistence.fetchByU_T(userId, type);
122 
123         if (delivery == null) {
124             delivery = announcementsDeliveryLocalService.addUserDelivery(
125                 userId, type);
126         }
127 
128         return delivery;
129     }
130 
131     public AnnouncementsDelivery updateDelivery(
132             long userId, String type, boolean email, boolean sms,
133             boolean website)
134         throws PortalException, SystemException {
135 
136         AnnouncementsDelivery delivery = getUserDelivery(userId, type);
137 
138         delivery.setEmail(email);
139         delivery.setSms(sms);
140         delivery.setWebsite(website);
141 
142         announcementsDeliveryPersistence.update(delivery, false);
143 
144         return delivery;
145     }
146 
147     private static Log _log = LogFactoryUtil.getLog(
148         AnnouncementsDeliveryLocalServiceImpl.class);
149 
150 }