1
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
45 public class AnnouncementsDeliveryLocalServiceImpl
46 extends AnnouncementsDeliveryLocalServiceBaseImpl {
47
48 public AnnouncementsDelivery addUserDelivery(long userId, String type)
49 throws PortalException, SystemException {
50
51 User user = userPersistence.findByPrimaryKey(userId);
52
53 long deliveryId = counterLocalService.increment();
54
55 AnnouncementsDelivery delivery =
56 announcementsDeliveryPersistence.create(deliveryId);
57
58 delivery.setCompanyId(user.getCompanyId());
59 delivery.setUserId(user.getUserId());
60 delivery.setType(type);
61 delivery.setEmail(false);
62 delivery.setSms(false);
63 delivery.setWebsite(true);
64
65 try {
66 announcementsDeliveryPersistence.update(delivery, false);
67 }
68 catch (SystemException se) {
69 if (_log.isWarnEnabled()) {
70 _log.warn(
71 "Add failed, fetch {userId=" + userId + ", type=" +
72 type + "}");
73 }
74
75 delivery = announcementsDeliveryPersistence.fetchByU_T(
76 userId, type, false);
77
78 if (delivery == null) {
79 throw se;
80 }
81 }
82
83 return delivery;
84 }
85
86 public void deleteDeliveries(long userId) throws SystemException {
87 announcementsDeliveryPersistence.removeByUserId(userId);
88 }
89
90 public void deleteDelivery(long deliveryId)
91 throws PortalException, SystemException {
92
93 announcementsDeliveryPersistence.remove(deliveryId);
94 }
95
96 public void deleteDelivery(long userId, String type)
97 throws SystemException {
98
99 try {
100 announcementsDeliveryPersistence.removeByU_T(userId, type);
101 }
102 catch (NoSuchDeliveryException nsde) {
103 }
104 }
105
106 public AnnouncementsDelivery getDelivery(long deliveryId)
107 throws PortalException, SystemException {
108
109 return announcementsDeliveryPersistence.findByPrimaryKey(deliveryId);
110 }
111
112 public List<AnnouncementsDelivery> getUserDeliveries(long userId)
113 throws PortalException, SystemException {
114
115 List<AnnouncementsDelivery> deliveries =
116 new ArrayList<AnnouncementsDelivery>(
117 AnnouncementsEntryImpl.TYPES.length);
118
119 for (String type : AnnouncementsEntryImpl.TYPES) {
120 deliveries.add(getUserDelivery(userId, type));
121 }
122
123 return deliveries;
124 }
125
126 public AnnouncementsDelivery getUserDelivery(long userId, String type)
127 throws PortalException, SystemException {
128
129 AnnouncementsDelivery delivery =
130 announcementsDeliveryPersistence.fetchByU_T(userId, type);
131
132 if (delivery == null) {
133 delivery = announcementsDeliveryLocalService.addUserDelivery(
134 userId, type);
135 }
136
137 return delivery;
138 }
139
140 public AnnouncementsDelivery updateDelivery(
141 long userId, String type, boolean email, boolean sms,
142 boolean website)
143 throws PortalException, SystemException {
144
145 AnnouncementsDelivery delivery = getUserDelivery(userId, type);
146
147 delivery.setEmail(email);
148 delivery.setSms(sms);
149 delivery.setWebsite(website);
150
151 announcementsDeliveryPersistence.update(delivery, false);
152
153 return delivery;
154 }
155
156 private static Log _log =
157 LogFactoryUtil.getLog(AnnouncementsDeliveryLocalServiceImpl.class);
158
159 }