1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.social.service.impl;
24  
25  import com.liferay.portal.PortalException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.dao.orm.QueryUtil;
28  import com.liferay.portal.model.User;
29  import com.liferay.portal.util.PortalUtil;
30  import com.liferay.portlet.social.NoSuchActivityException;
31  import com.liferay.portlet.social.model.SocialActivity;
32  import com.liferay.portlet.social.service.base.SocialActivityLocalServiceBaseImpl;
33  import com.liferay.portlet.social.util.SocialActivityThreadLocal;
34  
35  import java.util.Date;
36  import java.util.List;
37  
38  /**
39   * <a href="SocialActivityLocalServiceImpl.java.html"><b><i>View Source</i></b>
40   * </a>
41   *
42   * @author Brian Wing Shun Chan
43   *
44   */
45  public class SocialActivityLocalServiceImpl
46      extends SocialActivityLocalServiceBaseImpl {
47  
48      public SocialActivity addActivity(
49              long userId, long groupId, String className, long classPK, int type,
50              String extraData, long receiverUserId)
51          throws PortalException, SystemException {
52  
53          return addActivity(
54              userId, groupId, new Date(), className, classPK, type, extraData,
55              receiverUserId);
56      }
57  
58      public SocialActivity addActivity(
59              long userId, long groupId, Date createDate, String className,
60              long classPK, int type, String extraData, long receiverUserId)
61          throws PortalException, SystemException {
62  
63          if (!SocialActivityThreadLocal.isEnabled()) {
64              return null;
65          }
66  
67          User user = userPersistence.findByPrimaryKey(userId);
68          long classNameId = PortalUtil.getClassNameId(className);
69  
70          long activityId = counterLocalService.increment(
71              SocialActivity.class.getName());
72  
73          SocialActivity activity = socialActivityPersistence.create(
74              activityId);
75  
76          activity.setGroupId(groupId);
77          activity.setCompanyId(user.getCompanyId());
78          activity.setUserId(user.getUserId());
79          activity.setCreateDate(createDate);
80          activity.setMirrorActivityId(0);
81          activity.setClassNameId(classNameId);
82          activity.setClassPK(classPK);
83          activity.setType(type);
84          activity.setExtraData(extraData);
85          activity.setReceiverUserId(receiverUserId);
86  
87          socialActivityPersistence.update(activity, false);
88  
89          if ((receiverUserId > 0) && (userId != receiverUserId)) {
90              long mirrorActivityId = counterLocalService.increment(
91                  SocialActivity.class.getName());
92  
93              SocialActivity mirrorActivity = socialActivityPersistence.create(
94                  mirrorActivityId);
95  
96              mirrorActivity.setGroupId(groupId);
97              mirrorActivity.setCompanyId(user.getCompanyId());
98              mirrorActivity.setUserId(receiverUserId);
99              mirrorActivity.setCreateDate(createDate);
100             mirrorActivity.setMirrorActivityId(activityId);
101             mirrorActivity.setClassNameId(classNameId);
102             mirrorActivity.setClassPK(classPK);
103             mirrorActivity.setType(type);
104             mirrorActivity.setExtraData(extraData);
105             mirrorActivity.setReceiverUserId(user.getUserId());
106 
107             socialActivityPersistence.update(mirrorActivity, false);
108         }
109 
110         return activity;
111     }
112 
113     public SocialActivity addUniqueActivity(
114             long userId, long groupId, String className, long classPK, int type,
115             String extraData, long receiverUserId)
116         throws PortalException, SystemException {
117 
118         return addUniqueActivity(
119             userId, groupId, new Date(), className, classPK, type, extraData,
120             receiverUserId);
121     }
122 
123     public SocialActivity addUniqueActivity(
124             long userId, long groupId, Date createDate, String className,
125             long classPK, int type, String extraData, long receiverUserId)
126         throws PortalException, SystemException {
127 
128         long classNameId = PortalUtil.getClassNameId(className);
129 
130         SocialActivity socialActivity =
131             socialActivityPersistence.fetchByG_U_CD_C_C_T_R(
132                 groupId, userId, createDate, classNameId, classPK, type,
133                 receiverUserId);
134 
135         if (socialActivity != null) {
136             return socialActivity;
137         }
138 
139         return addActivity(
140             userId, groupId, createDate, className, classPK, type, extraData,
141             receiverUserId);
142     }
143 
144     public void deleteActivities(String className, long classPK)
145         throws SystemException {
146 
147         long classNameId = PortalUtil.getClassNameId(className);
148 
149         deleteActivities(classNameId, classPK);
150     }
151 
152     public void deleteActivities(long classNameId, long classPK)
153         throws SystemException {
154 
155         socialActivityPersistence.removeByC_C(classNameId, classPK);
156     }
157 
158     public void deleteActivity(long activityId)
159         throws PortalException, SystemException {
160 
161         SocialActivity activity = socialActivityPersistence.findByPrimaryKey(
162             activityId);
163 
164         try {
165             socialActivityPersistence.removeByMirrorActivityId(activityId);
166         }
167         catch (NoSuchActivityException nsae) {
168         }
169 
170         socialActivityPersistence.remove(activity);
171     }
172 
173     public void deleteUserActivities(long userId) throws SystemException {
174         List<SocialActivity> activities =
175             socialActivityPersistence.findByUserId(
176                 userId, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
177 
178         for (SocialActivity activity : activities) {
179             socialActivityPersistence.remove(activity);
180         }
181 
182         activities = socialActivityPersistence.findByReceiverUserId(
183             userId, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
184 
185         for (SocialActivity activity : activities) {
186             socialActivityPersistence.remove(activity);
187         }
188     }
189 
190     public List<SocialActivity> getActivities(
191             String className, int start, int end)
192         throws SystemException {
193 
194         long classNameId = PortalUtil.getClassNameId(className);
195 
196         return getActivities(classNameId, start, end);
197     }
198 
199     public List<SocialActivity> getActivities(
200             long classNameId, int start, int end)
201         throws SystemException {
202 
203         return socialActivityPersistence.findByClassNameId(
204             classNameId, start, end);
205     }
206 
207     public List<SocialActivity> getActivities(
208             long mirrorActivityId, String className, long classPK, int start,
209             int end)
210         throws SystemException {
211 
212         long classNameId = PortalUtil.getClassNameId(className);
213 
214         return getActivities(
215             mirrorActivityId, classNameId, classPK, start, end);
216     }
217 
218     public List<SocialActivity> getActivities(
219             long mirrorActivityId, long classNameId, long classPK, int start,
220             int end)
221         throws SystemException {
222 
223         return socialActivityPersistence.findByM_C_C(
224             mirrorActivityId, classNameId, classPK, start, end);
225     }
226 
227     public int getActivitiesCount(String className) throws SystemException {
228         long classNameId = PortalUtil.getClassNameId(className);
229 
230         return getActivitiesCount(classNameId);
231     }
232 
233     public int getActivitiesCount(long classNameId) throws SystemException {
234         return socialActivityPersistence.countByClassNameId(classNameId);
235     }
236 
237     public int getActivitiesCount(
238             long mirrorActivityId, String className, long classPK)
239         throws SystemException {
240 
241         long classNameId = PortalUtil.getClassNameId(className);
242 
243         return getActivitiesCount(mirrorActivityId, classNameId, classPK);
244     }
245 
246     public int getActivitiesCount(
247             long mirrorActivityId, long classNameId, long classPK)
248         throws SystemException {
249 
250         return socialActivityPersistence.countByM_C_C(
251             mirrorActivityId, classNameId, classPK);
252     }
253 
254     public SocialActivity getActivity(long activityId)
255         throws PortalException, SystemException {
256 
257         return socialActivityPersistence.findByPrimaryKey(activityId);
258     }
259 
260     public List<SocialActivity> getGroupActivities(
261             long groupId, int start, int end)
262         throws SystemException {
263 
264         return socialActivityFinder.findByGroupId(groupId, start, end);
265     }
266 
267     public int getGroupActivitiesCount(long groupId) throws SystemException {
268         return socialActivityFinder.countByGroupId(groupId);
269     }
270 
271     public List<SocialActivity> getGroupUsersActivities(
272             long groupId, int start, int end)
273         throws SystemException {
274 
275         return socialActivityFinder.findByGroupUsers(groupId, start, end);
276     }
277 
278     public int getGroupUsersActivitiesCount(long groupId)
279         throws SystemException {
280 
281         return socialActivityFinder.countByGroupUsers(groupId);
282     }
283 
284     public SocialActivity getMirrorActivity(long mirrorActivityId)
285         throws PortalException, SystemException {
286 
287         return socialActivityPersistence.findByMirrorActivityId(
288             mirrorActivityId);
289     }
290 
291     public List<SocialActivity> getOrganizationActivities(
292             long organizationId, int start, int end)
293         throws SystemException {
294 
295         return socialActivityFinder.findByOrganizationId(
296             organizationId, start, end);
297     }
298 
299     public int getOrganizationActivitiesCount(long organizationId)
300         throws SystemException {
301 
302         return socialActivityFinder.countByOrganizationId(organizationId);
303     }
304 
305     public List<SocialActivity> getOrganizationUsersActivities(
306             long organizationId, int start, int end)
307         throws SystemException {
308 
309         return socialActivityFinder.findByOrganizationUsers(
310             organizationId, start, end);
311     }
312 
313     public int getOrganizationUsersActivitiesCount(long organizationId)
314         throws SystemException {
315 
316         return socialActivityFinder.countByOrganizationUsers(organizationId);
317     }
318 
319     public List<SocialActivity> getRelationActivities(
320             long userId, int start, int end)
321         throws SystemException {
322 
323         return socialActivityFinder.findByRelation(userId, start, end);
324     }
325 
326     public List<SocialActivity> getRelationActivities(
327             long userId, int type, int start, int end)
328         throws SystemException {
329 
330         return socialActivityFinder.findByRelationType(
331             userId, type, start, end);
332     }
333 
334     public int getRelationActivitiesCount(long userId) throws SystemException {
335         return socialActivityFinder.countByRelation(userId);
336     }
337 
338     public int getRelationActivitiesCount(long userId, int type)
339         throws SystemException {
340 
341         return socialActivityFinder.countByRelationType(userId, type);
342     }
343 
344     public List<SocialActivity> getUserActivities(
345             long userId, int start, int end)
346         throws SystemException {
347 
348         return socialActivityPersistence.findByUserId(userId, start, end);
349     }
350 
351     public int getUserActivitiesCount(long userId) throws SystemException {
352         return socialActivityPersistence.countByUserId(userId);
353     }
354 
355     public List<SocialActivity> getUserGroupsActivities(
356             long userId, int start, int end)
357         throws SystemException {
358 
359         return socialActivityFinder.findByUserGroups(userId, start, end);
360     }
361 
362     public int getUserGroupsActivitiesCount(long userId)
363         throws SystemException {
364 
365         return socialActivityFinder.countByUserGroups(userId);
366     }
367 
368     public List<SocialActivity> getUserGroupsAndOrganizationsActivities(
369             long userId, int start, int end)
370         throws SystemException {
371 
372         return socialActivityFinder.findByUserGroupsAndOrganizations(
373             userId, start, end);
374     }
375 
376     public int getUserGroupsAndOrganizationsActivitiesCount(long userId)
377         throws SystemException {
378 
379         return socialActivityFinder.countByUserGroupsAndOrganizations(userId);
380     }
381 
382     public List<SocialActivity> getUserOrganizationsActivities(
383             long userId, int start, int end)
384         throws SystemException {
385 
386         return socialActivityFinder.findByUserOrganizations(userId, start, end);
387     }
388 
389     public int getUserOrganizationsActivitiesCount(long userId)
390         throws SystemException {
391 
392         return socialActivityFinder.countByUserOrganizations(userId);
393     }
394 
395 }