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