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