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