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