001    /**
002     * Copyright (c) 2000-2013 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.exception.SystemException;
019    import com.liferay.portal.kernel.json.JSONObject;
020    import com.liferay.portal.kernel.notifications.NotificationEvent;
021    import com.liferay.portal.model.User;
022    import com.liferay.portal.model.UserNotificationEvent;
023    import com.liferay.portal.service.ServiceContext;
024    import com.liferay.portal.service.base.UserNotificationEventLocalServiceBaseImpl;
025    
026    import java.util.ArrayList;
027    import java.util.Collection;
028    import java.util.List;
029    
030    /**
031     * @author Edward Han
032     * @author Brian Wing Shun Chan
033     */
034    public class UserNotificationEventLocalServiceImpl
035            extends UserNotificationEventLocalServiceBaseImpl {
036    
037            @Override
038            public UserNotificationEvent addUserNotificationEvent(
039                            long userId, NotificationEvent notificationEvent)
040                    throws PortalException, SystemException {
041    
042                    JSONObject payloadJSONObject = notificationEvent.getPayload();
043    
044                    ServiceContext serviceContext = new ServiceContext();
045    
046                    serviceContext.setUuid(notificationEvent.getUuid());
047    
048                    return addUserNotificationEvent(
049                            userId, notificationEvent.getType(),
050                            notificationEvent.getTimestamp(), notificationEvent.getDeliverBy(),
051                            payloadJSONObject.toString(), notificationEvent.isArchived(),
052                            serviceContext);
053            }
054    
055            @Override
056            public UserNotificationEvent addUserNotificationEvent(
057                            long userId, String type, long timestamp, long deliverBy,
058                            String payload, boolean archived, ServiceContext serviceContext)
059                    throws PortalException, SystemException {
060    
061                    User user = userPersistence.findByPrimaryKey(userId);
062    
063                    long userNotificationEventId = counterLocalService.increment();
064    
065                    UserNotificationEvent userNotificationEvent =
066                            userNotificationEventPersistence.create(userNotificationEventId);
067    
068                    userNotificationEvent.setUuid(serviceContext.getUuid());
069                    userNotificationEvent.setCompanyId(user.getCompanyId());
070                    userNotificationEvent.setUserId(userId);
071                    userNotificationEvent.setType(type);
072                    userNotificationEvent.setTimestamp(timestamp);
073                    userNotificationEvent.setDeliverBy(deliverBy);
074                    userNotificationEvent.setDelivered(false);
075                    userNotificationEvent.setPayload(payload);
076                    userNotificationEvent.setArchived(archived);
077    
078                    userNotificationEventPersistence.update(userNotificationEvent);
079    
080                    return userNotificationEvent;
081            }
082    
083            @Override
084            public List<UserNotificationEvent> addUserNotificationEvents(
085                            long userId, Collection<NotificationEvent> notificationEvents)
086                    throws PortalException, SystemException {
087    
088                    List<UserNotificationEvent> userNotificationEvents =
089                            new ArrayList<UserNotificationEvent>(notificationEvents.size());
090    
091                    for (NotificationEvent notificationEvent : notificationEvents) {
092                            UserNotificationEvent userNotificationEvent =
093                                    addUserNotificationEvent(userId, notificationEvent);
094    
095                            userNotificationEvents.add(userNotificationEvent);
096                    }
097    
098                    return userNotificationEvents;
099            }
100    
101            @Override
102            public void deleteUserNotificationEvent(String uuid, long companyId)
103                    throws SystemException {
104    
105                    userNotificationEventPersistence.removeByUuid_C(uuid, companyId);
106            }
107    
108            @Override
109            public void deleteUserNotificationEvents(
110                            Collection<String> uuids, long companyId)
111                    throws SystemException {
112    
113                    for (String uuid : uuids) {
114                            deleteUserNotificationEvent(uuid, companyId);
115                    }
116            }
117    
118            @Override
119            public List<UserNotificationEvent> getArchivedUserNotificationEvents(
120                            long userId, boolean archived)
121                    throws SystemException {
122    
123                    return userNotificationEventPersistence.findByU_A(userId, archived);
124            }
125    
126            @Override
127            public List<UserNotificationEvent> getArchivedUserNotificationEvents(
128                            long userId, boolean archived, int start, int end)
129                    throws SystemException {
130    
131                    return userNotificationEventPersistence.findByU_A(
132                            userId, archived, start, end);
133            }
134    
135            @Override
136            public int getArchivedUserNotificationEventsCount(
137                            long userId, boolean archived)
138                    throws SystemException {
139    
140                    return userNotificationEventPersistence.countByU_A(userId, archived);
141            }
142    
143            @Override
144            public List<UserNotificationEvent> getDeliveredUserNotificationEvents(
145                            long userId, boolean delivered)
146                    throws SystemException {
147    
148                    return userNotificationEventPersistence.findByU_D(userId, delivered);
149            }
150    
151            @Override
152            public List<UserNotificationEvent> getDeliveredUserNotificationEvents(
153                            long userId, boolean delivered, int start, int end)
154                    throws SystemException {
155    
156                    return userNotificationEventPersistence.findByU_D(
157                            userId, delivered, start, end);
158            }
159    
160            @Override
161            public int getDeliveredUserNotificationEventsCount(
162                            long userId, boolean delivered)
163                    throws SystemException {
164    
165                    return userNotificationEventPersistence.countByU_D(userId, delivered);
166            }
167    
168            @Override
169            public List<UserNotificationEvent> getUserNotificationEvents(long userId)
170                    throws SystemException {
171    
172                    return userNotificationEventPersistence.findByUserId(userId);
173            }
174    
175            /**
176             * @deprecated As of 6.2.0 {@link #getArchivedUserNotificationEvents(long,
177             *             boolean)}
178             */
179            @Override
180            public List<UserNotificationEvent> getUserNotificationEvents(
181                            long userId, boolean archived)
182                    throws SystemException {
183    
184                    return getArchivedUserNotificationEvents(userId, archived);
185            }
186    
187            /**
188             * @deprecated As of 6.2.0 {@link #getArchivedUserNotificationEvents(long,
189             *             boolean, int, int)}
190             */
191            @Override
192            public List<UserNotificationEvent> getUserNotificationEvents(
193                            long userId, boolean archived, int start, int end)
194                    throws SystemException {
195    
196                    return getArchivedUserNotificationEvents(userId, archived, start, end);
197            }
198    
199            @Override
200            public List<UserNotificationEvent> getUserNotificationEvents(
201                            long userId, int start, int end)
202                    throws SystemException {
203    
204                    return userNotificationEventPersistence.findByUserId(
205                            userId, start, end);
206            }
207    
208            @Override
209            public int getUserNotificationEventsCount(long userId)
210                    throws SystemException {
211    
212                    return userNotificationEventPersistence.countByUserId(userId);
213            }
214    
215            /**
216             * @deprecated As of 6.2.0 {@link
217             *             #getArchivedUserNotificationEventsCount(long, boolean)}
218             */
219            @Override
220            public int getUserNotificationEventsCount(long userId, boolean archived)
221                    throws SystemException {
222    
223                    return getArchivedUserNotificationEventsCount(userId, archived);
224            }
225    
226            @Override
227            public UserNotificationEvent updateUserNotificationEvent(
228                            String uuid, long companyId, boolean archive)
229                    throws SystemException {
230    
231                    List<UserNotificationEvent> userNotificationEvents =
232                            userNotificationEventPersistence.findByUuid_C(uuid, companyId);
233    
234                    if (userNotificationEvents.isEmpty()) {
235                            return null;
236                    }
237    
238                    UserNotificationEvent userNotificationEvent =
239                            userNotificationEvents.get(0);
240    
241                    userNotificationEvent.setArchived(archive);
242    
243                    userNotificationEventPersistence.update(userNotificationEvent);
244    
245                    return userNotificationEvent;
246            }
247    
248            @Override
249            public List<UserNotificationEvent> updateUserNotificationEvents(
250                            Collection<String> uuids, long companyId, boolean archive)
251                    throws SystemException {
252    
253                    List<UserNotificationEvent> userNotificationEvents =
254                            new ArrayList<UserNotificationEvent>();
255    
256                    for (String uuid : uuids) {
257                            userNotificationEvents.add(
258                                    updateUserNotificationEvent(uuid, companyId, archive));
259                    }
260    
261                    return userNotificationEvents;
262            }
263    
264    }