001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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 UserNotificationEvent updateUserNotificationEvent(
133                            String uuid, boolean archive)
134                    throws SystemException {
135    
136                    List<UserNotificationEvent> userNotificationEvents =
137                            userNotificationEventPersistence.findByUuid(uuid);
138    
139                    if (userNotificationEvents.isEmpty()) {
140                            return null;
141                    }
142    
143                    UserNotificationEvent userNotificationEvent =
144                            userNotificationEvents.get(0);
145    
146                    userNotificationEvent.setArchived(archive);
147    
148                    userNotificationEventPersistence.update(userNotificationEvent, false);
149    
150                    return userNotificationEvent;
151            }
152    
153            public List<UserNotificationEvent> updateUserNotificationEvents(
154                            Collection<String> uuids, boolean archive)
155                    throws SystemException {
156    
157                    List<UserNotificationEvent> userNotificationEvents =
158                            new ArrayList<UserNotificationEvent>();
159    
160                    for (String uuid : uuids) {
161                            userNotificationEvents.add(
162                                    updateUserNotificationEvent(uuid, archive));
163                    }
164    
165                    return userNotificationEvents;
166            }
167    
168    }