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, false);
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)
098                    throws SystemException {
099    
100                    userNotificationEventPersistence.removeByUuid(uuid);
101            }
102    
103            public void deleteUserNotificationEvents(Collection<String> uuids)
104                    throws SystemException {
105    
106                    for (String uuid : uuids) {
107                            deleteUserNotificationEvent(uuid);
108                    }
109            }
110    
111            public List<UserNotificationEvent> getUserNotificationEvents(long userId)
112                    throws SystemException {
113    
114                    return userNotificationEventPersistence.findByUserId(userId);
115            }
116    
117            public List<UserNotificationEvent> getUserNotificationEvents(
118                            long userId, boolean archived)
119                    throws SystemException {
120    
121                    return userNotificationEventPersistence.findByU_A(userId, archived);
122            }
123    
124            public List<UserNotificationEvent> getUserNotificationEvents(
125                            long userId, boolean archived, int start, int end)
126                    throws SystemException {
127    
128                    return userNotificationEventPersistence.findByU_A(
129                            userId, archived, start, end);
130            }
131    
132            public List<UserNotificationEvent> getUserNotificationEvents(
133                            long userId, int start, int end)
134                    throws SystemException {
135    
136                    return userNotificationEventPersistence.findByUserId(
137                            userId, start, end);
138            }
139    
140            public int getUserNotificationEventsCount(long userId)
141                    throws SystemException {
142    
143                    return userNotificationEventPersistence.countByUserId(userId);
144            }
145    
146            public int getUserNotificationEventsCount(long userId, boolean archived)
147                    throws SystemException {
148    
149                    return userNotificationEventPersistence.countByU_A(userId, archived);
150            }
151    
152            public UserNotificationEvent updateUserNotificationEvent(
153                            String uuid, boolean archive)
154                    throws SystemException {
155    
156                    List<UserNotificationEvent> userNotificationEvents =
157                            userNotificationEventPersistence.findByUuid(uuid);
158    
159                    if (userNotificationEvents.isEmpty()) {
160                            return null;
161                    }
162    
163                    UserNotificationEvent userNotificationEvent =
164                            userNotificationEvents.get(0);
165    
166                    userNotificationEvent.setArchived(archive);
167    
168                    userNotificationEventPersistence.update(userNotificationEvent, false);
169    
170                    return userNotificationEvent;
171            }
172    
173            public List<UserNotificationEvent> updateUserNotificationEvents(
174                            Collection<String> uuids, boolean archive)
175                    throws SystemException {
176    
177                    List<UserNotificationEvent> userNotificationEvents =
178                            new ArrayList<UserNotificationEvent>();
179    
180                    for (String uuid : uuids) {
181                            userNotificationEvents.add(
182                                    updateUserNotificationEvent(uuid, archive));
183                    }
184    
185                    return userNotificationEvents;
186            }
187    
188    }