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.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    /**
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(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            /**
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 = 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            @Override
370            public List<UserNotificationEvent> getUserNotificationEvents(
371                    long userId, int deliveryType) {
372    
373                    return userNotificationEventPersistence.findByU_DT(
374                            userId, deliveryType);
375            }
376    
377            @Override
378            public List<UserNotificationEvent> getUserNotificationEvents(
379                    long userId, int start, int end) {
380    
381                    return userNotificationEventPersistence.findByUserId(
382                            userId, start, end);
383            }
384    
385            @Override
386            public List<UserNotificationEvent> getUserNotificationEvents(
387                    long userId, int deliveryType, int start, int end) {
388    
389                    return userNotificationEventPersistence.findByU_DT(
390                            userId, deliveryType, start, end);
391            }
392    
393            @Override
394            public int getUserNotificationEventsCount(long userId) {
395                    return userNotificationEventPersistence.countByUserId(userId);
396            }
397    
398            @Override
399            public int getUserNotificationEventsCount(long userId, int deliveryType) {
400                    return userNotificationEventPersistence.countByU_DT(
401                            userId, deliveryType);
402            }
403    
404            @Override
405            public int getUserNotificationEventsCount(
406                    long userId, String type, int deliveryType, boolean archived) {
407    
408                    return userNotificationEventPersistence.countByU_T_DT_D(
409                            userId, type, deliveryType, archived);
410            }
411    
412            @Override
413            public UserNotificationEvent sendUserNotificationEvents(
414                            long userId, String portletId, int deliveryType,
415                            boolean actionRequired, JSONObject notificationEventJSONObject)
416                    throws PortalException {
417    
418                    NotificationEvent notificationEvent =
419                            NotificationEventFactoryUtil.createNotificationEvent(
420                                    System.currentTimeMillis(), portletId,
421                                    notificationEventJSONObject);
422    
423                    notificationEvent.setDeliveryType(deliveryType);
424    
425                    UserNotificationEvent userNotificationEvent = addUserNotificationEvent(
426                            userId, actionRequired, notificationEvent);
427    
428                    if (deliveryType == UserNotificationDeliveryConstants.TYPE_PUSH) {
429                            sendPushNotification(notificationEvent);
430                    }
431    
432                    return userNotificationEvent;
433            }
434    
435            @Override
436            public UserNotificationEvent sendUserNotificationEvents(
437                            long userId, String portletId, int deliveryType,
438                            JSONObject notificationEventJSONObject)
439                    throws PortalException {
440    
441                    return sendUserNotificationEvents(
442                            userId, portletId, deliveryType, false,
443                            notificationEventJSONObject);
444            }
445    
446            @Override
447            public UserNotificationEvent updateUserNotificationEvent(
448                    String uuid, long companyId, boolean archive) {
449    
450                    List<UserNotificationEvent> userNotificationEvents =
451                            userNotificationEventPersistence.findByUuid_C(uuid, companyId);
452    
453                    if (userNotificationEvents.isEmpty()) {
454                            return null;
455                    }
456    
457                    UserNotificationEvent userNotificationEvent =
458                            userNotificationEvents.get(0);
459    
460                    userNotificationEvent.setArchived(archive);
461    
462                    userNotificationEventPersistence.update(userNotificationEvent);
463    
464                    return userNotificationEvent;
465            }
466    
467            @Override
468            public List<UserNotificationEvent> updateUserNotificationEvents(
469                    Collection<String> uuids, long companyId, boolean archive) {
470    
471                    List<UserNotificationEvent> userNotificationEvents = new ArrayList<>();
472    
473                    for (String uuid : uuids) {
474                            userNotificationEvents.add(
475                                    updateUserNotificationEvent(uuid, companyId, archive));
476                    }
477    
478                    return userNotificationEvents;
479            }
480    
481            protected void sendPushNotification(
482                    final NotificationEvent notificationEvent) {
483    
484                    TransactionCommitCallbackUtil.registerCallback(
485                            new Callable<Void>() {
486    
487                                    @Override
488                                    public Void call() throws Exception {
489                                            Message message = new Message();
490    
491                                            message.setPayload(notificationEvent.getPayload());
492    
493                                            MessageBusUtil.sendMessage(
494                                                    DestinationNames.PUSH_NOTIFICATION, message);
495    
496                                            return null;
497                                    }
498    
499                            });
500            }
501    
502    }