001    /**
002     * Copyright (c) 2000-2012 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            public UserNotificationEvent addUserNotificationEvent(
038                            long userId, NotificationEvent notificationEvent)
039                    throws PortalException, SystemException {
040    
041                    JSONObject payloadJSONObject = notificationEvent.getPayload();
042    
043                    ServiceContext serviceContext = new ServiceContext();
044    
045                    serviceContext.setUuid(notificationEvent.getUuid());
046    
047                    return addUserNotificationEvent(
048                            userId, notificationEvent.getType(),
049                            notificationEvent.getTimestamp(), notificationEvent.getDeliverBy(),
050                            payloadJSONObject.toString(), notificationEvent.isArchived(),
051                            serviceContext);
052            }
053    
054            public UserNotificationEvent addUserNotificationEvent(
055                            long userId, String type, long timestamp, long deliverBy,
056                            String payload, boolean archived, ServiceContext serviceContext)
057                    throws PortalException, SystemException {
058    
059                    User user = userPersistence.findByPrimaryKey(userId);
060    
061                    long userNotificationEventId = counterLocalService.increment();
062    
063                    UserNotificationEvent userNotificationEvent =
064                            userNotificationEventPersistence.create(userNotificationEventId);
065    
066                    userNotificationEvent.setUuid(serviceContext.getUuid());
067                    userNotificationEvent.setCompanyId(user.getCompanyId());
068                    userNotificationEvent.setUserId(userId);
069                    userNotificationEvent.setType(type);
070                    userNotificationEvent.setTimestamp(timestamp);
071                    userNotificationEvent.setDeliverBy(deliverBy);
072                    userNotificationEvent.setPayload(payload);
073                    userNotificationEvent.setArchived(archived);
074    
075                    userNotificationEventPersistence.update(userNotificationEvent);
076    
077                    return userNotificationEvent;
078            }
079    
080            public List<UserNotificationEvent> addUserNotificationEvents(
081                            long userId, Collection<NotificationEvent> notificationEvents)
082                    throws PortalException, SystemException {
083    
084                    List<UserNotificationEvent> userNotificationEvents =
085                            new ArrayList<UserNotificationEvent>(notificationEvents.size());
086    
087                    for (NotificationEvent notificationEvent : notificationEvents) {
088                            UserNotificationEvent userNotificationEvent =
089                                    addUserNotificationEvent(userId, notificationEvent);
090    
091                            userNotificationEvents.add(userNotificationEvent);
092                    }
093    
094                    return userNotificationEvents;
095            }
096    
097            public void deleteUserNotificationEvent(String uuid, long companyId)
098                    throws SystemException {
099    
100                    userNotificationEventPersistence.removeByUuid_C(uuid, companyId);
101            }
102    
103            public void deleteUserNotificationEvents(
104                            Collection<String> uuids, long companyId)
105                    throws SystemException {
106    
107                    for (String uuid : uuids) {
108                            deleteUserNotificationEvent(uuid, companyId);
109                    }
110            }
111    
112            public List<UserNotificationEvent> getUserNotificationEvents(long userId)
113                    throws SystemException {
114    
115                    return userNotificationEventPersistence.findByUserId(userId);
116            }
117    
118            public List<UserNotificationEvent> getUserNotificationEvents(
119                            long userId, boolean archived)
120                    throws SystemException {
121    
122                    return userNotificationEventPersistence.findByU_A(userId, archived);
123            }
124    
125            public List<UserNotificationEvent> getUserNotificationEvents(
126                            long userId, boolean archived, int start, int end)
127                    throws SystemException {
128    
129                    return userNotificationEventPersistence.findByU_A(
130                            userId, archived, start, end);
131            }
132    
133            public List<UserNotificationEvent> getUserNotificationEvents(
134                            long userId, int start, int end)
135                    throws SystemException {
136    
137                    return userNotificationEventPersistence.findByUserId(
138                            userId, start, end);
139            }
140    
141            public int getUserNotificationEventsCount(long userId)
142                    throws SystemException {
143    
144                    return userNotificationEventPersistence.countByUserId(userId);
145            }
146    
147            public int getUserNotificationEventsCount(long userId, boolean archived)
148                    throws SystemException {
149    
150                    return userNotificationEventPersistence.countByU_A(userId, archived);
151            }
152    
153            public UserNotificationEvent updateUserNotificationEvent(
154                            String uuid, long companyId, boolean archive)
155                    throws SystemException {
156    
157                    List<UserNotificationEvent> userNotificationEvents =
158                            userNotificationEventPersistence.findByUuid_C(uuid, companyId);
159    
160                    if (userNotificationEvents.isEmpty()) {
161                            return null;
162                    }
163    
164                    UserNotificationEvent userNotificationEvent =
165                            userNotificationEvents.get(0);
166    
167                    userNotificationEvent.setArchived(archive);
168    
169                    userNotificationEventPersistence.update(userNotificationEvent);
170    
171                    return userNotificationEvent;
172            }
173    
174            public List<UserNotificationEvent> updateUserNotificationEvents(
175                            Collection<String> uuids, long companyId, boolean archive)
176                    throws SystemException {
177    
178                    List<UserNotificationEvent> userNotificationEvents =
179                            new ArrayList<UserNotificationEvent>();
180    
181                    for (String uuid : uuids) {
182                            userNotificationEvents.add(
183                                    updateUserNotificationEvent(uuid, companyId, archive));
184                    }
185    
186                    return userNotificationEvents;
187            }
188    
189    }