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(true);
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> getTypeNotificationEvents(String type) {
361 return userNotificationEventPersistence.findByType(type);
362 }
363
364 @Override
365 public List<UserNotificationEvent> getUserNotificationEvents(long userId) {
366 return userNotificationEventPersistence.findByUserId(userId);
367 }
368
369
373 @Deprecated
374 @Override
375 public List<UserNotificationEvent> getUserNotificationEvents(
376 long userId, boolean archived) {
377
378 return getArchivedUserNotificationEvents(userId, archived);
379 }
380
381
385 @Deprecated
386 @Override
387 public List<UserNotificationEvent> getUserNotificationEvents(
388 long userId, boolean archived, int start, int end) {
389
390 return getArchivedUserNotificationEvents(userId, archived, start, end);
391 }
392
393 @Override
394 public List<UserNotificationEvent> getUserNotificationEvents(
395 long userId, int deliveryType) {
396
397 return userNotificationEventPersistence.findByU_DT(
398 userId, deliveryType);
399 }
400
401 @Override
402 public List<UserNotificationEvent> getUserNotificationEvents(
403 long userId, int start, int end) {
404
405 return userNotificationEventPersistence.findByUserId(
406 userId, start, end);
407 }
408
409 @Override
410 public List<UserNotificationEvent> getUserNotificationEvents(
411 long userId, int deliveryType, int start, int end) {
412
413 return userNotificationEventPersistence.findByU_DT(
414 userId, deliveryType, start, end);
415 }
416
417 @Override
418 public int getUserNotificationEventsCount(long userId) {
419 return userNotificationEventPersistence.countByUserId(userId);
420 }
421
422
426 @Deprecated
427 @Override
428 public int getUserNotificationEventsCount(long userId, boolean archived) {
429 return getArchivedUserNotificationEventsCount(userId, archived);
430 }
431
432 @Override
433 public int getUserNotificationEventsCount(long userId, int deliveryType) {
434 return userNotificationEventPersistence.countByU_DT(
435 userId, deliveryType);
436 }
437
438 @Override
439 public int getUserNotificationEventsCount(
440 long userId, String type, int deliveryType, boolean archived) {
441
442 return userNotificationEventPersistence.countByU_T_DT_D(
443 userId, type, deliveryType, archived);
444 }
445
446 @Override
447 public UserNotificationEvent sendUserNotificationEvents(
448 long userId, String portletId, int deliveryType,
449 boolean actionRequired, JSONObject notificationEventJSONObject)
450 throws PortalException {
451
452 NotificationEvent notificationEvent =
453 NotificationEventFactoryUtil.createNotificationEvent(
454 System.currentTimeMillis(), portletId,
455 notificationEventJSONObject);
456
457 notificationEvent.setDeliveryType(deliveryType);
458
459 UserNotificationEvent userNotificationEvent = addUserNotificationEvent(
460 userId, actionRequired, notificationEvent);
461
462 if (deliveryType == UserNotificationDeliveryConstants.TYPE_PUSH) {
463 sendPushNotification(notificationEvent);
464 }
465
466 return userNotificationEvent;
467 }
468
469 @Override
470 public UserNotificationEvent sendUserNotificationEvents(
471 long userId, String portletId, int deliveryType,
472 JSONObject notificationEventJSONObject)
473 throws PortalException {
474
475 return sendUserNotificationEvents(
476 userId, portletId, deliveryType, false,
477 notificationEventJSONObject);
478 }
479
480 @Override
481 public UserNotificationEvent updateUserNotificationEvent(
482 String uuid, long companyId, boolean archive) {
483
484 List<UserNotificationEvent> userNotificationEvents =
485 userNotificationEventPersistence.findByUuid_C(uuid, companyId);
486
487 if (userNotificationEvents.isEmpty()) {
488 return null;
489 }
490
491 UserNotificationEvent userNotificationEvent =
492 userNotificationEvents.get(0);
493
494 userNotificationEvent.setArchived(archive);
495
496 userNotificationEventPersistence.update(userNotificationEvent);
497
498 return userNotificationEvent;
499 }
500
501 @Override
502 public List<UserNotificationEvent> updateUserNotificationEvents(
503 Collection<String> uuids, long companyId, boolean archive) {
504
505 List<UserNotificationEvent> userNotificationEvents = new ArrayList<>();
506
507 for (String uuid : uuids) {
508 userNotificationEvents.add(
509 updateUserNotificationEvent(uuid, companyId, archive));
510 }
511
512 return userNotificationEvents;
513 }
514
515 protected void sendPushNotification(
516 final NotificationEvent notificationEvent) {
517
518 TransactionCommitCallbackUtil.registerCallback(
519 new Callable<Void>() {
520
521 @Override
522 public Void call() throws Exception {
523 Message message = new Message();
524
525 message.setPayload(notificationEvent.getPayload());
526
527 MessageBusUtil.sendMessage(
528 DestinationNames.PUSH_NOTIFICATION, message);
529
530 return null;
531 }
532
533 });
534 }
535
536 }