001    /**
002     * Copyright (c) 2000-2010 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.model.Group;
021    import com.liferay.portal.model.Layout;
022    import com.liferay.portal.model.User;
023    import com.liferay.portal.util.PortalUtil;
024    import com.liferay.portlet.social.NoSuchActivityException;
025    import com.liferay.portlet.social.model.SocialActivity;
026    import com.liferay.portlet.social.service.base.SocialActivityLocalServiceBaseImpl;
027    import com.liferay.portlet.social.util.SocialActivityThreadLocal;
028    
029    import java.util.Date;
030    import java.util.List;
031    
032    /**
033     * @author Brian Wing Shun Chan
034     */
035    public class SocialActivityLocalServiceImpl
036            extends SocialActivityLocalServiceBaseImpl {
037    
038            public SocialActivity addActivity(
039                            long userId, long groupId, Date createDate, String className,
040                            long classPK, int type, String extraData, long receiverUserId)
041                    throws PortalException, SystemException {
042    
043                    if (!SocialActivityThreadLocal.isEnabled()) {
044                            return null;
045                    }
046    
047                    User user = userPersistence.findByPrimaryKey(userId);
048                    long classNameId = PortalUtil.getClassNameId(className);
049    
050                    if (groupId > 0) {
051                            Group group = groupLocalService.getGroup(groupId);
052    
053                            if (group.isLayout()) {
054                                    Layout layout = layoutLocalService.getLayout(
055                                            group.getClassPK());
056    
057                                    groupId = layout.getGroupId();
058                            }
059                    }
060    
061                    long activityId = counterLocalService.increment(
062                            SocialActivity.class.getName());
063    
064                    SocialActivity activity = socialActivityPersistence.create(
065                            activityId);
066    
067                    activity.setGroupId(groupId);
068                    activity.setCompanyId(user.getCompanyId());
069                    activity.setUserId(user.getUserId());
070                    activity.setCreateDate(createDate.getTime());
071                    activity.setMirrorActivityId(0);
072                    activity.setClassNameId(classNameId);
073                    activity.setClassPK(classPK);
074                    activity.setType(type);
075                    activity.setExtraData(extraData);
076                    activity.setReceiverUserId(receiverUserId);
077    
078                    socialActivityPersistence.update(activity, false);
079    
080                    if ((receiverUserId > 0) && (userId != receiverUserId)) {
081                            long mirrorActivityId = counterLocalService.increment(
082                                    SocialActivity.class.getName());
083    
084                            SocialActivity mirrorActivity = socialActivityPersistence.create(
085                                    mirrorActivityId);
086    
087                            mirrorActivity.setGroupId(groupId);
088                            mirrorActivity.setCompanyId(user.getCompanyId());
089                            mirrorActivity.setUserId(receiverUserId);
090                            mirrorActivity.setCreateDate(createDate.getTime());
091                            mirrorActivity.setMirrorActivityId(activityId);
092                            mirrorActivity.setClassNameId(classNameId);
093                            mirrorActivity.setClassPK(classPK);
094                            mirrorActivity.setType(type);
095                            mirrorActivity.setExtraData(extraData);
096                            mirrorActivity.setReceiverUserId(user.getUserId());
097    
098                            socialActivityPersistence.update(mirrorActivity, false);
099                    }
100    
101                    return activity;
102            }
103    
104            public SocialActivity addActivity(
105                            long userId, long groupId, String className, long classPK, int type,
106                            String extraData, long receiverUserId)
107                    throws PortalException, SystemException {
108    
109                    Date createDate = new Date();
110    
111                    long classNameId = PortalUtil.getClassNameId(className);
112    
113                    while (true) {
114                            SocialActivity socialActivity =
115                                    socialActivityPersistence.fetchByG_U_CD_C_C_T_R(
116                                            groupId, userId, createDate.getTime(), classNameId, classPK,
117                                            type, receiverUserId);
118    
119                            if (socialActivity != null) {
120                                    createDate = new Date(createDate.getTime() + 1);
121                            }
122                            else {
123                                    break;
124                            }
125                    }
126    
127                    return addActivity(
128                            userId, groupId, createDate, className, classPK, type, extraData,
129                            receiverUserId);
130            }
131    
132            public SocialActivity addUniqueActivity(
133                            long userId, long groupId, Date createDate, String className,
134                            long classPK, int type, String extraData, long receiverUserId)
135                    throws PortalException, SystemException {
136    
137                    long classNameId = PortalUtil.getClassNameId(className);
138    
139                    SocialActivity socialActivity =
140                            socialActivityPersistence.fetchByG_U_CD_C_C_T_R(
141                                    groupId, userId, createDate.getTime(), classNameId, classPK,
142                                    type, receiverUserId);
143    
144                    if (socialActivity != null) {
145                            return socialActivity;
146                    }
147    
148                    return addActivity(
149                            userId, groupId, createDate, className, classPK, type, extraData,
150                            receiverUserId);
151            }
152    
153            public SocialActivity addUniqueActivity(
154                            long userId, long groupId, String className, long classPK, int type,
155                            String extraData, long receiverUserId)
156                    throws PortalException, SystemException {
157    
158                    return addUniqueActivity(
159                            userId, groupId, new Date(), className, classPK, type, extraData,
160                            receiverUserId);
161            }
162    
163            public void deleteActivities(long classNameId, long classPK)
164                    throws SystemException {
165    
166                    socialActivityPersistence.removeByC_C(classNameId, classPK);
167            }
168    
169            public void deleteActivities(String className, long classPK)
170                    throws SystemException {
171    
172                    long classNameId = PortalUtil.getClassNameId(className);
173    
174                    deleteActivities(classNameId, classPK);
175            }
176    
177            public void deleteActivity(long activityId)
178                    throws PortalException, SystemException {
179    
180                    SocialActivity activity = socialActivityPersistence.findByPrimaryKey(
181                            activityId);
182    
183                    deleteActivity(activity);
184            }
185    
186            public void deleteActivity(SocialActivity activity) throws SystemException {
187                    socialActivityPersistence.remove(activity);
188    
189                    try {
190                            socialActivityPersistence.removeByMirrorActivityId(
191                                    activity.getActivityId());
192                    }
193                    catch (NoSuchActivityException nsae) {
194                    }
195            }
196    
197            public void deleteUserActivities(long userId) throws SystemException {
198                    List<SocialActivity> activities =
199                            socialActivityPersistence.findByUserId(
200                                    userId, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
201    
202                    for (SocialActivity activity : activities) {
203                            socialActivityPersistence.remove(activity);
204                    }
205    
206                    activities = socialActivityPersistence.findByReceiverUserId(
207                            userId, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
208    
209                    for (SocialActivity activity : activities) {
210                            socialActivityPersistence.remove(activity);
211                    }
212            }
213    
214            public List<SocialActivity> getActivities(
215                            long classNameId, int start, int end)
216                    throws SystemException {
217    
218                    return socialActivityPersistence.findByClassNameId(
219                            classNameId, start, end);
220            }
221    
222            public List<SocialActivity> getActivities(
223                            long mirrorActivityId, long classNameId, long classPK, int start,
224                            int end)
225                    throws SystemException {
226    
227                    return socialActivityPersistence.findByM_C_C(
228                            mirrorActivityId, classNameId, classPK, start, end);
229            }
230    
231            public List<SocialActivity> getActivities(
232                            long mirrorActivityId, String className, long classPK, int start,
233                            int end)
234                    throws SystemException {
235    
236                    long classNameId = PortalUtil.getClassNameId(className);
237    
238                    return getActivities(
239                            mirrorActivityId, classNameId, classPK, start, end);
240            }
241    
242            public List<SocialActivity> getActivities(
243                            String className, int start, int end)
244                    throws SystemException {
245    
246                    long classNameId = PortalUtil.getClassNameId(className);
247    
248                    return getActivities(classNameId, start, end);
249            }
250    
251            public int getActivitiesCount(long classNameId) throws SystemException {
252                    return socialActivityPersistence.countByClassNameId(classNameId);
253            }
254    
255            public int getActivitiesCount(
256                            long mirrorActivityId, long classNameId, long classPK)
257                    throws SystemException {
258    
259                    return socialActivityPersistence.countByM_C_C(
260                            mirrorActivityId, classNameId, classPK);
261            }
262    
263            public int getActivitiesCount(
264                            long mirrorActivityId, String className, long classPK)
265                    throws SystemException {
266    
267                    long classNameId = PortalUtil.getClassNameId(className);
268    
269                    return getActivitiesCount(mirrorActivityId, classNameId, classPK);
270            }
271    
272            public int getActivitiesCount(String className) throws SystemException {
273                    long classNameId = PortalUtil.getClassNameId(className);
274    
275                    return getActivitiesCount(classNameId);
276            }
277    
278            public SocialActivity getActivity(long activityId)
279                    throws PortalException, SystemException {
280    
281                    return socialActivityPersistence.findByPrimaryKey(activityId);
282            }
283    
284            public List<SocialActivity> getGroupActivities(
285                            long groupId, int start, int end)
286                    throws SystemException {
287    
288                    return socialActivityFinder.findByGroupId(groupId, start, end);
289            }
290    
291            public int getGroupActivitiesCount(long groupId) throws SystemException {
292                    return socialActivityFinder.countByGroupId(groupId);
293            }
294    
295            public List<SocialActivity> getGroupUsersActivities(
296                            long groupId, int start, int end)
297                    throws SystemException {
298    
299                    return socialActivityFinder.findByGroupUsers(groupId, start, end);
300            }
301    
302            public int getGroupUsersActivitiesCount(long groupId)
303                    throws SystemException {
304    
305                    return socialActivityFinder.countByGroupUsers(groupId);
306            }
307    
308            public SocialActivity getMirrorActivity(long mirrorActivityId)
309                    throws PortalException, SystemException {
310    
311                    return socialActivityPersistence.findByMirrorActivityId(
312                            mirrorActivityId);
313            }
314    
315            public List<SocialActivity> getOrganizationActivities(
316                            long organizationId, int start, int end)
317                    throws SystemException {
318    
319                    return socialActivityFinder.findByOrganizationId(
320                            organizationId, start, end);
321            }
322    
323            public int getOrganizationActivitiesCount(long organizationId)
324                    throws SystemException {
325    
326                    return socialActivityFinder.countByOrganizationId(organizationId);
327            }
328    
329            public List<SocialActivity> getOrganizationUsersActivities(
330                            long organizationId, int start, int end)
331                    throws SystemException {
332    
333                    return socialActivityFinder.findByOrganizationUsers(
334                            organizationId, start, end);
335            }
336    
337            public int getOrganizationUsersActivitiesCount(long organizationId)
338                    throws SystemException {
339    
340                    return socialActivityFinder.countByOrganizationUsers(organizationId);
341            }
342    
343            public List<SocialActivity> getRelationActivities(
344                            long userId, int start, int end)
345                    throws SystemException {
346    
347                    return socialActivityFinder.findByRelation(userId, start, end);
348            }
349    
350            public List<SocialActivity> getRelationActivities(
351                            long userId, int type, int start, int end)
352                    throws SystemException {
353    
354                    return socialActivityFinder.findByRelationType(
355                            userId, type, start, end);
356            }
357    
358            public int getRelationActivitiesCount(long userId) throws SystemException {
359                    return socialActivityFinder.countByRelation(userId);
360            }
361    
362            public int getRelationActivitiesCount(long userId, int type)
363                    throws SystemException {
364    
365                    return socialActivityFinder.countByRelationType(userId, type);
366            }
367    
368            public List<SocialActivity> getUserActivities(
369                            long userId, int start, int end)
370                    throws SystemException {
371    
372                    return socialActivityPersistence.findByUserId(userId, start, end);
373            }
374    
375            public int getUserActivitiesCount(long userId) throws SystemException {
376                    return socialActivityPersistence.countByUserId(userId);
377            }
378    
379            public List<SocialActivity> getUserGroupsActivities(
380                            long userId, int start, int end)
381                    throws SystemException {
382    
383                    return socialActivityFinder.findByUserGroups(userId, start, end);
384            }
385    
386            public int getUserGroupsActivitiesCount(long userId)
387                    throws SystemException {
388    
389                    return socialActivityFinder.countByUserGroups(userId);
390            }
391    
392            public List<SocialActivity> getUserGroupsAndOrganizationsActivities(
393                            long userId, int start, int end)
394                    throws SystemException {
395    
396                    return socialActivityFinder.findByUserGroupsAndOrganizations(
397                            userId, start, end);
398            }
399    
400            public int getUserGroupsAndOrganizationsActivitiesCount(long userId)
401                    throws SystemException {
402    
403                    return socialActivityFinder.countByUserGroupsAndOrganizations(userId);
404            }
405    
406            public List<SocialActivity> getUserOrganizationsActivities(
407                            long userId, int start, int end)
408                    throws SystemException {
409    
410                    return socialActivityFinder.findByUserOrganizations(userId, start, end);
411            }
412    
413            public int getUserOrganizationsActivitiesCount(long userId)
414                    throws SystemException {
415    
416                    return socialActivityFinder.countByUserOrganizations(userId);
417            }
418    
419    }