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