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          Date createDate = new Date();
54  
55          long classNameId = PortalUtil.getClassNameId(className);
56  
57          while (true) {
58              SocialActivity socialActivity =
59                  socialActivityPersistence.fetchByG_U_CD_C_C_T_R(
60                      groupId, userId, createDate.getTime(), classNameId, classPK,
61                      type, receiverUserId);
62  
63              if (socialActivity != null) {
64                  createDate = new Date(createDate.getTime() + 1);
65              }
66              else {
67                  break;
68              }
69          }
70  
71          return addActivity(
72              userId, groupId, createDate, className, classPK, type, extraData,
73              receiverUserId);
74      }
75  
76      public SocialActivity addActivity(
77              long userId, long groupId, Date createDate, String className,
78              long classPK, int type, String extraData, long receiverUserId)
79          throws PortalException, SystemException {
80  
81          if (!SocialActivityThreadLocal.isEnabled()) {
82              return null;
83          }
84  
85          User user = userPersistence.findByPrimaryKey(userId);
86          long classNameId = PortalUtil.getClassNameId(className);
87  
88          long activityId = counterLocalService.increment(
89              SocialActivity.class.getName());
90  
91          SocialActivity activity = socialActivityPersistence.create(
92              activityId);
93  
94          activity.setGroupId(groupId);
95          activity.setCompanyId(user.getCompanyId());
96          activity.setUserId(user.getUserId());
97          activity.setCreateDate(createDate.getTime());
98          activity.setMirrorActivityId(0);
99          activity.setClassNameId(classNameId);
100         activity.setClassPK(classPK);
101         activity.setType(type);
102         activity.setExtraData(extraData);
103         activity.setReceiverUserId(receiverUserId);
104 
105         socialActivityPersistence.update(activity, false);
106 
107         if ((receiverUserId > 0) && (userId != receiverUserId)) {
108             long mirrorActivityId = counterLocalService.increment(
109                 SocialActivity.class.getName());
110 
111             SocialActivity mirrorActivity = socialActivityPersistence.create(
112                 mirrorActivityId);
113 
114             mirrorActivity.setGroupId(groupId);
115             mirrorActivity.setCompanyId(user.getCompanyId());
116             mirrorActivity.setUserId(receiverUserId);
117             mirrorActivity.setCreateDate(createDate.getTime());
118             mirrorActivity.setMirrorActivityId(activityId);
119             mirrorActivity.setClassNameId(classNameId);
120             mirrorActivity.setClassPK(classPK);
121             mirrorActivity.setType(type);
122             mirrorActivity.setExtraData(extraData);
123             mirrorActivity.setReceiverUserId(user.getUserId());
124 
125             socialActivityPersistence.update(mirrorActivity, false);
126         }
127 
128         return activity;
129     }
130 
131     public SocialActivity addUniqueActivity(
132             long userId, long groupId, String className, long classPK, int type,
133             String extraData, long receiverUserId)
134         throws PortalException, SystemException {
135 
136         return addUniqueActivity(
137             userId, groupId, new Date(), className, classPK, type, extraData,
138             receiverUserId);
139     }
140 
141     public SocialActivity addUniqueActivity(
142             long userId, long groupId, Date createDate, String className,
143             long classPK, int type, String extraData, long receiverUserId)
144         throws PortalException, SystemException {
145 
146         long classNameId = PortalUtil.getClassNameId(className);
147 
148         SocialActivity socialActivity =
149             socialActivityPersistence.fetchByG_U_CD_C_C_T_R(
150                 groupId, userId, createDate.getTime(), classNameId, classPK,
151                 type, receiverUserId);
152 
153         if (socialActivity != null) {
154             return socialActivity;
155         }
156 
157         return addActivity(
158             userId, groupId, createDate, className, classPK, type, extraData,
159             receiverUserId);
160     }
161 
162     public void deleteActivities(String className, long classPK)
163         throws SystemException {
164 
165         long classNameId = PortalUtil.getClassNameId(className);
166 
167         deleteActivities(classNameId, classPK);
168     }
169 
170     public void deleteActivities(long classNameId, long classPK)
171         throws SystemException {
172 
173         socialActivityPersistence.removeByC_C(classNameId, classPK);
174     }
175 
176     public void deleteActivity(long activityId)
177         throws PortalException, SystemException {
178 
179         SocialActivity activity = socialActivityPersistence.findByPrimaryKey(
180             activityId);
181 
182         try {
183             socialActivityPersistence.removeByMirrorActivityId(activityId);
184         }
185         catch (NoSuchActivityException nsae) {
186         }
187 
188         socialActivityPersistence.remove(activity);
189     }
190 
191     public void deleteUserActivities(long userId) throws SystemException {
192         List<SocialActivity> activities =
193             socialActivityPersistence.findByUserId(
194                 userId, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
195 
196         for (SocialActivity activity : activities) {
197             socialActivityPersistence.remove(activity);
198         }
199 
200         activities = socialActivityPersistence.findByReceiverUserId(
201             userId, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
202 
203         for (SocialActivity activity : activities) {
204             socialActivityPersistence.remove(activity);
205         }
206     }
207 
208     public List<SocialActivity> getActivities(
209             String className, int start, int end)
210         throws SystemException {
211 
212         long classNameId = PortalUtil.getClassNameId(className);
213 
214         return getActivities(classNameId, start, end);
215     }
216 
217     public List<SocialActivity> getActivities(
218             long classNameId, int start, int end)
219         throws SystemException {
220 
221         return socialActivityPersistence.findByClassNameId(
222             classNameId, start, end);
223     }
224 
225     public List<SocialActivity> getActivities(
226             long mirrorActivityId, String className, long classPK, int start,
227             int end)
228         throws SystemException {
229 
230         long classNameId = PortalUtil.getClassNameId(className);
231 
232         return getActivities(
233             mirrorActivityId, classNameId, classPK, start, end);
234     }
235 
236     public List<SocialActivity> getActivities(
237             long mirrorActivityId, long classNameId, long classPK, int start,
238             int end)
239         throws SystemException {
240 
241         return socialActivityPersistence.findByM_C_C(
242             mirrorActivityId, classNameId, classPK, start, end);
243     }
244 
245     public int getActivitiesCount(String className) throws SystemException {
246         long classNameId = PortalUtil.getClassNameId(className);
247 
248         return getActivitiesCount(classNameId);
249     }
250 
251     public int getActivitiesCount(long classNameId) throws SystemException {
252         return socialActivityPersistence.countByClassNameId(classNameId);
253     }
254 
255     public int getActivitiesCount(
256             long mirrorActivityId, String className, long classPK)
257         throws SystemException {
258 
259         long classNameId = PortalUtil.getClassNameId(className);
260 
261         return getActivitiesCount(mirrorActivityId, classNameId, classPK);
262     }
263 
264     public int getActivitiesCount(
265             long mirrorActivityId, long classNameId, long classPK)
266         throws SystemException {
267 
268         return socialActivityPersistence.countByM_C_C(
269             mirrorActivityId, classNameId, classPK);
270     }
271 
272     public SocialActivity getActivity(long activityId)
273         throws PortalException, SystemException {
274 
275         return socialActivityPersistence.findByPrimaryKey(activityId);
276     }
277 
278     public List<SocialActivity> getGroupActivities(
279             long groupId, int start, int end)
280         throws SystemException {
281 
282         return socialActivityFinder.findByGroupId(groupId, start, end);
283     }
284 
285     public int getGroupActivitiesCount(long groupId) throws SystemException {
286         return socialActivityFinder.countByGroupId(groupId);
287     }
288 
289     public List<SocialActivity> getGroupUsersActivities(
290             long groupId, int start, int end)
291         throws SystemException {
292 
293         return socialActivityFinder.findByGroupUsers(groupId, start, end);
294     }
295 
296     public int getGroupUsersActivitiesCount(long groupId)
297         throws SystemException {
298 
299         return socialActivityFinder.countByGroupUsers(groupId);
300     }
301 
302     public SocialActivity getMirrorActivity(long mirrorActivityId)
303         throws PortalException, SystemException {
304 
305         return socialActivityPersistence.findByMirrorActivityId(
306             mirrorActivityId);
307     }
308 
309     public List<SocialActivity> getOrganizationActivities(
310             long organizationId, int start, int end)
311         throws SystemException {
312 
313         return socialActivityFinder.findByOrganizationId(
314             organizationId, start, end);
315     }
316 
317     public int getOrganizationActivitiesCount(long organizationId)
318         throws SystemException {
319 
320         return socialActivityFinder.countByOrganizationId(organizationId);
321     }
322 
323     public List<SocialActivity> getOrganizationUsersActivities(
324             long organizationId, int start, int end)
325         throws SystemException {
326 
327         return socialActivityFinder.findByOrganizationUsers(
328             organizationId, start, end);
329     }
330 
331     public int getOrganizationUsersActivitiesCount(long organizationId)
332         throws SystemException {
333 
334         return socialActivityFinder.countByOrganizationUsers(organizationId);
335     }
336 
337     public List<SocialActivity> getRelationActivities(
338             long userId, int start, int end)
339         throws SystemException {
340 
341         return socialActivityFinder.findByRelation(userId, start, end);
342     }
343 
344     public List<SocialActivity> getRelationActivities(
345             long userId, int type, int start, int end)
346         throws SystemException {
347 
348         return socialActivityFinder.findByRelationType(
349             userId, type, start, end);
350     }
351 
352     public int getRelationActivitiesCount(long userId) throws SystemException {
353         return socialActivityFinder.countByRelation(userId);
354     }
355 
356     public int getRelationActivitiesCount(long userId, int type)
357         throws SystemException {
358 
359         return socialActivityFinder.countByRelationType(userId, type);
360     }
361 
362     public List<SocialActivity> getUserActivities(
363             long userId, int start, int end)
364         throws SystemException {
365 
366         return socialActivityPersistence.findByUserId(userId, start, end);
367     }
368 
369     public int getUserActivitiesCount(long userId) throws SystemException {
370         return socialActivityPersistence.countByUserId(userId);
371     }
372 
373     public List<SocialActivity> getUserGroupsActivities(
374             long userId, int start, int end)
375         throws SystemException {
376 
377         return socialActivityFinder.findByUserGroups(userId, start, end);
378     }
379 
380     public int getUserGroupsActivitiesCount(long userId)
381         throws SystemException {
382 
383         return socialActivityFinder.countByUserGroups(userId);
384     }
385 
386     public List<SocialActivity> getUserGroupsAndOrganizationsActivities(
387             long userId, int start, int end)
388         throws SystemException {
389 
390         return socialActivityFinder.findByUserGroupsAndOrganizations(
391             userId, start, end);
392     }
393 
394     public int getUserGroupsAndOrganizationsActivitiesCount(long userId)
395         throws SystemException {
396 
397         return socialActivityFinder.countByUserGroupsAndOrganizations(userId);
398     }
399 
400     public List<SocialActivity> getUserOrganizationsActivities(
401             long userId, int start, int end)
402         throws SystemException {
403 
404         return socialActivityFinder.findByUserOrganizations(userId, start, end);
405     }
406 
407     public int getUserOrganizationsActivitiesCount(long userId)
408         throws SystemException {
409 
410         return socialActivityFinder.countByUserOrganizations(userId);
411     }
412 
413 }