001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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.TransactionCommitCallbackRegistryUtil;
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    /**
037     * @author Edward Han
038     * @author Brian Wing Shun Chan
039     */
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            /**
115             * @deprecated As of 7.0.0 {@link #addUserNotificationEvent(long, String,
116             *             long, int, long, String, boolean, ServiceContext)}
117             */
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 =
137                            new ArrayList<UserNotificationEvent>(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            /**
365             * @deprecated As of 6.2.0 {@link #getArchivedUserNotificationEvents(long,
366             *             boolean)}
367             */
368            @Deprecated
369            @Override
370            public List<UserNotificationEvent> getUserNotificationEvents(
371                    long userId, boolean archived) {
372    
373                    return getArchivedUserNotificationEvents(userId, archived);
374            }
375    
376            /**
377             * @deprecated As of 6.2.0 {@link #getArchivedUserNotificationEvents(long,
378             *             boolean, int, int)}
379             */
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            /**
418             * @deprecated As of 6.2.0 {@link
419             *             #getArchivedUserNotificationEventsCount(long, boolean)}
420             */
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 =
493                            new ArrayList<UserNotificationEvent>();
494    
495                    for (String uuid : uuids) {
496                            userNotificationEvents.add(
497                                    updateUserNotificationEvent(uuid, companyId, archive));
498                    }
499    
500                    return userNotificationEvents;
501            }
502    
503            protected void sendPushNotification(
504                    final NotificationEvent notificationEvent) {
505    
506                    TransactionCommitCallbackRegistryUtil.registerCallback(
507                            new Callable<Void>() {
508    
509                                    @Override
510                                    public Void call() throws Exception {
511                                            Message message = new Message();
512    
513                                            message.setPayload(notificationEvent.getPayload());
514    
515                                            MessageBusUtil.sendMessage(
516                                                    DestinationNames.PUSH_NOTIFICATION, message);
517    
518                                            return null;
519                                    }
520    
521                            });
522            }
523    
524    }