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