001
014
015 package com.liferay.portal.service.impl;
016
017 import com.liferay.portal.kernel.exception.PortalException;
018 import com.liferay.portal.kernel.json.JSONObject;
019 import com.liferay.portal.kernel.messaging.DestinationNames;
020 import com.liferay.portal.kernel.messaging.Message;
021 import com.liferay.portal.kernel.messaging.MessageBusUtil;
022 import com.liferay.portal.kernel.notifications.NotificationEvent;
023 import com.liferay.portal.kernel.notifications.NotificationEventFactoryUtil;
024 import com.liferay.portal.kernel.transaction.TransactionCommitCallbackUtil;
025 import com.liferay.portal.model.User;
026 import com.liferay.portal.model.UserNotificationDeliveryConstants;
027 import com.liferay.portal.model.UserNotificationEvent;
028 import com.liferay.portal.service.ServiceContext;
029 import com.liferay.portal.service.base.UserNotificationEventLocalServiceBaseImpl;
030
031 import java.util.ArrayList;
032 import java.util.Collection;
033 import java.util.List;
034 import java.util.concurrent.Callable;
035
036
040 public class UserNotificationEventLocalServiceImpl
041 extends UserNotificationEventLocalServiceBaseImpl {
042
043 @Override
044 public UserNotificationEvent addUserNotificationEvent(
045 long userId, boolean actionRequired,
046 NotificationEvent notificationEvent)
047 throws PortalException {
048
049 JSONObject payloadJSONObject = notificationEvent.getPayload();
050
051 ServiceContext serviceContext = new ServiceContext();
052
053 serviceContext.setUuid(notificationEvent.getUuid());
054
055 return addUserNotificationEvent(
056 userId, notificationEvent.getType(),
057 notificationEvent.getTimestamp(),
058 notificationEvent.getDeliveryType(),
059 notificationEvent.getDeliverBy(), payloadJSONObject.toString(),
060 actionRequired, notificationEvent.isArchived(), serviceContext);
061 }
062
063 @Override
064 public UserNotificationEvent addUserNotificationEvent(
065 long userId, NotificationEvent notificationEvent)
066 throws PortalException {
067
068 return addUserNotificationEvent(userId, false, notificationEvent);
069 }
070
071 @Override
072 public UserNotificationEvent addUserNotificationEvent(
073 long userId, String type, long timestamp, int deliveryType,
074 long deliverBy, String payload, boolean actionRequired,
075 boolean archived, ServiceContext serviceContext)
076 throws PortalException {
077
078 User user = userPersistence.findByPrimaryKey(userId);
079
080 long userNotificationEventId = counterLocalService.increment();
081
082 UserNotificationEvent userNotificationEvent =
083 userNotificationEventPersistence.create(userNotificationEventId);
084
085 userNotificationEvent.setUuid(serviceContext.getUuid());
086 userNotificationEvent.setCompanyId(user.getCompanyId());
087 userNotificationEvent.setUserId(userId);
088 userNotificationEvent.setType(type);
089 userNotificationEvent.setTimestamp(timestamp);
090 userNotificationEvent.setDeliveryType(deliveryType);
091 userNotificationEvent.setDeliverBy(deliverBy);
092 userNotificationEvent.setDelivered(false);
093 userNotificationEvent.setPayload(payload);
094 userNotificationEvent.setActionRequired(actionRequired);
095 userNotificationEvent.setArchived(archived);
096
097 userNotificationEventPersistence.update(userNotificationEvent);
098
099 return userNotificationEvent;
100 }
101
102 @Override
103 public UserNotificationEvent addUserNotificationEvent(
104 long userId, String type, long timestamp, int deliveryType,
105 long deliverBy, String payload, boolean archived,
106 ServiceContext serviceContext)
107 throws PortalException {
108
109 return addUserNotificationEvent(
110 userId, type, timestamp, deliveryType, deliverBy, payload, false,
111 archived, serviceContext);
112 }
113
114
118 @Deprecated
119 @Override
120 public UserNotificationEvent addUserNotificationEvent(
121 long userId, String type, long timestamp, long deliverBy,
122 String payload, boolean archived, ServiceContext serviceContext)
123 throws PortalException {
124
125 return addUserNotificationEvent(
126 userId, type, timestamp,
127 UserNotificationDeliveryConstants.TYPE_WEBSITE, deliverBy, payload,
128 archived, serviceContext);
129 }
130
131 @Override
132 public List<UserNotificationEvent> addUserNotificationEvents(
133 long userId, Collection<NotificationEvent> notificationEvents)
134 throws PortalException {
135
136 List<UserNotificationEvent> userNotificationEvents = new ArrayList<>(
137 notificationEvents.size());
138
139 for (NotificationEvent notificationEvent : notificationEvents) {
140 UserNotificationEvent userNotificationEvent =
141 addUserNotificationEvent(userId, notificationEvent);
142
143 userNotificationEvents.add(userNotificationEvent);
144 }
145
146 return userNotificationEvents;
147 }
148
149 @Override
150 public void deleteUserNotificationEvent(String uuid, long companyId) {
151 userNotificationEventPersistence.removeByUuid_C(uuid, companyId);
152 }
153
154 @Override
155 public void deleteUserNotificationEvents(
156 Collection<String> uuids, long companyId) {
157
158 for (String uuid : uuids) {
159 deleteUserNotificationEvent(uuid, companyId);
160 }
161 }
162
163 @Override
164 public List<UserNotificationEvent> getArchivedUserNotificationEvents(
165 long userId, boolean archived) {
166
167 return userNotificationEventPersistence.findByU_A(userId, archived);
168 }
169
170 @Override
171 public List<UserNotificationEvent> getArchivedUserNotificationEvents(
172 long userId, boolean actionRequired, boolean archived) {
173
174 return userNotificationEventPersistence.findByU_A_A(
175 userId, actionRequired, archived);
176 }
177
178 @Override
179 public List<UserNotificationEvent> getArchivedUserNotificationEvents(
180 long userId, boolean actionRequired, boolean archived, int start,
181 int end) {
182
183 return userNotificationEventPersistence.findByU_A_A(
184 userId, actionRequired, archived, start, end);
185 }
186
187 @Override
188 public List<UserNotificationEvent> getArchivedUserNotificationEvents(
189 long userId, boolean archived, int start, int end) {
190
191 return userNotificationEventPersistence.findByU_A(
192 userId, archived, start, end);
193 }
194
195 @Override
196 public List<UserNotificationEvent> getArchivedUserNotificationEvents(
197 long userId, int deliveryType, boolean archived) {
198
199 return userNotificationEventPersistence.findByU_DT_A(
200 userId, deliveryType, archived);
201 }
202
203 @Override
204 public List<UserNotificationEvent> getArchivedUserNotificationEvents(
205 long userId, int deliveryType, boolean actionRequired,
206 boolean archived) {
207
208 return userNotificationEventPersistence.findByU_DT_A_A(
209 userId, deliveryType, actionRequired, archived);
210 }
211
212 @Override
213 public List<UserNotificationEvent> getArchivedUserNotificationEvents(
214 long userId, int deliveryType, boolean actionRequired, boolean archived,
215 int start, int end) {
216
217 return userNotificationEventPersistence.findByU_DT_A_A(
218 userId, deliveryType, actionRequired, archived, start, end);
219 }
220
221 @Override
222 public List<UserNotificationEvent> getArchivedUserNotificationEvents(
223 long userId, int deliveryType, boolean archived, int start, int end) {
224
225 return userNotificationEventPersistence.findByU_DT_A(
226 userId, deliveryType, archived, start, end);
227 }
228
229 @Override
230 public int getArchivedUserNotificationEventsCount(
231 long userId, boolean archived) {
232
233 return userNotificationEventPersistence.countByU_A(userId, archived);
234 }
235
236 @Override
237 public int getArchivedUserNotificationEventsCount(
238 long userId, boolean actionRequired, boolean archived) {
239
240 return userNotificationEventPersistence.countByU_A_A(
241 userId, actionRequired, archived);
242 }
243
244 @Override
245 public int getArchivedUserNotificationEventsCount(
246 long userId, int deliveryType, boolean archived) {
247
248 return userNotificationEventPersistence.countByU_DT_A(
249 userId, deliveryType, archived);
250 }
251
252 @Override
253 public int getArchivedUserNotificationEventsCount(
254 long userId, int deliveryType, boolean actionRequired,
255 boolean archived) {
256
257 return userNotificationEventPersistence.countByU_DT_A_A(
258 userId, deliveryType, actionRequired, archived);
259 }
260
261 @Override
262 public List<UserNotificationEvent> getDeliveredUserNotificationEvents(
263 long userId, boolean delivered) {
264
265 return userNotificationEventPersistence.findByU_D(userId, delivered);
266 }
267
268 @Override
269 public List<UserNotificationEvent> getDeliveredUserNotificationEvents(
270 long userId, boolean delivered, boolean actionRequired) {
271
272 return userNotificationEventPersistence.findByU_D_A(
273 userId, delivered, actionRequired);
274 }
275
276 @Override
277 public List<UserNotificationEvent> getDeliveredUserNotificationEvents(
278 long userId, boolean delivered, boolean actionRequired, int start,
279 int end) {
280
281 return userNotificationEventPersistence.findByU_D_A(
282 userId, delivered, actionRequired, start, end);
283 }
284
285 @Override
286 public List<UserNotificationEvent> getDeliveredUserNotificationEvents(
287 long userId, boolean delivered, int start, int end) {
288
289 return userNotificationEventPersistence.findByU_D(
290 userId, delivered, start, end);
291 }
292
293 @Override
294 public List<UserNotificationEvent> getDeliveredUserNotificationEvents(
295 long userId, int deliveryType, boolean delivered) {
296
297 return userNotificationEventPersistence.findByU_DT_D(
298 userId, deliveryType, delivered);
299 }
300
301 @Override
302 public List<UserNotificationEvent> getDeliveredUserNotificationEvents(
303 long userId, int deliveryType, boolean delivered,
304 boolean actionRequired) {
305
306 return userNotificationEventPersistence.findByU_DT_D_A(
307 userId, deliveryType, delivered, actionRequired);
308 }
309
310 @Override
311 public List<UserNotificationEvent> getDeliveredUserNotificationEvents(
312 long userId, int deliveryType, boolean delivered,
313 boolean actionRequired, int start, int end) {
314
315 return userNotificationEventPersistence.findByU_DT_D_A(
316 userId, deliveryType, delivered, actionRequired, start, end);
317 }
318
319 @Override
320 public List<UserNotificationEvent> getDeliveredUserNotificationEvents(
321 long userId, int deliveryType, boolean delivered, int start, int end) {
322
323 return userNotificationEventPersistence.findByU_DT_D(
324 userId, deliveryType, delivered, start, end);
325 }
326
327 @Override
328 public int getDeliveredUserNotificationEventsCount(
329 long userId, boolean delivered) {
330
331 return userNotificationEventPersistence.countByU_D(userId, delivered);
332 }
333
334 @Override
335 public int getDeliveredUserNotificationEventsCount(
336 long userId, boolean delivered, boolean actionRequired) {
337
338 return userNotificationEventPersistence.countByU_D_A(
339 userId, delivered, actionRequired);
340 }
341
342 @Override
343 public int getDeliveredUserNotificationEventsCount(
344 long userId, int deliveryType, boolean delivered) {
345
346 return userNotificationEventPersistence.countByU_DT_D(
347 userId, deliveryType, delivered);
348 }
349
350 @Override
351 public int getDeliveredUserNotificationEventsCount(
352 long userId, int deliveryType, boolean delivered,
353 boolean actionRequired) {
354
355 return userNotificationEventPersistence.countByU_DT_D_A(
356 userId, deliveryType, delivered, actionRequired);
357 }
358
359 @Override
360 public List<UserNotificationEvent> getUserNotificationEvents(long userId) {
361 return userNotificationEventPersistence.findByUserId(userId);
362 }
363
364
368 @Deprecated
369 @Override
370 public List<UserNotificationEvent> getUserNotificationEvents(
371 long userId, boolean archived) {
372
373 return getArchivedUserNotificationEvents(userId, archived);
374 }
375
376
380 @Deprecated
381 @Override
382 public List<UserNotificationEvent> getUserNotificationEvents(
383 long userId, boolean archived, int start, int end) {
384
385 return getArchivedUserNotificationEvents(userId, archived, start, end);
386 }
387
388 @Override
389 public List<UserNotificationEvent> getUserNotificationEvents(
390 long userId, int deliveryType) {
391
392 return userNotificationEventPersistence.findByU_DT(
393 userId, deliveryType);
394 }
395
396 @Override
397 public List<UserNotificationEvent> getUserNotificationEvents(
398 long userId, int start, int end) {
399
400 return userNotificationEventPersistence.findByUserId(
401 userId, start, end);
402 }
403
404 @Override
405 public List<UserNotificationEvent> getUserNotificationEvents(
406 long userId, int deliveryType, int start, int end) {
407
408 return userNotificationEventPersistence.findByU_DT(
409 userId, deliveryType, start, end);
410 }
411
412 @Override
413 public int getUserNotificationEventsCount(long userId) {
414 return userNotificationEventPersistence.countByUserId(userId);
415 }
416
417
421 @Deprecated
422 @Override
423 public int getUserNotificationEventsCount(long userId, boolean archived) {
424 return getArchivedUserNotificationEventsCount(userId, archived);
425 }
426
427 @Override
428 public int getUserNotificationEventsCount(long userId, int deliveryType) {
429 return userNotificationEventPersistence.countByU_DT(
430 userId, deliveryType);
431 }
432
433 @Override
434 public UserNotificationEvent sendUserNotificationEvents(
435 long userId, String portletId, int deliveryType,
436 boolean actionRequired, JSONObject notificationEventJSONObject)
437 throws PortalException {
438
439 NotificationEvent notificationEvent =
440 NotificationEventFactoryUtil.createNotificationEvent(
441 System.currentTimeMillis(), portletId,
442 notificationEventJSONObject);
443
444 notificationEvent.setDeliveryType(deliveryType);
445
446 UserNotificationEvent userNotificationEvent = addUserNotificationEvent(
447 userId, actionRequired, notificationEvent);
448
449 if (deliveryType == UserNotificationDeliveryConstants.TYPE_PUSH) {
450 sendPushNotification(notificationEvent);
451 }
452
453 return userNotificationEvent;
454 }
455
456 @Override
457 public UserNotificationEvent sendUserNotificationEvents(
458 long userId, String portletId, int deliveryType,
459 JSONObject notificationEventJSONObject)
460 throws PortalException {
461
462 return sendUserNotificationEvents(
463 userId, portletId, deliveryType, false,
464 notificationEventJSONObject);
465 }
466
467 @Override
468 public UserNotificationEvent updateUserNotificationEvent(
469 String uuid, long companyId, boolean archive) {
470
471 List<UserNotificationEvent> userNotificationEvents =
472 userNotificationEventPersistence.findByUuid_C(uuid, companyId);
473
474 if (userNotificationEvents.isEmpty()) {
475 return null;
476 }
477
478 UserNotificationEvent userNotificationEvent =
479 userNotificationEvents.get(0);
480
481 userNotificationEvent.setArchived(archive);
482
483 userNotificationEventPersistence.update(userNotificationEvent);
484
485 return userNotificationEvent;
486 }
487
488 @Override
489 public List<UserNotificationEvent> updateUserNotificationEvents(
490 Collection<String> uuids, long companyId, boolean archive) {
491
492 List<UserNotificationEvent> userNotificationEvents = new ArrayList<>();
493
494 for (String uuid : uuids) {
495 userNotificationEvents.add(
496 updateUserNotificationEvent(uuid, companyId, archive));
497 }
498
499 return userNotificationEvents;
500 }
501
502 protected void sendPushNotification(
503 final NotificationEvent notificationEvent) {
504
505 TransactionCommitCallbackUtil.registerCallback(
506 new Callable<Void>() {
507
508 @Override
509 public Void call() throws Exception {
510 Message message = new Message();
511
512 message.setPayload(notificationEvent.getPayload());
513
514 MessageBusUtil.sendMessage(
515 DestinationNames.PUSH_NOTIFICATION, message);
516
517 return null;
518 }
519
520 });
521 }
522
523 }