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