001    /**
002     * Copyright (c) 2000-present 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.json.JSONFactoryUtil;
019    import com.liferay.portal.kernel.json.JSONObject;
020    import com.liferay.portal.kernel.social.SocialActivityManagerUtil;
021    import com.liferay.portal.kernel.util.OrderByComparator;
022    import com.liferay.portal.model.Subscription;
023    import com.liferay.portal.model.SubscriptionConstants;
024    import com.liferay.portal.model.User;
025    import com.liferay.portal.service.base.SubscriptionLocalServiceBaseImpl;
026    import com.liferay.portlet.asset.model.AssetEntry;
027    import com.liferay.portlet.social.model.SocialActivityConstants;
028    
029    import java.util.List;
030    
031    /**
032     * Provides the local service for accessing, adding, and deleting notification
033     * subscriptions to entities. It handles subscriptions to entities found in many
034     * different places in the portal, including message boards, blogs, and
035     * documents and media.
036     *
037     * @author Charles May
038     * @author Zsolt Berentey
039     */
040    public class SubscriptionLocalServiceImpl
041            extends SubscriptionLocalServiceBaseImpl {
042    
043            /**
044             * Subscribes the user to the entity, notifying him the instant the entity
045             * is created, deleted, or modified.
046             *
047             * <p>
048             * If there is no asset entry with the class name and class PK a new asset
049             * entry is created.
050             * </p>
051             *
052             * <p>
053             * A social activity for the subscription is created using the asset entry
054             * associated with the class name and class PK, or the newly created asset
055             * entry.
056             * </p>
057             *
058             * @param  userId the primary key of the user
059             * @param  groupId the primary key of the entity's group
060             * @param  className the entity's class name
061             * @param  classPK the primary key of the entity's instance
062             * @return the subscription
063             * @throws PortalException if a matching user or group could not be found
064             */
065            @Override
066            public Subscription addSubscription(
067                            long userId, long groupId, String className, long classPK)
068                    throws PortalException {
069    
070                    return addSubscription(
071                            userId, groupId, className, classPK,
072                            SubscriptionConstants.FREQUENCY_INSTANT);
073            }
074    
075            /**
076             * Subscribes the user to the entity, notifying him at the given frequency.
077             *
078             * <p>
079             * If there is no asset entry with the class name and class PK a new asset
080             * entry is created.
081             * </p>
082             *
083             * <p>
084             * A social activity for the subscription is created using the asset entry
085             * associated with the class name and class PK, or the newly created asset
086             * entry.
087             * </p>
088             *
089             * @param  userId the primary key of the user
090             * @param  groupId the primary key of the entity's group
091             * @param  className the entity's class name
092             * @param  classPK the primary key of the entity's instance
093             * @param  frequency the frequency for notifications
094             * @return the subscription
095             * @throws PortalException if a matching user or group could not be found
096             */
097            @Override
098            public Subscription addSubscription(
099                            long userId, long groupId, String className, long classPK,
100                            String frequency)
101                    throws PortalException {
102    
103                    // Subscription
104    
105                    User user = userPersistence.findByPrimaryKey(userId);
106                    long classNameId = classNameLocalService.getClassNameId(className);
107    
108                    Subscription subscription = subscriptionPersistence.fetchByC_U_C_C(
109                            user.getCompanyId(), userId, classNameId, classPK);
110    
111                    if (subscription == null) {
112                            long subscriptionId = counterLocalService.increment();
113    
114                            subscription = subscriptionPersistence.create(subscriptionId);
115    
116                            subscription.setGroupId(groupId);
117                            subscription.setCompanyId(user.getCompanyId());
118                            subscription.setUserId(user.getUserId());
119                            subscription.setUserName(user.getFullName());
120                            subscription.setClassNameId(classNameId);
121                            subscription.setClassPK(classPK);
122                            subscription.setFrequency(frequency);
123    
124                            subscriptionPersistence.update(subscription);
125                    }
126    
127                    if (groupId > 0) {
128    
129                            // Asset
130    
131                            AssetEntry assetEntry = null;
132    
133                            try {
134                                    assetEntry = assetEntryLocalService.getEntry(
135                                            className, classPK);
136                            }
137                            catch (Exception e) {
138                                    assetEntry = assetEntryLocalService.updateEntry(
139                                            userId, groupId, subscription.getCreateDate(),
140                                            subscription.getModifiedDate(), className, classPK, null, 0,
141                                            null, null, false, null, null, null, null,
142                                            String.valueOf(groupId), null, null, null, null, 0, 0,
143                                            null);
144                            }
145    
146                            // Social
147    
148                            JSONObject extraDataJSONObject = JSONFactoryUtil.createJSONObject();
149    
150                            extraDataJSONObject.put("title", assetEntry.getTitle());
151    
152                            SocialActivityManagerUtil.addActivity(
153                                    userId, assetEntry, SocialActivityConstants.TYPE_SUBSCRIBE,
154                                    extraDataJSONObject.toString(), 0);
155                    }
156    
157                    return subscription;
158            }
159    
160            /**
161             * Deletes the subscription with the primary key. A social activity with the
162             * unsubscribe action is created.
163             *
164             * @param  subscriptionId the primary key of the subscription
165             * @return the subscription that was removed
166             * @throws PortalException if a portal exception occurred
167             */
168            @Override
169            public Subscription deleteSubscription(long subscriptionId)
170                    throws PortalException {
171    
172                    Subscription subscription = subscriptionPersistence.findByPrimaryKey(
173                            subscriptionId);
174    
175                    return deleteSubscription(subscription);
176            }
177    
178            /**
179             * Deletes the user's subscription to the entity. A social activity with the
180             * unsubscribe action is created.
181             *
182             * @param  userId the primary key of the user
183             * @param  className the entity's class name
184             * @param  classPK the primary key of the entity's instance
185             * @throws PortalException if a matching user or subscription could not be
186             *         found
187             */
188            @Override
189            public void deleteSubscription(long userId, String className, long classPK)
190                    throws PortalException {
191    
192                    User user = userPersistence.findByPrimaryKey(userId);
193                    long classNameId = classNameLocalService.getClassNameId(className);
194    
195                    Subscription subscription = subscriptionPersistence.fetchByC_U_C_C(
196                            user.getCompanyId(), userId, classNameId, classPK);
197    
198                    if (subscription != null) {
199                            deleteSubscription(subscription);
200                    }
201            }
202    
203            /**
204             * Deletes the subscription. A social activity with the unsubscribe action
205             * is created.
206             *
207             * @param  subscription the subscription
208             * @return the subscription that was removed
209             * @throws PortalException if a portal exception occurred
210             */
211            @Override
212            public Subscription deleteSubscription(Subscription subscription)
213                    throws PortalException {
214    
215                    // Subscription
216    
217                    subscriptionPersistence.remove(subscription);
218    
219                    // Social
220    
221                    AssetEntry assetEntry = assetEntryPersistence.fetchByC_C(
222                            subscription.getClassNameId(), subscription.getClassPK());
223    
224                    if (assetEntry != null) {
225                            JSONObject extraDataJSONObject = JSONFactoryUtil.createJSONObject();
226    
227                            extraDataJSONObject.put("title", assetEntry.getTitle());
228    
229                            SocialActivityManagerUtil.addActivity(
230                                    subscription.getUserId(), subscription,
231                                    SocialActivityConstants.TYPE_UNSUBSCRIBE,
232                                    extraDataJSONObject.toString(), 0);
233                    }
234    
235                    return subscription;
236            }
237    
238            /**
239             * Deletes all the subscriptions of the user.
240             *
241             * @param  userId the primary key of the user
242             * @throws PortalException if a portal exception occurred
243             */
244            @Override
245            public void deleteSubscriptions(long userId) throws PortalException {
246                    List<Subscription> subscriptions = subscriptionPersistence.findByUserId(
247                            userId);
248    
249                    for (Subscription subscription : subscriptions) {
250                            deleteSubscription(subscription);
251                    }
252            }
253    
254            @Override
255            public void deleteSubscriptions(long userId, long groupId)
256                    throws PortalException {
257    
258                    List<Subscription> subscriptions = subscriptionPersistence.findByG_U(
259                            groupId, userId);
260    
261                    for (Subscription subscription : subscriptions) {
262                            deleteSubscription(subscription);
263                    }
264            }
265    
266            /**
267             * Deletes all the subscriptions to the entity.
268             *
269             * @param  companyId the primary key of the company
270             * @param  className the entity's class name
271             * @param  classPK the primary key of the entity's instance
272             * @throws PortalException if a portal exception occurred
273             */
274            @Override
275            public void deleteSubscriptions(
276                            long companyId, String className, long classPK)
277                    throws PortalException {
278    
279                    long classNameId = classNameLocalService.getClassNameId(className);
280    
281                    List<Subscription> subscriptions = subscriptionPersistence.findByC_C_C(
282                            companyId, classNameId, classPK);
283    
284                    for (Subscription subscription : subscriptions) {
285                            deleteSubscription(subscription);
286                    }
287            }
288    
289            @Override
290            public Subscription fetchSubscription(
291                    long companyId, long userId, String className, long classPK) {
292    
293                    long classNameId = classNameLocalService.getClassNameId(className);
294    
295                    return subscriptionPersistence.fetchByC_U_C_C(
296                            companyId, userId, classNameId, classPK);
297            }
298    
299            /**
300             * Returns the subscription of the user to the entity.
301             *
302             * @param  companyId the primary key of the company
303             * @param  userId the primary key of the user
304             * @param  className the entity's class name
305             * @param  classPK the primary key of the entity's instance
306             * @return the subscription of the user to the entity
307             * @throws PortalException if a matching subscription could not be found
308             */
309            @Override
310            public Subscription getSubscription(
311                            long companyId, long userId, String className, long classPK)
312                    throws PortalException {
313    
314                    long classNameId = classNameLocalService.getClassNameId(className);
315    
316                    return subscriptionPersistence.findByC_U_C_C(
317                            companyId, userId, classNameId, classPK);
318            }
319    
320            /**
321             * Returns all the subscriptions of the user to the entities.
322             *
323             * @param  companyId the primary key of the company
324             * @param  userId the primary key of the user
325             * @param  className the entity's class name
326             * @param  classPKs the primary key of the entities
327             * @return the subscriptions of the user to the entities
328             */
329            @Override
330            public List<Subscription> getSubscriptions(
331                    long companyId, long userId, String className, long[] classPKs) {
332    
333                    long classNameId = classNameLocalService.getClassNameId(className);
334    
335                    return subscriptionPersistence.findByC_U_C_C(
336                            companyId, userId, classNameId, classPKs);
337            }
338    
339            /**
340             * Returns all the subscriptions to the entity.
341             *
342             * @param  companyId the primary key of the company
343             * @param  className the entity's class name
344             * @param  classPK the primary key of the entity's instance
345             * @return the subscriptions to the entity
346             */
347            @Override
348            public List<Subscription> getSubscriptions(
349                    long companyId, String className, long classPK) {
350    
351                    long classNameId = classNameLocalService.getClassNameId(className);
352    
353                    return subscriptionPersistence.findByC_C_C(
354                            companyId, classNameId, classPK);
355            }
356    
357            /**
358             * Returns an ordered range of all the subscriptions of the user.
359             *
360             * @param  userId the primary key of the user
361             * @param  start the lower bound of the range of results
362             * @param  end the upper bound of the range of results (not inclusive)
363             * @param  orderByComparator the comparator to order the subscriptions
364             * @return the range of subscriptions of the user
365             */
366            @Override
367            public List<Subscription> getUserSubscriptions(
368                    long userId, int start, int end,
369                    OrderByComparator<Subscription> orderByComparator) {
370    
371                    return subscriptionPersistence.findByUserId(
372                            userId, start, end, orderByComparator);
373            }
374    
375            /**
376             * Returns all the subscriptions of the user to the entities with the class
377             * name.
378             *
379             * @param  userId the primary key of the user
380             * @param  className the entity's class name
381             * @return the subscriptions of the user to the entities with the class name
382             */
383            @Override
384            public List<Subscription> getUserSubscriptions(
385                    long userId, String className) {
386    
387                    long classNameId = classNameLocalService.getClassNameId(className);
388    
389                    return subscriptionPersistence.findByU_C(userId, classNameId);
390            }
391    
392            /**
393             * Returns the number of subscriptions of the user.
394             *
395             * @param  userId the primary key of the user
396             * @return the number of subscriptions of the user
397             */
398            @Override
399            public int getUserSubscriptionsCount(long userId) {
400                    return subscriptionPersistence.countByUserId(userId);
401            }
402    
403            /**
404             * Returns <code>true</code> if the user is subscribed to the entity.
405             *
406             * @param  companyId the primary key of the company
407             * @param  userId the primary key of the user
408             * @param  className the entity's class name
409             * @param  classPK the primary key of the entity's instance
410             * @return <code>true</code> if the user is subscribed to the entity;
411             *         <code>false</code> otherwise
412             */
413            @Override
414            public boolean isSubscribed(
415                    long companyId, long userId, String className, long classPK) {
416    
417                    long classNameId = classNameLocalService.getClassNameId(className);
418    
419                    Subscription subscription = subscriptionPersistence.fetchByC_U_C_C(
420                            companyId, userId, classNameId, classPK);
421    
422                    if (subscription != null) {
423                            return true;
424                    }
425                    else {
426                            return false;
427                    }
428            }
429    
430            /**
431             * Returns <code>true</code> if the user is subscribed to any of the
432             * entities.
433             *
434             * @param  companyId the primary key of the company
435             * @param  userId the primary key of the user
436             * @param  className the entity's class name
437             * @param  classPKs the primary key of the entities
438             * @return <code>true</code> if the user is subscribed to any of the
439             *         entities; <code>false</code> otherwise
440             */
441            @Override
442            public boolean isSubscribed(
443                    long companyId, long userId, String className, long[] classPKs) {
444    
445                    long classNameId = classNameLocalService.getClassNameId(className);
446    
447                    int count = subscriptionPersistence.countByC_U_C_C(
448                            companyId, userId, classNameId, classPKs);
449    
450                    if (count > 0) {
451                            return true;
452                    }
453                    else {
454                            return false;
455                    }
456            }
457    
458    }