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