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.portlet.social.service.impl;
016    
017    import com.liferay.portal.kernel.dao.orm.QueryUtil;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.exception.SystemException;
020    import com.liferay.portal.kernel.lar.ExportImportThreadLocal;
021    import com.liferay.portal.kernel.messaging.async.Async;
022    import com.liferay.portal.model.Group;
023    import com.liferay.portal.model.Layout;
024    import com.liferay.portal.model.User;
025    import com.liferay.portal.util.PortalUtil;
026    import com.liferay.portal.util.PropsValues;
027    import com.liferay.portlet.asset.model.AssetEntry;
028    import com.liferay.portlet.social.model.SocialActivity;
029    import com.liferay.portlet.social.model.SocialActivityConstants;
030    import com.liferay.portlet.social.model.SocialActivityDefinition;
031    import com.liferay.portlet.social.service.base.SocialActivityLocalServiceBaseImpl;
032    import com.liferay.portlet.social.util.SocialActivityHierarchyEntry;
033    import com.liferay.portlet.social.util.SocialActivityHierarchyEntryThreadLocal;
034    
035    import java.util.Date;
036    import java.util.List;
037    
038    /**
039     * The social activity local service. This service provides the means to record
040     * and list social activities in groups and organizations.
041     *
042     * <p>
043     * Social activities are identified by their type and the type of asset they are
044     * done on. Each activity records the exact time of the action as well as human
045     * readable information needed for activity feeds.
046     * </p>
047     *
048     * <p>
049     * Most of the <i>get-</i> methods in this service order activities in
050     * descending order by their execution times, so the most recent activities are
051     * listed first.
052     * </p>
053     *
054     * @author Brian Wing Shun Chan
055     */
056    public class SocialActivityLocalServiceImpl
057            extends SocialActivityLocalServiceBaseImpl {
058    
059            /**
060             * Records an activity with the given time in the database.
061             *
062             * <p>
063             * This method records a social activity done on an asset, identified by its
064             * class name and class primary key, in the database. Additional information
065             * (such as the original message ID for a reply to a forum post) is passed
066             * in via the <code>extraData</code> in JSON format. For activities
067             * affecting another user, a mirror activity is generated that describes the
068             * action from the user's point of view. The target user's ID is passed in
069             * via the <code>receiverUserId</code>.
070             * </p>
071             *
072             * <p>
073             * Example for a mirrored activity:<br> When a user replies to a message
074             * boards post, the reply action is stored in the database with the
075             * <code>receiverUserId</code> being the ID of the author of the original
076             * message. The <code>extraData</code> contains the ID of the original
077             * message in JSON format. A mirror activity is generated with the values of
078             * the <code>userId</code> and the <code>receiverUserId</code> swapped. This
079             * mirror activity basically describes a "replied to" event.
080             * </p>
081             *
082             * <p>
083             * Mirror activities are most often used in relation to friend requests and
084             * activities.
085             * </p>
086             *
087             * @param  userId the primary key of the acting user
088             * @param  groupId the primary key of the group
089             * @param  createDate the activity's date
090             * @param  className the target asset's class name
091             * @param  classPK the primary key of the target asset
092             * @param  type the activity's type
093             * @param  extraData any extra data regarding the activity
094             * @param  receiverUserId the primary key of the receiving user
095             * @throws PortalException if the user or group could not be found
096             * @throws SystemException if a system exception occurred
097             */
098            @Override
099            public void addActivity(
100                            long userId, long groupId, Date createDate, String className,
101                            long classPK, int type, String extraData, long receiverUserId)
102                    throws PortalException, SystemException {
103    
104                    if (ExportImportThreadLocal.isImportInProcess()) {
105                            return;
106                    }
107    
108                    User user = userPersistence.findByPrimaryKey(userId);
109                    long classNameId = PortalUtil.getClassNameId(className);
110    
111                    if (groupId > 0) {
112                            Group group = groupLocalService.getGroup(groupId);
113    
114                            if (group.isLayout()) {
115                                    Layout layout = layoutLocalService.getLayout(
116                                            group.getClassPK());
117    
118                                    groupId = layout.getGroupId();
119                            }
120                    }
121    
122                    SocialActivity activity = socialActivityPersistence.create(0);
123    
124                    activity.setGroupId(groupId);
125                    activity.setCompanyId(user.getCompanyId());
126                    activity.setUserId(user.getUserId());
127                    activity.setCreateDate(createDate.getTime());
128                    activity.setMirrorActivityId(0);
129                    activity.setClassNameId(classNameId);
130                    activity.setClassPK(classPK);
131    
132                    SocialActivityHierarchyEntry activityHierarchyEntry =
133                            SocialActivityHierarchyEntryThreadLocal.peek();
134    
135                    if (activityHierarchyEntry != null) {
136                            activity.setParentClassNameId(
137                                    activityHierarchyEntry.getClassNameId());
138                            activity.setParentClassPK(activityHierarchyEntry.getClassPK());
139                    }
140    
141                    activity.setType(type);
142                    activity.setExtraData(extraData);
143                    activity.setReceiverUserId(receiverUserId);
144    
145                    AssetEntry assetEntry = assetEntryPersistence.fetchByC_C(
146                            classNameId, classPK);
147    
148                    activity.setAssetEntry(assetEntry);
149    
150                    SocialActivity mirrorActivity = null;
151    
152                    if ((receiverUserId > 0) && (userId != receiverUserId)) {
153                            mirrorActivity = socialActivityPersistence.create(0);
154    
155                            mirrorActivity.setGroupId(groupId);
156                            mirrorActivity.setCompanyId(user.getCompanyId());
157                            mirrorActivity.setUserId(receiverUserId);
158                            mirrorActivity.setCreateDate(createDate.getTime());
159                            mirrorActivity.setClassNameId(classNameId);
160                            mirrorActivity.setClassPK(classPK);
161    
162                            if (activityHierarchyEntry != null) {
163                                    mirrorActivity.setParentClassNameId(
164                                            activityHierarchyEntry.getClassNameId());
165                                    mirrorActivity.setParentClassPK(
166                                            activityHierarchyEntry.getClassPK());
167                            }
168    
169                            mirrorActivity.setType(type);
170                            mirrorActivity.setExtraData(extraData);
171                            mirrorActivity.setReceiverUserId(user.getUserId());
172                            mirrorActivity.setAssetEntry(assetEntry);
173                    }
174    
175                    socialActivityLocalService.addActivity(activity, mirrorActivity);
176            }
177    
178            /**
179             * Records an activity in the database, using a time based on the current
180             * time in an attempt to make the activity's time unique.
181             *
182             * @param  userId the primary key of the acting user
183             * @param  groupId the primary key of the group
184             * @param  className the target asset's class name
185             * @param  classPK the primary key of the target asset
186             * @param  type the activity's type
187             * @param  extraData any extra data regarding the activity
188             * @param  receiverUserId the primary key of the receiving user
189             * @throws PortalException if the user or group could not be found
190             * @throws SystemException if a system exception occurred
191             */
192            @Override
193            public void addActivity(
194                            long userId, long groupId, String className, long classPK, int type,
195                            String extraData, long receiverUserId)
196                    throws PortalException, SystemException {
197    
198                    if (ExportImportThreadLocal.isImportInProcess()) {
199                            return;
200                    }
201    
202                    Date createDate = new Date();
203    
204                    long classNameId = PortalUtil.getClassNameId(className);
205    
206                    while (true) {
207                            SocialActivity socialActivity =
208                                    socialActivityPersistence.fetchByG_U_CD_C_C_T_R(
209                                            groupId, userId, createDate.getTime(), classNameId, classPK,
210                                            type, receiverUserId);
211    
212                            if (socialActivity != null) {
213                                    createDate = new Date(createDate.getTime() + 1);
214                            }
215                            else {
216                                    break;
217                            }
218                    }
219    
220                    addActivity(
221                            userId, groupId, createDate, className, classPK, type, extraData,
222                            receiverUserId);
223            }
224    
225            @Async
226            @Override
227            public void addActivity(
228                            SocialActivity activity, SocialActivity mirrorActivity)
229                    throws PortalException, SystemException {
230    
231                    if (ExportImportThreadLocal.isImportInProcess()) {
232                            return;
233                    }
234    
235                    if ((activity.getActivityId() > 0) ||
236                            ((mirrorActivity != null) &&
237                             (mirrorActivity.getActivityId() > 0))) {
238    
239                            throw new PortalException(
240                                    "Activity and mirror activity must not have primary keys set");
241                    }
242    
243                    if (isLogActivity(activity)) {
244                            long activityId = counterLocalService.increment(
245                                    SocialActivity.class.getName());
246    
247                            activity.setActivityId(activityId);
248    
249                            socialActivityPersistence.update(activity);
250    
251                            if (mirrorActivity != null) {
252                                    long mirrorActivityId = counterLocalService.increment(
253                                            SocialActivity.class.getName());
254    
255                                    mirrorActivity.setActivityId(mirrorActivityId);
256                                    mirrorActivity.setMirrorActivityId(activity.getPrimaryKey());
257    
258                                    socialActivityPersistence.update(mirrorActivity);
259                            }
260    
261                            if (PropsValues.SOCIAL_ACTIVITY_SETS_ENABLED) {
262                                    socialActivityInterpreterLocalService.updateActivitySet(
263                                            activity.getActivityId());
264                            }
265                    }
266    
267                    socialActivityCounterLocalService.addActivityCounters(activity);
268            }
269    
270            /**
271             * Records an activity in the database, but only if there isn't already an
272             * activity with the same parameters.
273             *
274             * <p>
275             * For the main functionality see {@link #addActivity(long, long, Date,
276             * String, long, int, String, long)}
277             * </p>
278             *
279             * @param  userId the primary key of the acting user
280             * @param  groupId the primary key of the group
281             * @param  createDate the activity's date
282             * @param  className the target asset's class name
283             * @param  classPK the primary key of the target asset
284             * @param  type the activity's type
285             * @param  extraData any extra data regarding the activity
286             * @param  receiverUserId the primary key of the receiving user
287             * @throws PortalException if the user or group could not be found
288             * @throws SystemException if a system exception occurred
289             */
290            @Override
291            public void addUniqueActivity(
292                            long userId, long groupId, Date createDate, String className,
293                            long classPK, int type, String extraData, long receiverUserId)
294                    throws PortalException, SystemException {
295    
296                    long classNameId = PortalUtil.getClassNameId(className);
297    
298                    SocialActivity socialActivity =
299                            socialActivityPersistence.fetchByG_U_CD_C_C_T_R(
300                                    groupId, userId, createDate.getTime(), classNameId, classPK,
301                                    type, receiverUserId);
302    
303                    if (socialActivity != null) {
304                            return;
305                    }
306    
307                    addActivity(
308                            userId, groupId, createDate, className, classPK, type, extraData,
309                            receiverUserId);
310            }
311    
312            /**
313             * Records an activity with the current time in the database, but only if
314             * there isn't one with the same parameters.
315             *
316             * <p>
317             * For the main functionality see {@link #addActivity(long, long, Date,
318             * String, long, int, String, long)}
319             * </p>
320             *
321             * @param  userId the primary key of the acting user
322             * @param  groupId the primary key of the group
323             * @param  className the target asset's class name
324             * @param  classPK the primary key of the target asset
325             * @param  type the activity's type
326             * @param  extraData any extra data regarding the activity
327             * @param  receiverUserId the primary key of the receiving user
328             * @throws PortalException if the user or group could not be found
329             * @throws SystemException if a system exception occurred
330             */
331            @Override
332            public void addUniqueActivity(
333                            long userId, long groupId, String className, long classPK, int type,
334                            String extraData, long receiverUserId)
335                    throws PortalException, SystemException {
336    
337                    long classNameId = PortalUtil.getClassNameId(className);
338    
339                    int count = socialActivityPersistence.countByG_U_C_C_T_R(
340                            groupId, userId, classNameId, classPK, type, receiverUserId);
341    
342                    if (count > 0) {
343                            return;
344                    }
345    
346                    addActivity(
347                            userId, groupId, new Date(), className, classPK, type, extraData,
348                            receiverUserId);
349            }
350    
351            /**
352             * Removes stored activities for the asset.
353             *
354             * @param  assetEntry the asset from which to remove stored activities
355             * @throws PortalException if a portal exception occurred
356             * @throws SystemException if a system exception occurred
357             */
358            @Override
359            public void deleteActivities(AssetEntry assetEntry)
360                    throws PortalException, SystemException {
361    
362                    if (PropsValues.SOCIAL_ACTIVITY_SETS_ENABLED) {
363                            socialActivitySetLocalService.decrementActivityCount(
364                                    assetEntry.getClassNameId(), assetEntry.getClassPK());
365                    }
366    
367                    socialActivityPersistence.removeByC_C(
368                            assetEntry.getClassNameId(), assetEntry.getClassPK());
369    
370                    socialActivityCounterLocalService.deleteActivityCounters(assetEntry);
371            }
372    
373            @Override
374            public void deleteActivities(long groupId) throws SystemException {
375                    if (PropsValues.SOCIAL_ACTIVITY_SETS_ENABLED) {
376                            socialActivitySetPersistence.removeByGroupId(groupId);
377                    }
378    
379                    socialActivityPersistence.removeByGroupId(groupId);
380    
381                    socialActivityCounterPersistence.removeByGroupId(groupId);
382    
383                    socialActivityLimitPersistence.removeByGroupId(groupId);
384    
385                    socialActivitySettingPersistence.removeByGroupId(groupId);
386            }
387    
388            /**
389             * Removes stored activities for the asset identified by the class name and
390             * class primary key.
391             *
392             * @param  className the target asset's class name
393             * @param  classPK the primary key of the target asset
394             * @throws PortalException if the user's activity counters could not be
395             *         deleted
396             * @throws SystemException if a system exception occurred
397             */
398            @Override
399            public void deleteActivities(String className, long classPK)
400                    throws PortalException, SystemException {
401    
402                    long classNameId = PortalUtil.getClassNameId(className);
403    
404                    if (PropsValues.SOCIAL_ACTIVITY_SETS_ENABLED) {
405                            socialActivitySetLocalService.decrementActivityCount(
406                                    classNameId, classPK);
407                    }
408    
409                    socialActivityPersistence.removeByC_C(classNameId, classPK);
410            }
411    
412            /**
413             * Removes the stored activity from the database.
414             *
415             * @param  activityId the primary key of the stored activity
416             * @throws PortalException if the activity could not be found
417             * @throws SystemException if a system exception occurred
418             */
419            @Override
420            public void deleteActivity(long activityId)
421                    throws PortalException, SystemException {
422    
423                    SocialActivity activity = socialActivityPersistence.findByPrimaryKey(
424                            activityId);
425    
426                    deleteActivity(activity);
427            }
428    
429            /**
430             * Removes the stored activity and its mirror activity from the database.
431             *
432             * @param  activity the activity to be removed
433             * @throws PortalException if the user's activity counters could not be
434             *         deleted or if a portal exception occurred
435             * @throws SystemException if a system exception occurred
436             */
437            @Override
438            public void deleteActivity(SocialActivity activity)
439                    throws PortalException, SystemException {
440    
441                    if (PropsValues.SOCIAL_ACTIVITY_SETS_ENABLED) {
442                            socialActivitySetLocalService.decrementActivityCount(
443                                    activity.getActivitySetId());
444                    }
445    
446                    socialActivityPersistence.remove(activity);
447    
448                    SocialActivity mirrorActivity =
449                            socialActivityPersistence.fetchByMirrorActivityId(
450                                    activity.getActivityId());
451    
452                    if (mirrorActivity != null) {
453                            socialActivityPersistence.remove(mirrorActivity);
454                    }
455            }
456    
457            /**
458             * Removes the user's stored activities from the database.
459             *
460             * <p>
461             * This method removes all activities where the user is either the actor or
462             * the receiver.
463             * </p>
464             *
465             * @param  userId the primary key of the user
466             * @throws PortalException if the user's activity counters could not be
467             *         deleted
468             * @throws SystemException if a system exception occurred
469             */
470            @Override
471            public void deleteUserActivities(long userId)
472                    throws PortalException, SystemException {
473    
474                    List<SocialActivity> activities =
475                            socialActivityPersistence.findByUserId(
476                                    userId, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
477    
478                    for (SocialActivity activity : activities) {
479                            if (PropsValues.SOCIAL_ACTIVITY_SETS_ENABLED) {
480                                    socialActivitySetLocalService.decrementActivityCount(
481                                            activity.getActivitySetId());
482                            }
483    
484                            socialActivityPersistence.remove(activity);
485                    }
486    
487                    activities = socialActivityPersistence.findByReceiverUserId(
488                            userId, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
489    
490                    for (SocialActivity activity : activities) {
491                            if (PropsValues.SOCIAL_ACTIVITY_SETS_ENABLED) {
492                                    socialActivitySetLocalService.decrementActivityCount(
493                                            activity.getActivitySetId());
494                            }
495    
496                            socialActivityPersistence.remove(activity);
497                    }
498    
499                    socialActivityCounterLocalService.deleteActivityCounters(
500                            User.class.getName(), userId);
501            }
502    
503            @Override
504            public SocialActivity fetchFirstActivity(
505                            String className, long classPK, int type)
506                    throws SystemException {
507    
508                    long classNameId = PortalUtil.getClassNameId(className);
509    
510                    return socialActivityPersistence.fetchByC_C_T_First(
511                            classNameId, classPK, type, null);
512            }
513    
514            /**
515             * Returns a range of all the activities done on assets identified by the
516             * class name ID.
517             *
518             * <p>
519             * Useful when paginating results. Returns a maximum of <code>end -
520             * start</code> instances. <code>start</code> and <code>end</code> are not
521             * primary keys, they are indexes in the result set. Thus, <code>0</code>
522             * refers to the first result in the set. Setting both <code>start</code>
523             * and <code>end</code> to {@link
524             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
525             * result set.
526             * </p>
527             *
528             * @param  classNameId the target asset's class name ID
529             * @param  start the lower bound of the range of results
530             * @param  end the upper bound of the range of results (not inclusive)
531             * @return the range of matching activities
532             * @throws SystemException if a system exception occurred
533             */
534            @Override
535            public List<SocialActivity> getActivities(
536                            long classNameId, int start, int end)
537                    throws SystemException {
538    
539                    return socialActivityPersistence.findByClassNameId(
540                            classNameId, start, end);
541            }
542    
543            /**
544             * Returns a range of all the activities done on the asset identified by the
545             * class name ID and class primary key that are mirrors of the activity
546             * identified by the mirror activity ID.
547             *
548             * <p>
549             * Useful when paginating results. Returns a maximum of <code>end -
550             * start</code> instances. <code>start</code> and <code>end</code> are not
551             * primary keys, they are indexes in the result set. Thus, <code>0</code>
552             * refers to the first result in the set. Setting both <code>start</code>
553             * and <code>end</code> to {@link
554             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
555             * result set.
556             * </p>
557             *
558             * @param  mirrorActivityId the primary key of the mirror activity
559             * @param  classNameId the target asset's class name ID
560             * @param  classPK the primary key of the target asset
561             * @param  start the lower bound of the range of results
562             * @param  end the upper bound of the range of results (not inclusive)
563             * @return the range of matching activities
564             * @throws SystemException if a system exception occurred
565             */
566            @Override
567            public List<SocialActivity> getActivities(
568                            long mirrorActivityId, long classNameId, long classPK, int start,
569                            int end)
570                    throws SystemException {
571    
572                    return socialActivityPersistence.findByM_C_C(
573                            mirrorActivityId, classNameId, classPK, start, end);
574            }
575    
576            /**
577             * Returns a range of all the activities done on the asset identified by the
578             * class name and the class primary key that are mirrors of the activity
579             * identified by the mirror activity ID.
580             *
581             * <p>
582             * Useful when paginating results. Returns a maximum of <code>end -
583             * start</code> instances. <code>start</code> and <code>end</code> are not
584             * primary keys, they are indexes in the result set. Thus, <code>0</code>
585             * refers to the first result in the set. Setting both <code>start</code>
586             * and <code>end</code> to {@link
587             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
588             * result set.
589             * </p>
590             *
591             * @param  mirrorActivityId the primary key of the mirror activity
592             * @param  className the target asset's class name
593             * @param  classPK the primary key of the target asset
594             * @param  start the lower bound of the range of results
595             * @param  end the upper bound of the range of results (not inclusive)
596             * @return the range of matching activities
597             * @throws SystemException if a system exception occurred
598             */
599            @Override
600            public List<SocialActivity> getActivities(
601                            long mirrorActivityId, String className, long classPK, int start,
602                            int end)
603                    throws SystemException {
604    
605                    long classNameId = PortalUtil.getClassNameId(className);
606    
607                    return getActivities(
608                            mirrorActivityId, classNameId, classPK, start, end);
609            }
610    
611            /**
612             * Returns a range of all the activities done on assets identified by the
613             * class name.
614             *
615             * <p>
616             * Useful when paginating results. Returns a maximum of <code>end -
617             * start</code> instances. <code>start</code> and <code>end</code> are not
618             * primary keys, they are indexes in the result set. Thus, <code>0</code>
619             * refers to the first result in the set. Setting both <code>start</code>
620             * and <code>end</code> to {@link
621             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
622             * result set.
623             * </p>
624             *
625             * @param  className the target asset's class name
626             * @param  start the lower bound of the range of results
627             * @param  end the upper bound of the range of results (not inclusive)
628             * @return the range of matching activities
629             * @throws SystemException if a system exception occurred
630             */
631            @Override
632            public List<SocialActivity> getActivities(
633                            String className, int start, int end)
634                    throws SystemException {
635    
636                    long classNameId = PortalUtil.getClassNameId(className);
637    
638                    return getActivities(classNameId, start, end);
639            }
640    
641            /**
642             * Returns the number of activities done on assets identified by the class
643             * name ID.
644             *
645             * @param  classNameId the target asset's class name ID
646             * @return the number of matching activities
647             * @throws SystemException if a system exception occurred
648             */
649            @Override
650            public int getActivitiesCount(long classNameId) throws SystemException {
651                    return socialActivityPersistence.countByClassNameId(classNameId);
652            }
653    
654            /**
655             * Returns the number of activities done on the asset identified by the
656             * class name ID and class primary key that are mirrors of the activity
657             * identified by the mirror activity ID.
658             *
659             * @param  mirrorActivityId the primary key of the mirror activity
660             * @param  classNameId the target asset's class name ID
661             * @param  classPK the primary key of the target asset
662             * @return the number of matching activities
663             * @throws SystemException if a system exception occurred
664             */
665            @Override
666            public int getActivitiesCount(
667                            long mirrorActivityId, long classNameId, long classPK)
668                    throws SystemException {
669    
670                    return socialActivityPersistence.countByM_C_C(
671                            mirrorActivityId, classNameId, classPK);
672            }
673    
674            /**
675             * Returns the number of activities done on the asset identified by the
676             * class name and class primary key that are mirrors of the activity
677             * identified by the mirror activity ID.
678             *
679             * @param  mirrorActivityId the primary key of the mirror activity
680             * @param  className the target asset's class name
681             * @param  classPK the primary key of the target asset
682             * @return the number of matching activities
683             * @throws SystemException if a system exception occurred
684             */
685            @Override
686            public int getActivitiesCount(
687                            long mirrorActivityId, String className, long classPK)
688                    throws SystemException {
689    
690                    long classNameId = PortalUtil.getClassNameId(className);
691    
692                    return getActivitiesCount(mirrorActivityId, classNameId, classPK);
693            }
694    
695            /**
696             * Returns the number of activities done on assets identified by class name.
697             *
698             * @param  className the target asset's class name
699             * @return the number of matching activities
700             * @throws SystemException if a system exception occurred
701             */
702            @Override
703            public int getActivitiesCount(String className) throws SystemException {
704                    long classNameId = PortalUtil.getClassNameId(className);
705    
706                    return getActivitiesCount(classNameId);
707            }
708    
709            /**
710             * Returns the activity identified by its primary key.
711             *
712             * @param  activityId the primary key of the activity
713             * @return Returns the activity
714             * @throws PortalException if the activity could not be found
715             * @throws SystemException if a system exception occurred
716             */
717            @Override
718            public SocialActivity getActivity(long activityId)
719                    throws PortalException, SystemException {
720    
721                    return socialActivityPersistence.findByPrimaryKey(activityId);
722            }
723    
724            @Override
725            public List<SocialActivity> getActivitySetActivities(
726                            long activitySetId, int start, int end)
727                    throws SystemException {
728    
729                    return socialActivityPersistence.findByActivitySetId(
730                            activitySetId, start, end);
731            }
732    
733            /**
734             * Returns a range of all the activities done in the group.
735             *
736             * <p>
737             * This method only finds activities without mirrors.
738             * </p>
739             *
740             * <p>
741             * Useful when paginating results. Returns a maximum of <code>end -
742             * start</code> instances. <code>start</code> and <code>end</code> are not
743             * primary keys, they are indexes in the result set. Thus, <code>0</code>
744             * refers to the first result in the set. Setting both <code>start</code>
745             * and <code>end</code> to {@link
746             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
747             * result set.
748             * </p>
749             *
750             * @param  groupId the primary key of the group
751             * @param  start the lower bound of the range of results
752             * @param  end the upper bound of the range of results (not inclusive)
753             * @return the range of matching activities
754             * @throws SystemException if a system exception occurred
755             */
756            @Override
757            public List<SocialActivity> getGroupActivities(
758                            long groupId, int start, int end)
759                    throws SystemException {
760    
761                    return socialActivityFinder.findByGroupId(groupId, start, end);
762            }
763    
764            /**
765             * Returns the number of activities done in the group.
766             *
767             * <p>
768             * This method only counts activities without mirrors.
769             * </p>
770             *
771             * @param  groupId the primary key of the group
772             * @return the number of matching activities
773             * @throws SystemException if a system exception occurred
774             */
775            @Override
776            public int getGroupActivitiesCount(long groupId) throws SystemException {
777                    return socialActivityFinder.countByGroupId(groupId);
778            }
779    
780            /**
781             * Returns a range of activities done by users that are members of the
782             * group.
783             *
784             * <p>
785             * This method only finds activities without mirrors.
786             * </p>
787             *
788             * <p>
789             * Useful when paginating results. Returns a maximum of <code>end -
790             * start</code> instances. <code>start</code> and <code>end</code> are not
791             * primary keys, they are indexes in the result set. Thus, <code>0</code>
792             * refers to the first result in the set. Setting both <code>start</code>
793             * and <code>end</code> to {@link
794             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
795             * result set.
796             * </p>
797             *
798             * @param  groupId the primary key of the group
799             * @param  start the lower bound of the range of results
800             * @param  end the upper bound of the range of results (not inclusive)
801             * @return the range of matching activities
802             * @throws SystemException if a system exception occurred
803             */
804            @Override
805            public List<SocialActivity> getGroupUsersActivities(
806                            long groupId, int start, int end)
807                    throws SystemException {
808    
809                    return socialActivityFinder.findByGroupUsers(groupId, start, end);
810            }
811    
812            /**
813             * Returns the number of activities done by users that are members of the
814             * group.
815             *
816             * <p>
817             * This method only counts activities without mirrors.
818             * </p>
819             *
820             * @param  groupId the primary key of the group
821             * @return the number of matching activities
822             * @throws SystemException if a system exception occurred
823             */
824            @Override
825            public int getGroupUsersActivitiesCount(long groupId)
826                    throws SystemException {
827    
828                    return socialActivityFinder.countByGroupUsers(groupId);
829            }
830    
831            /**
832             * Returns the activity that has the mirror activity.
833             *
834             * @param  mirrorActivityId the primary key of the mirror activity
835             * @return Returns the mirror activity
836             * @throws PortalException if the mirror activity could not be found
837             * @throws SystemException if a system exception occurred
838             */
839            @Override
840            public SocialActivity getMirrorActivity(long mirrorActivityId)
841                    throws PortalException, SystemException {
842    
843                    return socialActivityPersistence.findByMirrorActivityId(
844                            mirrorActivityId);
845            }
846    
847            /**
848             * Returns a range of all the activities done in the organization. This
849             * method only finds activities without mirrors.
850             *
851             * <p>
852             * Useful when paginating results. Returns a maximum of <code>end -
853             * start</code> instances. <code>start</code> and <code>end</code> are not
854             * primary keys, they are indexes in the result set. Thus, <code>0</code>
855             * refers to the first result in the set. Setting both <code>start</code>
856             * and <code>end</code> to {@link
857             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
858             * result set.
859             * </p>
860             *
861             * @param  organizationId the primary key of the organization
862             * @param  start the lower bound of the range of results
863             * @param  end the upper bound of the range of results (not inclusive)
864             * @return the range of matching activities
865             * @throws SystemException if a system exception occurred
866             */
867            @Override
868            public List<SocialActivity> getOrganizationActivities(
869                            long organizationId, int start, int end)
870                    throws SystemException {
871    
872                    return socialActivityFinder.findByOrganizationId(
873                            organizationId, start, end);
874            }
875    
876            /**
877             * Returns the number of activities done in the organization. This method
878             * only counts activities without mirrors.
879             *
880             * @param  organizationId the primary key of the organization
881             * @return the number of matching activities
882             * @throws SystemException if a system exception occurred
883             */
884            @Override
885            public int getOrganizationActivitiesCount(long organizationId)
886                    throws SystemException {
887    
888                    return socialActivityFinder.countByOrganizationId(organizationId);
889            }
890    
891            /**
892             * Returns a range of all the activities done by users of the organization.
893             * This method only finds activities without mirrors.
894             *
895             * <p>
896             * Useful when paginating results. Returns a maximum of <code>end -
897             * start</code> instances. <code>start</code> and <code>end</code> are not
898             * primary keys, they are indexes in the result set. Thus, <code>0</code>
899             * refers to the first result in the set. Setting both <code>start</code>
900             * and <code>end</code> to {@link
901             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
902             * result set.
903             * </p>
904             *
905             * @param  organizationId the primary key of the organization
906             * @param  start the lower bound of the range of results
907             * @param  end the upper bound of the range of results (not inclusive)
908             * @return the range of matching activities
909             * @throws SystemException if a system exception occurred
910             */
911            @Override
912            public List<SocialActivity> getOrganizationUsersActivities(
913                            long organizationId, int start, int end)
914                    throws SystemException {
915    
916                    return socialActivityFinder.findByOrganizationUsers(
917                            organizationId, start, end);
918            }
919    
920            /**
921             * Returns the number of activities done by users of the organization. This
922             * method only counts activities without mirrors.
923             *
924             * @param  organizationId the primary key of the organization
925             * @return the number of matching activities
926             * @throws SystemException if a system exception occurred
927             */
928            @Override
929            public int getOrganizationUsersActivitiesCount(long organizationId)
930                    throws SystemException {
931    
932                    return socialActivityFinder.countByOrganizationUsers(organizationId);
933            }
934    
935            /**
936             * Returns a range of all the activities done by users in a relationship
937             * with the user identified by the user ID.
938             *
939             * <p>
940             * Useful when paginating results. Returns a maximum of <code>end -
941             * start</code> instances. <code>start</code> and <code>end</code> are not
942             * primary keys, they are indexes in the result set. Thus, <>0</code> refers
943             * to the first result in the set. Setting both <code>start</code> and
944             * <code>end</code> to {@link
945             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
946             * result set.
947             * </p>
948             *
949             * @param  userId the primary key of the user
950             * @param  start the lower bound of the range of results
951             * @param  end the upper bound of the range of results (not inclusive)
952             * @return the range of matching activities
953             * @throws SystemException if a system exception occurred
954             */
955            @Override
956            public List<SocialActivity> getRelationActivities(
957                            long userId, int start, int end)
958                    throws SystemException {
959    
960                    return socialActivityFinder.findByRelation(userId, start, end);
961            }
962    
963            /**
964             * Returns a range of all the activities done by users in a relationship of
965             * type <code>type</code> with the user identified by <code>userId</code>.
966             * This method only finds activities without mirrors.
967             *
968             * <p>
969             * Useful when paginating results. Returns a maximum of <code>end -
970             * start</code> instances. <code>start</code> and <code>end</code> are not
971             * primary keys, they are indexes in the result set. Thus, <code>0</code>
972             * refers to the first result in the set. Setting both <code>start</code>
973             * and <code>end</code> to {@link
974             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
975             * result set.
976             * </p>
977             *
978             * @param  userId the primary key of the user
979             * @param  type the relationship type
980             * @param  start the lower bound of the range of results
981             * @param  end the upper bound of the range of results (not inclusive)
982             * @return the range of matching activities
983             * @throws SystemException if a system exception occurred
984             */
985            @Override
986            public List<SocialActivity> getRelationActivities(
987                            long userId, int type, int start, int end)
988                    throws SystemException {
989    
990                    return socialActivityFinder.findByRelationType(
991                            userId, type, start, end);
992            }
993    
994            /**
995             * Returns the number of activities done by users in a relationship with the
996             * user identified by userId.
997             *
998             * @param  userId the primary key of the user
999             * @return the number of matching activities
1000             * @throws SystemException if a system exception occurred
1001             */
1002            @Override
1003            public int getRelationActivitiesCount(long userId) throws SystemException {
1004                    return socialActivityFinder.countByRelation(userId);
1005            }
1006    
1007            /**
1008             * Returns the number of activities done by users in a relationship of type
1009             * <code>type</code> with the user identified by <code>userId</code>. This
1010             * method only counts activities without mirrors.
1011             *
1012             * @param  userId the primary key of the user
1013             * @param  type the relationship type
1014             * @return the number of matching activities
1015             * @throws SystemException if a system exception occurred
1016             */
1017            @Override
1018            public int getRelationActivitiesCount(long userId, int type)
1019                    throws SystemException {
1020    
1021                    return socialActivityFinder.countByRelationType(userId, type);
1022            }
1023    
1024            /**
1025             * Returns a range of all the activities done by the user.
1026             *
1027             * <p>
1028             * Useful when paginating results. Returns a maximum of <code>end -
1029             * start</code> instances. <code>start</code> and <code>end</code> are not
1030             * primary keys, they are indexes in the result set. Thus, <code>0</code>
1031             * refers to the first result in the set. Setting both <code>start</code>
1032             * and <code>end</code> to {@link
1033             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
1034             * result set.
1035             * </p>
1036             *
1037             * @param  userId the primary key of the user
1038             * @param  start the lower bound of the range of results
1039             * @param  end the upper bound of the range of results (not inclusive)
1040             * @return the range of matching activities
1041             * @throws SystemException if a system exception occurred
1042             */
1043            @Override
1044            public List<SocialActivity> getUserActivities(
1045                            long userId, int start, int end)
1046                    throws SystemException {
1047    
1048                    return socialActivityPersistence.findByUserId(userId, start, end);
1049            }
1050    
1051            /**
1052             * Returns the number of activities done by the user.
1053             *
1054             * @param  userId the primary key of the user
1055             * @return the number of matching activities
1056             * @throws SystemException if a system exception occurred
1057             */
1058            @Override
1059            public int getUserActivitiesCount(long userId) throws SystemException {
1060                    return socialActivityPersistence.countByUserId(userId);
1061            }
1062    
1063            /**
1064             * Returns a range of all the activities done in the user's groups. This
1065             * method only finds activities without mirrors.
1066             *
1067             * <p>
1068             * Useful when paginating results. Returns a maximum of <code>end -
1069             * start</code> instances. <code>start</code> and <code>end</code> are not
1070             * primary keys, they are indexes in the result set. Thus, <code>0</code>
1071             * refers to the first result in the set. Setting both <code>start</code>
1072             * and <code>end</code> to {@link
1073             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
1074             * result set.
1075             * </p>
1076             *
1077             * @param  userId the primary key of the user
1078             * @param  start the lower bound of the range of results
1079             * @param  end the upper bound of the range of results (not inclusive)
1080             * @return the range of matching activities
1081             * @throws SystemException if a system exception occurred
1082             */
1083            @Override
1084            public List<SocialActivity> getUserGroupsActivities(
1085                            long userId, int start, int end)
1086                    throws SystemException {
1087    
1088                    return socialActivityFinder.findByUserGroups(userId, start, end);
1089            }
1090    
1091            /**
1092             * Returns the number of activities done in user's groups. This method only
1093             * counts activities without mirrors.
1094             *
1095             * @param  userId the primary key of the user
1096             * @return the number of matching activities
1097             * @throws SystemException if a system exception occurred
1098             */
1099            @Override
1100            public int getUserGroupsActivitiesCount(long userId)
1101                    throws SystemException {
1102    
1103                    return socialActivityFinder.countByUserGroups(userId);
1104            }
1105    
1106            /**
1107             * Returns a range of all the activities done in the user's groups and
1108             * organizations. This method only finds activities without mirrors.
1109             *
1110             * <p>
1111             * Useful when paginating results. Returns a maximum of <code>end -
1112             * start</code> instances. <code>start</code> and <code>end</code> are not
1113             * primary keys, they are indexes in the result set. Thus, <code>0</code>
1114             * refers to the first result in the set. Setting both <code>start</code>
1115             * and <code>end</code> to {@link
1116             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
1117             * result set.
1118             * </p>
1119             *
1120             * @param  userId the primary key of the user
1121             * @param  start the lower bound of the range of results
1122             * @param  end the upper bound of the range of results (not inclusive)
1123             * @return the range of matching activities
1124             * @throws SystemException if a system exception occurred
1125             */
1126            @Override
1127            public List<SocialActivity> getUserGroupsAndOrganizationsActivities(
1128                            long userId, int start, int end)
1129                    throws SystemException {
1130    
1131                    return socialActivityFinder.findByUserGroupsAndOrganizations(
1132                            userId, start, end);
1133            }
1134    
1135            /**
1136             * Returns the number of activities done in user's groups and organizations.
1137             * This method only counts activities without mirrors.
1138             *
1139             * @param  userId the primary key of the user
1140             * @return the number of matching activities
1141             * @throws SystemException if a system exception occurred
1142             */
1143            @Override
1144            public int getUserGroupsAndOrganizationsActivitiesCount(long userId)
1145                    throws SystemException {
1146    
1147                    return socialActivityFinder.countByUserGroupsAndOrganizations(userId);
1148            }
1149    
1150            /**
1151             * Returns a range of all activities done in the user's organizations. This
1152             * method only finds activities without mirrors.
1153             *
1154             * <p>
1155             * Useful when paginating results. Returns a maximum of <code>end -
1156             * start</code> instances. <code>start</code> and <code>end</code> are not
1157             * primary keys, they are indexes in the result set. Thus, <code>0</code>
1158             * refers to the first result in the set. Setting both <code>start</code>
1159             * and <code>end</code> to {@link
1160             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
1161             * result set.
1162             * </p>
1163             *
1164             * @param  userId the primary key of the user
1165             * @param  start the lower bound of the range of results
1166             * @param  end the upper bound of the range of results (not inclusive)
1167             * @return the range of matching activities
1168             * @throws SystemException if a system exception occurred
1169             */
1170            @Override
1171            public List<SocialActivity> getUserOrganizationsActivities(
1172                            long userId, int start, int end)
1173                    throws SystemException {
1174    
1175                    return socialActivityFinder.findByUserOrganizations(userId, start, end);
1176            }
1177    
1178            /**
1179             * Returns the number of activities done in the user's organizations. This
1180             * method only counts activities without mirrors.
1181             *
1182             * @param  userId the primary key of the user
1183             * @return the number of matching activities
1184             * @throws SystemException if a system exception occurred
1185             */
1186            @Override
1187            public int getUserOrganizationsActivitiesCount(long userId)
1188                    throws SystemException {
1189    
1190                    return socialActivityFinder.countByUserOrganizations(userId);
1191            }
1192    
1193            protected boolean isLogActivity(SocialActivity activity)
1194                    throws SystemException {
1195    
1196                    if (activity.getType() == SocialActivityConstants.TYPE_DELETE) {
1197                            if (activity.getParentClassPK() == 0) {
1198                                    return true;
1199                            }
1200    
1201                            return false;
1202                    }
1203    
1204                    SocialActivityDefinition activityDefinition =
1205                            socialActivitySettingLocalService.getActivityDefinition(
1206                                    activity.getGroupId(), activity.getClassName(),
1207                                    activity.getType());
1208    
1209                    if (activityDefinition != null) {
1210                            return activityDefinition.isLogActivity();
1211                    }
1212    
1213                    if (activity.getType() < SocialActivityConstants.TYPE_VIEW) {
1214                            return true;
1215                    }
1216    
1217                    return false;
1218            }
1219    
1220    }