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.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     * Provides the local service for accessing, adding, and deleting notification
038     * subscriptions to entities. It handles subscriptions to entities found in many
039     * different places in the portal, including message boards, blogs, and
040     * 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                                    if (classPK != groupId) {
168                                            socialActivityLocalService.addActivity(
169                                                    userId, groupId, className, classPK,
170                                                    SocialActivityConstants.TYPE_SUBSCRIBE,
171                                                    StringPool.BLANK, 0);
172                                    }
173                            }
174                    }
175    
176                    return subscription;
177            }
178    
179            /**
180             * Deletes the subscription with the primary key. A social activity with the
181             * unsubscribe action is created.
182             *
183             * @param  subscriptionId the primary key of the subscription
184             * @return the subscription that was removed
185             * @throws PortalException if a portal exception occurred
186             * @throws SystemException if a system exception occurred
187             */
188            @Override
189            public Subscription deleteSubscription(long subscriptionId)
190                    throws PortalException, SystemException {
191    
192                    Subscription subscription = subscriptionPersistence.fetchByPrimaryKey(
193                            subscriptionId);
194    
195                    return deleteSubscription(subscription);
196            }
197    
198            /**
199             * Deletes the user's subscription to the entity. A social activity with the
200             * unsubscribe action is created.
201             *
202             * @param  userId the primary key of the user
203             * @param  className the entity's class name
204             * @param  classPK the primary key of the entity's instance
205             * @throws PortalException if a matching user or subscription could not be
206             *         found
207             * @throws SystemException if a system exception occurred
208             */
209            public void deleteSubscription(long userId, String className, long classPK)
210                    throws PortalException, SystemException {
211    
212                    User user = userPersistence.findByPrimaryKey(userId);
213                    long classNameId = PortalUtil.getClassNameId(className);
214    
215                    Subscription subscription = subscriptionPersistence.findByC_U_C_C(
216                            user.getCompanyId(), userId, classNameId, classPK);
217    
218                    deleteSubscription(subscription);
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             * @throws SystemException if a system exception occurred
229             */
230            @Override
231            public Subscription deleteSubscription(Subscription subscription)
232                    throws PortalException, SystemException {
233    
234                    // Subscription
235    
236                    subscriptionPersistence.remove(subscription);
237    
238                    // Social
239    
240                    AssetEntry assetEntry = assetEntryPersistence.fetchByC_C(
241                            subscription.getClassNameId(), subscription.getClassPK());
242    
243                    if (assetEntry != null) {
244                            String className = PortalUtil.getClassName(
245                                    subscription.getClassNameId());
246    
247                            socialActivityLocalService.addActivity(
248                                    subscription.getUserId(), assetEntry.getGroupId(), className,
249                                    subscription.getClassPK(),
250                                    SocialActivityConstants.TYPE_UNSUBSCRIBE, StringPool.BLANK, 0);
251                    }
252    
253                    return subscription;
254            }
255    
256            /**
257             * Deletes all the subscriptions of the user.
258             *
259             * @param  userId the primary key of the user
260             * @throws PortalException if a portal exception occurred
261             * @throws SystemException if a system exception occurred
262             */
263            public void deleteSubscriptions(long userId)
264                    throws PortalException, SystemException {
265    
266                    List<Subscription> subscriptions = subscriptionPersistence.findByUserId(
267                            userId);
268    
269                    for (Subscription subscription : subscriptions) {
270                            deleteSubscription(subscription);
271                    }
272            }
273    
274            /**
275             * Deletes all the subscriptions to the entity.
276             *
277             * @param  companyId the primary key of the company
278             * @param  className the entity's class name
279             * @param  classPK the primary key of the entity's instance
280             * @throws PortalException if a portal exception occurred
281             * @throws SystemException if a system exception occurred
282             */
283            public void deleteSubscriptions(
284                            long companyId, String className, long classPK)
285                    throws PortalException, SystemException {
286    
287                    long classNameId = PortalUtil.getClassNameId(className);
288    
289                    List<Subscription> subscriptions = subscriptionPersistence.findByC_C_C(
290                            companyId, classNameId, classPK);
291    
292                    for (Subscription subscription : subscriptions) {
293                            deleteSubscription(subscription);
294                    }
295            }
296    
297            /**
298             * Returns the subscription of the user to the entity.
299             *
300             * @param  companyId the primary key of the company
301             * @param  userId the primary key of the user
302             * @param  className the entity's class name
303             * @param  classPK the primary key of the entity's instance
304             * @return the subscription of the user to the entity
305             * @throws PortalException if a matching subscription could not be found
306             * @throws SystemException if a system exception occurred
307             */
308            public Subscription getSubscription(
309                            long companyId, long userId, String className, long classPK)
310                    throws PortalException, SystemException {
311    
312                    long classNameId = PortalUtil.getClassNameId(className);
313    
314                    return subscriptionPersistence.findByC_U_C_C(
315                            companyId, userId, classNameId, classPK);
316            }
317    
318            /**
319             * Returns all the subscriptions of the user to the entities.
320             *
321             * @param  companyId the primary key of the company
322             * @param  userId the primary key of the user
323             * @param  className the entity's class name
324             * @param  classPKs the primary key of the entities
325             * @return the subscriptions of the user to the entities
326             * @throws SystemException if a system exception occurred
327             */
328            public List<Subscription> getSubscriptions(
329                            long companyId, long userId, String className, long[] classPKs)
330                    throws SystemException {
331    
332                    long classNameId = PortalUtil.getClassNameId(className);
333    
334                    return subscriptionPersistence.findByC_U_C_C(
335                            companyId, userId, classNameId, classPKs);
336            }
337    
338            /**
339             * Returns all the subscriptions to the entity.
340             *
341             * @param  companyId the primary key of the company
342             * @param  className the entity's class name
343             * @param  classPK the primary key of the entity's instance
344             * @return the subscriptions to the entity
345             * @throws SystemException if a system exception occurred
346             */
347            public List<Subscription> getSubscriptions(
348                            long companyId, String className, long classPK)
349                    throws SystemException {
350    
351                    long classNameId = PortalUtil.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             * @return the range of subscriptions of the user
364             * @throws SystemException if a system exception occurred
365             */
366            public List<Subscription> getUserSubscriptions(
367                            long userId, int start, int end,
368                            OrderByComparator orderByComparator)
369                    throws SystemException {
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             * @throws SystemException if a system exception occurred
383             */
384            public List<Subscription> getUserSubscriptions(
385                            long userId, String className)
386                    throws SystemException {
387    
388                    long classNameId = PortalUtil.getClassNameId(className);
389    
390                    return subscriptionPersistence.findByU_C(userId, classNameId);
391            }
392    
393            /**
394             * Returns the number of subscriptions of the user.
395             *
396             * @param  userId the primary key of the user
397             * @return the number of subscriptions of the user
398             * @throws SystemException if a system exception occurred
399             */
400            public int getUserSubscriptionsCount(long userId) throws SystemException {
401                    return subscriptionPersistence.countByUserId(userId);
402            }
403    
404            /**
405             * Returns <code>true</code> if the user is subscribed to the entity.
406             *
407             * @param  companyId the primary key of the company
408             * @param  userId the primary key of the user
409             * @param  className the entity's class name
410             * @param  classPK the primary key of the entity's instance
411             * @return <code>true</code> if the user is subscribed to the entity;
412             *         <code>false</code> otherwise
413             * @throws SystemException if a system exception occurred
414             */
415            public boolean isSubscribed(
416                            long companyId, long userId, String className, long classPK)
417                    throws SystemException {
418    
419                    long classNameId = PortalUtil.getClassNameId(className);
420    
421                    Subscription subscription = subscriptionPersistence.fetchByC_U_C_C(
422                            companyId, userId, classNameId, classPK);
423    
424                    if (subscription != null) {
425                            return true;
426                    }
427                    else {
428                            return false;
429                    }
430            }
431    
432            /**
433             * Returns <code>true</code> if the user is subscribed to any of the
434             * entities.
435             *
436             * @param  companyId the primary key of the company
437             * @param  userId the primary key of the user
438             * @param  className the entity's class name
439             * @param  classPKs the primary key of the entities
440             * @return <code>true</code> if the user is subscribed to any of the
441             *         entities; <code>false</code> otherwise
442             * @throws SystemException if a system exception occurred
443             */
444            public boolean isSubscribed(
445                            long companyId, long userId, String className, long[] classPKs)
446                    throws SystemException {
447    
448                    long classNameId = PortalUtil.getClassNameId(className);
449    
450                    int count = subscriptionPersistence.countByC_U_C_C(
451                            companyId, userId, classNameId, classPKs);
452    
453                    if (count > 0) {
454                            return true;
455                    }
456                    else {
457                            return false;
458                    }
459            }
460    
461    }