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.setPayload(payload);
075                    userNotificationEvent.setArchived(archived);
076    
077                    userNotificationEventPersistence.update(userNotificationEvent, false);
078    
079                    return userNotificationEvent;
080            }
081    
082            @Override
083            public List<UserNotificationEvent> addUserNotificationEvents(
084                            long userId, Collection<NotificationEvent> notificationEvents)
085                    throws PortalException, SystemException {
086    
087                    List<UserNotificationEvent> userNotificationEvents =
088                            new ArrayList<UserNotificationEvent>(notificationEvents.size());
089    
090                    for (NotificationEvent notificationEvent : notificationEvents) {
091                            UserNotificationEvent userNotificationEvent =
092                                    addUserNotificationEvent(userId, notificationEvent);
093    
094                            userNotificationEvents.add(userNotificationEvent);
095                    }
096    
097                    return userNotificationEvents;
098            }
099    
100            @Override
101            public void deleteUserNotificationEvent(String uuid)
102                    throws SystemException {
103    
104                    userNotificationEventPersistence.removeByUuid(uuid);
105            }
106    
107            @Override
108            public void deleteUserNotificationEvents(Collection<String> uuids)
109                    throws SystemException {
110    
111                    for (String uuid : uuids) {
112                            deleteUserNotificationEvent(uuid);
113                    }
114            }
115    
116            @Override
117            public List<UserNotificationEvent> getUserNotificationEvents(long userId)
118                    throws SystemException {
119    
120                    return userNotificationEventPersistence.findByUserId(userId);
121            }
122    
123            @Override
124            public List<UserNotificationEvent> getUserNotificationEvents(
125                            long userId, boolean archived)
126                    throws SystemException {
127    
128                    return userNotificationEventPersistence.findByU_A(userId, archived);
129            }
130    
131            @Override
132            public List<UserNotificationEvent> getUserNotificationEvents(
133                            long userId, boolean archived, int start, int end)
134                    throws SystemException {
135    
136                    return userNotificationEventPersistence.findByU_A(
137                            userId, archived, start, end);
138            }
139    
140            @Override
141            public List<UserNotificationEvent> getUserNotificationEvents(
142                            long userId, int start, int end)
143                    throws SystemException {
144    
145                    return userNotificationEventPersistence.findByUserId(
146                            userId, start, end);
147            }
148    
149            @Override
150            public int getUserNotificationEventsCount(long userId)
151                    throws SystemException {
152    
153                    return userNotificationEventPersistence.countByUserId(userId);
154            }
155    
156            @Override
157            public int getUserNotificationEventsCount(long userId, boolean archived)
158                    throws SystemException {
159    
160                    return userNotificationEventPersistence.countByU_A(userId, archived);
161            }
162    
163            @Override
164            public UserNotificationEvent updateUserNotificationEvent(
165                            String uuid, boolean archive)
166                    throws SystemException {
167    
168                    List<UserNotificationEvent> userNotificationEvents =
169                            userNotificationEventPersistence.findByUuid(uuid);
170    
171                    if (userNotificationEvents.isEmpty()) {
172                            return null;
173                    }
174    
175                    UserNotificationEvent userNotificationEvent =
176                            userNotificationEvents.get(0);
177    
178                    userNotificationEvent.setArchived(archive);
179    
180                    userNotificationEventPersistence.update(userNotificationEvent, false);
181    
182                    return userNotificationEvent;
183            }
184    
185            @Override
186            public List<UserNotificationEvent> updateUserNotificationEvents(
187                            Collection<String> uuids, boolean archive)
188                    throws SystemException {
189    
190                    List<UserNotificationEvent> userNotificationEvents =
191                            new ArrayList<UserNotificationEvent>();
192    
193                    for (String uuid : uuids) {
194                            userNotificationEvents.add(
195                                    updateUserNotificationEvent(uuid, archive));
196                    }
197    
198                    return userNotificationEvents;
199            }
200    
201    }