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