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