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.util.StringPool;
020    import com.liferay.portal.security.auth.PrincipalException;
021    import com.liferay.portal.security.permission.ActionKeys;
022    import com.liferay.portal.security.permission.PermissionChecker;
023    import com.liferay.portal.service.ServiceContext;
024    import com.liferay.portal.util.PropsValues;
025    import com.liferay.portlet.social.model.SocialActivity;
026    import com.liferay.portlet.social.model.SocialActivityInterpreter;
027    import com.liferay.portlet.social.model.impl.SocialActivityInterpreterImpl;
028    import com.liferay.portlet.social.service.base.SocialActivityServiceBaseImpl;
029    
030    import java.util.ArrayList;
031    import java.util.List;
032    
033    /**
034     * Provides the remote service for accessing social activities. Its methods
035     * include permission checks.
036     *
037     * @author Zsolt Berentey
038     */
039    public class SocialActivityServiceImpl extends SocialActivityServiceBaseImpl {
040    
041            /**
042             * Returns a range of all the activities done on assets identified by the
043             * class name ID.
044             *
045             * <p>
046             * Useful when paginating results. Returns a maximum of <code>end -
047             * start</code> instances. <code>start</code> and <code>end</code> are not
048             * primary keys, they are indexes in the result set. Thus, <code>0</code>
049             * refers to the first result in the set. Setting both <code>start</code>
050             * and <code>end</code> to {@link
051             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
052             * result set.
053             * </p>
054             *
055             * @param  classNameId the target asset's class name ID
056             * @param  start the lower bound of the range of results
057             * @param  end the upper bound of the range of results (not inclusive)
058             * @return the range of matching activities
059             * @throws PortalException if a permission checker was not initialized
060             */
061            @Override
062            public List<SocialActivity> getActivities(
063                            long classNameId, int start, int end)
064                    throws PortalException {
065    
066                    List<SocialActivity> activities =
067                            socialActivityLocalService.getActivities(
068                                    classNameId, 0,
069                                    end + PropsValues.SOCIAL_ACTIVITY_FILTER_SEARCH_LIMIT);
070    
071                    return filterActivities(activities, start, end);
072            }
073    
074            /**
075             * Returns a range of all the activities done on the asset identified by the
076             * class name ID and class primary key that are mirrors of the activity
077             * identified by the mirror activity ID.
078             *
079             * <p>
080             * Useful when paginating results. Returns a maximum of <code>end -
081             * start</code> instances. <code>start</code> and <code>end</code> are not
082             * primary keys, they are indexes in the result set. Thus, <code>0</code>
083             * refers to the first result in the set. Setting both <code>start</code>
084             * and <code>end</code> to {@link
085             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
086             * result set.
087             * </p>
088             *
089             * @param  mirrorActivityId the primary key of the mirror activity
090             * @param  classNameId the target asset's class name ID
091             * @param  classPK the primary key of the target asset
092             * @param  start the lower bound of the range of results
093             * @param  end the upper bound of the range of results (not inclusive)
094             * @return the range of matching activities
095             * @throws PortalException if a permission checker was not initialized
096             */
097            @Override
098            public List<SocialActivity> getActivities(
099                            long mirrorActivityId, long classNameId, long classPK, int start,
100                            int end)
101                    throws PortalException {
102    
103                    List<SocialActivity> activities =
104                            socialActivityLocalService.getActivities(
105                                    mirrorActivityId, classNameId, classPK, 0,
106                                    end + PropsValues.SOCIAL_ACTIVITY_FILTER_SEARCH_LIMIT);
107    
108                    return filterActivities(activities, start, end);
109            }
110    
111            /**
112             * Returns a range of all the activities done on the asset identified by the
113             * class name and the class primary key that are mirrors of the activity
114             * identified by the mirror activity ID.
115             *
116             * <p>
117             * Useful when paginating results. Returns a maximum of <code>end -
118             * start</code> instances. <code>start</code> and <code>end</code> are not
119             * primary keys, they are indexes in the result set. Thus, <code>0</code>
120             * refers to the first result in the set. Setting both <code>start</code>
121             * and <code>end</code> to {@link
122             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
123             * result set.
124             * </p>
125             *
126             * @param  mirrorActivityId the primary key of the mirror activity
127             * @param  className the target asset's class name
128             * @param  classPK the primary key of the target asset
129             * @param  start the lower bound of the range of results
130             * @param  end the upper bound of the range of results (not inclusive)
131             * @return the range of matching activities
132             * @throws PortalException if a permission checker was not initialized
133             */
134            @Override
135            public List<SocialActivity> getActivities(
136                            long mirrorActivityId, String className, long classPK, int start,
137                            int end)
138                    throws PortalException {
139    
140                    long classNameId = classNameLocalService.getClassNameId(className);
141    
142                    List<SocialActivity> activities =
143                            socialActivityLocalService.getActivities(
144                                    mirrorActivityId, classNameId, classPK, 0,
145                                    end + PropsValues.SOCIAL_ACTIVITY_FILTER_SEARCH_LIMIT);
146    
147                    return filterActivities(activities, start, end);
148            }
149    
150            /**
151             * Returns a range of all the activities done on assets identified by the
152             * class name.
153             *
154             * <p>
155             * Useful when paginating results. Returns a maximum of <code>end -
156             * start</code> instances. <code>start</code> and <code>end</code> are not
157             * primary keys, they are indexes in the result set. Thus, <code>0</code>
158             * refers to the first result in the set. Setting both <code>start</code>
159             * and <code>end</code> to {@link
160             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
161             * result set.
162             * </p>
163             *
164             * @param  className the target asset's class name
165             * @param  start the lower bound of the range of results
166             * @param  end the upper bound of the range of results (not inclusive)
167             * @return the range of matching activities
168             * @throws PortalException if a permission checker was not initialized
169             */
170            @Override
171            public List<SocialActivity> getActivities(
172                            String className, int start, int end)
173                    throws PortalException {
174    
175                    long classNameId = classNameLocalService.getClassNameId(className);
176    
177                    List<SocialActivity> activities =
178                            socialActivityLocalService.getActivities(
179                                    classNameId, 0,
180                                    end + PropsValues.SOCIAL_ACTIVITY_FILTER_SEARCH_LIMIT);
181    
182                    return filterActivities(activities, start, end);
183            }
184    
185            /**
186             * Returns the number of activities done on assets identified by the class
187             * name ID.
188             *
189             * @param  classNameId the target asset's class name ID
190             * @return the number of matching activities
191             */
192            @Override
193            public int getActivitiesCount(long classNameId) {
194                    return socialActivityLocalService.getActivitiesCount(classNameId);
195            }
196    
197            /**
198             * Returns the number of activities done on the asset identified by the
199             * class name ID and class primary key that are mirrors of the activity
200             * identified by the mirror activity ID.
201             *
202             * @param  mirrorActivityId the primary key of the mirror activity
203             * @param  classNameId the target asset's class name ID
204             * @param  classPK the primary key of the target asset
205             * @return the number of matching activities
206             */
207            @Override
208            public int getActivitiesCount(
209                    long mirrorActivityId, long classNameId, long classPK) {
210    
211                    return socialActivityLocalService.getActivitiesCount(
212                            mirrorActivityId, classNameId, classPK);
213            }
214    
215            /**
216             * Returns the number of activities done on the asset identified by the
217             * class name and class primary key that are mirrors of the activity
218             * identified by the mirror activity ID.
219             *
220             * @param  mirrorActivityId the primary key of the mirror activity
221             * @param  className the target asset's class name
222             * @param  classPK the primary key of the target asset
223             * @return the number of matching activities
224             */
225            @Override
226            public int getActivitiesCount(
227                    long mirrorActivityId, String className, long classPK) {
228    
229                    long classNameId = classNameLocalService.getClassNameId(className);
230    
231                    return getActivitiesCount(mirrorActivityId, classNameId, classPK);
232            }
233    
234            /**
235             * Returns the number of activities done on assets identified by class name.
236             *
237             * @param  className the target asset's class name
238             * @return the number of matching activities
239             */
240            @Override
241            public int getActivitiesCount(String className) {
242                    long classNameId = classNameLocalService.getClassNameId(className);
243    
244                    return getActivitiesCount(classNameId);
245            }
246    
247            /**
248             * Returns the activity identified by its primary key.
249             *
250             * @param  activityId the primary key of the activity
251             * @return Returns the activity
252             * @throws PortalException if the activity could not be found
253             */
254            @Override
255            public SocialActivity getActivity(long activityId) throws PortalException {
256                    SocialActivity activity = socialActivityLocalService.getActivity(
257                            activityId);
258    
259                    List<SocialActivityInterpreter> activityInterpreters =
260                            socialActivityInterpreterLocalService.getActivityInterpreters(
261                                    StringPool.BLANK);
262    
263                    if (!hasPermission(activity, activityInterpreters)) {
264                            throw new PrincipalException();
265                    }
266    
267                    return activity;
268            }
269    
270            @Override
271            public List<SocialActivity> getActivitySetActivities(
272                            long activitySetId, int start, int end)
273                    throws PortalException {
274    
275                    List<SocialActivity> activities =
276                            socialActivityLocalService.getActivitySetActivities(
277                                    activitySetId, start, end);
278    
279                    return filterActivities(activities, start, end);
280            }
281    
282            /**
283             * Returns a range of all the activities done in the group.
284             *
285             * <p>
286             * This method only finds activities without mirrors.
287             * </p>
288             *
289             * <p>
290             * Useful when paginating results. Returns a maximum of <code>end -
291             * start</code> instances. <code>start</code> and <code>end</code> are not
292             * primary keys, they are indexes in the result set. Thus, <code>0</code>
293             * refers to the first result in the set. Setting both <code>start</code>
294             * and <code>end</code> to {@link
295             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
296             * result set.
297             * </p>
298             *
299             * @param  groupId the primary key of the group
300             * @param  start the lower bound of the range of results
301             * @param  end the upper bound of the range of results (not inclusive)
302             * @return the range of matching activities
303             * @throws PortalException if a permission checker was not initialized
304             */
305            @Override
306            public List<SocialActivity> getGroupActivities(
307                            long groupId, int start, int end)
308                    throws PortalException {
309    
310                    List<SocialActivity> activities =
311                            socialActivityLocalService.getGroupActivities(
312                                    groupId, 0,
313                                    end + PropsValues.SOCIAL_ACTIVITY_FILTER_SEARCH_LIMIT);
314    
315                    return filterActivities(activities, start, end);
316            }
317    
318            /**
319             * Returns the number of activities done in the group.
320             *
321             * <p>
322             * This method only counts activities without mirrors.
323             * </p>
324             *
325             * @param  groupId the primary key of the group
326             * @return the number of matching activities
327             */
328            @Override
329            public int getGroupActivitiesCount(long groupId) {
330                    return socialActivityLocalService.getGroupActivitiesCount(groupId);
331            }
332    
333            /**
334             * Returns a range of activities done by users that are members of the
335             * group.
336             *
337             * <p>
338             * This method only finds activities without mirrors.
339             * </p>
340             *
341             * <p>
342             * Useful when paginating results. Returns a maximum of <code>end -
343             * start</code> instances. <code>start</code> and <code>end</code> are not
344             * primary keys, they are indexes in the result set. Thus, <code>0</code>
345             * refers to the first result in the set. Setting both <code>start</code>
346             * and <code>end</code> to {@link
347             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
348             * result set.
349             * </p>
350             *
351             * @param  groupId the primary key of the group
352             * @param  start the lower bound of the range of results
353             * @param  end the upper bound of the range of results (not inclusive)
354             * @return the range of matching activities
355             * @throws PortalException if a permission checker was not initialized
356             */
357            @Override
358            public List<SocialActivity> getGroupUsersActivities(
359                            long groupId, int start, int end)
360                    throws PortalException {
361    
362                    List<SocialActivity> activities =
363                            socialActivityLocalService.getGroupUsersActivities(
364                                    groupId, 0,
365                                    end + PropsValues.SOCIAL_ACTIVITY_FILTER_SEARCH_LIMIT);
366    
367                    return filterActivities(activities, start, end);
368            }
369    
370            /**
371             * Returns the number of activities done by users that are members of the
372             * group.
373             *
374             * <p>
375             * This method only counts activities without mirrors.
376             * </p>
377             *
378             * @param  groupId the primary key of the group
379             * @return the number of matching activities
380             */
381            @Override
382            public int getGroupUsersActivitiesCount(long groupId) {
383                    return socialActivityLocalService.getGroupUsersActivitiesCount(groupId);
384            }
385    
386            /**
387             * Returns the activity that has the mirror activity.
388             *
389             * @param  mirrorActivityId the primary key of the mirror activity
390             * @return Returns the mirror activity
391             * @throws PortalException if the mirror activity could not be found
392             */
393            @Override
394            public SocialActivity getMirrorActivity(long mirrorActivityId)
395                    throws PortalException {
396    
397                    SocialActivity activity = socialActivityLocalService.getMirrorActivity(
398                            mirrorActivityId);
399    
400                    List<SocialActivityInterpreter> activityInterpreters =
401                            socialActivityInterpreterLocalService.getActivityInterpreters(
402                                    StringPool.BLANK);
403    
404                    if (!hasPermission(activity, activityInterpreters)) {
405                            throw new PrincipalException();
406                    }
407    
408                    return activity;
409            }
410    
411            /**
412             * Returns a range of all the activities done in the organization. This
413             * method only finds activities without mirrors.
414             *
415             * <p>
416             * Useful when paginating results. Returns a maximum of <code>end -
417             * start</code> instances. <code>start</code> and <code>end</code> are not
418             * primary keys, they are indexes in the result set. Thus, <code>0</code>
419             * refers to the first result in the set. Setting both <code>start</code>
420             * and <code>end</code> to {@link
421             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
422             * result set.
423             * </p>
424             *
425             * @param  organizationId the primary key of the organization
426             * @param  start the lower bound of the range of results
427             * @param  end the upper bound of the range of results (not inclusive)
428             * @return the range of matching activities
429             * @throws PortalException if a permission checker was not initialized
430             */
431            @Override
432            public List<SocialActivity> getOrganizationActivities(
433                            long organizationId, int start, int end)
434                    throws PortalException {
435    
436                    List<SocialActivity> activities =
437                            socialActivityLocalService.getOrganizationActivities(
438                                    organizationId, 0,
439                                    end + PropsValues.SOCIAL_ACTIVITY_FILTER_SEARCH_LIMIT);
440    
441                    return filterActivities(activities, start, end);
442            }
443    
444            /**
445             * Returns the number of activities done in the organization. This method
446             * only counts activities without mirrors.
447             *
448             * @param  organizationId the primary key of the organization
449             * @return the number of matching activities
450             */
451            @Override
452            public int getOrganizationActivitiesCount(long organizationId) {
453                    return socialActivityLocalService.getOrganizationActivitiesCount(
454                            organizationId);
455            }
456    
457            /**
458             * Returns a range of all the activities done by users of the organization.
459             * This method only finds activities without mirrors.
460             *
461             * <p>
462             * Useful when paginating results. Returns a maximum of <code>end -
463             * start</code> instances. <code>start</code> and <code>end</code> are not
464             * primary keys, they are indexes in the result set. Thus, <code>0</code>
465             * refers to the first result in the set. Setting both <code>start</code>
466             * and <code>end</code> to {@link
467             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
468             * result set.
469             * </p>
470             *
471             * @param  organizationId the primary key of the organization
472             * @param  start the lower bound of the range of results
473             * @param  end the upper bound of the range of results (not inclusive)
474             * @return the range of matching activities
475             * @throws PortalException if a permission checker was not initialized
476             */
477            @Override
478            public List<SocialActivity> getOrganizationUsersActivities(
479                            long organizationId, int start, int end)
480                    throws PortalException {
481    
482                    List<SocialActivity> activities =
483                            socialActivityLocalService.getOrganizationUsersActivities(
484                                    organizationId, 0,
485                                    end + PropsValues.SOCIAL_ACTIVITY_FILTER_SEARCH_LIMIT);
486    
487                    return filterActivities(activities, start, end);
488            }
489    
490            /**
491             * Returns the number of activities done by users of the organization. This
492             * method only counts activities without mirrors.
493             *
494             * @param  organizationId the primary key of the organization
495             * @return the number of matching activities
496             */
497            @Override
498            public int getOrganizationUsersActivitiesCount(long organizationId) {
499                    return socialActivityLocalService.getOrganizationUsersActivitiesCount(
500                            organizationId);
501            }
502    
503            /**
504             * Returns a range of all the activities done by users in a relationship
505             * with the user identified by the user ID.
506             *
507             * <p>
508             * Useful when paginating results. Returns a maximum of <code>end -
509             * start</code> instances. <code>start</code> and <code>end</code> are not
510             * primary keys, they are indexes in the result set. Thus, <>0</code> refers
511             * to the first result in the set. Setting both <code>start</code> and
512             * <code>end</code> to {@link
513             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
514             * result set.
515             * </p>
516             *
517             * @param  userId the primary key of the user
518             * @param  start the lower bound of the range of results
519             * @param  end the upper bound of the range of results (not inclusive)
520             * @return the range of matching activities
521             * @throws PortalException if a permission checker was not initialized
522             */
523            @Override
524            public List<SocialActivity> getRelationActivities(
525                            long userId, int start, int end)
526                    throws PortalException {
527    
528                    List<SocialActivity> activities =
529                            socialActivityLocalService.getRelationActivities(
530                                    userId, 0,
531                                    end + PropsValues.SOCIAL_ACTIVITY_FILTER_SEARCH_LIMIT);
532    
533                    return filterActivities(activities, start, end);
534            }
535    
536            /**
537             * Returns a range of all the activities done by users in a relationship of
538             * type <code>type</code> with the user identified by <code>userId</code>.
539             * This method only finds activities without mirrors.
540             *
541             * <p>
542             * Useful when paginating results. Returns a maximum of <code>end -
543             * start</code> instances. <code>start</code> and <code>end</code> are not
544             * primary keys, they are indexes in the result set. Thus, <code>0</code>
545             * refers to the first result in the set. Setting both <code>start</code>
546             * and <code>end</code> to {@link
547             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
548             * result set.
549             * </p>
550             *
551             * @param  userId the primary key of the user
552             * @param  type the relationship type
553             * @param  start the lower bound of the range of results
554             * @param  end the upper bound of the range of results (not inclusive)
555             * @return the range of matching activities
556             * @throws PortalException if a permission checker was not initialized
557             */
558            @Override
559            public List<SocialActivity> getRelationActivities(
560                            long userId, int type, int start, int end)
561                    throws PortalException {
562    
563                    List<SocialActivity> activities =
564                            socialActivityLocalService.getRelationActivities(
565                                    userId, type, 0,
566                                    end + PropsValues.SOCIAL_ACTIVITY_FILTER_SEARCH_LIMIT);
567    
568                    return filterActivities(activities, start, end);
569            }
570    
571            /**
572             * Returns the number of activities done by users in a relationship with the
573             * user identified by userId.
574             *
575             * @param  userId the primary key of the user
576             * @return the number of matching activities
577             */
578            @Override
579            public int getRelationActivitiesCount(long userId) {
580                    return socialActivityLocalService.getRelationActivitiesCount(userId);
581            }
582    
583            /**
584             * Returns the number of activities done by users in a relationship of type
585             * <code>type</code> with the user identified by <code>userId</code>. This
586             * method only counts activities without mirrors.
587             *
588             * @param  userId the primary key of the user
589             * @param  type the relationship type
590             * @return the number of matching activities
591             */
592            @Override
593            public int getRelationActivitiesCount(long userId, int type) {
594                    return socialActivityLocalService.getRelationActivitiesCount(
595                            userId, type);
596            }
597    
598            /**
599             * Returns a range of all the activities done by the user.
600             *
601             * <p>
602             * Useful when paginating results. Returns a maximum of <code>end -
603             * start</code> instances. <code>start</code> and <code>end</code> are not
604             * primary keys, they are indexes in the result set. Thus, <code>0</code>
605             * refers to the first result in the set. Setting both <code>start</code>
606             * and <code>end</code> to {@link
607             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
608             * result set.
609             * </p>
610             *
611             * @param  userId the primary key of the user
612             * @param  start the lower bound of the range of results
613             * @param  end the upper bound of the range of results (not inclusive)
614             * @return the range of matching activities
615             * @throws PortalException if a permission checker was not initialized
616             */
617            @Override
618            public List<SocialActivity> getUserActivities(
619                            long userId, int start, int end)
620                    throws PortalException {
621    
622                    List<SocialActivity> activities =
623                            socialActivityLocalService.getUserActivities(
624                                    userId, 0,
625                                    end + PropsValues.SOCIAL_ACTIVITY_FILTER_SEARCH_LIMIT);
626    
627                    return filterActivities(activities, start, end);
628            }
629    
630            /**
631             * Returns the number of activities done by the user.
632             *
633             * @param  userId the primary key of the user
634             * @return the number of matching activities
635             */
636            @Override
637            public int getUserActivitiesCount(long userId) {
638                    return socialActivityLocalService.getUserActivitiesCount(userId);
639            }
640    
641            /**
642             * Returns a range of all the activities done in the user's groups. This
643             * method only finds activities without mirrors.
644             *
645             * <p>
646             * Useful when paginating results. Returns a maximum of <code>end -
647             * start</code> instances. <code>start</code> and <code>end</code> are not
648             * primary keys, they are indexes in the result set. Thus, <code>0</code>
649             * refers to the first result in the set. Setting both <code>start</code>
650             * and <code>end</code> to {@link
651             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
652             * result set.
653             * </p>
654             *
655             * @param  userId the primary key of the user
656             * @param  start the lower bound of the range of results
657             * @param  end the upper bound of the range of results (not inclusive)
658             * @return the range of matching activities
659             * @throws PortalException if a permission checker was not initialized
660             */
661            @Override
662            public List<SocialActivity> getUserGroupsActivities(
663                            long userId, int start, int end)
664                    throws PortalException {
665    
666                    List<SocialActivity> activities =
667                            socialActivityLocalService.getUserGroupsActivities(
668                                    userId, 0,
669                                    end + PropsValues.SOCIAL_ACTIVITY_FILTER_SEARCH_LIMIT);
670    
671                    return filterActivities(activities, start, end);
672            }
673    
674            /**
675             * Returns the number of activities done in user's groups. This method only
676             * counts activities without mirrors.
677             *
678             * @param  userId the primary key of the user
679             * @return the number of matching activities
680             */
681            @Override
682            public int getUserGroupsActivitiesCount(long userId) {
683                    return socialActivityLocalService.getUserGroupsActivitiesCount(userId);
684            }
685    
686            /**
687             * Returns a range of all the activities done in the user's groups and
688             * organizations. This method only finds activities without mirrors.
689             *
690             * <p>
691             * Useful when paginating results. Returns a maximum of <code>end -
692             * start</code> instances. <code>start</code> and <code>end</code> are not
693             * primary keys, they are indexes in the result set. Thus, <code>0</code>
694             * refers to the first result in the set. Setting both <code>start</code>
695             * and <code>end</code> to {@link
696             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
697             * result set.
698             * </p>
699             *
700             * @param  userId the primary key of the user
701             * @param  start the lower bound of the range of results
702             * @param  end the upper bound of the range of results (not inclusive)
703             * @return the range of matching activities
704             * @throws PortalException if a permission checker was not initialized
705             */
706            @Override
707            public List<SocialActivity> getUserGroupsAndOrganizationsActivities(
708                            long userId, int start, int end)
709                    throws PortalException {
710    
711                    List<SocialActivity> activities =
712                            socialActivityLocalService.getUserGroupsAndOrganizationsActivities(
713                                    userId, 0,
714                                    end + PropsValues.SOCIAL_ACTIVITY_FILTER_SEARCH_LIMIT);
715    
716                    return filterActivities(activities, start, end);
717            }
718    
719            /**
720             * Returns the number of activities done in user's groups and organizations.
721             * This method only counts activities without mirrors.
722             *
723             * @param  userId the primary key of the user
724             * @return the number of matching activities
725             */
726            @Override
727            public int getUserGroupsAndOrganizationsActivitiesCount(long userId) {
728                    return socialActivityLocalService.
729                            getUserGroupsAndOrganizationsActivitiesCount(userId);
730            }
731    
732            /**
733             * Returns a range of all activities done in the user's organizations. This
734             * method only finds activities without mirrors.
735             *
736             * <p>
737             * Useful when paginating results. Returns a maximum of <code>end -
738             * start</code> instances. <code>start</code> and <code>end</code> are not
739             * primary keys, they are indexes in the result set. Thus, <code>0</code>
740             * refers to the first result in the set. Setting both <code>start</code>
741             * and <code>end</code> to {@link
742             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
743             * result set.
744             * </p>
745             *
746             * @param  userId the primary key of the user
747             * @param  start the lower bound of the range of results
748             * @param  end the upper bound of the range of results (not inclusive)
749             * @return the range of matching activities
750             * @throws PortalException if a permission checker was not initialized
751             */
752            @Override
753            public List<SocialActivity> getUserOrganizationsActivities(
754                            long userId, int start, int end)
755                    throws PortalException {
756    
757                    List<SocialActivity> activities =
758                            socialActivityLocalService.getUserOrganizationsActivities(
759                                    userId, 0,
760                                    end + PropsValues.SOCIAL_ACTIVITY_FILTER_SEARCH_LIMIT);
761    
762                    return filterActivities(activities, start, end);
763            }
764    
765            /**
766             * Returns the number of activities done in the user's organizations. This
767             * method only counts activities without mirrors.
768             *
769             * @param  userId the primary key of the user
770             * @return the number of matching activities
771             */
772            @Override
773            public int getUserOrganizationsActivitiesCount(long userId) {
774                    return socialActivityLocalService.getUserOrganizationsActivitiesCount(
775                            userId);
776            }
777    
778            protected List<SocialActivity> filterActivities(
779                            List<SocialActivity> activities, int start, int end)
780                    throws PortalException {
781    
782                    List<SocialActivity> filteredActivities =
783                            new ArrayList<SocialActivity>();
784    
785                    List<SocialActivityInterpreter> activityInterpreters =
786                            socialActivityInterpreterLocalService.getActivityInterpreters(
787                                    StringPool.BLANK);
788    
789                    for (SocialActivity activity : activities) {
790                            if (hasPermission(activity, activityInterpreters)) {
791                                    filteredActivities.add(activity);
792                            }
793    
794                            if ((end != QueryUtil.ALL_POS) &&
795                                    (filteredActivities.size() > end)) {
796    
797                                    break;
798                            }
799                    }
800    
801                    if ((end != QueryUtil.ALL_POS) && (start != QueryUtil.ALL_POS)) {
802                            if (end > filteredActivities.size()) {
803                                    end = filteredActivities.size();
804                            }
805    
806                            if (start > filteredActivities.size()) {
807                                    start = filteredActivities.size();
808                            }
809    
810                            filteredActivities = filteredActivities.subList(start, end);
811                    }
812    
813                    return filteredActivities;
814            }
815    
816            protected boolean hasPermission(
817                            SocialActivity activity,
818                            List<SocialActivityInterpreter> activityInterpreters)
819                    throws PortalException {
820    
821                    PermissionChecker permissionChecker = getPermissionChecker();
822                    ServiceContext serviceContext = new ServiceContext();
823    
824                    for (int i = 0; i < activityInterpreters.size(); i++) {
825                            SocialActivityInterpreterImpl activityInterpreterImpl =
826                                    (SocialActivityInterpreterImpl)activityInterpreters.get(i);
827    
828                            if (activityInterpreterImpl.hasClassName(activity.getClassName())) {
829                                    try {
830                                            if (activityInterpreterImpl.hasPermission(
831                                                            permissionChecker, activity, ActionKeys.VIEW,
832                                                            serviceContext)) {
833    
834                                                    return true;
835                                            }
836                                    }
837                                    catch (Exception e) {
838                                    }
839                            }
840                    }
841    
842                    return false;
843            }
844    
845    }