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.model.User;
28  import com.liferay.portlet.announcements.NoSuchDeliveryException;
29  import com.liferay.portlet.announcements.model.AnnouncementsDelivery;
30  import com.liferay.portlet.announcements.model.impl.AnnouncementsEntryImpl;
31  import com.liferay.portlet.announcements.service.base.AnnouncementsDeliveryLocalServiceBaseImpl;
32  
33  import java.util.ArrayList;
34  import java.util.List;
35  
36  /**
37   * <a href="AnnouncementsDeliveryLocalServiceImpl.java.html"><b><i>View Source
38   * </i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   *
42   */
43  public class AnnouncementsDeliveryLocalServiceImpl
44      extends AnnouncementsDeliveryLocalServiceBaseImpl {
45  
46      public void deleteDeliveries(long userId) throws SystemException {
47          announcementsDeliveryPersistence.removeByUserId(userId);
48      }
49  
50      public void deleteDelivery(long deliveryId)
51          throws PortalException, SystemException {
52  
53          announcementsDeliveryPersistence.remove(deliveryId);
54      }
55  
56      public void deleteDelivery(long userId, String type)
57          throws SystemException {
58  
59          try {
60              announcementsDeliveryPersistence.removeByU_T(userId, type);
61          }
62          catch (NoSuchDeliveryException nsde) {
63          }
64      }
65  
66      public AnnouncementsDelivery getDelivery(long deliveryId)
67          throws PortalException, SystemException {
68  
69          return announcementsDeliveryPersistence.findByPrimaryKey(deliveryId);
70      }
71  
72      public List<AnnouncementsDelivery> getUserDeliveries(long userId)
73          throws PortalException, SystemException {
74  
75          List<AnnouncementsDelivery> deliveries =
76              new ArrayList<AnnouncementsDelivery>(
77                  AnnouncementsEntryImpl.TYPES.length);
78  
79          for (String type : AnnouncementsEntryImpl.TYPES) {
80              deliveries.add(getUserDelivery(userId, type));
81          }
82  
83          return deliveries;
84      }
85  
86      public AnnouncementsDelivery getUserDelivery(long userId, String type)
87          throws PortalException, SystemException {
88  
89          AnnouncementsDelivery delivery =
90              announcementsDeliveryPersistence.fetchByU_T(userId, type);
91  
92          if (delivery == null) {
93              User user = userPersistence.findByPrimaryKey(userId);
94  
95              long deliveryId = counterLocalService.increment();
96  
97              delivery = announcementsDeliveryPersistence.create(deliveryId);
98  
99              delivery.setCompanyId(user.getCompanyId());
100             delivery.setUserId(user.getUserId());
101             delivery.setType(type);
102             delivery.setEmail(false);
103             delivery.setSms(false);
104             delivery.setWebsite(true);
105 
106             announcementsDeliveryPersistence.update(delivery, false);
107         }
108 
109         return delivery;
110     }
111 
112     public AnnouncementsDelivery updateDelivery(
113             long userId, String type, boolean email, boolean sms,
114             boolean website)
115         throws PortalException, SystemException {
116 
117         AnnouncementsDelivery delivery = getUserDelivery(userId, type);
118 
119         delivery.setEmail(email);
120         delivery.setSms(sms);
121         delivery.setWebsite(website);
122 
123         announcementsDeliveryPersistence.update(delivery, false);
124 
125         return delivery;
126     }
127 
128 }