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